kubernetes go client patch example

放肆的年华 提交于 2019-12-23 02:43:24

问题


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.


回答1:


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"}]'


来源:https://stackoverflow.com/questions/43415728/kubernetes-go-client-patch-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!