尝试使用
kustomize
修补 Kubernetes 资源。但是,输出中 initContainers
列表的顺序/顺序不同。
例如输入为
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', "sleep 3600"]
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', "sleep 7200"]
打补丁后,输出变成
apiVersion: v1
kind: Pod
metadata:
labels:
app: myapp
name: myapp-pod
spec:
containers:
- command:
- sh
- -c
- echo The app is running! && sleep 3600
image: busybox:1.28
name: myapp-container
initContainers:
- command:
- sh
- -c
- sleep 7200
env:
- name: HTTP_ADDR
value: https://[$(HOST_IP)]:8501
image: busybox:1.28
name: init-myservice
- command:
- sh
- -c
- sleep 3600
env:
- name: HTTP_ADDR
value: https://[$(HOST_IP)]:8501
image: busybox:1.28
name: init-mydb
已尝试使用
--reorder
参数,但没有帮助。
测试版本:
{Version:kustomize/v4.1.3 GitCommit:0f614e92f72f1b938a9171b964d90b197ca8fb68 BuildDate:2021-05-20T20:52:40Z GoOs:linux GoArch:amd64}
kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- source.yaml
patches:
- path: ./pod-patch.yaml
target:
kind: Pod
name: ".*"
pod-patch.yaml
apiVersion: apps/v1
kind: Pod
metadata:
name: doesNotMatter
spec:
initContainers:
- name: init-myservice
env:
- name: HTTP_ADDR
value: https://[$(HOST_IP)]:8501
- name: init-mydb
env:
- name: HTTP_ADDR
value: https://[$(HOST_IP)]:8501
这实际上是 Kustomize >3.0 引入的一个(恕我直言)错误。
参见:https://github.com/kubernetes-sigs/kustomize/issues/3912#issuecomment-1022218731 引用 Github 的 severinraez :
“我现在使用的解决方法(v4.4.0)是在补丁中以正确的顺序引用 initContainers,但不更改它们”
apiVersion: apps/v1
kind: Pod
metadata:
name: doesnotmatter
spec:
template:
spec:
initContainers:
- name: init-mydb # required so order does not change
- name: init-myservice
command:
- "/usr/bin/bash"
- "-c"
- "do what ever needs to be done"
[...]
tl;dr:按所需顺序将所有初始化容器添加到补丁中
这不是问题。顺序不同,因为您在
pod-patch.yaml
中颠倒了它。
在
source.yaml
中,initContainers
的顺序是[init-mydb, init-myservice]
。在 pod-patch.yaml
中,它是 [init-myservice, init-mydb]
。