summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCheng Fang <cfang@redhat.com>2024-11-08 09:46:40 -0500
committerGitHub <noreply@github.com>2024-11-08 09:46:40 -0500
commit8146cf1c31b64009604e763d035aa13d595fb39f (patch)
tree6935ec9d3238258e5c0c0e22c8e71a88e08edcf6
parente06eb69decc205b511d56eb5c81d0a6470a480d5 (diff)
fix: #896 App of apps being overwritten by image-updater (#918)
Signed-off-by: Cheng Fang <cfang@redhat.com>
-rw-r--r--pkg/argocd/argocd.go41
-rw-r--r--pkg/argocd/argocd_test.go2
2 files changed, 15 insertions, 28 deletions
diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go
index 63ac7a7..f064eaa 100644
--- a/pkg/argocd/argocd.go
+++ b/pkg/argocd/argocd.go
@@ -29,8 +29,6 @@ type k8sClient struct {
// GetApplication retrieves an application by name across all namespaces.
func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v1alpha1.Application, error) {
- log.Debugf("Getting application %s across all namespaces", appName)
-
// List all applications across all namespaces (using empty labelSelector)
appList, err := client.ListApplications(v1.NamespaceAll)
if err != nil {
@@ -38,35 +36,13 @@ func (client *k8sClient) GetApplication(ctx context.Context, appName string) (*v
}
// Filter applications by name using nameMatchesPattern
- app, err := findApplicationByName(appList, appName)
- if err != nil {
- log.Errorf("error getting application: %v", err)
- return nil, fmt.Errorf("error getting application: %w", err)
- }
-
- // Retrieve the application in the specified namespace
- return app, nil
-}
-
-// ListApplications lists all applications across all namespaces.
-func (client *k8sClient) ListApplications(labelSelector string) ([]v1alpha1.Application, error) {
- list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
- if err != nil {
- return nil, fmt.Errorf("error listing applications: %w", err)
- }
- log.Debugf("Applications listed: %d", len(list.Items))
- return list.Items, nil
-}
-
-// findApplicationByName filters the list of applications by name using nameMatchesPattern.
-func findApplicationByName(appList []v1alpha1.Application, appName string) (*v1alpha1.Application, error) {
- var matchedApps []*v1alpha1.Application
+ var matchedApps []v1alpha1.Application
for _, app := range appList {
log.Debugf("Found application: %s in namespace %s", app.Name, app.Namespace)
if nameMatchesPattern(app.Name, []string{appName}) {
log.Debugf("Application %s matches the pattern", app.Name)
- matchedApps = append(matchedApps, &app)
+ matchedApps = append(matchedApps, app)
}
}
@@ -78,7 +54,18 @@ func findApplicationByName(appList []v1alpha1.Application, appName string) (*v1a
return nil, fmt.Errorf("multiple applications found matching %s", appName)
}
- return matchedApps[0], nil
+ // Retrieve the application in the specified namespace
+ return &matchedApps[0], nil
+}
+
+// ListApplications lists all applications across all namespaces.
+func (client *k8sClient) ListApplications(labelSelector string) ([]v1alpha1.Application, error) {
+ list, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(v1.NamespaceAll).List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
+ if err != nil {
+ return nil, fmt.Errorf("error listing applications: %w", err)
+ }
+ log.Debugf("Applications listed: %d", len(list.Items))
+ return list.Items, nil
}
func (client *k8sClient) UpdateSpec(ctx context.Context, spec *application.ApplicationUpdateSpecRequest) (*v1alpha1.ApplicationSpec, error) {
diff --git a/pkg/argocd/argocd_test.go b/pkg/argocd/argocd_test.go
index ce565f2..28124f0 100644
--- a/pkg/argocd/argocd_test.go
+++ b/pkg/argocd/argocd_test.go
@@ -1095,7 +1095,7 @@ func TestKubernetesClient(t *testing.T) {
// Test GetApplication with multiple matching applications
_, err = client.GetApplication(context.TODO(), "test-app")
assert.Error(t, err)
- assert.EqualError(t, err, "error getting application: multiple applications found matching test-app")
+ assert.EqualError(t, err, "multiple applications found matching test-app")
})
}