0

i'm trying to path deployment by using kustomize i need to change only env in the spec.template.spec.containers. but during kustomisation other fields are deleting too

tree:

k8s
|_base
  |_ deployment.yaml
  |_ kustomisation.yaml
  |_ svc.yaml
|_overlays
  |prod
    |_ kustomisation.yaml
    |_ patches.yaml
  |_stage
  |_dev

base.deployment.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: __NAME__
  name: __NAME__
  namespace: __NAME__
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/name: __NAME__
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: __NAME__
        app.kubernetes.io/name: __NAME__
    spec:
      containers:
      - name: __NAME__
        image: __IMAGE__ 
        env:
          - name: name_
            value: value_
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 15
          periodSeconds: 20
          successThreshold: 1
          timeoutSeconds: 1
        ports:
        - containerPort: 8081
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          limits:
            cpu: 1500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 128Mi
        securityContext:
          runAsGroup: 1001
          runAsUser: 1001
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

overlays.prod.kustomization.yaml

resources:
- ../../base/
patches:
- target:
    kind: Deployment
    name: __NAME__
  path: patches.yaml

overlays.prod.patches.yaml

kind: Deployment
metadata:
  name: __NAME__
spec:
  template:
    spec:
      containers:
      - name:  __NAME__
        env:
        - name: ENV_VAR_NAME_1
          value: new_value_1
        - name: ENV_VAR_NAME_2
          value: new_value_2

after the command kubectl kustomize overlays/prod/ -o stage_test.yaml output is

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: __NAME__
  name: __NAME__
  namespace: __NAME__
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/name: __NAME__
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: __NAME__
        app.kubernetes.io/name: __NAME__
    spec:
      containers:
      - env:
        - name: ENV_VAR_NAME_1
          value: new_value_1
        - name: ENV_VAR_NAME_2
          value: new_value_2
        name: __NAME__
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

as you can see kustomise - deleting probes, ports and othes stuff

How to avoid deleting? i need only to change envs. ofc if i put all other stuff into patches.yaml kustomize will add it into output

tried to set annotation annotations: kustomize.kubernetes.io/ignore-patches: "true" and run kustomize with it (not worked - same output)

pathes to patchesStrategicMerge (worked identically)

versions if it's matter:

  • Kustomize Version: v4.5.7

  • Client Version: 1.26

  • Platform: darwin/arm64

  • There are some indentation problems in `patches.yaml` and `overlays/prod/kustomization.yaml` that render them invalid; you should fix those in your question. – larsks Mar 27 '23 at 14:43
  • @larsks fixed here, in the code i've got normal indentation – Ivan Sokolov Mar 27 '23 at 14:50

1 Answers1

0

The root cause here is that you've defined your Deployment as:

apiVersion: extensions/v1beta1
kind: Deployment

But that's not the correct apiVersion for a Deployment; it should be:

apiVersion: apps/v1
kind: Deployment

Support for the extensions/v1beta1 API was removed in Kubernetes 1.16.

Kustomize "knows" about a regular Kubernetes Deployment and will properly merge your configuration; with that single change (and using Kustomize v5.0.1), your patch works correctly.

Compare before the change:

$ kustomize build overlays/prod | yq .spec.template.spec.containers
[
  {
    "env": [
      {
        "name": "ENV_VAR_NAME_1",
        "value": "new_value_1"
      },
      {
        "name": "ENV_VAR_NAME_2",
        "value": "new_value_2"
      }
    ],
    "name": "__NAME__"
  }
]

To:

$ sed -i '/^apiVersion:/ s|apiVersion:.*|apiVersion: apps/v1|' base/deployment.yaml
$ kustomize build overlays/prod | yq .spec.template.spec.containers
[
  {
    "env": [
      {
        "name": "ENV_VAR_NAME_1",
        "value": "new_value_1"
      },
      {
        "name": "ENV_VAR_NAME_2",
        "value": "new_value_2"
      },
      {
        "name": "name_",
        "value": "value_"
      }
    ],
    "image": "__IMAGE__",
    "imagePullPolicy": "IfNotPresent",
    "livenessProbe": {
      "failureThreshold": 3,
      "httpGet": {
        "path": "/healthz",
        "port": 8081,
        "scheme": "HTTP"
      },
      "initialDelaySeconds": 15,
      "periodSeconds": 20,
      "successThreshold": 1,
      "timeoutSeconds": 1
    },
    "name": "__NAME__",
    "ports": [
      {
        "containerPort": 8081,
        "protocol": "TCP"
      }
    ],
    "readinessProbe": {
      "failureThreshold": 3,
      "httpGet": {
        "path": "/healthz",
        "port": 8081,
        "scheme": "HTTP"
      },
      "initialDelaySeconds": 5,
      "periodSeconds": 10,
      "successThreshold": 1,
      "timeoutSeconds": 1
    },
    "resources": {
      "limits": {
        "cpu": "1500m",
        "memory": "1Gi"
      },
      "requests": {
        "cpu": "250m",
        "memory": "128Mi"
      }
    },
    "securityContext": {
      "runAsGroup": 1001,
      "runAsUser": 1001
    },
    "terminationMessagePath": "/dev/termination-log",
    "terminationMessagePolicy": "File"
  }
]

If you are forced to use the extensions/v1beta1 API, you may want to look at this document for instructions on how to provide Kustomize with information about how to handle merges for custom resource types.

This article explores the process in more detail.

larsks
  • 277,717
  • 41
  • 399
  • 399