Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crenshaw <[email protected]>
  • Loading branch information
crenshaw-dev committed Dec 20, 2024
1 parent 3d72f41 commit 5cc575e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 4 additions & 8 deletions controller/appcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()})
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions util/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,20 @@ 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,
Message: fmt.Sprintf("Unable to get cluster: %v", err),
})
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)
}
Expand All @@ -357,7 +357,7 @@ func ValidateRepo(
repoClient,
permittedHelmRepos,
helmOptions,
cluster,
destCluster,
apiGroups,
proj,
permittedHelmCredentials,
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions util/argo/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5cc575e

Please sign in to comment.