Reference autogenerated secret with name prefix

huangapple go评论91阅读模式
英文:

Reference autogenerated secret with name prefix

问题

I'm using kustomize to manage a rather standard deployment. I have a namePrefix to modify the resource names.

我正在使用kustomize来管理一个相当标准的部署。我有一个 namePrefix 来修改资源名称。

I need to add a custom resource to my configuration which itself autogenerates a secret after creation. The secret name consists of a fixed prefix and the name of the custom resource. I want to reference this secret in my deployment.

我需要在我的配置中添加一个自定义资源,它在创建后会自动生成一个密钥。密钥的名称由固定前缀和自定义资源的名称组成。我想在我的部署中引用这个密钥。

  1. # kustomization.yaml
  2. resources:
  3. - deployment.yaml
  4. - custom-resource.yaml
  5. namePrefix: my-prefix-
  6. secretGenerator:
  7. - name: my-secret
  8. files:
  9. - password.txt
  1. # deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-deployment
  6. labels:
  7. app: my-app
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. app: my-app
  13. template:
  14. metadata:
  15. labels:
  16. app: my-app
  17. spec:
  18. containers:
  19. - name: my-image
  20. image: my-image:latest
  21. envFrom:
  22. - secretRef:
  23. name: my-secret
  24. - secretRef:
  25. name: prefix-my-custom-resource <-- does not work
  1. # custom-resource.yaml
  2. apiVersion: some.crd.io/v1
  3. kind: CustomResource
  4. metadata:
  5. name: my-custom-resource

The custom resource will autogenerate: (not result of kubectl kustomize .)

自定义资源将自动生成:(不是 kubectl kustomize . 的结果)

  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: prefix-my-custom-resource

Due to the use of the PrefixTransformer, the name of the custom resource is changed to my-prefix-my-custom-resource. Therefore, the secretRef in the deployment yaml needs to be updated to prefix-my-prefix-my-custom-resource. I tried to solve this with a nameReference configuration, but I don't think the fieldSpec allows for a substring. Is there any solution to this?

由于使用了 PrefixTransformer,自定义资源的名称更改为 my-prefix-my-custom-resource。因此,部署 yaml 中的 secretRef 需要更新为 prefix-my-prefix-my-custom-resource。我尝试使用 nameReference 配置解决这个问题,但我认为 fieldSpec 不允许子字符串。是否有解决方法?

英文:

I'm using kustomize to manage a rather standard deployment. I have a namePrefix to modify the resource names.

I need to add a custom resource to my configuration which itself autogenerates a secret after creation. The secret name consists of a fixed prefix and the name of the custom resource. I want to reference this secret in my deployment.

  1. # kustomization.yaml
  2. resources:
  3. - deployment.yaml
  4. - custom-resource.yaml
  5. namePrefix: my-prefix-
  6. secretGenerator:
  7. - name: my-secret
  8. files:
  9. - password.txt
  1. # deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-deployment
  6. labels:
  7. app: my-app
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. app: my-app
  13. template:
  14. metadata:
  15. labels:
  16. app: my-app
  17. spec:
  18. containers:
  19. - name: my-image
  20. image: my-image:latest
  21. envFrom:
  22. - secretRef:
  23. name: my-secret
  24. - secretRef:
  25. name: prefix-my-custom-resource <-- does not work
  1. # custom-resource.yaml
  2. apiVersion: some.crd.io/v1
  3. kind: CustomResource
  4. metadata:
  5. name: my-custom-resource

The custom resource will autogenerate: (not result of kubectl kustomize .)

  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: prefix-my-custom-resource

Due to the use of the PrefixTransformer, the name of the custom resource is changed to my-prefix-my-custom-resource. Therefore, the secretRef in the deployment yaml needs to be updated to prefix-my-prefix-my-custom-resource. I tried to solve this with a nameReference configuration, but I don't think the fieldSpec allows for a substring. Is there any solution to this?

答案1

得分: 1

With the help of a colleague, we were able to solve this with vars:

  1. # kustomization.yaml
  2. resources:
  3. - deployment.yaml
  4. - custom-resource.yaml
  5. namePrefix: my-prefix-
  6. secretGenerator:
  7. - name: my-secret
  8. files:
  9. - password.txt
  10. configurations:
  11. - configurations/var-reference.yaml
  12. vars:
  13. - name: MY-VARIABLE
  14. objref:
  15. kind: CustomResource
  16. name: my-custom-resource
  17. apiVersion: some.crd.io/v1
  18. fieldref:
  19. fieldpath: metadata.name
  1. # configurations/var-reference.yaml
  2. varReference:
  3. - kind: Deployment
  4. path: spec/template/spec/containers/envFrom/secretRef/name
  1. # deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-deployment
  6. labels:
  7. app: my-app
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. app: my-app
  13. template:
  14. metadata:
  15. labels:
  16. app: my-app
  17. spec:
  18. containers:
  19. - name: my-image
  20. image: my-image:latest
  21. envFrom:
  22. - secretRef:
  23. name: my-secret
  24. - secretRef:
  25. name: prefix-$(MY-VARIABLE)

I'm aware that vars are deprecated and might try to find a solution with replacements, but for now I'm good with this solution.

英文:

With the help of a colleague, we were able to solve this with vars:

  1. # kustomization.yaml
  2. resources:
  3. - deployment.yaml
  4. - custom-resource.yaml
  5. namePrefix: my-prefix-
  6. secretGenerator:
  7. - name: my-secret
  8. files:
  9. - password.txt
  10. configurations:
  11. - configurations/var-reference.yaml
  12. vars:
  13. - name: MY-VARIABLE
  14. objref:
  15. kind: CustomResource
  16. name: my-custom-resource
  17. apiVersion: some.crd.io/v1
  18. fieldref:
  19. fieldpath: metadata.name
  1. # configurations/var-reference.yaml
  2. varReference:
  3. - kind: Deployment
  4. path: spec/template/spec/containers/envFrom/secretRef/name
  1. # deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-deployment
  6. labels:
  7. app: my-app
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. app: my-app
  13. template:
  14. metadata:
  15. labels:
  16. app: my-app
  17. spec:
  18. containers:
  19. - name: my-image
  20. image: my-image:latest
  21. envFrom:
  22. - secretRef:
  23. name: my-secret
  24. - secretRef:
  25. name: prefix-$(MY-VARIABLE)

I'm aware that vars are deprecated and might try to find a solution with replacements, but for now I'm good with this solution.

huangapple
  • 本文由 发表于 2023年5月10日 20:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定