Skip to content

Commit

Permalink
Inactive task definition after delete all of its taskset (#4592)
Browse files Browse the repository at this point in the history
* Store taskSet object instead of taskSetArn

Signed-off-by: khanhtc1202 <[email protected]>

* Remove service object store in stage metastore

Signed-off-by: khanhtc1202 <[email protected]>

* Inactive task definition after delete its taskset

Signed-off-by: khanhtc1202 <[email protected]>

* Update pkg/app/piped/executor/ecs/ecs.go

Co-authored-by: Kenta Kozuka <[email protected]>
Signed-off-by: khanhtc1202 <[email protected]>

---------

Signed-off-by: khanhtc1202 <[email protected]>
Co-authored-by: Kenta Kozuka <[email protected]>
  • Loading branch information
khanhtc1202 and kentakozuka authored Sep 5, 2023
1 parent d9d30af commit 5f7a9fc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 48 deletions.
60 changes: 19 additions & 41 deletions pkg/app/piped/executor/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ import (
)

const (
activeServiceKeyName = "active-service-object"
// Canary task set metadata keys.
canaryTaskSetARNKeyName = "canary-taskset-arn"
canaryTaskSetKeyName = "canary-taskset-object"
// Stage metadata keys.
trafficRoutePrimaryMetadataKey = "primary-percentage"
trafficRouteCanaryMetadataKey = "canary-percentage"
Expand Down Expand Up @@ -237,7 +236,7 @@ func createPrimaryTaskSet(ctx context.Context, client provider.Client, service t

// Remove old taskSet if existed.
if prevPrimaryTaskSet != nil {
if err = client.DeleteTaskSet(ctx, service, *prevPrimaryTaskSet.TaskSetArn); err != nil {
if err = client.DeleteTaskSet(ctx, *prevPrimaryTaskSet); err != nil {
return err
}
}
Expand Down Expand Up @@ -266,17 +265,6 @@ func sync(ctx context.Context, in *executor.Input, platformProviderName string,
return false
}

// Store ACTIVE service to delete its unused TaskSet later.
serviceObjData, err := json.Marshal(service)
if err != nil {
in.LogPersister.Errorf("Unable to store applied service to metadata store: %v", err)
return false
}
if err := in.MetadataStore.Shared().Put(ctx, activeServiceKeyName, string(serviceObjData)); err != nil {
in.LogPersister.Errorf("Unable to store applied service to metadata store: %v", err)
return false
}

in.LogPersister.Infof("Start rolling out ECS task set")
if err := createPrimaryTaskSet(ctx, client, *service, *td, targetGroup); err != nil {
in.LogPersister.Errorf("Failed to rolling out ECS task set for service %s: %v", *serviceDefinition.ServiceName, err)
Expand Down Expand Up @@ -314,17 +302,6 @@ func rollout(ctx context.Context, in *executor.Input, platformProviderName strin
return false
}

// Store ACTIVE service to delete its unused TaskSet later.
serviceObjData, err := json.Marshal(service)
if err != nil {
in.LogPersister.Errorf("Unable to store applied service to metadata store: %v", err)
return false
}
if err := in.MetadataStore.Shared().Put(ctx, activeServiceKeyName, string(serviceObjData)); err != nil {
in.LogPersister.Errorf("Unable to store applied service to metadata store: %v", err)
return false
}

// Create a task set in the specified cluster and service.
in.LogPersister.Infof("Start rolling out ECS task set")
if in.StageConfig.Name == model.StageECSPrimaryRollout {
Expand Down Expand Up @@ -355,7 +332,12 @@ func rollout(ctx context.Context, in *executor.Input, platformProviderName strin
return false
}
// Store created ACTIVE TaskSet (CANARY variant) to delete later.
if err := in.MetadataStore.Shared().Put(ctx, canaryTaskSetARNKeyName, *taskSet.TaskSetArn); err != nil {
taskSetObjData, err := json.Marshal(taskSet)
if err != nil {
in.LogPersister.Errorf("Unable to store created active taskSet to metadata store: %v", err)
return false
}
if err := in.MetadataStore.Shared().Put(ctx, canaryTaskSetKeyName, string(taskSetObjData)); err != nil {
in.LogPersister.Errorf("Unable to store created active taskSet to metadata store: %v", err)
return false
}
Expand All @@ -378,30 +360,26 @@ func clean(ctx context.Context, in *executor.Input, platformProviderName string,
return false
}

// Get service object from metadata store.
serviceObjData, ok := in.MetadataStore.Shared().Get(activeServiceKeyName)
// Get task set object from metadata store.
taskSetObjData, ok := in.MetadataStore.Shared().Get(canaryTaskSetKeyName)
if !ok {
in.LogPersister.Errorf("Unable to restore service to clean: Not found")
in.LogPersister.Error("Unable to restore taskset to clean: Not found")
return false
}
service := &types.Service{}
if err := json.Unmarshal([]byte(serviceObjData), service); err != nil {
in.LogPersister.Errorf("Unable to restore service to clean: %v", err)
taskSet := &types.TaskSet{}
if err := json.Unmarshal([]byte(taskSetObjData), taskSet); err != nil {
in.LogPersister.Errorf("Unable to restore taskset to clean: %v", err)
return false
}

// Delete canary task set if present.
taskSetArn, ok := in.MetadataStore.Shared().Get(canaryTaskSetARNKeyName)
if ok {
in.LogPersister.Infof("Cleaning CANARY task set %s from service %s", taskSetArn, *service.ServiceName)
if err := client.DeleteTaskSet(ctx, *service, taskSetArn); err != nil {
in.LogPersister.Errorf("Failed to clean CANARY task set %s: %v", taskSetArn, err)
return false
}
return true
in.LogPersister.Infof("Cleaning CANARY task set %s from service %s", *taskSet.TaskSetArn, *taskSet.ServiceArn)
if err := client.DeleteTaskSet(ctx, *taskSet); err != nil {
in.LogPersister.Errorf("Failed to clean CANARY task set %s: %v", *taskSet.TaskSetArn, err)
return false
}

in.LogPersister.Info("No task set found in metadata store to clean")
in.LogPersister.Infof("Successfully cleaned CANARY task set %s from service %s", *taskSet.TaskSetArn, *taskSet.ServiceArn)
return true
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/executor/ecs/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func rollback(ctx context.Context, in *executor.Input, platformProviderName stri
in.LogPersister.Infof("Start deleting previous ACTIVE taskSets")
for _, ts := range prevTaskSets {
in.LogPersister.Infof("Deleting previous ACTIVE taskSet %s", *ts.TaskSetArn)
if err := client.DeleteTaskSet(ctx, *service, *ts.TaskSetArn); err != nil {
if err := client.DeleteTaskSet(ctx, *ts); err != nil {
in.LogPersister.Errorf("Failed to remove previous ACTIVE taskSet %s: %v", *ts.TaskSetArn, err)
return false
}
Expand Down
18 changes: 13 additions & 5 deletions pkg/app/piped/platformprovider/ecs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,22 @@ func (c *client) WaitServiceStable(ctx context.Context, service types.Service) e
return err
}

func (c *client) DeleteTaskSet(ctx context.Context, service types.Service, taskSetArn string) error {
func (c *client) DeleteTaskSet(ctx context.Context, taskSet types.TaskSet) error {
input := &ecs.DeleteTaskSetInput{
Cluster: service.ClusterArn,
Service: service.ServiceArn,
TaskSet: aws.String(taskSetArn),
Cluster: taskSet.ClusterArn,
Service: taskSet.ServiceArn,
TaskSet: taskSet.TaskSetArn,
}
if _, err := c.ecsClient.DeleteTaskSet(ctx, input); err != nil {
return fmt.Errorf("failed to delete ECS task set %s: %w", taskSetArn, err)
return fmt.Errorf("failed to delete ECS task set %s: %w", *taskSet.TaskSetArn, err)
}

// Inactive deleted taskset's task definition.
taskDefInput := &ecs.DeregisterTaskDefinitionInput{
TaskDefinition: taskSet.TaskDefinition,
}
if _, err := c.ecsClient.DeregisterTaskDefinition(ctx, taskDefInput); err != nil {
return fmt.Errorf("failed to inactive ECS task definition %s: %w", *taskSet.TaskDefinition, err)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/platformprovider/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ECS interface {
GetPrimaryTaskSet(ctx context.Context, service types.Service) (*types.TaskSet, error)
GetServiceTaskSets(ctx context.Context, service types.Service) ([]*types.TaskSet, error)
CreateTaskSet(ctx context.Context, service types.Service, taskDefinition types.TaskDefinition, targetGroup *types.LoadBalancer, scale int) (*types.TaskSet, error)
DeleteTaskSet(ctx context.Context, service types.Service, taskSetArn string) error
DeleteTaskSet(ctx context.Context, taskSet types.TaskSet) error
UpdateServicePrimaryTaskSet(ctx context.Context, service types.Service, taskSet types.TaskSet) (*types.TaskSet, error)
TagResource(ctx context.Context, resourceArn string, tags []types.Tag) error
}
Expand Down

0 comments on commit 5f7a9fc

Please sign in to comment.