kustomize:将imagePullSecrets添加到所有部署

时间:2019-12-06 22:38:48

标签: kubernetes kustomize

我有一组在一个环境中工作的kubernetes配置文件。我希望部署到另一个环境中,在该环境中,需要向所有Deployment配置中添加一个imagePullSecrets条目。

我可以做到:

regcred-1.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: deployment-1
spec:
  template:
    spec:
      imagePullSecrets:
      - name: regcred

kustomization.yaml:

bases:
  - ../base

patchesStrategicMerge:
  - regcred-1.yaml

这只会修补deployment-1

是否可以将补丁程序应用于所有部署?

3 个答案:

答案 0 :(得分:1)

您可以使用patches字段代替patchesStrategicMerge来修补多个资源。

基于此demo example,您可以通过指定补丁和目标选择器来做到这一点:

patches:
- path: <PatchFile>   target:
    group: <Group>
    version: <Version>
    kind: <Kind>
    name: <Name>
    namespace: <Namespace>
    labelSelector: <LabelSelector>
    annotationSelector: <AnnotationSelector>

在这种情况下,您的kustomization.yaml应该如下所示:

bases:
  - ../base

patches:
- path: regcred-1.yaml
 target:
   kind: Deployment

让我知道这是否解决了您的情况。

答案 1 :(得分:1)

类似这样的事情似乎可以添加一个imagePullSecret

patches:
    -   target:
            kind: Deployment
        patch: |-
            - op: add
              path: /spec/template/spec/imagePullSecrets/-
              value:
                name: regcred
    -   target:
            kind: CronJob
        patch: |-
            - op: replace
              path: /spec/jobTemplate/spec/template/spec/imagePullSecrets
              value:
                - name: regcred

或更简单地说,您只需运行一次:

kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "regcred"}]}'

答案 2 :(得分:0)

使用 Inline Patch

kind: Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1
resources:
  - ../../base
patches:
  - target:
      kind: Deployment
    patch: |-
      - op: add
        path: /spec/template/spec/imagePullSecrets
        value: [{ name: image-pull-secret }]

参考:Patching multiple resources at once.