Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWS provider #118

Merged
merged 3 commits into from
Sep 18, 2024
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
6 changes: 6 additions & 0 deletions cmd/woodpecker-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (

"go.woodpecker-ci.org/autoscaler/config"
"go.woodpecker-ci.org/autoscaler/engine"
"go.woodpecker-ci.org/autoscaler/providers/aws"
"go.woodpecker-ci.org/autoscaler/providers/hetznercloud"
"go.woodpecker-ci.org/autoscaler/server"
)

func setupProvider(ctx *cli.Context, config *config.Config) (engine.Provider, error) {
switch ctx.String("provider") {
case "aws":
return aws.New(ctx, config)
case "hetznercloud":
return hetznercloud.New(ctx, config)
// TODO: Temp disabled due to the security issue https://github.com/woodpecker-ci/autoscaler/issues/91
Expand Down Expand Up @@ -129,6 +132,9 @@ func main() {
// Enable it again when the issue is fixed.
// app.Flags = append(app.Flags, linode.ProviderFlags...)

// Register aws flags
app.Flags = append(app.Flags, aws.DriverFlags...)

if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("")
}
Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.22.0
toolchain go1.23.0

require (
github.com/aws/aws-sdk-go-v2 v1.26.0
github.com/aws/aws-sdk-go-v2/config v1.27.9
github.com/aws/aws-sdk-go-v2/service/ec2 v1.153.0
github.com/hetznercloud/hcloud-go/v2 v2.13.1
github.com/joho/godotenv v1.5.1
github.com/linode/linodego v1.39.0
Expand All @@ -18,11 +21,23 @@ require (
)

require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.9 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5 // indirect
github.com/aws/smithy-go v1.20.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-resty/resty/v2 v2.13.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
34 changes: 34 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
github.com/aws/aws-sdk-go-v2 v1.26.0 h1:/Ce4OCiM3EkpW7Y+xUnfAFpchU78K7/Ug01sZni9PgA=
github.com/aws/aws-sdk-go-v2 v1.26.0/go.mod h1:35hUlJVYd+M++iLI3ALmVwMOyRYMmRqUXpTtRGW+K9I=
github.com/aws/aws-sdk-go-v2/config v1.27.9 h1:gRx/NwpNEFSk+yQlgmk1bmxxvQ5TyJ76CWXs9XScTqg=
github.com/aws/aws-sdk-go-v2/config v1.27.9/go.mod h1:dK1FQfpwpql83kbD873E9vz4FyAxuJtR22wzoXn3qq0=
github.com/aws/aws-sdk-go-v2/credentials v1.17.9 h1:N8s0/7yW+h8qR8WaRlPQeJ6czVMNQVNtNdUqf6cItao=
github.com/aws/aws-sdk-go-v2/credentials v1.17.9/go.mod h1:446YhIdmSV0Jf/SLafGZalQo+xr2iw7/fzXGDPTU1yQ=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 h1:af5YzcLf80tv4Em4jWVD75lpnOHSBkPUZxZfGkrI3HI=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0/go.mod h1:nQ3how7DMnFMWiU1SpECohgC82fpn4cKZ875NDMmwtA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4 h1:0ScVK/4qZ8CIW0k8jOeFVsyS/sAiXpYxRBLolMkuLQM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4/go.mod h1:84KyjNZdHC6QZW08nfHI6yZgPd+qRgaWcYsyLUo3QY8=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4 h1:sHmMWWX5E7guWEFQ9SVo6A3S4xpPrWnd77a6y4WM6PU=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4/go.mod h1:WjpDrhWisWOIoS9n3nk67A3Ll1vfULJ9Kq6h29HTD48=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.153.0 h1:8dTwpqHb0B3SKEmmXdLRtMNOlL0rivjX8cB/ykqskag=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.153.0/go.mod h1:TeZ9dVQzGaLG+SBIgdLIDbJ6WmfFvksLeG3EHGnNfZM=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 h1:EyBZibRTVAs6ECHZOw5/wlylS9OcTzwyjeQMudmREjE=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1/go.mod h1:JKpmtYhhPs7D97NL/ltqz7yCkERFW5dOlHyVl66ZYF8=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.6 h1:b+E7zIUHMmcB4Dckjpkapoy47W6C9QBv/zoUP+Hn8Kc=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.6/go.mod h1:S2fNV0rxrP78NhPbCZeQgY8H9jdDMeGtwcfZIRxzBqU=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.3 h1:mnbuWHOcM70/OFUlZZ5rcdfA8PflGXXiefU/O+1S3+8=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.3/go.mod h1:5HFu51Elk+4oRBZVxmHrSds5jFXmFj8C3w7DVF2gnrs=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3 h1:uLq0BKatTmDzWa/Nu4WO0M1AaQDaPpwTKAeByEc6WFM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3/go.mod h1:b+qdhjnxj8GSR6t5YfphOffeoQSQ1KmpoVVuBn+PWxs=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5 h1:J/PpTf/hllOjx8Xu9DMflff3FajfLxqM5+tepvVXmxg=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5/go.mod h1:0ih0Z83YDH/QeQ6Ori2yGE2XvWYv/Xm+cZc01LC6oK0=
github.com/aws/smithy-go v1.20.1 h1:4SZlSlMr36UEqC7XOyRVb27XMeZubNcBNN+9IgEPIQw=
github.com/aws/smithy-go v1.20.1/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g=
Expand All @@ -16,6 +45,9 @@ github.com/hetznercloud/hcloud-go/v2 v2.13.1 h1:jq0GP4QaYE5d8xR/Zw17s9qoaESRJMXf
github.com/hetznercloud/hcloud-go/v2 v2.13.1/go.mod h1:dhix40Br3fDiBhwaSG/zgaYOFFddpfBm/6R1Zz0IiF0=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down Expand Up @@ -48,6 +80,7 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down Expand Up @@ -124,5 +157,6 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
63 changes: 63 additions & 0 deletions providers/aws/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package aws

import "github.com/urfave/cli/v2"

const Category = "AWS"

var DriverFlags = []cli.Flag{
// aws
&cli.StringFlag{
Name: "aws-instance-type",
Usage: "EC2 instance type",
EnvVars: []string{"WOODPECKER_AWS_INSTANCE_TYPE"},
Category: Category,
},
&cli.StringFlag{
Name: "aws-ami-id",
Usage: "AMI id",
EnvVars: []string{"WOODPECKER_AWS_AMI_ID"},
Category: Category,
},
&cli.StringSliceFlag{
Name: "aws-tags",
Usage: "additional tags for your EC2 instances",
EnvVars: []string{"WOODPECKER_AWS_TAGS"},
Category: Category,
},
&cli.StringFlag{
Name: "aws-region",
Usage: "AWS region",
EnvVars: []string{"WOODPECKER_AWS_REGION"},
Category: Category,
},
&cli.StringSliceFlag{
Name: "aws-subnets",
Usage: "VPC subnets IDs, e.g. subnet-0987a87c8b37348ef",
EnvVars: []string{"WOODPECKER_AWS_SUBNETS"},
Category: Category,
},
&cli.StringFlag{
Name: "aws-iam-instance-profile-arn",
Usage: "IAM instance profile ARN",
EnvVars: []string{"WOODPECKER_AWS_IAM_INSTANCE_PROFILE_ARN"},
Category: Category,
},
&cli.StringSliceFlag{
Name: "aws-security-groups",
Usage: "security groups attached to EC2 instances",
EnvVars: []string{"WOODPECKER_AWS_SECURITY_GROUPS"},
Category: Category,
},
&cli.BoolFlag{
Name: "aws-use-spot-instances",
Usage: "use spot instances",
EnvVars: []string{"WOODPECKER_AWS_USE_SPOT_INSTANCES"},
Category: Category,
},
&cli.StringFlag{
Name: "aws-ssh-key-name",
Usage: "ssh keypair name",
EnvVars: []string{"WOODPECKER_AWS_SSH_KEYNAME"},
Category: Category,
},
}
190 changes: 190 additions & 0 deletions providers/aws/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package aws

import (
"context"
b64 "encoding/base64"
"fmt"
"sync"
"text/template"

"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
zerolog "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"

"go.woodpecker-ci.org/autoscaler/config"
"go.woodpecker-ci.org/autoscaler/engine"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

type Provider struct {
name string
config *config.Config
instanceType string
amiID string
tags []string
region string
subnets []string
securityGroups []string
iamInstanceProfileArn string
useSpotInstances bool
client *ec2.Client
lock sync.Mutex
subnetRR int
sshKeyName string
}

func New(c *cli.Context, config *config.Config) (engine.Provider, error) {
if len(c.StringSlice("aws-subnets")) == 0 {
return nil, fmt.Errorf("aws-subnets must be set")
}
d := &Provider{
name: "aws",
config: config,
instanceType: c.String("aws-instance-type"),
amiID: c.String("aws-ami-id"),
tags: c.StringSlice("aws-tags"),
region: c.String("aws-region"),
subnets: c.StringSlice("aws-subnets"),
iamInstanceProfileArn: c.String("aws-iam-instance-profile-arn"),
securityGroups: c.StringSlice("aws-security-groups"),
useSpotInstances: c.Bool("aws-use-spot-instances"),
sshKeyName: c.String("aws-ssh-key-name"),
}
cfg, err := awsconfig.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, fmt.Errorf("failed to load configuration, %w", err)
}
d.client = ec2.NewFromConfig(cfg)

return d, nil
}

func (p *Provider) DeployAgent(ctx context.Context, agent *woodpecker.Agent) error {
runInstancesInput := ec2.RunInstancesInput{
IamInstanceProfile: &types.IamInstanceProfileSpecification{
Arn: aws.String(p.iamInstanceProfileArn),
},
ImageId: aws.String(p.amiID),
InstanceType: types.InstanceType(p.instanceType),
MetadataOptions: &types.InstanceMetadataOptionsRequest{
HttpEndpoint: types.InstanceMetadataEndpointStateEnabled,
HttpPutResponseHopLimit: aws.Int32(1),
HttpTokens: types.HttpTokensStateRequired,
},
SecurityGroupIds: p.securityGroups,
MinCount: aws.Int32(1),
MaxCount: aws.Int32(1),
TagSpecifications: []types.TagSpecification{
{
ResourceType: "instance",
Tags: []types.Tag{{
Key: aws.String("Name"),
Value: aws.String(agent.Name),
}, {
Key: aws.String(engine.LabelPool),
Value: aws.String(p.config.PoolID),
}},
},
},
}

// When multiple subnets are given, assign agent to a subnet in a round-robin fashion.
p.lock.Lock()
runInstancesInput.SubnetId = aws.String(p.subnets[p.subnetRR])
p.subnetRR = (p.subnetRR + 1) % len(p.subnets)
p.lock.Unlock()

if p.useSpotInstances {
runInstancesInput.InstanceMarketOptions = &types.InstanceMarketOptionsRequest{
MarketType: types.MarketTypeSpot,
}
}

if p.sshKeyName != "" {
runInstancesInput.KeyName = aws.String(p.sshKeyName)
}

userDataStr := engine.CloudInitUserDataUbuntuDefault
userDataTmpl, err := template.New("user-data").Parse(userDataStr)
if err != nil {
return fmt.Errorf("%s: template.New.Parse %w", p.name, err)
}
userData, err := engine.RenderUserDataTemplate(p.config, agent, userDataTmpl)
if err != nil {
return fmt.Errorf("%s: engine.RenderUserDataTemplate: %w", p.name, err)
}

runInstancesInput.UserData = aws.String(b64.StdEncoding.EncodeToString([]byte(userData)))
_, err = p.client.RunInstances(ctx, &runInstancesInput)
if err != nil {
return fmt.Errorf("%s: Server.Create: %w", p.name, err)
}
return nil
}

func (p *Provider) getAgent(ctx context.Context, agent *woodpecker.Agent) (*types.Instance, error) {
instances, err := p.client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{agent.Name},
},
},
})
if err != nil {
return nil, err
}
if len(instances.Reservations) != 1 {
return nil, fmt.Errorf("expected 1 reservation with tag:Name=%s, got %d", agent.Name, len(instances.Reservations))
}
if len(instances.Reservations[0].Instances) != 1 {
return nil, fmt.Errorf("expected 1 instance with tag:Name=%s, got %d", agent.Name, len(instances.Reservations[0].Instances))
}
return &instances.Reservations[0].Instances[0], nil
anbraten marked this conversation as resolved.
Show resolved Hide resolved
}

func (p *Provider) RemoveAgent(ctx context.Context, agent *woodpecker.Agent) error {
instance, err := p.getAgent(ctx, agent)
if err != nil {
return err
}
_, err = p.client.TerminateInstances(ctx, &ec2.TerminateInstancesInput{
InstanceIds: []string{*instance.InstanceId},
})
return err
}

func (p *Provider) ListDeployedAgentNames(ctx context.Context) ([]string, error) {
zerolog.Debug().Msgf("List deployed agent names")

var names []string
instances, err := p.client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: aws.String(fmt.Sprintf("tag:%s", engine.LabelPool)),
Values: []string{p.config.PoolID},
},
},
})
if err != nil {
return nil, err
}
for _, reservation := range instances.Reservations {
for _, instance := range reservation.Instances {
if instance.State.Name != types.InstanceStateNamePending &&
instance.State.Name != types.InstanceStateNameRunning {
continue
}
for _, tag := range instance.Tags {
if *tag.Key == "Name" {
zerolog.Debug().Msgf("Found agent %s", *tag.Value)
names = append(names, *tag.Value)
}
}
}
}
return names, nil
}