Skip to content

secretGenerator to generate Secret with stringData manifest #5142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 2 tasks
valerytschopp opened this issue Apr 20, 2023 · 13 comments · May be fixed by #5894
Open
1 of 2 tasks

secretGenerator to generate Secret with stringData manifest #5142

valerytschopp opened this issue Apr 20, 2023 · 13 comments · May be fixed by #5894
Labels
kind/feature Categorizes issue or PR as related to a new feature. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@valerytschopp
Copy link

Eschewed features

  • This issue is not requesting templating, unstuctured edits, build-time side-effects from args or env vars, or any other eschewed feature.

What would you like to have added?

The secretGenerator should be able to generate Secret manifest with unencoded stringData.

We understand that kustomize eschews parameterization, but the resulting manifest is valid YAML, and can be processed by various tools (envsubst, flux post-build variable substitution, ...).
Therefore we ask the Kustomize SIG to consider this feature.

Example:

cat<<EOF> secret-values.yaml
# Helm values
---
db:
  auth:
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
  host: ${DB_HOST}
EOF
cat<<EOF> kustomization.yaml
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: secret-values
  files:
  - values.yaml=secret-values.yaml
generatorOptions:
  disableNameSuffixHash: true
  stringData: true
EOF
kustomize build .
apiVersion: v1
kind: Secret
metadata:
  name: secret-secret-values
stringData:
  values.yaml: |-
    db:
      auth:
        username: ${DB_USERNAME}
        password: ${DB_PASSWORD}
      host: ${DB_HOST}

Why is this needed?

We are doing GitOps with Flux's post-build variable substitution which currently requires stringData in the secret.
Base64 encoded values would not be able to be substitued.

Can you accomplish the motivating task without this feature, and if so, how?

Create the Secret by hand with stringData:

---
apiVersion: v1
kind: Secret
metadata:
    name: secret-secret-values
stringData:
  values.yaml: |-
    db:
      auth:
        username: ${DB_USERNAME}
        password: ${DB_PASSWORD}
      host: ${DB_HOST}

But this negate the usefulness of Kustomize generators.

What other solutions have you considered?

Don't use secretGenerator for Helm secret values

Anything else we should know?

No response

Feature ownership

  • I am interested in contributing this feature myself! 🎉
@valerytschopp valerytschopp added the kind/feature Categorizes issue or PR as related to a new feature. label Apr 20, 2023
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Apr 20, 2023
@valerytschopp
Copy link
Author

This old issue #1444 was simply closed without being commented, or addressed.

@seh
Copy link
Contributor

seh commented Apr 24, 2023

We are doing GitOps with Flux's post-build variable substitution which currently requires stringData in the secret.

To clarify, post-build substitution does not require use of the "stringData" field in a candidate manifest, but you want to use that field to sidestep the Base64 encoding, which masks the content you wish to replace.

Flux doesn't pay any attention to field names in the manifests. It serializes them as YAML, and runs envsubst over the bytes.

@valerytschopp
Copy link
Author

The Flux's post-build variable substitution documentation clearly states:

Note: For substituting variables in a secret, .spec.stringData field must be used i.e:

---
apiVersion: v1
kind: Secret
metadata:
  name: secret
  namespace: flux-system
type: Opaque
stringData:
  token: ${token}

@seh
Copy link
Contributor

seh commented Apr 25, 2023

Yes, that's the documentation clarifying a common situation in which post-build substitution cannot see the content you'd like it to act upon. You have the same problem editing comments in PEM-encoded X.509 certificate bundles that occur in a (Valida|Muta)tingWebhookConfiguration's "webhooks[*].clientConfig.caBundle" field, which is also Base64-encoded.

The Flux documentation is correct, but not complete. I was explaining why you need to use the Secret "stringData" field here.

@simion
Copy link

simion commented May 24, 2023

We also need this, we're using envsubst and it does not work on Secret/data. We need stringData option :D

@seh
Copy link
Contributor

seh commented May 24, 2023

I could see implementing this, accepting an option in the "secretGenerator[*].options" field, though that field is of type api/types/GeneratorOptions, which means that it's shared with other generators. Alternately, we could lift such an option up into api/types/SecretArgs which already bears the Secret-specific "type" field.

The api/internal/generators.MakeSecret function appears to be the place to handle such a change.

@annasong20
Copy link
Contributor

Hi @valerytschopp, thank you for explaining your use case and providing an example in your description. Thanks @seh for investigating.

Yes, we believe it is a reasonable use case to specify the secretGenerator add the data under a stringData field. This may also apply to the binaryData field for ConfigMaps. Can whoever wants to contribute to this feature write a mini-KEP aka kustomize proposal? We will start PR reviews for this feature once that KEP has been approved.

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 2, 2023
@filedeploy
Copy link

filedeploy commented Aug 16, 2023

Edit: Doesn't work - secretGenerator base64 encodes the values, but stringData expects plaintext.

This also works in the meantime:

secretGenerator:
  - name: my-secret
    ...details...

patches:
  # Patch the above secret `data` -> `stringData` because secretGenerator doesn't support `stringData`
  - patch: |-
      - op: move
        from: /data
        path: /stringData
    target:
      kind: Secret
      name: my-secret

@seh
Copy link
Contributor

seh commented Aug 16, 2023

This also works in the meantime:

What about the Base64 encoding of the "data" field's value?

@filedeploy
Copy link

filedeploy commented Aug 16, 2023

You're right, it still gets Base64 encoded. No dice.

@bossm8
Copy link

bossm8 commented Jan 12, 2024

Thanks to @filedeploy I found an ugly workaround which works at least for environment variables for pods, which we use with generators so that deployments get restarted once the values change (although this probably doesn't work as well for files):

secretGenerator:
  - name: my-secret
    namespace: my-namespace
    literals: []

patches:
  - patch: |-
      - op: move
        from: /data
        path: /stringData
      - op: add
        path: /stringData
        value:
          API_KEY: ${API_KEY}
          SECRET: ${SECRET_KEY}
    target:
      kind: Secret
      name: my-secret

EDIT: Just found out that the secret suffix does not change anymore with this approach, so this is not really a solution either.

@wasoeki
Copy link

wasoeki commented Dec 5, 2024

Hi, it would be really useful also with using vals for managing secrets. I hope it will land soon inside the options of secretGenerator or generatorOptions. Something like useStringData: true.
Usage : kustomize build . | vals eval | kubectl apply -f -

bossm8 added a commit to bossm8/k8s-playground that referenced this issue Mar 3, 2025
@stealthybox
Copy link

I'm so glad you filed this issue.
This has annoyed me in the past.
Seeing Flux users in this discussion motivated me to write a patch.
I succeeded in adding a stringData bool to api/types/SecretArgs like @seh suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Development

Successfully merging a pull request may close this issue.

9 participants