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

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

Kustomize: Patch multiple resources that starts with same name

问题

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

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

deployment-v1.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-v1
  5. labels:
  6. app: web
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: web
  11. replicas: 5
  12. strategy:
  13. type: RollingUpdate
  14. template:
  15. metadata:
  16. labels:
  17. app: web
  18. spec:
  19. containers:
  20. - name: nginx
  21. image: nginx
  22. ports:
  23. - containerPort: 80

deployment-v2.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: web
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: web
  11. replicas: 5
  12. strategy:
  13. type: RollingUpdate
  14. template:
  15. metadata:
  16. labels:
  17. app: web
  18. spec:
  19. containers:
  20. - name: nginx
  21. image: nginx
  22. resources:
  23. limits:
  24. memory: 200Mi
  25. requests:
  26. cpu: 100m
  27. memory: 200Mi
  28. ports:
  29. - containerPort: 80

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

overlay.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. namePrefix: nginx-
  5. spec:
  6. replicas: 10

kustomization.yml

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. resources:
  4. - deployment-v1.yaml
  5. - deployment-v2.yaml
  6. patchesStrategicMerge:
  7. - 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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-v1
  5. labels:
  6. app: web
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: web
  11. replicas: 5
  12. strategy:
  13. type: RollingUpdate
  14. template:
  15. metadata:
  16. labels:
  17. app: web
  18. spec:
  19. containers:
  20. name: nginx
  21. image: nginx
  22. ports:
  23. containerPort: 80

deployment-v2.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: web
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: web
  11. replicas: 5
  12. strategy:
  13. type: RollingUpdate
  14. template:
  15. metadata:
  16. labels:
  17. app: web
  18. spec:
  19. containers:
  20. name: nginx
  21. image: nginx
  22. resources:
  23. limits:
  24. memory: 200Mi
  25. requests:
  26. cpu: 100m
  27. memory: 200Mi
  28. ports:
  29. containerPort: 80

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

overlay.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. namePrefix: nginx-
  5. spec:
  6. replicas: 10

kustomization.yml

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. resources:
  4. - deployment-v1.yaml
  5. - deployment-v2.yaml
  6. patchesStrategicMerge:
  7. - 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

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. resources:
  4. - deployment-v1.yaml
  5. - deployment-v2.yaml
  6. patches:
  7. - target:
  8. kind: Deployment
  9. patch: |
  10. apiVersion: apps/v1
  11. kind: Deployment
  12. metadata:
  13. name: this_value_is_ignored
  14. spec:
  15. replicas: 10

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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: web
  6. name: nginx-deployment
  7. spec:
  8. replicas: 10
  9. .
  10. .
  11. .

以及:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: web
  6. name: nginx-v1
  7. spec:
  8. replicas: 10
  9. .
  10. .
  11. .

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

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. resources:
  4. - deployment-v1.yaml
  5. - deployment-v2.yaml
  6. patches:
  7. - target:
  8. kind: Deployment
  9. name: nginx.*
  10. patch: |
  11. apiVersion: apps/v1
  12. kind: Deployment
  13. metadata:
  14. name: this_value_is_ignored
  15. spec:
  16. 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:

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. resources:
  4. - deployment-v1.yaml
  5. - deployment-v2.yaml
  6. patches:
  7. - target:
  8. kind: Deployment
  9. patch: |
  10. apiVersion: apps/v1
  11. kind: Deployment
  12. metadata:
  13. name: this_value_is_ignored
  14. spec:
  15. replicas: 10

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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: web
  6. name: nginx-deployment
  7. spec:
  8. replicas: 10
  9. .
  10. .
  11. .

and:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: web
  6. name: nginx-v1
  7. spec:
  8. replicas: 10
  9. .
  10. .
  11. .

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:

  1. apiVersion: kustomize.config.k8s.io/v1beta1
  2. kind: Kustomization
  3. resources:
  4. - deployment-v1.yaml
  5. - deployment-v2.yaml
  6. patches:
  7. - target:
  8. kind: Deployment
  9. name: nginx.*
  10. patch: |
  11. apiVersion: apps/v1
  12. kind: Deployment
  13. metadata:
  14. name: this_value_is_ignored
  15. spec:
  16. 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:

确定