Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/utils/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Comment thread
darshanime marked this conversation as resolved.
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 {
Expand Down
35 changes: 35 additions & 0 deletions pkg/utils/kube/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ metadata:
name: nginx-deployment
labels:
foo: bar
annotations:
foo: bar-annotations
spec:
template:
metadata:
Expand Down Expand Up @@ -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)
}
}