use Patch API to label namespace to avoid conflicts
While labeling 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:
		| @@ -18,6 +18,7 @@ package apimachinery | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"strings" | 	"strings" | ||||||
| @@ -2127,13 +2128,13 @@ func labelNamespace(f *framework.Framework, namespace string) { | |||||||
| 	client := f.ClientSet | 	client := f.ClientSet | ||||||
|  |  | ||||||
| 	// Add a unique label to the namespace | 	// Add a unique label to the namespace | ||||||
| 	ns, err := client.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{}) | 	nsPatch, err := json.Marshal(map[string]interface{}{ | ||||||
| 	framework.ExpectNoError(err, "error getting namespace %s", namespace) | 		"metadata": map[string]interface{}{ | ||||||
| 	if ns.Labels == nil { | 			"labels": map[string]string{f.UniqueName: "true"}, | ||||||
| 		ns.Labels = map[string]string{} | 		}, | ||||||
| 	} | 	}) | ||||||
| 	ns.Labels[f.UniqueName] = "true" | 	framework.ExpectNoError(err, "error marshaling namespace %s", namespace) | ||||||
| 	_, err = client.CoreV1().Namespaces().Update(context.TODO(), ns, metav1.UpdateOptions{}) | 	_, err = client.CoreV1().Namespaces().Patch(context.TODO(), namespace, types.StrategicMergePatchType, nsPatch, metav1.PatchOptions{}) | ||||||
| 	framework.ExpectNoError(err, "error labeling namespace %s", namespace) | 	framework.ExpectNoError(err, "error labeling namespace %s", namespace) | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Chaitanya Bandi
					Chaitanya Bandi