Skip to content

Commit

Permalink
Add pipecd builtin tags to ECS resources (#4140)
Browse files Browse the repository at this point in the history
* Add tags to aws resources.

* Small fix

* Small fix

* Fix initial slice length.

* Add TagResource function

* Fix deploy process

* Add tags to aws resources.

* Small fix

* Small fix

* Fix initial slice length.

* Add TagResource function

* Fix deploy process

* Delete Unnecessary blank lines

* Change how to add task's tags

* Delete TagResource method

* Fix based on linter

* Apply review

* add builtin-labels

* Add tagResource func

* Set default value of PropagateTags

* Apply review

* Apply review
  • Loading branch information
TonkyH authored and knanao committed Feb 7, 2023
1 parent 6cd51a4 commit 99e56f0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
21 changes: 21 additions & 0 deletions pkg/app/piped/executor/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ func loadServiceDefinition(in *executor.Input, serviceDefinitionFile string, ds
return types.Service{}, false
}

serviceDefinition.Tags = append(
serviceDefinition.Tags,
provider.MakeTags(map[string]string{
provider.LabelManagedBy: provider.ManagedByPiped,
provider.LabelPiped: in.PipedConfig.PipedID,
provider.LabelApplication: in.Deployment.ApplicationId,
provider.LabelCommitHash: in.Deployment.CommitHash(),
})...,
)

in.LogPersister.Infof("Successfully loaded the ECS service definition at commit %s", ds.Revision)
return serviceDefinition, true
}
Expand Down Expand Up @@ -147,6 +157,10 @@ func applyServiceDefinition(ctx context.Context, cli provider.Client, serviceDef
if err != nil {
return nil, fmt.Errorf("failed to update ECS service %s: %v", *serviceDefinition.ServiceName, err)
}
if err := cli.TagResource(ctx, *service.ServiceArn, serviceDefinition.Tags); err != nil {
return nil, fmt.Errorf("failed to update tags of ECS service %s: %v", *serviceDefinition.ServiceName, err)
}

} else {
service, err = cli.CreateService(ctx, serviceDefinition)
if err != nil {
Expand All @@ -172,6 +186,12 @@ func runStandaloneTask(
}

in.LogPersister.Infof("Start applying the ECS task definition")
tags := provider.MakeTags(map[string]string{
provider.LabelManagedBy: provider.ManagedByPiped,
provider.LabelPiped: in.PipedConfig.PipedID,
provider.LabelApplication: in.Deployment.ApplicationId,
provider.LabelCommitHash: in.Deployment.CommitHash(),
})
td, err := applyTaskDefinition(ctx, client, taskDefinition)
if err != nil {
in.LogPersister.Errorf("Failed to apply ECS task definition: %v", err)
Expand All @@ -184,6 +204,7 @@ func runStandaloneTask(
ecsInput.ClusterArn,
ecsInput.LaunchType,
&ecsInput.AwsVpcConfiguration,
tags,
)
if err != nil {
in.LogPersister.Errorf("Failed to run ECS task: %v", err)
Expand Down
18 changes: 15 additions & 3 deletions pkg/app/piped/platformprovider/ecs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ func (c *client) CreateService(ctx context.Context, service types.Service) (*typ
PlacementConstraints: service.PlacementConstraints,
PlacementStrategy: service.PlacementStrategy,
PlatformVersion: service.PlatformVersion,
PropagateTags: service.PropagateTags,
PropagateTags: types.PropagateTagsService,
Role: service.RoleArn,
SchedulingStrategy: service.SchedulingStrategy,
ServiceRegistries: service.ServiceRegistries,
Tags: service.Tags,
}

output, err := c.ecsClient.CreateService(ctx, input)
if err != nil {
return nil, fmt.Errorf("failed to create ECS service %s: %w", *service.ServiceName, err)
Expand Down Expand Up @@ -151,7 +150,7 @@ func (c *client) RegisterTaskDefinition(ctx context.Context, taskDefinition type
return output.TaskDefinition, nil
}

func (c *client) RunTask(ctx context.Context, taskDefinition types.TaskDefinition, clusterArn string, launchType string, awsVpcConfiguration *appconfig.ECSVpcConfiguration) error {
func (c *client) RunTask(ctx context.Context, taskDefinition types.TaskDefinition, clusterArn string, launchType string, awsVpcConfiguration *appconfig.ECSVpcConfiguration, tags []types.Tag) error {
if taskDefinition.TaskDefinitionArn == nil {
return fmt.Errorf("failed to run task of task family %s: no task definition provided", *taskDefinition.Family)
}
Expand All @@ -160,6 +159,7 @@ func (c *client) RunTask(ctx context.Context, taskDefinition types.TaskDefinitio
TaskDefinition: taskDefinition.Family,
Cluster: aws.String(clusterArn),
LaunchType: types.LaunchType(launchType),
Tags: tags,
}

if len(awsVpcConfiguration.Subnets) > 0 {
Expand Down Expand Up @@ -341,3 +341,15 @@ func (c *client) ModifyListener(ctx context.Context, listenerArn string, routing
_, err := c.elbClient.ModifyListener(ctx, input)
return err
}

func (c *client) TagResource(ctx context.Context, resourceArn string, tags []types.Tag) error {
input := &ecs.TagResourceInput{
ResourceArn: aws.String(resourceArn),
Tags: tags,
}
_, err := c.ecsClient.TagResource(ctx, input)
if err != nil {
return fmt.Errorf("failed to update tag of resource %s: %w", resourceArn, err)
}
return nil
}
20 changes: 19 additions & 1 deletion pkg/app/piped/platformprovider/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ import (
"path/filepath"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"go.uber.org/zap"
"golang.org/x/sync/singleflight"

"github.com/pipe-cd/pipecd/pkg/config"
)

const (
LabelManagedBy string = "pipecd-dev-managed-by" // Always be piped.
LabelPiped string = "pipecd-dev-piped" // The id of piped handling this application.
LabelApplication string = "pipecd-dev-application" // The application this resource belongs to.
LabelCommitHash string = "pipecd-dev-commit-hash" // Hash value of the deployed commit.
ManagedByPiped string = "piped"
)

// Client is wrapper of ECS client.
type Client interface {
ECS
Expand All @@ -37,11 +46,12 @@ type ECS interface {
CreateService(ctx context.Context, service types.Service) (*types.Service, error)
UpdateService(ctx context.Context, service types.Service) (*types.Service, error)
RegisterTaskDefinition(ctx context.Context, taskDefinition types.TaskDefinition) (*types.TaskDefinition, error)
RunTask(ctx context.Context, taskDefinition types.TaskDefinition, clusterArn string, launchType string, awsVpcConfiguration *config.ECSVpcConfiguration) error
RunTask(ctx context.Context, taskDefinition types.TaskDefinition, clusterArn string, launchType string, awsVpcConfiguration *config.ECSVpcConfiguration, tags []types.Tag) error
GetPrimaryTaskSet(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
UpdateServicePrimaryTaskSet(ctx context.Context, service types.Service, taskSet types.TaskSet) (*types.TaskSet, error)
TagResource(ctx context.Context, resourceArn string, tags []types.Tag) error
}

type ELB interface {
Expand Down Expand Up @@ -109,3 +119,11 @@ var defaultRegistry = &registry{
func DefaultRegistry() Registry {
return defaultRegistry
}

func MakeTags(tags map[string]string) []types.Tag {
resourceTags := make([]types.Tag, 0, len(tags))
for key, value := range tags {
resourceTags = append(resourceTags, types.Tag{Key: aws.String(key), Value: aws.String(value)})
}
return resourceTags
}

0 comments on commit 99e56f0

Please sign in to comment.