-
-
Notifications
You must be signed in to change notification settings - Fork 41
Add DigitalOcean autoscaler support #580
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
Open
RJW34
wants to merge
2
commits into
woodpecker-ci:main
Choose a base branch
from
RJW34:feat/digitalocean-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package digitalocean | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| const category = "DigitalOcean" | ||
|
|
||
| var ProviderFlags = []cli.Flag{ | ||
| &cli.StringFlag{ | ||
| Name: "digitalocean-api-token", | ||
| Usage: "DigitalOcean API token", | ||
| Sources: cli.NewValueSourceChain( | ||
| cli.EnvVar("WOODPECKER_DIGITALOCEAN_API_TOKEN"), | ||
| cli.File(os.Getenv("WOODPECKER_DIGITALOCEAN_API_TOKEN_FILE")), | ||
| ), | ||
| Category: category, | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "digitalocean-region", | ||
| Value: "nyc1", | ||
| Usage: "DigitalOcean region slug", | ||
| Sources: cli.EnvVars("WOODPECKER_DIGITALOCEAN_REGION"), | ||
| Category: category, | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "digitalocean-size", | ||
| Value: "s-1vcpu-1gb", | ||
| Usage: "DigitalOcean droplet size slug", | ||
| Sources: cli.EnvVars("WOODPECKER_DIGITALOCEAN_SIZE"), | ||
| Category: category, | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "digitalocean-image", | ||
| Value: "ubuntu-22-04-x64", | ||
| Usage: "DigitalOcean image slug or ID", | ||
| Sources: cli.EnvVars("WOODPECKER_DIGITALOCEAN_IMAGE"), | ||
| Category: category, | ||
| }, | ||
| &cli.StringSliceFlag{ | ||
| Name: "digitalocean-ssh-keys", | ||
| Usage: "DigitalOcean SSH key fingerprints or IDs", | ||
| Sources: cli.EnvVars("WOODPECKER_DIGITALOCEAN_SSH_KEYS"), | ||
| Category: category, | ||
| }, | ||
| &cli.StringSliceFlag{ | ||
| Name: "digitalocean-tags", | ||
| Usage: "DigitalOcean droplet tags", | ||
| Sources: cli.EnvVars("WOODPECKER_DIGITALOCEAN_TAGS"), | ||
| Category: category, | ||
| }, | ||
| &cli.BoolFlag{ | ||
| Name: "digitalocean-ipv6", | ||
| Value: false, | ||
| Usage: "enable IPv6 for droplets", | ||
| Sources: cli.EnvVars("WOODPECKER_DIGITALOCEAN_IPV6"), | ||
| Category: category, | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "digitalocean-vpc-uuid", | ||
| Usage: "DigitalOcean VPC UUID", | ||
| Sources: cli.EnvVars("WOODPECKER_DIGITALOCEAN_VPC_UUID"), | ||
| Category: category, | ||
| }, | ||
| // TODO: Deprecated remove in v2.0 | ||
| &cli.StringFlag{ | ||
| Name: "digitalocean-user-data", | ||
| Usage: "DigitalOcean userdata template (deprecated)", | ||
| Sources: cli.NewValueSourceChain( | ||
| cli.EnvVar("WOODPECKER_DIGITALOCEAN_USERDATA"), | ||
| cli.File(os.Getenv("WOODPECKER_DIGITALOCEAN_USERDATA_FILE")), | ||
| ), | ||
| Category: category, | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| package digitalocean | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| "text/template" | ||
|
|
||
| "github.com/digitalocean/godo" | ||
| "github.com/rs/zerolog/log" | ||
| "github.com/urfave/cli/v3" | ||
|
|
||
| "go.woodpecker-ci.org/autoscaler/config" | ||
| "go.woodpecker-ci.org/autoscaler/engine" | ||
| "go.woodpecker-ci.org/woodpecker/v3/woodpecker-go/woodpecker" | ||
| ) | ||
|
|
||
| var ( | ||
| ErrIllegalLabelPrefix = errors.New("illegal label prefix") | ||
| ) | ||
|
|
||
| // DropletsService abstracts the godo.DropletsService for testability. | ||
| type DropletsService interface { | ||
| Create(ctx context.Context, createRequest *godo.DropletCreateRequest) (*godo.Droplet, *godo.Response, error) | ||
| Delete(ctx context.Context, dropletID int) (*godo.Response, error) | ||
| List(ctx context.Context, opt *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) | ||
| } | ||
|
|
||
| type Provider struct { | ||
| name string | ||
| region string | ||
| size string | ||
| image string | ||
| sshKeys []string | ||
| tags []string | ||
| enableIPv6 bool | ||
| vpcUUID string | ||
| userDataTemplate *template.Template | ||
| config *config.Config | ||
| droplets DropletsService | ||
| } | ||
|
|
||
| func New(_ context.Context, c *cli.Command, cfg *config.Config) (engine.Provider, error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| token := c.String("digitalocean-api-token") | ||
| client := godo.NewFromToken(token) | ||
|
|
||
| p := &Provider{ | ||
| name: "digitalocean", | ||
| region: c.String("digitalocean-region"), | ||
| size: c.String("digitalocean-size"), | ||
| image: c.String("digitalocean-image"), | ||
| sshKeys: c.StringSlice("digitalocean-ssh-keys"), | ||
| enableIPv6: c.Bool("digitalocean-ipv6"), | ||
| vpcUUID: c.String("digitalocean-vpc-uuid"), | ||
| config: cfg, | ||
| droplets: client.Droplets, | ||
| } | ||
|
|
||
| // TODO: Deprecated remove in v2.0 | ||
| if u := c.String("digitalocean-user-data"); u != "" { | ||
| log.Warn().Msg("digitalocean-user-data is deprecated, please use provider-user-data instead") | ||
| userDataTmpl, err := template.New("user-data").Parse(u) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("%s: template.New.Parse %w", p.name, err) | ||
| } | ||
| p.userDataTemplate = userDataTmpl | ||
| } | ||
|
|
||
| // Build default tags (used for listing deployed agents) | ||
| defaultTags := []string{ | ||
| fmt.Sprintf("%s=%s", engine.LabelPool, cfg.PoolID), | ||
| fmt.Sprintf("%s=%s", engine.LabelImage, p.image), | ||
| } | ||
|
|
||
| userTags := c.StringSlice("digitalocean-tags") | ||
| for _, tag := range userTags { | ||
| key, _, _ := strings.Cut(tag, "=") | ||
| if strings.HasPrefix(key, engine.LabelPrefix) { | ||
| return nil, fmt.Errorf("%s: %w: %s", p.name, ErrIllegalLabelPrefix, engine.LabelPrefix) | ||
| } | ||
| } | ||
| p.tags = append(defaultTags, userTags...) | ||
|
|
||
| return p, nil | ||
| } | ||
|
|
||
| func (p *Provider) DeployAgent(ctx context.Context, agent *woodpecker.Agent) error { | ||
| userData, err := engine.RenderUserDataTemplate(p.config, agent, p.userDataTemplate) | ||
| if err != nil { | ||
| return fmt.Errorf("%s: engine.RenderUserDataTemplate: %w", p.name, err) | ||
| } | ||
|
|
||
| sshKeys := make([]godo.DropletCreateSSHKey, 0, len(p.sshKeys)) | ||
| for _, key := range p.sshKeys { | ||
| sshKeys = append(sshKeys, godo.DropletCreateSSHKey{Fingerprint: key}) | ||
| } | ||
|
|
||
| createReq := &godo.DropletCreateRequest{ | ||
| Name: agent.Name, | ||
| Region: p.region, | ||
| Size: p.size, | ||
| UserData: userData, | ||
| SSHKeys: sshKeys, | ||
| Tags: p.tags, | ||
| IPv6: p.enableIPv6, | ||
| Image: godo.DropletCreateImage{ | ||
| Slug: p.image, | ||
| }, | ||
| } | ||
|
|
||
| if p.vpcUUID != "" { | ||
| createReq.VPCUUID = p.vpcUUID | ||
| } | ||
|
|
||
| log.Info().Msgf("create agent: region = %s size = %s", p.region, p.size) | ||
|
|
||
| _, _, err = p.droplets.Create(ctx, createReq) | ||
| if err != nil { | ||
| return fmt.Errorf("%s: Droplets.Create: %w", p.name, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *Provider) RemoveAgent(ctx context.Context, agent *woodpecker.Agent) error { | ||
| droplet, err := p.findDropletByName(ctx, agent.Name) | ||
| if err != nil { | ||
| return fmt.Errorf("%s: findDropletByName: %w", p.name, err) | ||
| } | ||
|
|
||
| if droplet == nil { | ||
| return nil | ||
| } | ||
|
|
||
| _, err = p.droplets.Delete(ctx, droplet.ID) | ||
| if err != nil { | ||
| return fmt.Errorf("%s: Droplets.Delete: %w", p.name, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *Provider) findDropletByName(ctx context.Context, name string) (*godo.Droplet, error) { | ||
| page := 1 | ||
| for { | ||
| droplets, resp, err := p.droplets.List(ctx, &godo.ListOptions{ | ||
| Page: page, | ||
| PerPage: 200, //nolint:mnd | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for i := range droplets { | ||
| if droplets[i].Name == name { | ||
| return &droplets[i], nil | ||
| } | ||
| } | ||
|
|
||
| if resp == nil || resp.Links == nil || resp.Links.Pages == nil || resp.Links.Pages.Next == "" { | ||
| break | ||
| } | ||
| page++ | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| func (p *Provider) ListDeployedAgentNames(ctx context.Context) ([]string, error) { | ||
| var names []string | ||
|
|
||
| page := 1 | ||
| for { | ||
| droplets, resp, err := p.droplets.List(ctx, &godo.ListOptions{ | ||
| Page: page, | ||
| PerPage: 200, //nolint:mnd | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("%s: Droplets.List: %w", p.name, err) | ||
| } | ||
|
|
||
| poolTag := fmt.Sprintf("%s=%s", engine.LabelPool, p.config.PoolID) | ||
| for _, d := range droplets { | ||
| for _, tag := range d.Tags { | ||
| if tag == poolTag { | ||
| names = append(names, d.Name) | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if resp == nil || resp.Links == nil || resp.Links.Pages == nil || resp.Links.Pages.Next == "" { | ||
| break | ||
| } | ||
| page++ | ||
| } | ||
|
|
||
| return names, nil | ||
| } | ||
|
|
||
| // Ensure Provider implements engine.Provider at compile time. | ||
| var _ engine.Provider = (*Provider)(nil) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we dont add deprecations we remove them...