diff --git a/controller/appcontroller.go b/controller/appcontroller.go index 734a7fe6d6379..028deb09c67e6 100644 --- a/controller/appcontroller.go +++ b/controller/appcontroller.go @@ -1410,8 +1410,7 @@ func (ctrl *ApplicationController) processRequestedAppOperation(app *appv1.Appli } ts.AddCheckpoint("initial_operation_stage_ms") - destCluster, err := argo.GetDestinationCluster(context.Background(), app.Spec.Destination, ctrl.db) - if err != nil { + if destCluster, err := argo.GetDestinationCluster(context.Background(), app.Spec.Destination, ctrl.db); err != nil { state.Phase = synccommon.OperationFailed state.Message = err.Error() } else { @@ -1420,7 +1419,7 @@ func (ctrl *ApplicationController) processRequestedAppOperation(app *appv1.Appli ts.AddCheckpoint("validate_and_sync_app_state_ms") // Check whether application is allowed to use project - _, err = ctrl.getAppProj(app) + _, err := ctrl.getAppProj(app) ts.AddCheckpoint("get_app_proj_ms") if err != nil { state.Phase = synccommon.OperationError @@ -2324,11 +2323,8 @@ func (ctrl *ApplicationController) newApplicationInformerAndLister() (cache.Shar // log an error. if _, err := ctrl.getAppProj(app); err != nil { ctrl.setAppCondition(app, ctrl.projectErrorToCondition(err, app)) - } else { - // If the cluster can't be found, log an error as an App Condition. - if _, err := argo.GetDestinationCluster(context.Background(), app.Spec.Destination, ctrl.db); err != nil { - ctrl.setAppCondition(app, appv1.ApplicationCondition{Type: appv1.ApplicationConditionInvalidSpecError, Message: err.Error()}) - } + } else if _, err = argo.GetDestinationCluster(context.Background(), app.Spec.Destination, ctrl.db); err != nil { + ctrl.setAppCondition(app, appv1.ApplicationCondition{Type: appv1.ApplicationConditionInvalidSpecError, Message: err.Error()}) } } } diff --git a/util/argo/argo.go b/util/argo/argo.go index 3a251965eecb9..26b97749953fc 100644 --- a/util/argo/argo.go +++ b/util/argo/argo.go @@ -323,7 +323,7 @@ func ValidateRepo( return nil, fmt.Errorf("error getting permitted repo creds: %w", err) } - cluster, err := GetDestinationCluster(context.Background(), spec.Destination, db) + destCluster, err := GetDestinationCluster(context.Background(), spec.Destination, db) if err != nil { conditions = append(conditions, argoappv1.ApplicationCondition{ Type: argoappv1.ApplicationConditionInvalidSpecError, @@ -331,12 +331,12 @@ func ValidateRepo( }) return conditions, nil } - config, err := cluster.RESTConfig() + config, err := destCluster.RESTConfig() if err != nil { return nil, fmt.Errorf("error getting cluster REST config: %w", err) } // nolint:staticcheck - cluster.ServerVersion, err = kubectl.GetServerVersion(config) + destCluster.ServerVersion, err = kubectl.GetServerVersion(config) if err != nil { return nil, fmt.Errorf("error getting k8s server version: %w", err) } @@ -357,7 +357,7 @@ func ValidateRepo( repoClient, permittedHelmRepos, helmOptions, - cluster, + destCluster, apiGroups, proj, permittedHelmCredentials, @@ -949,6 +949,10 @@ type ClusterGetter interface { GetClusterServersByName(ctx context.Context, server string) ([]string, error) } +// GetDestinationCluster returns the cluster object based on the destination server or name. If both are provided or +// both are empty, an error is returned. If the destination server is provided, the cluster is fetched by the server +// URL. If the destination name is provided, the cluster is fetched by the name. If multiple clusters have the specified +// name, an error is returned. func GetDestinationCluster(ctx context.Context, destination argoappv1.ApplicationDestination, db ClusterGetter) (*argoappv1.Cluster, error) { if destination.Name != "" && destination.Server != "" { return nil, fmt.Errorf("application destination can't have both name and server defined: %s %s", destination.Name, destination.Server) diff --git a/util/argo/argo_test.go b/util/argo/argo_test.go index 866acfa8c7f1e..aaaba5c8554d3 100644 --- a/util/argo/argo_test.go +++ b/util/argo/argo_test.go @@ -937,10 +937,10 @@ func TestGetDestinationCluster(t *testing.T) { db := &dbmocks.ArgoDB{} db.On("GetCluster", context.Background(), "https://127.0.0.1:6443").Return(expectedCluster, nil) - cluster, err := GetDestinationCluster(context.Background(), dest, db) + destCluster, err := GetDestinationCluster(context.Background(), dest, db) require.NoError(t, err) require.NotNil(t, expectedCluster) - assert.Equal(t, expectedCluster, cluster) + assert.Equal(t, expectedCluster, destCluster) }) t.Run("Validate destination with server name", func(t *testing.T) {