diff --git a/controller/appcontroller.go b/controller/appcontroller.go index 400e314ec4eec..89353d755076b 100644 --- a/controller/appcontroller.go +++ b/controller/appcontroller.go @@ -1497,17 +1497,7 @@ func (ctrl *ApplicationController) refreshAppConditions(app *appv1.Application) errorConditions := make([]appv1.ApplicationCondition, 0) proj, err := ctrl.getAppProj(app) if err != nil { - if apierr.IsNotFound(err) { - errorConditions = append(errorConditions, appv1.ApplicationCondition{ - Type: appv1.ApplicationConditionInvalidSpecError, - Message: fmt.Sprintf("Application referencing project %s which does not exist", app.Spec.Project), - }) - } else { - errorConditions = append(errorConditions, appv1.ApplicationCondition{ - Type: appv1.ApplicationConditionUnknownError, - Message: err.Error(), - }) - } + errorConditions = append(errorConditions, ctrl.projectErrorToCondition(err, app)) } else { specConditions, err := argo.ValidatePermissions(context.Background(), &app.Spec, proj, ctrl.db) if err != nil { @@ -1798,7 +1788,7 @@ func (ctrl *ApplicationController) newApplicationInformerAndLister() (cache.Shar // If the application is not allowed to use the project, // log an error. if _, err := ctrl.getAppProj(app); err != nil { - ctrl.setAppCondition(app, appv1.ApplicationCondition{Type: appv1.ApplicationConditionUnknownError, Message: err.Error()}) + ctrl.setAppCondition(app, ctrl.projectErrorToCondition(err, app)) } } @@ -1869,6 +1859,19 @@ func (ctrl *ApplicationController) newApplicationInformerAndLister() (cache.Shar return informer, lister } +func (ctrl *ApplicationController) projectErrorToCondition(err error, app *appv1.Application) appv1.ApplicationCondition { + var condition appv1.ApplicationCondition + if apierr.IsNotFound(err) { + condition = appv1.ApplicationCondition{ + Type: appv1.ApplicationConditionInvalidSpecError, + Message: fmt.Sprintf("Application referencing project %s which does not exist", app.Spec.Project), + } + } else { + condition = appv1.ApplicationCondition{Type: appv1.ApplicationConditionUnknownError, Message: err.Error()} + } + return condition +} + func (ctrl *ApplicationController) RegisterClusterSecretUpdater(ctx context.Context) { updater := NewClusterInfoUpdater(ctrl.stateCache, ctrl.db, ctrl.appLister.Applications(""), ctrl.cache, ctrl.clusterFilter, ctrl.getAppProj, ctrl.namespace) go updater.Run(ctx) diff --git a/controller/appcontroller_test.go b/controller/appcontroller_test.go index 6113d48989812..037af67ef715f 100644 --- a/controller/appcontroller_test.go +++ b/controller/appcontroller_test.go @@ -1068,6 +1068,34 @@ func TestUpdateReconciledAt(t *testing.T) { } +func TestProjectErrorToCondition(t *testing.T) { + app := newFakeApp() + app.Spec.Project = "wrong project" + ctrl := newFakeController(&fakeData{ + apps: []runtime.Object{app, &defaultProj}, + manifestResponse: &apiclient.ManifestResponse{ + Manifests: []string{}, + Namespace: test.FakeDestNamespace, + Server: test.FakeClusterURL, + Revision: "abc123", + }, + managedLiveObjs: make(map[kube.ResourceKey]*unstructured.Unstructured), + }) + key, _ := cache.MetaNamespaceKeyFunc(app) + ctrl.appRefreshQueue.Add(key) + ctrl.requestAppRefresh(app.Name, CompareWithRecent.Pointer(), nil) + + ctrl.processAppRefreshQueueItem() + + obj, ok, err := ctrl.appInformer.GetIndexer().GetByKey(key) + assert.True(t, ok) + assert.NoError(t, err) + updatedApp := obj.(*argoappv1.Application) + assert.Equal(t, argoappv1.ApplicationConditionInvalidSpecError, updatedApp.Status.Conditions[0].Type) + assert.Equal(t, "Application referencing project wrong project which does not exist", updatedApp.Status.Conditions[0].Message) + assert.Equal(t, argoappv1.ApplicationConditionInvalidSpecError, updatedApp.Status.Conditions[0].Type) +} + func TestFinalizeProjectDeletion_HasApplications(t *testing.T) { app := newFakeApp() proj := &argoappv1.AppProject{ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: test.FakeArgoCDNamespace}}