3

Trying to use the kustomize to patch a Kubernetes resource. However, the order/sequence of the initContainers list is different in the output.

For example, the input is

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"]

after the patch, the output become

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

Have tried with the --reorder argument but doesn't help.

Version tested:

{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
ChinKang
  • 4,212
  • 2
  • 17
  • 29

2 Answers2

0

It's actually a (IMHO) bug introduced with Kustomize >3.0.

see: https://github.com/kubernetes-sigs/kustomize/issues/3912#issuecomment-1022218731 quote severinraez of Github:

"the workaround I'm using for now (v4.4.0) is to reference the initContainers in their correct order in the patch, but not change them"

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: add all init-containers in the desired order to the patch

Markus
  • 1,887
  • 18
  • 23
-1

This is a non-issue. The order is different because you've inverted it in your pod-patch.yaml.

In source.yaml, the order of the initContainers is [init-mydb, init-myservice]. In pod-patch.yaml it's [init-myservice, init-mydb].

DasSuper
  • 19
  • 3