@@ -17,6 +17,7 @@ package deployment
1717import (
1818 "context"
1919 "encoding/json"
20+ "errors"
2021 "fmt"
2122
2223 "github.com/aws/aws-sdk-go-v2/service/ecs/types"
@@ -147,3 +148,56 @@ func canaryRollout(
147148 lp .Successf ("Successfully rolled out CANARY task set %s for service %s" , * taskSet .TaskSetArn , * service .ServiceName )
148149 return taskSet , nil
149150}
151+
152+ func (p * ECSPlugin ) executeECSCanaryCleanStage (
153+ ctx context.Context ,
154+ input * sdk.ExecuteStageInput [ecsconfig.ECSApplicationSpec ],
155+ deployTarget * sdk.DeployTarget [ecsconfig.ECSDeployTargetConfig ],
156+ ) sdk.StageStatus {
157+ lp := input .Client .LogPersister ()
158+
159+ taskSetData , found , err := input .Client .GetDeploymentPluginMetadata (ctx , canaryTaskSetMetadataKey )
160+ if err != nil {
161+ lp .Errorf ("Failed to retrieve canary task set from metadata store: %v" , err )
162+ return sdk .StageStatusFailure
163+ }
164+ if ! found {
165+ lp .Info ("No canary task set found in metadata store, nothing to clean up" )
166+ return sdk .StageStatusSuccess
167+ }
168+
169+ var taskSet types.TaskSet
170+ if err := json .Unmarshal ([]byte (taskSetData ), & taskSet ); err != nil {
171+ lp .Errorf ("Failed to unmarshal canary task set from metadata store: %v" , err )
172+ return sdk .StageStatusFailure
173+ }
174+
175+ client , err := provider .DefaultRegistry ().Client (deployTarget .Name , deployTarget .Config )
176+ if err != nil {
177+ lp .Errorf ("Failed to get ECS client for deploy target %s: %v" , deployTarget .Name , err )
178+ return sdk .StageStatusFailure
179+ }
180+
181+ if err := canaryClean (ctx , lp , client , taskSet ); err != nil {
182+ lp .Errorf ("Failed to clean up ECS canary task set: %v" , err )
183+ return sdk .StageStatusFailure
184+ }
185+
186+ return sdk .StageStatusSuccess
187+ }
188+
189+ // canaryClean deletes the canary task set
190+ func canaryClean (ctx context.Context , lp sdk.StageLogPersister , client provider.Client , taskSet types.TaskSet ) error {
191+ lp .Infof ("Deleting canary task set %s" , * taskSet .TaskSetArn )
192+ if err := client .DeleteTaskSet (ctx , taskSet ); err != nil {
193+ // If the task set is already gone, treat as success
194+ var notFound * types.TaskSetNotFoundException
195+ if errors .As (err , & notFound ) {
196+ lp .Infof ("Canary task set %s already deleted, skipping" , * taskSet .TaskSetArn )
197+ return nil
198+ }
199+ return fmt .Errorf ("failed to delete canary task set %s: %w" , * taskSet .TaskSetArn , err )
200+ }
201+ lp .Successf ("Successfully deleted canary task set %s" , * taskSet .TaskSetArn )
202+ return nil
203+ }
0 commit comments