4

经过一番搜索后,我找不到使用任何策略在 Patch 执行的 golang Kube 客户端示例...我正在寻找执行此操作的 golang 示例:

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

谁能给我举个例子?谢谢。

4

1 回答 1

6

所以,我想我有一个通过 kubectl 资源 helper.go 代码挖掘后工作的例子,这里是:

首先,创建一个这样的结构:

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

然后创建一个数组:

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

然后将该数组转换为包含数据结构的 JSON 版本的字节数组:

patchBytes, err4 := json.Marshal(things)

最后,调用这个 API 来执行这种类型的补丁:

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

这大致相当于这个 kubectl 命令:

kubectl patch pgupgrades junk --type='json' -p='[{"op":"replace", "path":"/spec/ccpimagetag","value":"newimage"}]'
于 2017-04-14T20:33:12.653 回答