Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Parche la etiqueta de kubernetes con el carácter "/"

Tengo el siguiente código que funciona bien. Agrega la etiqueta example: yes en el objeto kubernetes:

 package main import ( "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) } } }

El problema es que necesito agregar la etiqueta example/test , que contiene el carácter / , que creo que es el origen de mi problema. Al ejecutar el código anterior con el payload:

 payload := []patchStringValue{{ Op: "replace", Path: "/metadata/labels/test/example", Value: "yes", }}

Me sale el error: "the server rejected our request due to an error in our request" .

Sé que una forma alternativa es usar Update en lugar de Patch . Pero, ¿hay alguna solución a este problema usando Patch ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

De acuerdo con la especificación de notación de puntero JSON que usa el parche JSON, debe usar ~1 para codificar / . Entonces su carga útil sería la siguiente:

 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]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!