Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

kubernetes - Patching a pvc using go client

I am trying to update a pvc label using go library's patch option from this interface.

Is there any example on how to use this? Also if the label is not there will patch also add the label?

I am looking to update/add my_label in below object:

kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: "yes"
    pv.kubernetes.io/bound-by-controller: "yes"
    volume.beta.kubernetes.io/storage-class: ""
  creationTimestamp: "2021-01-25T18:53:02Z"
  finalizers:
  - kubernetes.io/pvc-protection
  labels:
      my_label: my_value
question from:https://stackoverflow.com/questions/65927298/patching-a-pvc-using-go-client

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It could be done the following way:

    persVolC := client.CoreV1().PersistentVolumeClaims("default")

    data := `
    [
      { "op": "add", "path": "/metadata/labels/mylabel", "value": "myvalue" }
    ]
    `
    updatedPvc, err := persVolC.Patch(ctx, "name-of-pvc", types.JSONPatchType, []byte(data), meta_v1.PatchOptions{})
    if err != nil {
        log.Fatal(err)
    }

Full example at go playground.

And here you can read about JSON patch standard.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...