Kustomize:修补以相同名称开头的多个资源

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

Kustomize: Patch multiple resources that starts with same name

问题

我想要对以相同的namePrefix开头而不是针对特定资源进行多个部署的修补。

例如,我有两个nginx部署deployment-v1.yamldeployment-v2.yaml。我想要使用**nginx-**前缀来修补这两个部署。

deployment-v1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-v1
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 5
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

deployment-v2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 5
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        ports:
        - containerPort: 80

现在,我想要使用通用的overlay-patch覆盖这两个部署。我尝试像这样做。

overlay.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  namePrefix: nginx-
spec:
  replicas: 10

kustomization.yml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment-v1.yaml
  - deployment-v2.yaml
patchesStrategicMerge:
  - overlay.yaml

但它不起作用,因为它期望一个名称与目标匹配,完全忽略了namePrefix。任何帮助都将不胜感激。

英文:

I want to patch multiple deployments that starts with same namePrefix instead of targeting specific resource.

For example, I have 2 deployments of nginx deployment-v1.yaml and deployment-v2.yaml. I want to patch both the deployment using nginx- prefix.

deployment-v1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-v1
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 5
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
       —name: nginx
          image: nginx
          ports:
           —containerPort: 80 

deployment-v2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 5
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
       —name: nginx
          image: nginx
          resources:
            limits:
              memory: 200Mi
            requests:
              cpu: 100m
              memory: 200Mi
          ports:
           —containerPort: 80

Now I want to overlay both the deployments with common overlay-patch. I am trying something like this.

overlay.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  namePrefix: nginx-
spec:
  replicas: 10

kustomization.yml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment-v1.yaml
  - deployment-v2.yaml
patchesStrategicMerge:
  - overlay.yaml

But it is not working as it is expecting a name to match the target and totally ignoring namePrefix. Any help is appreciated.

答案1

得分: 1

你可以使用补丁中的 target 属性来将补丁应用于多个资源。根据您的示例(在我评论中指出错误后进行修正),我们可以编写一个像这样的 kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment-v1.yaml
- deployment-v2.yaml

patches:
  - target:
      kind: Deployment
    patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: this_value_is_ignored
      spec:
        replicas: 10      

target 属性控制了此补丁将应用于哪些资源。使用上述配置运行 kustomize build 会得到以下结果:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: nginx-deployment
spec:
  replicas: 10
.
.
.

以及:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: nginx-v1
spec:
  replicas: 10
.
.
.

上述配置将会将补丁应用于 所有 在您的 kustomization 中的部署。如果您想将补丁限制为仅适用于名称前缀匹配的部署,您可以编写如下:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment-v1.yaml
- deployment-v2.yaml

patches:
  - target:
      kind: Deployment
      name: nginx.*
    patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: this_value_is_ignored
      spec:
        replicas: 10      

请注意,name 模式是正则表达式,而不是 shell 风格的通配符。

英文:

You can apply a patch to multiple resources using the target attribute in a patch. Given your examples (after fixing the errors I pointed out in my comment), we can write a kustomization.yaml like this:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment-v1.yaml
- deployment-v2.yaml

patches:
  - target:
      kind: Deployment
    patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: this_value_is_ignored
      spec:
        replicas: 10

The target attribute controls to what resources this patch will apply. With the above configuration, running kustomize build results in:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: nginx-deployment
spec:
  replicas: 10
.
.
.

and:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: nginx-v1
spec:
  replicas: 10
.
.
.

The above configuration would apply the patch to all deployments in your kustomization. If you wanted to limit the patching to only deployments matching a specific name prefix, you could write:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment-v1.yaml
- deployment-v2.yaml

patches:
  - target:
      kind: Deployment
      name: nginx.*
    patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: this_value_is_ignored
      spec:
        replicas: 10

Note that the name pattern is regular expression, not a shell-style glob.

huangapple
  • 本文由 发表于 2023年7月13日 18:13:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678256.html
匿名

发表评论

匿名网友

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

确定