diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index 63ac7a79..f064eaaa 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 ce565f21..28124f0c 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") }) }