Skip to content

Commit

Permalink
fix: #896 App of apps being overwritten by image-updater (#918)
Browse files Browse the repository at this point in the history
Signed-off-by: Cheng Fang <[email protected]>
  • Loading branch information
chengfang authored Nov 8, 2024
1 parent e06eb69 commit 8146cf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
41 changes: 14 additions & 27 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,20 @@ 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 {
return nil, fmt.Errorf("error listing applications: %w", err)
}

// 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)
}
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

Expand Down

0 comments on commit 8146cf1

Please sign in to comment.