【问题标题】:kubernetes go client patch exampleKubernetes Go 客户端补丁示例
【发布时间】:2017-09-10 23:03:41
【问题描述】:

经过一番搜索,我无法找到使用任何策略在 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

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

【问题讨论】:

    标签: kubernetes-go-client


    【解决方案1】:

    所以,我想我有一个在挖掘 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"}]'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-01
      • 2016-09-05
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多