<p>In my case, I have to deploy a deployment first and then patch a preStop hook to the deployment in jenkins.</p>
<p>I try to use</p>
<pre><code>kubectl -n mobile patch deployment hero-orders-app --type &quot;json&quot; -p '[
{&quot;op&quot;:&quot;add&quot;,&quot;path&quot;:&quot;/spec/template/spec/containers/0/lifecycle/preStop/exec/command&quot;,&quot;value&quot;:[]},
{&quot;op&quot;:&quot;add&quot;,&quot;path&quot;:&quot;/spec/template/spec/containers/0/lifecycle/preStop/exec/command/-&quot;,&quot;value&quot;:&quot;/bin/sleep&quot;},
{&quot;op&quot;:&quot;add&quot;,&quot;path&quot;:&quot;/spec/template/spec/containers/0/lifecycle/preStop/exec/command/-&quot;,&quot;value&quot;:&quot;10&quot;}]'
</code></pre>
<p>but it returns</p>
<pre><code>the request is invaild
</code></pre>
<p>If patch command can add non-existence path? or I need to change another solution?</p>
<p>And here is hero-orders-app deployment file</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: hero-orders-app$SUFFIX
  namespace: $K8S_NAMESPACE
  labels:
    branch: $LABEL
    run: hero-orders-app$SUFFIX
spec:
  selector:
    matchLabels:
      run: hero-orders-app$SUFFIX
  revisionHistoryLimit: 5
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  template:
    metadata:
      labels:
        run: hero-orders-app$SUFFIX
        namespace: $K8S_NAMESPACE
        branch: $LABEL
        role: hero-orders-app
    spec:
      dnsConfig:
        options:
          - name: ndots
            value: &quot;1&quot;
      topologySpreadConstraints:
      - maxSkew: 1
        topologyKey: topology.kubernetes.io/zone
        whenUnsatisfiable: ScheduleAnyway
        labelSelector:
          matchLabels:
            run: hero-orders-app$SUFFIX
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: run
                      operator: In
                      values:
                        - hero-orders-app$SUFFIX
                topologyKey: kubernetes.io/hostname
      imagePullSecrets:
        - name: $K8S_IMAGE_SECRETS
      containers:
        - name: hero-orders-$CLUSTER_NAME
          image: $K8S_IMAGE
          imagePullPolicy: IfNotPresent
          securityContext:
            runAsUser: 1000
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - CHOWN
                - NET_RAW
                - SETPCAP
          ports:
            - containerPort: 3000
              protocol: TCP
          resources:
            limits:
              cpu: $K8S_CPU_LIMITS
              memory: $K8S_RAM_LIMITS
            requests:
              cpu: $K8S_CPU_REQUESTS
              memory: $K8S_RAM_REQUESTS
          readinessProbe:
            httpGet:
              path: /gw-api/v2/_manage/health
              port: 3000
            initialDelaySeconds: 15
            timeoutSeconds: 10
          livenessProbe:
            httpGet:
              path: /gw-api/v2/_manage/health
              port: 3000
            initialDelaySeconds: 20
            timeoutSeconds: 10
            periodSeconds: 45
</code></pre>
<p>And it running on AWS with service, pdb and hpa.</p>
<p>here is my kubectl version</p>
<pre><code>Client Version: version.Info{Major:&quot;1&quot;, Minor:&quot;21&quot;, GitVersion:&quot;v1.21.2&quot;, GitCommit:&quot;092fbfbf53427de67cac1e9fa54aaa09a28371d7&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-06-16T12:59:11Z&quot;, GoVersion:&quot;go1.16.5&quot;, Compiler:&quot;gc&quot;, Platform:&quot;darwin/amd64&quot;}
Server Version: version.Info{Major:&quot;1&quot;, Minor:&quot;20&quot;, GitVersion:&quot;v1.20.9&quot;, GitCommit:&quot;7a576bc3935a6b555e33346fd73ad77c925e9e4a&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-07-15T20:56:38Z&quot;, GoVersion:&quot;go1.15.14&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}
</code></pre>

tingyu gu
<p>I will use a simpler deployment to demonstrate patching a lifecycle hook, where you can use the same technique for your own deployment.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox
        image: busybox
        command: [&quot;ash&quot;,&quot;-c&quot;,&quot;while :; do echo $(date); sleep 1; done&quot;]
</code></pre>
<p>The path is up to <code>/spec/template/spec/containers/0/lifecycle</code> or you will get the response <strong>&quot;The request is invalid.&quot;</strong></p>
<pre><code>kubectl patch deployment busybox --type json -p '[{&quot;op&quot;:&quot;add&quot;,&quot;path&quot;:&quot;/spec/template/spec/containers/0/lifecycle&quot;,&quot;value&quot;:{&quot;preStop&quot;: {&quot;exec&quot;: {&quot;command&quot;: [&quot;/bin/sleep&quot;,&quot;10&quot;]}}}}]'

deployment.apps/busybox patched
</code></pre>
<p>Upon patched the deployment will restart. You can do a <code>kubectl get deployment busybox -o yaml</code> to examine the patched. If you patch again with same value there will be no change.</p>
