oc patch replace image name error " json: cannot unmarshal object into Go value of type jsonpatch.Patch"

8/3/2020

I need apply a oc patch to the following deploy, changing de value of "image". But I can´t do that, caused by an error:

DEPLOY YML:

root@oc-jump-pod:/# oc get  deploy deploy-test -o json
{
    "apiVersion": "extensions/v1beta1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "3",
            "meta.helm.sh/release-name": "teste",
            "meta.helm.sh/release-namespace": "sda-test"
        },
        "creationTimestamp": "2020-05-25T07:01:14Z",
        "generation": 23,
        "labels": {
            "app.kubernetes.io/instance": "test",
            "app.kubernetes.io/managed-by": "Helm",
            "app.kubernetes.io/name": "test",
            "app.kubernetes.io/version": "latest",
            "helm.sh/chart": "test-1.0.0"
        },
        "name": "test",
        "namespace": "test-1",
        "resourceVersion": "19316664",
        "selfLink": "/apis/extensions/v1beta1/namespaces/test/deployments/test",
        "uid": "863d7397"
    },
    "spec": {
        "progressDeadlineSeconds": 600,
        "replicas": 1,
        "revisionHistoryLimit": 10,
        "selector": {
            "matchLabels": {
                "app.kubernetes.io/instance": "test",
                "app.kubernetes.io/name": "test"
            }
        },
        "strategy": {
            "rollingUpdate": {
                "maxSurge": "25%",
                "maxUnavailable": "25%"
            },
            "type": "RollingUpdate"
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "app.kubernetes.io/instance": "test",
                    "app.kubernetes.io/name": "test"
                }
            },
            "spec": {
                "containers": [
                    {
                        "env": [
                            {
                                "name": "APP_NAME",
                                "value": "test"
                            },
                            {
                                "name": "JAVA_OPTS_EXT",
                                "value": "-Djava.security.egd=file:/dev/./urandom -Dcom.sun.net.ssl.checkRevocation=false  -Djavax.net.ssl.trustStore=/etc/truststore/jssecacerts -Djavax.net.ssl.trustStorePassword=changeit"
                            },
                            {
                                "name": "SPRING_CLOUD_CONFIG_PROFILE",
                                "value": "pre"
                            },
                            {
                                "name": "TZ",
                                "value": "Europe/Madrid"
                            },
                            {
                                "name": "WILY_MOM_PORT",
                                "value": "5001"
                            },
                            {
                                "name": "spring_application_name",
                                "value": "test"
                            },
                            {
                                "name": "spring_cloud_config_uri",
                                "value": "https://config.test.svc.cluster.local"
                            }
                        ],
                        "image": "registry.sdi.dev.weu.azure.paas.cloudcenter.corp/test-dev/test-java:0.0.2",
                        "imagePullPolicy": "Always",
                        "name": "test",
                        "ports": [
                            {
                                "containerPort": 8080,
                                "protocol": "TCP"
                            }
  ],
                        "resources": {
                        ...

I´m triying the following:

root@oc-jump-pod:/# oc patch deploy push-engine --type='json' -p='{"spec":{"template":{"metadata":{"spec":{"containers":{"image":"registry.sdi.
dev.weu.azure.paas.cloudcenter.corp/test-dev/test:0.0.1"}}}}}}'
Error from server (BadRequest): json: cannot unmarshal object into Go value of type jsonpatch.Patch

and this to get the value

root@oc-jump-pod:/# oc get deploy push-engine -o=jsonpath='{..image}'
registry.sdi.dev.weu.azure.paas.cloudcenter.corp/test-dev/test-java:0.0.2

I need do that, to change the tag of the image from 0.0.2 to 0.0.1 (or others). Probably I don´t understand oc patch yet, actually I do the change manually on the oc console. But this method, is rude and not follow the CI/CD.

-- RCat
json
kubernetes
openshift

1 Answer

8/3/2020

The correct JSON Patch document for your Deployment may look like this:

[
    {
        "op": "replace",
        "path": "/spec/template/spec/containers/0/image",
        "value": "registry.sdi.dev.weu.azure.paas.cloudcenter.corp/test-dev/test:0.0.1"
    }
]

Your example is not going to work as it doesn't reflect the structure of your original yaml file. Note that it contains arrays [...] and you treated it as if it contained only maps {...}.

Your final oc patch command may look as follows:

oc patch deploy push-engine --type='json' -p '[{ "op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "registry.sdi.dev.weu.azure.paas.cloudcenter.corp/test-dev/test:0.0.1" }]'
-- mario
Source: StackOverflow