Report custom labels set by agent admins back (#4141)

This commit is contained in:
6543
2024-10-06 17:13:41 +02:00
committed by GitHub
parent 0f7b607a40
commit f8cfda1ea9
22 changed files with 441 additions and 234 deletions

View File

@@ -20,6 +20,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"maps"
"net/http"
"os"
"strings"
@@ -198,7 +199,22 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
log.Debug().Msgf("loaded %s backend engine", backendEngine.Name())
maxWorkflows := int(c.Int("max-workflows"))
agentConfig.AgentID, err = client.RegisterAgent(grpcCtx, engInfo.Platform, backendEngine.Name(), version.String(), maxWorkflows) //nolint:contextcheck
customLabels := make(map[string]string)
if err := stringSliceAddToMap(c.StringSlice("labels"), customLabels); err != nil {
return err
}
if len(customLabels) != 0 {
log.Debug().Msgf("custom labels detected: %#v", customLabels)
}
agentConfig.AgentID, err = client.RegisterAgent(grpcCtx, rpc.AgentInfo{ //nolint:contextcheck
Version: version.String(),
Backend: backendEngine.Name(),
Platform: engInfo.Platform,
Capacity: maxWorkflows,
CustomLabels: customLabels,
})
if err != nil {
return err
}
@@ -210,7 +226,7 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
<-agentCtx.Done()
// Remove stateless agents from server
if !agentConfigPersisted.Load() {
log.Debug().Msg("unregistering agent from server ...")
log.Debug().Msg("unregister agent from server ...")
// we want to run it explicit run when context got canceled so run it in background
err := client.UnregisterAgent(grpcClientCtx)
if err != nil {
@@ -228,16 +244,15 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
}
}
// set default labels ...
labels := map[string]string{
"hostname": hostname,
"platform": engInfo.Platform,
"backend": backendEngine.Name(),
"repo": "*", // allow all repos by default
}
if err := stringSliceAddToMap(c.StringSlice("filter"), labels); err != nil {
return err
}
// ... and let it overwrite by custom ones
maps.Copy(labels, customLabels)
log.Debug().Any("labels", labels).Msgf("agent configured with labels")

View File

@@ -60,8 +60,9 @@ var flags = []cli.Flag{
Value: "/etc/woodpecker/agent.conf",
},
&cli.StringSliceFlag{
Sources: cli.EnvVars("WOODPECKER_FILTER_LABELS"),
Name: "filter",
Sources: cli.EnvVars("WOODPECKER_AGENT_LABELS", "WOODPECKER_FILTER_LABELS"), // remove WOODPECKER_FILTER_LABELS in v4.x
Name: "labels",
Aliases: []string{"filter"}, // remove in v4.x
Usage: "List of labels to filter tasks on. An agent must be assigned every tag listed in a task to be selected.",
},
&cli.IntFlag{

View File

@@ -4589,6 +4589,12 @@ const docTemplate = `{
"created": {
"type": "integer"
},
"custom_labels": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"id": {
"type": "integer"
},