简体   繁体   English

在 kustomize 中修补特定资源

[英]Patch specific resource in kustomize

I have a base resource that gets imported in an overlay, which in the overlays also makes other resources from the same base but with a different suffix.我有一个基础资源,它在叠加层中导入,叠加层中的其他资源也来自相同的基础但具有不同的后缀。 In the overlay, the base resource needs to be patched without affecting the other newly created resources.在叠加层中,需要对基础资源进行修补,而不会影响其他新创建的资源。 However, all three of them are changed.然而,这三个人都变了。 How can I just modify the one I am intending to modify?我怎样才能修改我打算修改的那个?

Below is an example of this.下面是一个例子。 The base resource is a Deployment with 1 replica.基本资源是具有 1 个副本的部署。 In the overlay, the base is added as a resource but I try to patch it and set the replicas to 0. Unfortunately, all replicas of all deployments get set to 0.在叠加层中,基础作为资源添加,但我尝试对其进行修补并将副本设置为 0。不幸的是,所有部署的所有副本都设置为 0。

Base根据

EX_HOME=$(mktemp -d)
BASE=$EX_HOME/base
mkdir $BASE

cat <<EOF >$BASE/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
  labels:
    app: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - name: example
          image: alpine:latest
          command: ["sh","-c", "sleep 1h"]
EOF

cat <<EOF>$BASE/kustomization.yaml
resources:
- deployment.yaml
EOF

Overlay覆盖

OVERLAYS=$EX_HOME/overlays
mkdir -p $OVERLAYS/srv0
mkdir -p $OVERLAYS/srv1

cat <<EOF>$OVERLAYS/kustomization.yaml
resources:
  - svr0
  - svr1
  - ../base

patches:
  - patch: |-
      - op: replace
        path: /spec/replicas
        value: 0
    target:
      kind: Deployment
      group: apps
      version: v1
      name: example
EOF

cat <<EOF>$OVERLAYS/svr0/kustomization.yaml
resources:
  - ../../base
nameSuffix: -svr0
EOF

cat <<EOF>$OVERLAYS/svr0/kustomize.yaml
resources:
  - ../../base
nameSuffix: -svr1
EOF

The following should work.以下应该工作。 I took the liberty to re-organize a bit the directory structure to make it clearer.我冒昧地重新组织了一下目录结构以使其更清晰。 Since you only update the replica count I also switched to the replicas transformer instead of a patch:由于您只更新副本计数,我还切换到副本转换器而不是补丁:

base/kustomization.yaml : base/kustomization.yaml

resources:
  - deployment.yaml

overlays/svr0/kustomization.yaml : overlays/svr0/kustomization.yaml

resources:
  - ../../base

nameSuffix: -svr0

overlays/svr1/kustomization.yaml : overlays/svr1/kustomization.yaml

resources:
  - ../../base

nameSuffix: -svr1

`overlays/all/kustomization.yaml`:
```yaml
resources:
  - ../../base
  - ../svr0
  - ../svr1

replicas:
  - name: example-svr0
    count: 0
  - name: example-svr1
    count: 0

On a side note: There is two ways to go about it.附带说明:关于它的 go 有两种方法。 Here I add the replicas transformer at the top.在这里,我在顶部添加了replicas转换器。 You could also add it once in overlays/svr0/kustomization.yaml and once in verlays/svr1/kustomization.yaml instead.您也可以在overlays/svr0/kustomization.yamlverlays/svr1/kustomization.yaml中添加一次。 If you do so the transformer will become:如果这样做,变压器将变为:

replicas:
  - name: example
    count: 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM