0

i would like to make a PATCH request in the OpenShift API. To make it easier, we consider that we see this in the OpenShift API :

The content can be this :

{ 
  "limits":{ 
    "cpu":"10m"
  },
  "requests":{ 
    "memory":"1G"
  }
}

Or this :

{ 
  "limits":{ 
    "cpu":"30m",
    "memory":"2G"
  },
  "requests":{ 
    "memory":"5G"
  }
}

Or ..., You see what i mean ;) Keys from the Json can be or cannot be.

So now, i would like to know how to make a Patch without having to know what was in in the past, because the code will only the receive new values, empty or not.

I hope to be clear :) I use JDK 13 and Spring with Maven

Thanks

Jaime S
  • 1,488
  • 1
  • 16
  • 31
  • I don't think this has anything to do with spring. This depends on what you're using to track the relevant resources. If you're using a database, you could read the old version out, update _only_ the fields contained in the given JSON, and then commit it back. If you're using a `Map` you do the same after retrieving the old instance from it. Etc. – BeUndead Feb 13 '20 at 14:16
  • I can't know what was there before, i only have new values into a json –  Feb 13 '20 at 14:19
  • Right, but what are you wanting to do with those values... Presumably update some existing resource with them, in which case my above answer still stands. – BeUndead Feb 13 '20 at 14:25
  • I know how i will do it, i will make a new http get into the api to know what was there, and then i will be able to identify what is new or not :) –  Feb 13 '20 at 14:28
  • I've never tried but I think that newest version of Openshift API supports json-patch so probably you don't even need to know the existing parameters, just send the new ones { "op": "replace", ... }, – Jaime S Feb 13 '20 at 14:52

2 Answers2

0

The answer : Get the old data by making a get method to the API which contains the old date. And then, i will be able to know changes for patching ! :)

0

The most appropriate solution seems to be making your requests as json-patch requests. Formatting Docs: http://jsonpatch.com/

In my example, I am updating the "image" property of my DeploymentConfig, but this should work for other kinds of objects. Note that you can make more than one patch command per request, and each patch command only needs to know about the properties making up the path you are altering.

My request setup: configurations docs

  • Request: PATCH /apis/apps.openshift.io/v1/namespaces/$NAMESPACE/deploymentconfigs/$NAME
  • Auth: Bearer $TOKEN
  • Headers:

    • Content-Type: application/json-patch+json
    • Accept: application/json
    • Connection: close
  • Body:

[
    {
        "op": "replace", 
        "path": "/spec/template/spec/containers/0/image", 
        "value": "my-repo-image.url/my-image:v10"
    },
    { "op": "add", ... },
    { "op": "remove", ...},
    ...
]