Kubernetes JSON Deployment patch definition

6/1/2019

I am trying to patch a deployment, using below command.I trying to set environment variable in the pod by passing existing value from the metadata label definition of the my yaml.

Patching Command

kubectl patch deployment deploytest3 --type=json -p='[{"op": "add", "path": "/spec/template/spec/containers/1/env/2","value": {"name": "CUSTOMERID","valueFrom": {"fieldRef": {"fieldPath": "metadata.labels['customerid']"}}}}]'

Below is my Yaml for reference.(This is for reference not the original)

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "10"
  creationTimestamp: 2019-01-22T11:24:17Z
  generation: 10
  labels:
    app: my-cloud
  name: deploytest3
  namespace: default
  resourceVersion: "60076984"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/deploytest3
  uid: 41950c24-1e38-11e9-ba0c-42010a8000d6
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: my-cloud
      customerid: "gx0388d"
      customername: deploytest3
      environment: dev
      tier: backend
      version: 11.0-latest
  strategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-cloud
        customerid: "gx0388d"
        customername: deploytest3
        environment: dev
        tier: backend
        version: 11.0-latest
    spec:
      containers:
      - args:
        - -c
        - /bin/aSoftMS --simple-media-server;/bin/core_find.sh
        command:
        - /bin/bash
        image: us.gcr.io/data/myms:11.0-latest
        imagePullPolicy: IfNotPresent
        name: ms
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /opt/latest/system_rw/
          name: deploytest3
      - args:
        - -c
        command:
        - /bin/bash
        image: us.gcr.io/latest/core:11.0-latest
        imagePullPolicy: IfNotPresent
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/CoreDumpGen
        livenessProbe:
          failureThreshold: 3
          httpGet:
            httpHeaders:
            - name: User-Agent
              value: DeploymentAdmin
            path: /livenessstatus
            port: 8443
            scheme: HTTPS
          initialDelaySeconds: 60
          periodSeconds: 15
          successThreshold: 1
          timeoutSeconds: 15
        env:
        - name: latest_CUSTOMER
          value: abc-test
        - name: latest
          value: abc
        image: us.gcr.io/latest/latest/core2:11.0-latest
        imagePullPolicy: IfNotPresent
        name: core2
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      hostname: deploytest3
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 60
      volumes:
      - name: bucket-auth
        secret:
          defaultMode: 256
          secretName: bucket-auth
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: 2019-01-22T11:24:17Z
    lastUpdateTime: 2019-01-22T11:24:17Z
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: 2019-01-22T11:24:17Z
    lastUpdateTime: 2019-05-31T13:49:03Z
    message: ReplicaSet "deploytest3-7ddbc9b9c4" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 10
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

Below is error, i am getting while executing the above shared command

The Deployment "deploytest3" is invalid: spec.template.spec.containers[2].env[2].valueFrom.fieldRef.fieldPath: Invalid value: "metadata.labels[customerid]": error converting fieldPath:
 field label not supported: metadata.labels[customerid]

I am manually add the environment variable in the deployment it works. I think i am not setting correct with json format, Can someone help me.

-- Panchaxari Hiremath
kubernetes

1 Answer

6/11/2019

"fieldPath": "metadata.labels['customerid']" - is incorrect

Apparently, it should be "fieldPath": "spec.template.metadata.labels['customerid']"

This is not exactly kubectl patch, but this will also set env variable from deployments label definition

kubectl set env deployment/deploytest3 CUSTOMERID=$(kubectl get deployments deploytest3 -o jsonpath='{$.spec.template.metadata.labels.customerid}')
-- A_Suh
Source: StackOverflow