英文:
How to add init container with kustomize
问题
我理解你的要求,这是翻译好的内容:
我想要向我的所有Pod(具有特定注释的)添加一个initContainer,新添加的initContainer应该是第一个initContainer。我的补丁如下所示。
```yaml
patches:
- target:
kind: Pod
annotationSelector: "database_init=True"
patch: |-
- op: add
path: /spec/initContainers/0
value:
name: database-init
...
这对于已经有initContainer的所有Pod都可以正常工作。不幸的是,我有一些没有initContainer的Pod,补丁在那里失败并显示错误信息“add操作不适用:文档缺少路径:/spec/initContainers/0”。我应该如何编写一个适用于所有我的Pod的补丁?
完整示例:
.
├── base
│ ├── kustomization.yaml
│ ├── pod-1.yaml
│ └── pod-2.yaml
└── overlay
└── kustomization.yaml
# cat base/pod-1.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod-1
annotations:
database_init: True
spec:
initContainers:
- name: something
image: alpine:latest
command: ["sleep", "10"]
containers:
- name: main
image: alpine:latest
command: ["sleep", "60"]
# cat base/pod-2.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod-2
annotations:
database_init: True
spec:
# initContainers:
# - name: something
# image: alpine:latest
# command: ["sleep", "10"]
containers:
- name: main
image: alpine:latest
command: ["sleep", "60"]
# cat base/kustomization.yaml
resources:
- pod-1.yaml
- pod-2.yaml
# cat overlay/kustomization.yaml
resources:
- ../base
patches:
- target:
kind: Pod
annotationSelector: "database_init=True"
patch: |-
- op: add
path: /spec/initContainers/0
value:
name: database-init
image: alpine:latest
command: ["sleep", 10]
结果:
$ kubectl kustomize overlay
error: add operation does not apply: doc is missing path: "/spec/initContainers/0": missing value
在取消注释pod-2的initContainer后,一切都正常工作。
编辑:添加了annotationSelector。
编辑:添加了完整示例代码。
希望这对你有所帮助!
<details>
<summary>英文:</summary>
I want to add an initContainer to all of my pods (with a specific annotation) in my kustomize base. The newly added init container should be the first init container. My patch looks like this.
patches:
- target:
kind: Pod
annotationSelector: "database_init=True"
patch: |-- op: add
path: /spec/initContainers/0
value:
name: database-init
...
- op: add
This works fine for all pods that already have an init container. Unfortunately, I have pods without init containers and there the patch fails with the error ``add operation does not apply: doc is missing path: \"/spec/initContainers/0\"``.
How do I write a patch that works for all my pods?
Complete example:
.
├── base
│ ├── kustomization.yaml
│ ├── pod-1.yaml
│ └── pod-2.yaml
└── overlay
└── kustomization.yaml
cat base/pod-1.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod-1
annotations:
database_init: True
spec:
initContainers:
- name: something
image: alpine:latest
command: ["sleep", "10" ]
containers:
- name: main
image: alpine:latest
command: [ "sleep", "60" ]
cat base/pod-2.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod-2
annotations:
database_init: True
spec:
initContainers:
- name: something
image: alpine:latest
command: ["sleep", "10" ]
containers:
- name: main
image: alpine:latest
command: [ "sleep", "60" ]
cat base/kustomization.yaml
resources:
- pod-1.yaml
- pod-2.yaml
cat overlay/kustomization.yaml
resources:
- ../base
patches:
- target:
kind: Pod
annotationSelector: "database_init=True"
patch: |-- op: add
path: /spec/initContainers/0
value:
name: database-init
image: alpine:latest
command: [ "sleep", 10 ]
- op: add
Result:
$ kubectl kustomize overlay
error: add operation does not apply: doc is missing path: "/spec/initContainers/0": missing value
After uncommenting the init container of pod-2, everything works.
Edit: Added the annotationSelector.
Edit: Added the complete example code.
</details>
# 答案1
**得分**: 3
JSONPatch补丁通常无法处理“追加到现有列表或创建新列表”的情况。有关详细信息,请参见[此问题](https://stackoverflow.com/q/47670366/147356)。
幸运的是,使用Kustomize 5.0.1,似乎可以通过使用战略合并补丁来实现您的目标。根据您的示例配置,在`overlay/kustomization.yaml`中放置以下内容:
```yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base/
patches:
- target:
kind: Pod
annotationSelector: database_init=True
patch: |-
apiVersion: v1
kind: Pod
metadata:
name: __ignored__
spec:
initContainers:
- name: database-init
image: alpine:latest
command:
- init
- the
- database
然后运行kustomize build overlay
会产生以下输出:
apiVersion: v1
kind: Pod
metadata:
annotations:
database_init: "True"
name: mypod-1
spec:
containers:
- command:
- sleep
- "60"
image: alpine:latest
name: main
initContainers:
- command:
- init
- the
- database
image: alpine:latest
name: database-init
- command:
- sleep
- "10"
image: alpine:latest
name: something
---
apiVersion: v1
kind: Pod
metadata:
annotations:
database_init: "True"
name: mypod-2
spec:
containers:
- command:
- sleep
- "60"
image: alpine:latest
name: main
initContainers:
- command:
- init
- the
- database
image: alpine:latest
name: database-init
英文:
JSONPatch patches in general are fundamentally unable to handle the "append to existing list or create new list" situation. See e.g. this question.
Fortunately, with Kustomize 5.0.1 it appears to be possible to achieve your goal using a strategic merge patch. Given your example configuration, if I place the following in overlay/kustomization.yaml
:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base/
patches:
- target:
kind: Pod
annotationSelector: database_init=True
patch: |-
apiVersion: v1
kind: Pod
metadata:
name: __ignored__
spec:
initContainers:
- name: database-init
image: alpine:latest
command:
- init
- the
- database
Then running kustomize build overlay
produces the following output:
apiVersion: v1
kind: Pod
metadata:
annotations:
database_init: "True"
name: mypod-1
spec:
containers:
- command:
- sleep
- "60"
image: alpine:latest
name: main
initContainers:
- command:
- init
- the
- database
image: alpine:latest
name: database-init
- command:
- sleep
- "10"
image: alpine:latest
name: something
---
apiVersion: v1
kind: Pod
metadata:
annotations:
database_init: "True"
name: mypod-2
spec:
containers:
- command:
- sleep
- "60"
image: alpine:latest
name: main
initContainers:
- command:
- init
- the
- database
image: alpine:latest
name: database-init
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论