mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
use Patch API to add namespace annotations
While adding annotations to the namespace, using the Update API may result in conflicts as "the object has been modified; please apply your changes to the latest version and try again". Use Patch API to avoid this. Signed-off-by: Chaitanya Bandi <kbandi@cs.stonybrook.edu>
This commit is contained in:
parent
fcee7a0105
commit
93be90da9a
@ -19,6 +19,7 @@ package apps
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -137,7 +138,7 @@ var _ = SIGDescribe("Daemon set [Serial]", func() {
|
|||||||
|
|
||||||
c = f.ClientSet
|
c = f.ClientSet
|
||||||
|
|
||||||
updatedNS, err := updateNamespaceAnnotations(c, ns)
|
updatedNS, err := patchNamespaceAnnotations(c, ns)
|
||||||
framework.ExpectNoError(err)
|
framework.ExpectNoError(err)
|
||||||
|
|
||||||
ns = updatedNS.Name
|
ns = updatedNS.Name
|
||||||
@ -884,24 +885,24 @@ func clearDaemonSetNodeLabels(c clientset.Interface) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateNamespaceAnnotations sets node selectors related annotations on tests namespaces to empty
|
// patchNamespaceAnnotations sets node selectors related annotations on tests namespaces to empty
|
||||||
func updateNamespaceAnnotations(c clientset.Interface, nsName string) (*v1.Namespace, error) {
|
func patchNamespaceAnnotations(c clientset.Interface, nsName string) (*v1.Namespace, error) {
|
||||||
nsClient := c.CoreV1().Namespaces()
|
nsClient := c.CoreV1().Namespaces()
|
||||||
|
|
||||||
ns, err := nsClient.Get(context.TODO(), nsName, metav1.GetOptions{})
|
annotations := make(map[string]string)
|
||||||
|
for _, n := range NamespaceNodeSelectors {
|
||||||
|
annotations[n] = ""
|
||||||
|
}
|
||||||
|
nsPatch, err := json.Marshal(map[string]interface{}{
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"annotations": annotations,
|
||||||
|
},
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if ns.Annotations == nil {
|
return nsClient.Patch(context.TODO(), nsName, types.StrategicMergePatchType, nsPatch, metav1.PatchOptions{})
|
||||||
ns.Annotations = make(map[string]string)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, n := range NamespaceNodeSelectors {
|
|
||||||
ns.Annotations[n] = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return nsClient.Update(context.TODO(), ns, metav1.UpdateOptions{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setDaemonSetNodeLabels(c clientset.Interface, nodeName string, labels map[string]string) (*v1.Node, error) {
|
func setDaemonSetNodeLabels(c clientset.Interface, nodeName string, labels map[string]string) (*v1.Node, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user