0

I can patch my ingress resource from cli with kubectl running following command:

kubectl patch ingress ingress-resource --type=json -p='[{"op": "replace", "path": "/spec/rules/0/http/paths/0/backend/service/name", "value":"node-app-blue-helm-chart"}]' 

When I add following step to my cloudbuild.yaml and execute, it fails with following error.

Step #3: Running: kubectl patch ingress ingress-resource --type=json -p='[{"op": "replace", "path": "/spec/rules/0/http/paths/0/backend/service/name", "value":"node-app-blue-helm-chart"}]' 
Step #3: Error from server (BadRequest): json: cannot unmarshal string into Go value of type jsonpatch.Patch
Finished Step #3

Step I used:

  - name: 'gcr.io/cloud-builders/kubectl'
    args:
    - 'patch'
    - 'ingress'
    - 'ingress-resource'
    - '--type=json'
    - '-p=''[{"op": "replace", "path": "/spec/rules/0/http/paths/0/backend/service/name", "value":"node-app-green-helm-chart"}]'' '
    env:
    - 'CLOUDSDK_COMPUTE_ZONE=----'
    - 'CLOUDSDK_CONTAINER_CLUSTER=----'

What can be missing?

Pit
  • 736
  • 3
  • 17
bcan
  • 505
  • 1
  • 4
  • 14
  • Can you check the version of `gcr.io/cloud-builders/kubectl` in Cloud Build? Sometime there are old versions in cache and that can be the root of your issue. – guillaume blaquiere Jun 10 '21 at 18:33
  • 1
    I've been researching about it and, a part of what @guillaumeblaquiere says that it could be because the version, I have found that sometimes the issue with this error is the way we write it. In this case I found another Steps files where the part of the arg and env are written between claudators, (https://stackoverflow.com/questions/48650734/passing-a-json-string-command-arg-in-a-yaml-file), maybe you could try it in your case :) – Vicky Jun 11 '21 at 08:34
  • @Vicky The solution on the link didn't work itself but I iterated on solution and divided parameter and the problem solved. – bcan Jun 23 '21 at 05:09

1 Answers1

0

To solve this problem I tried to escape all " as mentioned here (Passing a json-string command arg in a YAML file). It didn't solve problem. But then I also divided "-p" and the rest and also used " on second part. So working step became like this:

  - name: 'gcr.io/cloud-builders/kubectl'
    args:
    - "patch"
    - "ingress"
    - "ingress-resource"
    - "--type=json"
    - "-p"
    - "[{\"op\": \"replace\", \"path\": \"/spec/rules/0/http/paths/0/backend/service/name\", \"value\":\"node-app-green-helm-chart\"}]"
    env:
    - 'CLOUDSDK_COMPUTE_ZONE=----'
    - 'CLOUDSDK_CONTAINER_CLUSTER=----'
bcan
  • 505
  • 1
  • 4
  • 14