kubernetes go客户端补丁示例 [英] kubernetes go client patch example
问题描述
经过一些搜索后,我找不到使用任何策略在Patch上执行的golang Kube客户端示例...我正在寻找执行此操作的golang示例:
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"}]'
我正在使用 https://github.com/kubernetes/client-go v2.0.0
I'm using https://github.com/kubernetes/client-go v2.0.0
有人可以给我指出一个例子吗?谢谢.
can anyone point me to an example? thanks.
推荐答案
所以,我认为在通过kubectl资源helper.go代码进行挖掘之后,我有一个示例可以工作,这里是:
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"
然后将数组转换为包含JSON版本的bytes数组 数据结构:
then convert the array into a bytes array holding a JSON version of the data structure:
patchBytes, err4 := json.Marshal(things)
最后,请执行以下API调用以执行此类修补程序:
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()
这大致等效于以下kubectl命令:
this is roughly equivalent to this kubectl command:
kubectl patch pgupgrades junk --type='json' -p='[{"op":"replace", "path":"/spec/ccpimagetag","value":"newimage"}]'
这篇关于kubernetes go客户端补丁示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!