Having trouble to patch a job in kubernetes using postman

7/12/2019

Created a job in kubernetes using post method using postman, now trying to patch the same job using patch method using postman and i am getting 400 bad request.

Headers for post: Content-type application/yaml

post method body:

---
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
  labels:
    app: dev
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4

Headers for patch : Content-type application/strategic-merge-patch+json

Content-Type in Headers

patch request body:

{
  "apiVersion": "batch/v1",
  "kind": "Job",
  "metadata": {
    "name": "pi"
  },
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "name": "pi",
            "image": "perl",
            "command": [
              "perl",
              "-Mbignum=bpi",
              "-wle",
              "print bpi(2000)"
            ]
          }
        ],
        "restartPolicy": "Never"
      }
    },
    "backoffLimit": 5
  }
}

Changed body please check the link. body of the patch request

Modification i did was changed backofflimit to 5. I was able to post and patch other resources like services and deployments but i am stuck at patching a job, I followed exactly same steps for others as well.

error i am getting error

-- naveen kumar
kubernetes
kubernetes-apiserver
kubernetes-jobs

2 Answers

7/17/2019

With Content-Type as application/strategic-merge-patch+json

and with body

{ "spec": { "backoffLimit": 7 }}

I have recreated the request and it worked fine.

Things that went wrong first time is that even though "content-type" is mentioned in headers correctly as "application/strategic-merge-patch+json" postman is taking a wrong Content-type, so i deleted the request and recreated the patch request with same body and headers it worked perfectly.

-- naveen kumar
Source: StackOverflow

7/12/2019

With 'strategic-merge-patch+json' type you should use in the Request body just this json patch:

{"spec":{"backoffLimit": 7}}

As your intent is to merge/replace existing object's value based on backoffLimit key.
Check documentation for example usage of strategic-merge-patch to update resources.

Update Please try yourself with curl:

  1. Start local Kubernetes proxy server: kubectl proxy
  2. curl -k -v -XPATCH -H "Content-Type: application/strategic-merge-patch+json" --data '{"spe":{"backoffLimit": 9}}' http://localhost:8001/apis/batch/v1/namespaces/default/jobs/pi

Output:

< HTTP/1.1 200 OK <-patch succeeded 
< Audit-Id: 02d97d05-2bfb-4500-ac34-c8eb04ff8503
< Content-Length: 1795
< Content-Type: application/json
< Date: Fri, 12 Jul 2019 13:54:37 GMT
< 
{
  "kind": "Job",
  "apiVersion": "batch/v1",
  "metadata": {
    "name": "pi",
    "namespace": "default",
    "selfLink": "/apis/batch/v1/namespaces/default/jobs/pi",
    "uid": "5ac61d6d-a4a0-11e9-abc7-42010a80012c",
    "resourceVersion": "4339038",
    "creationTimestamp": "2019-07-12T12:27:03Z",
    "labels": {
      "app": "dev"
    }
  },
  "spec": {
    "parallelism": 1,
    "completions": 1,
    "backoffLimit": 9 <-patch succeeded 

,

-- Nepomucen
Source: StackOverflow