kustomize、secretGenerator 和 patchStrategicMerge:envFrom.secretRef 不读取散列秘密名称

mpe*_*pen 7 kubernetes kustomize

在我的kustomization.yaml我有:

\n
...\nsecretGenerator:\n  - name: db-env\n    behavior: create\n    envs:\n      - my.env\npatchesStrategicMerge:\n  - app.yaml\n
Run Code Online (Sandbox Code Playgroud)\n

然后在我的app.yaml(补丁)中我有:

\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: app-deployment\nspec:\n  template:\n    spec:\n      containers:\n        - name: server\n          envFrom:\n            - secretRef:\n                name: db-env\n
Run Code Online (Sandbox Code Playgroud)\n

当我尝试通过构建这个时,kustomize build k8s/development我退出了:

\n
apiVersion: apps/v1\nkind: Deployment\n...\n    spec:\n      containers:\n      - envFrom:\n        - secretRef:\n            name: db-env\n        name: server\n
Run Code Online (Sandbox Code Playgroud)\n

什么时候应该是:

\n
      - envFrom:\n        - secretRef:\n            name: db-env-4g95hhmhfc\n
Run Code Online (Sandbox Code Playgroud)\n

如何让secretGenerator名称哈希也适用patchesStrategicMerge

\n

或者,将一些环境变量注入到特定覆盖的部署中的正确方法是什么?

\n

这是为了发展。

\n
\n

我的文件结构是这样的:

\n
\xe2\x9d\xaf tree k8s\nk8s\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 base\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 development\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 golinks.sql\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mariadb.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my.cnf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my.env\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 production\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ingress.yaml\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n
Run Code Online (Sandbox Code Playgroud)\n

哪里base/kustomization.yaml

\n
\xe2\x9d\xaf tree k8s\nk8s\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 base\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 development\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 golinks.sql\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mariadb.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my.cnf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my.env\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 production\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ingress.yaml\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n
Run Code Online (Sandbox Code Playgroud)\n

并且development/kustomization.yaml是:

\n
namespace: go-mpen\nresources:\n- app.yaml\nimages:\n- name: server\n  newName: reg/proj/server\n
Run Code Online (Sandbox Code Playgroud)\n

aci*_*uji 3

这对我来说效果很好kustomize v3.8.4。您能否检查一下您的版本,如果disableNameSuffixHash没有设置为 true。

\n

以下是我用来测试这一点的清单:

\n
\xe2\x9e\x9c  app.yaml  deployment.yaml  kustomization.yaml   my.env\n
Run Code Online (Sandbox Code Playgroud)\n

应用程序.yaml

\n
kind: Deployment\nmetadata:\n  name: app-deployment\nspec:\n  template:\n    spec:\n      containers:\n        - name: server\n          envFrom:\n            - secretRef:\n                name: db-env\n
Run Code Online (Sandbox Code Playgroud)\n

部署.yaml

\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: app-deployment \n  labels:\n    app: nginx\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      app: nginx\n  template:\n    metadata:\n      labels:\n        app: nginx\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:1.14.2\n        ports:\n        - containerPort: 80\n
Run Code Online (Sandbox Code Playgroud)\n

和我的 kustomization.yaml

\n
apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n\nsecretGenerator:\n  - name: db-env\n    behavior: create\n    envs:\n      - my.env\npatchesStrategicMerge:\n  - app.yaml\n\nresources: \n  - deployment.yaml \n
Run Code Online (Sandbox Code Playgroud)\n

这是结果:

\n
apiVersion: v1\ndata:\n  ASD: MTIz\nkind: Secret\nmetadata:\n  name: db-env-f5tt4gtd7d\ntype: Opaque\n---\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n  labels:\n    app: nginx\n  name: app-deployment\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      app: nginx\n  template:\n    metadata:\n      labels:\n        app: nginx\n    spec:\n      containers:\n      - image: nginx:1.14.2\n        name: nginx\n        ports:\n        - containerPort: 80\n      - envFrom:\n        - secretRef:\n            name: db-env-f5tt4gtd7d\n        name: server\n
Run Code Online (Sandbox Code Playgroud)\n