Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/app/ops/deploymentchaincontroller/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/piped/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/piped/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/trigger/determiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

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

Expand Down
25 changes: 5 additions & 20 deletions pkg/model/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
50 changes: 50 additions & 0 deletions pkg/model/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}