diff --git a/pkg/app/ops/deploymentchaincontroller/updater.go b/pkg/app/ops/deploymentchaincontroller/updater.go index 28eb29885e..3e11314d1b 100644 --- a/pkg/app/ops/deploymentchaincontroller/updater.go +++ b/pkg/app/ops/deploymentchaincontroller/updater.go @@ -83,7 +83,7 @@ func (u *updater) Run(ctx context.Context) error { if len(u.deploymentRefs) != 0 { for _, deploymentRef := range u.deploymentRefs { // Ignore finished deployment. - if model.IsCompletedDeployment(deploymentRef.Status) { + if deploymentRef.Status.IsCompleted() { continue } deployment, err := u.deploymentStore.Get(ctx, deploymentRef.DeploymentId) @@ -197,7 +197,7 @@ func (u *updater) isAllInChainDeploymentsCompleted() bool { return false } for _, dr := range u.deploymentRefs { - if !model.IsCompletedDeployment(dr.Status) { + if !dr.Status.IsCompleted() { return false } } diff --git a/pkg/app/piped/controller/controller.go b/pkg/app/piped/controller/controller.go index d82470f423..04760532fa 100644 --- a/pkg/app/piped/controller/controller.go +++ b/pkg/app/piped/controller/controller.go @@ -309,7 +309,7 @@ func (c *controller) syncPlanners(ctx context.Context) error { delete(c.planners, id) // Application will be marked as NOT deploying when planner's deployment was completed. - if model.IsCompletedDeployment(p.DoneDeploymentStatus()) { + if p.DoneDeploymentStatus().IsCompleted() { if err := reportApplicationDeployingStatus(ctx, c.apiClient, id, false); err != nil { c.logger.Error("failed to mark application as NOT deploying", zap.String("deployment", p.ID()), @@ -537,7 +537,7 @@ func (c *controller) syncSchedulers(ctx context.Context) error { delete(c.schedulers, id) // Application will be marked as NOT deploying when scheduler's deployment was completed. - if model.IsCompletedDeployment(s.DoneDeploymentStatus()) { + if s.DoneDeploymentStatus().IsCompleted() { if err := reportApplicationDeployingStatus(ctx, c.apiClient, id, false); err != nil { c.logger.Error("failed to mark application as NOT deploying", zap.String("deployment", s.ID()), diff --git a/pkg/app/piped/controller/scheduler.go b/pkg/app/piped/controller/scheduler.go index 0133de7c73..d482258cde 100644 --- a/pkg/app/piped/controller/scheduler.go +++ b/pkg/app/piped/controller/scheduler.go @@ -197,7 +197,7 @@ func (s *scheduler) Run(ctx context.Context) error { }() // If this deployment is already completed. Do nothing. - if model.IsCompletedDeployment(s.deployment.Status) { + if s.deployment.Status.IsCompleted() { s.logger.Info("this deployment is already completed") return nil } @@ -393,7 +393,7 @@ func (s *scheduler) Run(ctx context.Context) error { } } - if model.IsCompletedDeployment(deploymentStatus) { + if deploymentStatus.IsCompleted() { err := s.reportDeploymentCompleted(ctx, deploymentStatus, statusReason, cancelCommander) if err == nil && deploymentStatus == model.DeploymentStatus_DEPLOYMENT_SUCCESS { s.reportMostRecentlySuccessfulDeployment(ctx) diff --git a/pkg/app/piped/trigger/determiner.go b/pkg/app/piped/trigger/determiner.go index 4ec42b8667..f0f398d043 100644 --- a/pkg/app/piped/trigger/determiner.go +++ b/pkg/app/piped/trigger/determiner.go @@ -118,7 +118,7 @@ func (d *OnOutOfSyncDeterminer) ShouldTrigger(ctx context.Context, app *model.Ap // Check if it was already completed or not. // Not yet completed means the application is deploying currently, // so no need to trigger a new deployment for it. - if !model.IsCompletedDeployment(deployment.Status) { + if !deployment.Status.IsCompleted() { return false, nil } diff --git a/pkg/app/server/grpcapi/web_api.go b/pkg/app/server/grpcapi/web_api.go index 5937fb2f1e..fb5576978f 100644 --- a/pkg/app/server/grpcapi/web_api.go +++ b/pkg/app/server/grpcapi/web_api.go @@ -981,7 +981,7 @@ func (a *WebAPI) CancelDeployment(ctx context.Context, req *webservice.CancelDep return nil, status.Error(codes.PermissionDenied, "Requested deployment does not belong to your project") } - if model.IsCompletedDeployment(deployment.Status) { + if deployment.Status.IsCompleted() { return nil, status.Errorf(codes.FailedPrecondition, "could not cancel the deployment because it was already completed") } diff --git a/pkg/datastore/deploymentstore.go b/pkg/datastore/deploymentstore.go index c444828f26..247c73d69c 100644 --- a/pkg/datastore/deploymentstore.go +++ b/pkg/datastore/deploymentstore.go @@ -86,7 +86,7 @@ var ( toCompletedUpdateFunc = func(status model.DeploymentStatus, stageStatuses map[string]model.StageStatus, statusReason string, completedAt int64) func(*model.Deployment) error { return func(d *model.Deployment) error { - if !model.IsCompletedDeployment(status) { + if !status.IsCompleted() { return fmt.Errorf("deployment status %s is not completed value: %w", status, ErrInvalidArgument) } diff --git a/pkg/model/deployment.go b/pkg/model/deployment.go index e9ed27dc8a..1a7323582d 100644 --- a/pkg/model/deployment.go +++ b/pkg/model/deployment.go @@ -31,26 +31,11 @@ var notCompletedDeploymentStatuses = []DeploymentStatus{ DeploymentStatus_DEPLOYMENT_ROLLING_BACK, } -// IsCompletedDeployment checks whether the deployment is at a completion state. -func IsCompletedDeployment(status DeploymentStatus) bool { - if status.String() == "" { - return false - } - - switch status { - case DeploymentStatus_DEPLOYMENT_SUCCESS: - return true - case DeploymentStatus_DEPLOYMENT_FAILURE: - return true - case DeploymentStatus_DEPLOYMENT_CANCELLED: - return true - } - return false -} - -// IsSuccessfullyCompletedDeployment checks whether the deployment is at a successfully addressed. -func IsSuccessfullyCompletedDeployment(status DeploymentStatus) bool { - return status == DeploymentStatus_DEPLOYMENT_SUCCESS +// IsCompleted checks whether the deployment is at a completion state. +func (d DeploymentStatus) IsCompleted() bool { + return d == DeploymentStatus_DEPLOYMENT_SUCCESS || + d == DeploymentStatus_DEPLOYMENT_FAILURE || + d == DeploymentStatus_DEPLOYMENT_CANCELLED } // IsCompleted checks whether the stage is at a completion state. diff --git a/pkg/model/deployment_test.go b/pkg/model/deployment_test.go index 33e3f561e8..58c3ff6a71 100644 --- a/pkg/model/deployment_test.go +++ b/pkg/model/deployment_test.go @@ -150,3 +150,53 @@ func TestStageStatus_IsCompleted(t *testing.T) { }) } } + +func TestDeploymentStatus_IsCompleted(t *testing.T) { + testcases := []struct { + name string + status DeploymentStatus + want bool + }{ + { + name: "pending", + status: DeploymentStatus_DEPLOYMENT_PENDING, + want: false, + }, + { + name: "planned", + status: DeploymentStatus_DEPLOYMENT_PLANNED, + want: false, + }, + { + name: "running", + status: DeploymentStatus_DEPLOYMENT_RUNNING, + want: false, + }, + { + name: "rolling back", + status: DeploymentStatus_DEPLOYMENT_ROLLING_BACK, + want: false, + }, + { + name: "success", + status: DeploymentStatus_DEPLOYMENT_SUCCESS, + want: true, + }, + { + name: "failure", + status: DeploymentStatus_DEPLOYMENT_FAILURE, + want: true, + }, + { + name: "cancelled", + status: DeploymentStatus_DEPLOYMENT_CANCELLED, + want: true, + }, + } + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + got := tc.status.IsCompleted() + assert.Equal(t, tc.want, got) + }) + } +}