Skip to content

Commit c6b124c

Browse files
committed
Add HasLabel/Annotation DeleteLabel/Annotations
1 parent 078222c commit c6b124c

File tree

6 files changed

+126
-8
lines changed

6 files changed

+126
-8
lines changed

cmdutils/switches/switches_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,

configutils/configutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func getKubeconfigFlag() string {
112112
// It also applies saner defaults for QPS and burst based on the Kubernetes
113113
// controller manager defaults (20 QPS, 30 burst)
114114
//
115-
// Config precedence
115+
// # Config precedence
116116
//
117117
// * Kubeconfig / --kubeconfig value / flag pointing at a file
118118
//

metautils/metautils.go

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ func FilterList(list client.ObjectList, f func(obj client.Object) bool) error {
353353
return SetList(list, filtered)
354354
}
355355

356+
// HasLabel checks if the object has a label with the given key.
357+
func HasLabel(obj metav1.Object, key string) bool {
358+
_, ok := obj.GetLabels()[key]
359+
return ok
360+
}
361+
356362
// SetLabel sets the given label on the object.
357363
func SetLabel(obj metav1.Object, key, value string) {
358364
labels := obj.GetLabels()
@@ -368,15 +374,40 @@ func SetLabel(obj metav1.Object, key, value string) {
368374
func SetLabels(obj metav1.Object, set map[string]string) {
369375
labels := obj.GetLabels()
370376
if labels == nil {
371-
labels = make(map[string]string)
377+
labels = set
378+
} else {
379+
for k, v := range set {
380+
labels[k] = v
381+
}
372382
}
383+
obj.SetLabels(labels)
384+
}
373385

374-
for k, v := range set {
375-
labels[k] = v
386+
// DeleteLabel deletes the label with the given key from the object.
387+
func DeleteLabel(obj metav1.Object, key string) {
388+
labels := obj.GetLabels()
389+
delete(labels, key)
390+
obj.SetLabels(labels)
391+
}
392+
393+
// DeleteLabels deletes the labels with the given keys from the object.
394+
func DeleteLabels(obj metav1.Object, keys []string) {
395+
labels := obj.GetLabels()
396+
if len(labels) == 0 {
397+
return
398+
}
399+
for _, key := range keys {
400+
delete(labels, key)
376401
}
377402
obj.SetLabels(labels)
378403
}
379404

405+
// HasAnnotation checks if the object has an annotation with the given key.
406+
func HasAnnotation(obj metav1.Object, key string) bool {
407+
_, ok := obj.GetAnnotations()[key]
408+
return ok
409+
}
410+
380411
// SetAnnotation sets the given annotation on the object.
381412
func SetAnnotation(obj metav1.Object, key, value string) {
382413
annotations := obj.GetAnnotations()
@@ -392,11 +423,30 @@ func SetAnnotation(obj metav1.Object, key, value string) {
392423
func SetAnnotations(obj metav1.Object, set map[string]string) {
393424
annotations := obj.GetAnnotations()
394425
if annotations == nil {
395-
annotations = make(map[string]string)
426+
annotations = set
427+
} else {
428+
for k, v := range set {
429+
annotations[k] = v
430+
}
396431
}
432+
obj.SetAnnotations(annotations)
433+
}
397434

398-
for k, v := range set {
399-
annotations[k] = v
435+
// DeleteAnnotation deletes the annotation with the given key from the object.
436+
func DeleteAnnotation(obj metav1.Object, key string) {
437+
annotations := obj.GetAnnotations()
438+
delete(annotations, key)
439+
obj.SetAnnotations(annotations)
440+
}
441+
442+
// DeleteAnnotations deletes the annotations with the given keys from the object.
443+
func DeleteAnnotations(obj metav1.Object, keys []string) {
444+
annotations := obj.GetAnnotations()
445+
if len(annotations) == 0 {
446+
return
447+
}
448+
for _, key := range keys {
449+
delete(annotations, key)
400450
}
401451
obj.SetAnnotations(annotations)
402452
}

metautils/metautils_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ var _ = Describe("Metautils", func() {
511511
})
512512
})
513513

514+
DescribeTable("HasLabel",
515+
func(initLabels map[string]string, key string, expected bool) {
516+
obj := &metav1.ObjectMeta{Labels: initLabels}
517+
Expect(HasLabel(obj, key)).To(Equal(expected))
518+
},
519+
Entry("label present", map[string]string{"foo": ""}, "foo", true),
520+
Entry("nil labels", nil, "foo", false),
521+
Entry("different label present", map[string]string{"bar": ""}, "foo", false),
522+
)
523+
514524
DescribeTable("SetLabel",
515525
func(initLabels map[string]string, key, value string, expected map[string]string) {
516526
obj := &metav1.ObjectMeta{Labels: initLabels}
@@ -534,6 +544,39 @@ var _ = Describe("Metautils", func() {
534544
Entry("partial other keys, same key", map[string]string{"foo": "baz", "bar": "baz"}, map[string]string{"foo": "bar"}, map[string]string{"foo": "bar", "bar": "baz"}),
535545
)
536546

547+
DescribeTable("DeleteLabel",
548+
func(initLabels map[string]string, key string, expected map[string]string) {
549+
obj := &metav1.ObjectMeta{Labels: initLabels}
550+
DeleteLabel(obj, key)
551+
Expect(obj.Labels).To(Equal(expected))
552+
},
553+
Entry("key present", map[string]string{"foo": "bar"}, "foo", map[string]string{}),
554+
Entry("different key present", map[string]string{"bar": "baz"}, "foo", map[string]string{"bar": "baz"}),
555+
Entry("nil map", nil, "foo", nil),
556+
)
557+
558+
DescribeTable("DeleteLabels",
559+
func(initLabels map[string]string, keys []string, expected map[string]string) {
560+
obj := &metav1.ObjectMeta{Labels: initLabels}
561+
DeleteLabels(obj, keys)
562+
Expect(obj.Labels).To(Equal(expected))
563+
},
564+
Entry("keys present", map[string]string{"foo": "bar", "bar": "baz"}, []string{"foo", "bar"}, map[string]string{}),
565+
Entry("some keys present", map[string]string{"foo": "bar", "bar": "baz"}, []string{"foo"}, map[string]string{"bar": "baz"}),
566+
Entry("no keys present", map[string]string{"foo": "bar", "bar": "baz"}, []string{"qux"}, map[string]string{"foo": "bar", "bar": "baz"}),
567+
Entry("nil map", nil, []string{"foo", "bar"}, nil),
568+
)
569+
570+
DescribeTable("HasAnnotation",
571+
func(initAnnotations map[string]string, key string, expected bool) {
572+
obj := &metav1.ObjectMeta{Annotations: initAnnotations}
573+
Expect(HasAnnotation(obj, key)).To(Equal(expected))
574+
},
575+
Entry("annotation present", map[string]string{"foo": ""}, "foo", true),
576+
Entry("nil annotations", nil, "foo", false),
577+
Entry("different annotation present", map[string]string{"bar": ""}, "foo", false),
578+
)
579+
537580
DescribeTable("SetAnnotation",
538581
func(initAnnotations map[string]string, key, value string, expected map[string]string) {
539582
obj := &metav1.ObjectMeta{Annotations: initAnnotations}
@@ -557,6 +600,29 @@ var _ = Describe("Metautils", func() {
557600
Entry("partial other keys, same key", map[string]string{"foo": "baz", "bar": "baz"}, map[string]string{"foo": "bar"}, map[string]string{"foo": "bar", "bar": "baz"}),
558601
)
559602

603+
DescribeTable("DeleteAnnotation",
604+
func(initAnnotations map[string]string, key string, expected map[string]string) {
605+
obj := &metav1.ObjectMeta{Annotations: initAnnotations}
606+
DeleteAnnotation(obj, key)
607+
Expect(obj.Annotations).To(Equal(expected))
608+
},
609+
Entry("key present", map[string]string{"foo": "bar"}, "foo", map[string]string{}),
610+
Entry("different key present", map[string]string{"bar": "baz"}, "foo", map[string]string{"bar": "baz"}),
611+
Entry("nil map", nil, "foo", nil),
612+
)
613+
614+
DescribeTable("DeleteAnnotations",
615+
func(initAnnotations map[string]string, keys []string, expected map[string]string) {
616+
obj := &metav1.ObjectMeta{Annotations: initAnnotations}
617+
DeleteAnnotations(obj, keys)
618+
Expect(obj.Annotations).To(Equal(expected))
619+
},
620+
Entry("keys present", map[string]string{"foo": "bar", "bar": "baz"}, []string{"foo", "bar"}, map[string]string{}),
621+
Entry("some keys present", map[string]string{"foo": "bar", "bar": "baz"}, []string{"foo"}, map[string]string{"bar": "baz"}),
622+
Entry("no keys present", map[string]string{"foo": "bar", "bar": "baz"}, []string{"qux"}, map[string]string{"foo": "bar", "bar": "baz"}),
623+
Entry("nil map", nil, []string{"foo", "bar"}, nil),
624+
)
625+
560626
Describe("FilterList", func() {
561627
It("should filter the list with the given function", func() {
562628
list := &corev1.SecretList{

mock/controller-runtime/client/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
// Package client contains mocks for controller-runtime's client package.
16+
//
1617
//go:generate go run github.com/golang/mock/mockgen -copyright_file ../../../hack/boilerplate.go.txt -package client -destination mocks.go sigs.k8s.io/controller-runtime/pkg/client Client,FieldIndexer
1718
//go:generate go run github.com/golang/mock/mockgen -copyright_file ../../../hack/boilerplate.go.txt -package client -destination funcs.go github.com/onmetal/controller-utils/mock/controller-runtime/client IndexerFunc
1819
package client

mock/controller-utils/clientutils/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
// limitations under the License.
1414

1515
// Package clientutils contains mocks for the actual clientutils package.
16+
//
1617
//go:generate go run github.com/golang/mock/mockgen -copyright_file ../../../hack/boilerplate.go.txt -package clientutils -destination=mocks.go github.com/onmetal/controller-utils/clientutils PatchProvider
1718
package clientutils

0 commit comments

Comments
 (0)