Skip to content

Commit 230a831

Browse files
mudit-01jaellio
authored andcommitted
cli: Remove metrics annotation from namespaces removed from the mesh (openservicemesh#4539)
Remove MetricsAnnotation fixes openservicemesh#4447. Update unit tests to test removal of metrics annotation. Signed-off-by: mudit singh <[email protected]> Signed-off-by: jaellio <[email protected]> Co-authored-by: jaellio <[email protected]>
1 parent ce5b195 commit 230a831

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

cmd/cli/namespace_remove.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ func (r *namespaceRemoveCmd) run() error {
8484
"%s": null
8585
},
8686
"annotations": {
87+
"%s": null,
8788
"%s": null
8889
}
8990
}
90-
}`, constants.OSMKubeResourceMonitorAnnotation, constants.IgnoreLabel, constants.SidecarInjectionAnnotation)
91+
}`, constants.OSMKubeResourceMonitorAnnotation, constants.IgnoreLabel, constants.SidecarInjectionAnnotation, constants.MetricsAnnotation)
9192

9293
_, err = r.clientSet.CoreV1().Namespaces().Patch(ctx, r.namespace, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "")
9394

cmd/cli/namespace_test.go

+38-22
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var _ = Describe("Running the namespace add command", func() {
3737
out = new(bytes.Buffer)
3838
fakeClientSet = fake.NewSimpleClientset()
3939

40-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
40+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
4141
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
4242
Expect(err).To(BeNil())
4343

@@ -69,7 +69,7 @@ var _ = Describe("Running the namespace add command", func() {
6969
_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
7070
Expect(err).To(BeNil())
7171

72-
nsSpec := createNamespaceSpec(testNamespace, "", false, true)
72+
nsSpec := createNamespaceSpec(testNamespace, "", false, true, false)
7373
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
7474
Expect(err).ToNot(HaveOccurred())
7575

@@ -97,7 +97,7 @@ var _ = Describe("Running the namespace add command", func() {
9797
_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
9898
Expect(err).To(BeNil())
9999

100-
nsSpec := createNamespaceSpec(testNamespace, testMeshName, true, false)
100+
nsSpec := createNamespaceSpec(testNamespace, testMeshName, true, false, false)
101101
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
102102
Expect(err).ToNot(HaveOccurred())
103103

@@ -129,7 +129,7 @@ var _ = Describe("Running the namespace add command", func() {
129129
_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
130130
Expect(err).To(BeNil())
131131

132-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
132+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
133133
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
134134
Expect(err).To(BeNil())
135135

@@ -173,7 +173,7 @@ var _ = Describe("Running the namespace add command", func() {
173173
_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
174174
Expect(err).To(BeNil())
175175

176-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
176+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
177177
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
178178
Expect(err).ToNot(HaveOccurred())
179179

@@ -217,7 +217,7 @@ var _ = Describe("Running the namespace add command", func() {
217217
_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
218218
Expect(err).To(BeNil())
219219

220-
nsSpec := createNamespaceSpec(testNamespace, "", true, false)
220+
nsSpec := createNamespaceSpec(testNamespace, "", true, false, false)
221221
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
222222
Expect(err).ToNot(HaveOccurred())
223223

@@ -267,11 +267,11 @@ var _ = Describe("Running the namespace add command", func() {
267267
_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
268268
Expect(err).To(BeNil())
269269

270-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
270+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
271271
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
272272
Expect(err).To(BeNil())
273273

274-
nsSpec2 := createNamespaceSpec(testNamespace2, "", false, false)
274+
nsSpec2 := createNamespaceSpec(testNamespace2, "", false, false, false)
275275
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec2, metav1.CreateOptions{})
276276
Expect(err).To(BeNil())
277277

@@ -316,7 +316,7 @@ var _ = Describe("Running the namespace add command", func() {
316316
_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
317317
Expect(err).To(BeNil())
318318

319-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
319+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
320320
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
321321
Expect(err).To(BeNil())
322322

@@ -353,7 +353,7 @@ var _ = Describe("Running the namespace add command", func() {
353353
out = new(bytes.Buffer)
354354
fakeClientSet = fake.NewSimpleClientset()
355355

356-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
356+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
357357
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
358358
Expect(err).To(BeNil())
359359

@@ -422,7 +422,7 @@ var _ = Describe("Running the namespace remove command", func() {
422422
out = new(bytes.Buffer)
423423
fakeClientSet = fake.NewSimpleClientset()
424424

425-
nsSpec := createNamespaceSpec(testNamespace, testMeshName, false, false)
425+
nsSpec := createNamespaceSpec(testNamespace, testMeshName, false, false, false)
426426
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
427427
Expect(err).To(BeNil())
428428

@@ -451,7 +451,7 @@ var _ = Describe("Running the namespace remove command", func() {
451451
})
452452
})
453453

454-
Describe("with pre-existing namespace, correct label and annotation", func() {
454+
Describe("with pre-existing namespace, correct label, injection annotation, and metrics annotation", func() {
455455
var (
456456
out *bytes.Buffer
457457
fakeClientSet kubernetes.Interface
@@ -462,7 +462,7 @@ var _ = Describe("Running the namespace remove command", func() {
462462
out = new(bytes.Buffer)
463463
fakeClientSet = fake.NewSimpleClientset()
464464

465-
nsSpec := createNamespaceSpec(testNamespace, testMeshName, true, false)
465+
nsSpec := createNamespaceSpec(testNamespace, testMeshName, true, false, true)
466466
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
467467
Expect(err).ToNot(HaveOccurred())
468468

@@ -495,6 +495,12 @@ var _ = Describe("Running the namespace remove command", func() {
495495
Expect(err).ToNot(HaveOccurred())
496496
Expect(ns.Annotations).ShouldNot(HaveKey(constants.SidecarInjectionAnnotation))
497497
})
498+
499+
It("should correctly remove the metrics annotation on the namespace", func() {
500+
ns, err := fakeClientSet.CoreV1().Namespaces().Get(context.TODO(), testNamespace, metav1.GetOptions{})
501+
Expect(err).ToNot(HaveOccurred())
502+
Expect(ns.Annotations).ShouldNot(HaveKey(constants.MetricsAnnotation))
503+
})
498504
})
499505

500506
Describe("with pre-existing namespace and incorrect label", func() {
@@ -508,7 +514,7 @@ var _ = Describe("Running the namespace remove command", func() {
508514
out = new(bytes.Buffer)
509515
fakeClientSet = fake.NewSimpleClientset()
510516

511-
nsSpec := createNamespaceSpec(testNamespace, testMeshName, false, false)
517+
nsSpec := createNamespaceSpec(testNamespace, testMeshName, false, false, false)
512518
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
513519
Expect(err).To(BeNil())
514520

@@ -540,7 +546,7 @@ var _ = Describe("Running the namespace remove command", func() {
540546
out = new(bytes.Buffer)
541547
fakeClientSet = fake.NewSimpleClientset()
542548

543-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
549+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
544550
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
545551
Expect(err).To(BeNil())
546552

@@ -563,7 +569,7 @@ var _ = Describe("Running the namespace remove command", func() {
563569
})
564570
})
565571

566-
Describe("with pre-existing ignored namespace with annotation", func() {
572+
Describe("with pre-existing ignored namespace with injection and metrics annotations", func() {
567573
var (
568574
out *bytes.Buffer
569575
fakeClientSet kubernetes.Interface
@@ -574,7 +580,7 @@ var _ = Describe("Running the namespace remove command", func() {
574580
out = new(bytes.Buffer)
575581
fakeClientSet = fake.NewSimpleClientset()
576582

577-
nsSpec := createNamespaceSpec(testNamespace, testMeshName, true, true)
583+
nsSpec := createNamespaceSpec(testNamespace, testMeshName, true, true, true)
578584
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
579585
Expect(err).To(BeNil())
580586

@@ -613,6 +619,12 @@ var _ = Describe("Running the namespace remove command", func() {
613619
Expect(err).ToNot(HaveOccurred())
614620
Expect(ns.Annotations).ShouldNot(HaveKey(constants.SidecarInjectionAnnotation))
615621
})
622+
623+
It("should correctly remove the metrics annotation on the namespace", func() {
624+
ns, err := fakeClientSet.CoreV1().Namespaces().Get(context.TODO(), testNamespace, metav1.GetOptions{})
625+
Expect(err).ToNot(HaveOccurred())
626+
Expect(ns.Annotations).ShouldNot(HaveKey(constants.MetricsAnnotation))
627+
})
616628
})
617629

618630
Describe("with non-existent namespace", func() {
@@ -656,7 +668,7 @@ var _ = Describe("Running the namespace list command", func() {
656668

657669
// helper function that adds a name space to the clientset
658670
addNamespace := func(name, mesh string, enableSideCarInjection bool) {
659-
ns := createNamespaceSpec(name, mesh, enableSideCarInjection, false)
671+
ns := createNamespaceSpec(name, mesh, enableSideCarInjection, false, false)
660672
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
661673
Expect(err).To(BeNil())
662674
}
@@ -798,7 +810,7 @@ var _ = Describe("Running the namespace ignore command", func() {
798810
out = new(bytes.Buffer)
799811
fakeClientSet = fake.NewSimpleClientset()
800812

801-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
813+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
802814
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
803815
Expect(err).NotTo(HaveOccurred())
804816

@@ -829,11 +841,11 @@ var _ = Describe("Running the namespace ignore command", func() {
829841
out = new(bytes.Buffer)
830842
fakeClientSet = fake.NewSimpleClientset()
831843

832-
nsSpec := createNamespaceSpec(testNamespace, "", false, false)
844+
nsSpec := createNamespaceSpec(testNamespace, "", false, false, false)
833845
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
834846
Expect(err).NotTo(HaveOccurred())
835847

836-
nsSpec2 := createNamespaceSpec(testNamespace2, "", false, false)
848+
nsSpec2 := createNamespaceSpec(testNamespace2, "", false, false, false)
837849
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec2, metav1.CreateOptions{})
838850
Expect(err).NotTo(HaveOccurred())
839851

@@ -859,7 +871,7 @@ var _ = Describe("Running the namespace ignore command", func() {
859871
})
860872
})
861873

862-
func createNamespaceSpec(namespace, meshName string, enableSideCarInjection bool, ignoreNamespace bool) *v1.Namespace {
874+
func createNamespaceSpec(namespace, meshName string, enableSideCarInjection bool, ignoreNamespace bool, enableMetrics bool) *v1.Namespace {
863875
labelMap := make(map[string]string)
864876
if meshName != "" {
865877
labelMap[constants.OSMKubeResourceMonitorAnnotation] = meshName
@@ -879,5 +891,9 @@ func createNamespaceSpec(namespace, meshName string, enableSideCarInjection bool
879891
labelMap[constants.IgnoreLabel] = trueValue
880892
}
881893

894+
if enableMetrics {
895+
labelMap[constants.MetricsAnnotation] = "enabled"
896+
}
897+
882898
return ns
883899
}

0 commit comments

Comments
 (0)