Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions pkg/utils/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,21 @@ func MustToUnstructured(obj interface{}) *unstructured.Unstructured {

// GetAppInstanceLabel returns the application instance name from labels
func GetAppInstanceLabel(un *unstructured.Unstructured, key string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@darshanime , looks like GetAppInstanceLabel is not used in GitOps engine. Instead of making changes here please move GetAppInstanceIdentifier with dependencies to argoproj/argo-cd#4123 and then we can remove GetAppInstanceLabel from GitOps engine.

if labels := un.GetLabels(); labels != nil {
return labels[key]
return un.GetLabels()[key]
}

// GetAppInstanceAnnotation returns the application instance name from annotations
func GetAppInstanceAnnotation(un *unstructured.Unstructured, key string) string {
return un.GetAnnotations()[key]
}

// 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 ""
return GetAppInstanceLabel(un, key)
}

// UnsetLabel removes our app labels from an unstructured object
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)
}
}