Skip to content

Commit 77f1d3f

Browse files
committed
fix: Change to use annotation when getting the hub child applications
Prev. we used labels, but when chaning the resource tracking method in Argocd we need to adjust this function as well.
1 parent 2d5c05d commit 77f1d3f

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

internal/controller/argo.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,14 +1162,30 @@ func syncApplication(client argoclient.Interface, app *argoapi.Application, with
11621162

11631163
// returns the child applications owned by the app-of-apps parentApp
11641164
func getChildApplications(client argoclient.Interface, parentApp *argoapi.Application) ([]argoapi.Application, error) {
1165-
listOptions := metav1.ListOptions{
1166-
LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", parentApp.Name),
1167-
}
1168-
1169-
appList, err := client.ArgoprojV1alpha1().Applications("").List(context.Background(), listOptions)
1165+
appList, err := client.ArgoprojV1alpha1().
1166+
Applications("").
1167+
List(context.Background(), metav1.ListOptions{})
11701168
if err != nil {
11711169
return nil, fmt.Errorf("failed to list child applications of %s: %w", parentApp.Name, err)
11721170
}
11731171

1174-
return appList.Items, nil
1172+
var result []argoapi.Application
1173+
1174+
// Build expected tracking prefix
1175+
// Example:
1176+
// multicloud-gitops-hub:argoproj.io/Application:multicloud-gitops-hub/
1177+
expectedPrefix := fmt.Sprintf(
1178+
"%s:argoproj.io/Application:%s/",
1179+
parentApp.Name,
1180+
parentApp.Namespace,
1181+
)
1182+
1183+
for _, app := range appList.Items { //nolint:gocritic // rangeValCopy: each iteration copies 992 bytes
1184+
if trackingID, ok := app.Annotations["argocd.argoproj.io/tracking-id"]; ok {
1185+
if strings.HasPrefix(trackingID, expectedPrefix) {
1186+
result = append(result, app)
1187+
}
1188+
}
1189+
}
1190+
return result, nil
11751191
}

internal/controller/argo_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ var _ = Describe("SyncApplication", func() {
13181318
})
13191319
})
13201320

1321-
var _ = Describe("GetChildApplications", func() {
1321+
var _ = Describe("getChildApplications", func() {
13221322
var (
13231323
argocdclient *argoclient.Clientset
13241324
namespace string
@@ -1339,7 +1339,9 @@ var _ = Describe("GetChildApplications", func() {
13391339
ObjectMeta: metav1.ObjectMeta{
13401340
Name: "child-app",
13411341
Namespace: namespace,
1342-
Labels: map[string]string{"app.kubernetes.io/instance": "parent-app"},
1342+
Annotations: map[string]string{
1343+
"argocd.argoproj.io/tracking-id": fmt.Sprintf("parent-app:argoproj.io/Application:%s/child-app", namespace),
1344+
},
13431345
},
13441346
}
13451347
_, err := argocdclient.ArgoprojV1alpha1().Applications(namespace).Create(context.Background(), childApp, metav1.CreateOptions{})
@@ -2216,8 +2218,8 @@ var _ = Describe("getChildApplications", func() {
22162218
ObjectMeta: metav1.ObjectMeta{
22172219
Name: "child-app",
22182220
Namespace: "openshift-gitops",
2219-
Labels: map[string]string{
2220-
"app.kubernetes.io/instance": "parent-app",
2221+
Annotations: map[string]string{
2222+
"argocd.argoproj.io/tracking-id": "parent-app:argoproj.io/Application:openshift-gitops/child-app",
22212223
},
22222224
},
22232225
}

0 commit comments

Comments
 (0)