我有以下代碼工作正常。它將標簽添加到 kubernetes 對象中:example: yespackage mainimport ( "fmt" "encoding/json" "k8s.io/apimachinery/pkg/types" eksauth "github.com/chankh/eksutil/pkg/auth" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1")type patchStringValue struct { Op string `json:"op"` Path string `json:"path"` Value string `json:"value"`}func main() { var updateErr error cfg := &eksauth.ClusterConfig{ClusterName: "my cluster name"} clientset, _ := eksauth.NewAuthClient(cfg) api := clientset.CoreV1() // Get all pods from all namespaces without the "sent_alert_emailed" label. pods, _ := api.Pods("").List(metav1.ListOptions{}) for i, pod := range pods.Items { payload := []patchStringValue{{ Op: "replace", Path: "/metadata/labels/example", Value: "yes", }} payloadBytes, _ := json.Marshal(payload) _, updateErr = api.Pods(pod.GetNamespace()).Patch(pod.GetName(), types.JSONPatchType, payloadBytes) if updateErr == nil { fmt.Println(fmt.Sprintf("Pod %s labelled successfully.", pod.GetName())) } else { fmt.Println(updateErr) } }}問題是我需要添加標簽,其中包含字符,我認為這是我問題的根源。使用有效負載執(zhí)行前面的代碼時:example/test/ payload := []patchStringValue{{ Op: "replace", Path: "/metadata/labels/test/example", Value: "yes", }}我收到錯誤:。"the server rejected our request due to an error in our request"我知道另一種方法是使用而不是.但是,使用這個問題有什么解決方案嗎?UpdatePatchPatch
1 回答

寶慕林4294392
TA貢獻2021條經(jīng)驗 獲得超8個贊
根據(jù) JSON 補丁使用的 JSON 指針表示法規(guī)范,您需要使用 來編碼 。因此,您的有效負載將如下所示:~1/
payload := []patchStringValue{{
Op: "replace",
Path: "/metadata/labels/test~1example",
Value: "yes",
}}
# kubectl patch deploy mydeployment --type='json' -p='[{"op": "replace", "path": "/metadata/labels/example~1test", "value":"yes"}]'
deployment.apps/mydeployment patched
# kubectl get deploy mydeployment -o=jsonpath='{@.metadata.labels}'
map[example/test:yes]
- 1 回答
- 0 關(guān)注
- 209 瀏覽
添加回答
舉報
0/150
提交
取消