我有base/foo.yaml,我想将其应用于我的所有环境,它部分看起来像这个
Kubernetes:
deploymentPatches:
- patch: |-
- {"op": "add", "path": "/spec/template/spec/volumes/-", "value": {"name": "volume", "secret": {"secretName": "my-secret"}}}
- {"op": "add", "path": "/spec/template/spec/containers/0/volumeMounts/-", "value": {"mountPath": "/connections", "name": "volume"}}
现在我也想添加特定于环境的补丁,所以我在运行例如kubectl kustomize accept
后的最终目标应该是
Kubernetes:
deploymentPatches:
- patch: |-
- {"op": "add", "path": "/spec/template/spec/volumes/-", "value": {"name": "volume", "secret": {"secretName": "my-secret"}}}
- {"op": "add", "path": "/spec/template/spec/containers/0/volumeMounts/-", "value": {"mountPath": "/connections", "name": "volume"}}
- {"op": "add", "path": "/spec/template/spec/containers/0/env/-", "value": {"name": "MY_ENVIRONMENT", "value": "accept"}}
或
Kubernetes:
deploymentPatches:
- patch: |-
- {"op": "add", "path": "/spec/template/spec/volumes/-", "value": {"name": "volume", "secret": {"secretName": "my-secret"}}}
- {"op": "add", "path": "/spec/template/spec/containers/0/volumeMounts/-", "value": {"mountPath": "/connections", "name": "volume"}}
- patch: |-
- {"op": "add", "path": "/spec/template/spec/containers/0/env/-", "value": {"name": "MY_ENVIRONMENT", "value": "accept"}}
我该如何设置kustosize配置来实现这一点?到目前为止,我已经尝试了以下内容:
kustomize.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../base
patchesStrategicMerge:
- foo.yaml
接受/foo.yaml
Kubernetes:
deploymentPatches:
- patch: |-
- {"op": "add", "path": "/spec/template/spec/containers/0/env/-", "value": {"name": "MY_ENVIRONMENT", "value": "accept"}}
战略合并补丁文档中解释了我遇到的问题;
在标准JSON合并补丁中,JSON对象总是被合并,但列表总是被替换。通常这不是我们想要的。
为了解决这个问题,战略合并补丁程序使用API对象的go struct标记来确定哪些列表应该合并,哪些列表不应该合并。
这就是使用patchesStrategicMerge
添加其他容器(如果要附加的容器使用不同的名称(的原因。
然而,我试图将项目附加到的对象和列表并不是这样设置的,因此只是被替换了。
由于我无法更改设置,因此我的解决方案是使用patchesJson6902
。
补丁.yaml
- op: add
path: /spec/Kubernetes/deploymentPatches/-
value:
patch: |-
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: MY_ENVIRONMENT
value: accept
kustomation.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../base
patchesJson6902:
- target:
group: mygroup
version: v1
kind: myobject
name: myname
path: patch.yaml