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

5sxhfpxr  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(105)

我想修补多个以相同名称前缀开始的部署,而不是针对特定资源。
例如,我有2个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。任何帮助都是感激的。

jhkqcmku

jhkqcmku1#

您可以在修补程序中使用target属性来执行apply a patch to multiple resources。根据您的示例(在修复了我在评论中指出的错误之后),我们可以编写如下的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
.
.
.


上述配置会将补丁程序应用于您的客户化中的 * 所有 * 部署。如果要将修补限制为仅与特定名称前缀匹配的部署,可以编写:

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样式的glob。“

相关问题