kubernetes go client patch example

4/14/2017

after some searching I'm unable to find a golang Kube client example that performs at Patch using any strategy...I'm looking for a golang example of doing this:

kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'

I'm using https://github.com/kubernetes/client-go v2.0.0

can anyone point me to an example? thanks.

-- jeff mccormick
kubernetes-go-client

1 Answer

4/14/2017

so, I think I have an example working after digging thru the kubectl resource helper.go code, here it is:

first, create a structure like this:

type ThingSpec struct {
        Op    string `json:"op"`
        Path  string `json:"path"`
        Value string `json:"value"`
}

then create an array of those:

 things := make([]ThingSpec, 1)
        things[0].Op = "replace"
        things[0].Path = "/spec/ccpimagetag"
        things[0].Value = "newijeff"

then convert the array into a bytes array holding a JSON version of the data structure:

patchBytes, err4 := json.Marshal(things)

Lastly, make this API call to perform this type of patch:

result, err6 := tprclient.Patch(api.JSONPatchType).
        Namespace(api.NamespaceDefault).
        Resource("pgupgrades").
        Name("junk").
        Body(patchBytes).
        Do().
        Get()

this is roughly equivalent to this kubectl command:

kubectl patch pgupgrades junk --type='json' -p='[{"op":"replace", "path":"/spec/ccpimagetag","value":"newimage"}]'
-- jeff mccormick
Source: StackOverflow