From d608b9c4881a2179fb8d0a455c829fb450c1694a Mon Sep 17 00:00:00 2001 From: darshanime Date: Wed, 19 Aug 2020 09:48:32 +0530 Subject: [PATCH 1/2] feat: add methods to get annotations Signed-off-by: darshanime --- pkg/utils/kube/kube.go | 18 ++++++++++++++++++ pkg/utils/kube/kube_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pkg/utils/kube/kube.go b/pkg/utils/kube/kube.go index 82e1791b8..de856cd20 100644 --- a/pkg/utils/kube/kube.go +++ b/pkg/utils/kube/kube.go @@ -135,6 +135,24 @@ func GetAppInstanceLabel(un *unstructured.Unstructured, key string) string { return "" } +// GetAppInstanceAnnotation returns the application instance name from annotations +func GetAppInstanceAnnotation(un *unstructured.Unstructured, key string) string { + if annotations := un.GetAnnotations(); annotations != nil { + return annotations[key] + } + + return "" +} + +// GetAppInstanceIdentifier returns the application instance name from annotations else labels +func GetAppInstanceIdentifier(un *unstructured.Unstructured, key string) string { + annotation := GetAppInstanceAnnotation(un, key) + if annotation != "" { + return annotation + } + return GetAppInstanceLabel(un, key) +} + // UnsetLabel removes our app labels from an unstructured object func UnsetLabel(target *unstructured.Unstructured, key string) { if labels := target.GetLabels(); labels != nil { diff --git a/pkg/utils/kube/kube_test.go b/pkg/utils/kube/kube_test.go index d6a6ed3a5..fab5e0918 100644 --- a/pkg/utils/kube/kube_test.go +++ b/pkg/utils/kube/kube_test.go @@ -20,6 +20,8 @@ metadata: name: nginx-deployment labels: foo: bar + annotations: + foo: bar-annotations spec: template: metadata: @@ -155,3 +157,36 @@ func TestSplitYAML_TrailingNewLines(t *testing.T) { require.NoError(t, err) assert.Len(t, objs, 1) } + +func TestGetLabels(t *testing.T) { + for _, yamlStr := range [][]byte{[]byte(depWithLabel)} { + var obj unstructured.Unstructured + err := yaml.Unmarshal(yamlStr, &obj) + require.NoError(t, err) + + value := GetAppInstanceLabel(&obj, "foo") + assert.Equal(t, "bar", value) + } +} + +func TestGetAnnotations(t *testing.T) { + for _, yamlStr := range [][]byte{[]byte(depWithLabel)} { + var obj unstructured.Unstructured + err := yaml.Unmarshal(yamlStr, &obj) + require.NoError(t, err) + + value := GetAppInstanceAnnotation(&obj, "foo") + assert.Equal(t, "bar-annotations", value) + } +} + +func TestGetIdentifier(t *testing.T) { + for _, yamlStr := range [][]byte{[]byte(depWithLabel)} { + var obj unstructured.Unstructured + err := yaml.Unmarshal(yamlStr, &obj) + require.NoError(t, err) + + value := GetAppInstanceIdentifier(&obj, "foo") + assert.Equal(t, "bar-annotations", value) + } +} From 37f583dbbad3181ebfbdbe04726922fb078f9704 Mon Sep 17 00:00:00 2001 From: darshanime Date: Fri, 21 Aug 2020 15:40:08 +0530 Subject: [PATCH 2/2] feat: simplify label/annotation retrieval Signed-off-by: darshanime --- pkg/utils/kube/kube.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/pkg/utils/kube/kube.go b/pkg/utils/kube/kube.go index de856cd20..ca1319337 100644 --- a/pkg/utils/kube/kube.go +++ b/pkg/utils/kube/kube.go @@ -129,19 +129,12 @@ func MustToUnstructured(obj interface{}) *unstructured.Unstructured { // GetAppInstanceLabel returns the application instance name from labels func GetAppInstanceLabel(un *unstructured.Unstructured, key string) string { - if labels := un.GetLabels(); labels != nil { - return labels[key] - } - return "" + return un.GetLabels()[key] } // GetAppInstanceAnnotation returns the application instance name from annotations func GetAppInstanceAnnotation(un *unstructured.Unstructured, key string) string { - if annotations := un.GetAnnotations(); annotations != nil { - return annotations[key] - } - - return "" + return un.GetAnnotations()[key] } // GetAppInstanceIdentifier returns the application instance name from annotations else labels