Skip to content

Commit

Permalink
Some renamings and fail on parsing errors (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Jul 28, 2023
1 parent 6f9d78a commit a1b22e2
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .ecrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"vendor",
"fixtures",
"LICENSE",
"drivers/hetznercloud/driver.go",
"providers/hetznercloud/driver.go",
"_test.go",
"Makefile"
]
Expand Down
13 changes: 7 additions & 6 deletions cmd/woodpecker-autoscaler/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ var flags = []cli.Flag{
EnvVars: []string{"WOODPECKER_RECONCILIATION_INTERVAL"},
},
&cli.StringFlag{
Name: "pool-id",
Value: "1",
Usage: "id of the scaler pool",
Name: "pool-id",
Value: "1",
Usage: "id of the autoscaler pool",
EnvVars: []string{"WOODPECKER_POOL_ID"},
},
&cli.IntFlag{
Name: "min-agents",
Expand All @@ -49,13 +50,13 @@ var flags = []cli.Flag{
EnvVars: []string{"WOODPECKER_WORKFLOWS_PER_AGENT"},
},
&cli.StringFlag{
Name: "server",
Name: "server-url",
Value: "http://localhost:8000",
Usage: "woodpecker server address",
EnvVars: []string{"WOODPECKER_SERVER"},
},
&cli.StringFlag{
Name: "token",
Name: "server-token",
Usage: "woodpecker api token",
EnvVars: []string{"WOODPECKER_TOKEN"},
FilePath: os.Getenv("WOODPECKER_TOKEN_FILE"),
Expand All @@ -74,7 +75,7 @@ var flags = []cli.Flag{
},
&cli.StringFlag{
Name: "provider",
Value: "hetznercloud",
Value: "",
Usage: "cloud provider to use",
EnvVars: []string{"WOODPECKER_PROVIDER"},
},
Expand Down
10 changes: 5 additions & 5 deletions cmd/woodpecker-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/woodpecker-ci/autoscaler/drivers/hetznercloud"
"github.com/woodpecker-ci/autoscaler/engine"
"github.com/woodpecker-ci/autoscaler/providers/hetznercloud"
"github.com/woodpecker-ci/autoscaler/server"

_ "github.com/joho/godotenv/autoload"
Expand All @@ -22,13 +22,15 @@ func setupProvider(ctx *cli.Context, config *config.Config) (engine.Provider, er
switch driver := ctx.String("provider"); driver {
case "hetznercloud":
return hetznercloud.New(ctx, config, driver)
case "":
return nil, fmt.Errorf("Please select a provider")
}

return nil, fmt.Errorf("unknown provider: %s", ctx.String("provider"))
}

func run(ctx *cli.Context) error {
log.Log().Msgf("Start autoscaler LogLevel = %s", zerolog.GlobalLevel().String())
log.Log().Msgf("Starting autoscaler with log-level=%s", zerolog.GlobalLevel().String())

client, err := server.NewClient(ctx)
if err != nil {
Expand Down Expand Up @@ -77,9 +79,7 @@ func run(ctx *cli.Context) error {
case <-ctx.Done():
return nil
case <-time.After(reconciliationInterval):
if err := autoscaler.Reconcile(ctx.Context); err != nil {
return err
}
autoscaler.Reconcile(ctx.Context)
}
}
}
Expand Down
55 changes: 25 additions & 30 deletions engine/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func NewAutoscaler(provider Provider, client woodpecker.Client, config *config.C
}
}

// nolint:revive
func (a *Autoscaler) getQueueInfo(ctx context.Context) (freeTasks, runningTasks, pendingTasks int, err error) {
func (a *Autoscaler) getQueueInfo(_ context.Context) (freeTasks, runningTasks, pendingTasks int, err error) {
info, err := a.client.QueueInfo()
if err != nil {
return -1, -1, -1, fmt.Errorf("client.QueueInfo: %w", err)
Expand All @@ -37,8 +36,7 @@ func (a *Autoscaler) getQueueInfo(ctx context.Context) (freeTasks, runningTasks,
return info.Stats.Workers, info.Stats.Running, info.Stats.Pending + info.Stats.WaitingOnDeps, nil
}

// nolint:revive
func (a *Autoscaler) loadAgents(ctx context.Context) error {
func (a *Autoscaler) loadAgents(_ context.Context) error {
a.agents = []*woodpecker.Agent{}

agents, err := a.client.AgentList()
Expand Down Expand Up @@ -76,7 +74,7 @@ func (a *Autoscaler) createAgents(ctx context.Context, amount int) error {
return fmt.Errorf("client.AgentCreate: %w", err)
}

log.Info().Str("agent", agent.Name).Msg("deploy agent")
log.Info().Str("agent", agent.Name).Msg("deploying agent")

err = a.provider.DeployAgent(ctx, agent)
if err != nil {
Expand All @@ -89,8 +87,7 @@ func (a *Autoscaler) createAgents(ctx context.Context, amount int) error {
return nil
}

// nolint:revive
func (a *Autoscaler) drainAgents(ctx context.Context, amount int) error {
func (a *Autoscaler) drainAgents(_ context.Context, amount int) error {
now := time.Now()

for i := 0; i < amount; i++ {
Expand All @@ -112,7 +109,7 @@ func (a *Autoscaler) drainAgents(ctx context.Context, amount int) error {
return nil
}

func (a *Autoscaler) AgentIdle(agent *woodpecker.Agent) (bool, error) {
func (a *Autoscaler) isAgentIdle(agent *woodpecker.Agent) (bool, error) {
tasks, err := a.client.AgentTasksList(agent.ID)
if err != nil {
return false, fmt.Errorf("client.AgentTasksList: %w", err)
Expand All @@ -124,16 +121,16 @@ func (a *Autoscaler) AgentIdle(agent *woodpecker.Agent) (bool, error) {
func (a *Autoscaler) removeDrainedAgents(ctx context.Context) error {
for _, agent := range a.agents {
if agent.NoSchedule {
isIdle, err := a.AgentIdle(agent)
isIdle, err := a.isAgentIdle(agent)
if err != nil {
return err
}
if !isIdle {
log.Info().Str("agent", agent.Name).Msg("agent is processing workload")
log.Info().Str("agent", agent.Name).Msg("agent is still processing workload")
continue
}

log.Info().Str("agent", agent.Name).Msgf("remove agent")
log.Info().Str("agent", agent.Name).Msgf("removing agent")

err = a.provider.RemoveAgent(ctx, agent)
if err != nil {
Expand All @@ -152,7 +149,7 @@ func (a *Autoscaler) removeDrainedAgents(ctx context.Context) error {
return nil
}

func (a *Autoscaler) removeDetachedAgents(ctx context.Context) error {
func (a *Autoscaler) cleanupAgents(ctx context.Context) error {
registeredAgents := a.getPoolAgents(false)
deployedAgentNames, err := a.provider.ListDeployedAgentNames(ctx)
if err != nil {
Expand Down Expand Up @@ -223,46 +220,44 @@ func (a *Autoscaler) calcAgents(ctx context.Context) (float64, error) {
return reqPoolAgents, nil
}

func (a *Autoscaler) Reconcile(ctx context.Context) error {
func (a *Autoscaler) Reconcile(ctx context.Context) {
if err := a.loadAgents(ctx); err != nil {
log.Error().Err(err).Msg("load agents failed")
return nil
return
}

reqPoolAgents, err := a.calcAgents(ctx)
if err != nil {
log.Error().Err(err).Msg("calculate agents failed")
return nil
log.Error().Err(err).Msg("calculating agents failed")
return
}

if reqPoolAgents > 0 {
log.Debug().Msgf("start %v additional agents", reqPoolAgents)
log.Debug().Msgf("starting %f additional agents", reqPoolAgents)

if err := a.createAgents(ctx, int(reqPoolAgents)); err != nil {
log.Error().Err(err).Msgf("create agents failed")
return nil
log.Error().Err(err).Msg("creating agents failed")
return
}
}

if reqPoolAgents < 0 {
num := int(math.Abs(float64(reqPoolAgents)))
num := int(math.Abs(reqPoolAgents))

log.Debug().Msgf("try to stop %v agents", num)
log.Debug().Msgf("trying to stop %d agents", num)
if err := a.drainAgents(ctx, num); err != nil {
log.Error().Err(err).Msg("remove agents failed")
return nil
log.Error().Err(err).Msg("draining agents failed")
return
}
}

if err := a.removeDetachedAgents(ctx); err != nil {
log.Error().Err(err).Msg("remove agents failed")
return nil
if err := a.cleanupAgents(ctx); err != nil {
log.Error().Err(err).Msg("cleanup of agents failed")
return
}

if err := a.removeDrainedAgents(ctx); err != nil {
log.Error().Err(err).Msg("remove agents failed")
return nil
log.Error().Err(err).Msg("removing drained agents failed")
return
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func New(c *cli.Context, config *config.Config, name string) (engine.Provider, e
Networks: c.StringSlice("hetznercloud-networks"),
EnableIPv4: c.Bool("hetznercloud-public-ipv4-enable"),
EnableIPv6: c.Bool("hetznercloud-public-ipv6-enable"),
LabelPrefix: "wp.scaler/",
LabelPrefix: "wp.autoscaler/",
Config: config,
}

Expand Down
File renamed without changes.
26 changes: 12 additions & 14 deletions server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@ import (
// NewClient returns a new client from the CLI context.
func NewClient(c *cli.Context) (woodpecker.Client, error) {
var (
skip = c.Bool("skip-verify")
socks = c.String("socks-proxy")
socksoff = c.Bool("socks-proxy-off")
token = c.String("token")
server = c.String("server")
skip = c.Bool("skip-verify")
socks = c.String("socks-proxy")
socksoff = c.Bool("socks-proxy-off")
serverToken = c.String("server-token")
serverURL = c.String("server-url")
)
server = strings.TrimRight(server, "/")
serverURL = strings.TrimRight(serverURL, "/")

// if no server url is provided we can default
// to the hosted Woodpecker service.
if len(server) == 0 {
return nil, fmt.Errorf("Error: you must provide the Woodpecker server address")
if len(serverURL) == 0 {
return nil, fmt.Errorf("Error: Please provide the Woodpecker server address")
}
if len(token) == 0 {
return nil, fmt.Errorf("Error: you must provide your Woodpecker access token")
if len(serverToken) == 0 {
return nil, fmt.Errorf("Error: Please provide a Woodpecker access token")
}

// attempt to find system CA certs
Expand All @@ -48,7 +46,7 @@ func NewClient(c *cli.Context) (woodpecker.Client, error) {
client := config.Client(
c.Context,
&oauth2.Token{
AccessToken: token,
AccessToken: serverToken,
},
)

Expand All @@ -71,5 +69,5 @@ func NewClient(c *cli.Context) (woodpecker.Client, error) {
}
}

return woodpecker.NewClient(server, client), nil
return woodpecker.NewClient(serverURL, client), nil
}

0 comments on commit a1b22e2

Please sign in to comment.