From fe52d864a4796c6eb1b8a3cdfec5a56544369a5d Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Sun, 12 Nov 2023 22:24:44 +0100 Subject: [PATCH] change agent creation logic to use agentToken instead of systemToken --- cmd/woodpecker-autoscaler.go | 21 ++++++--- internal/hetzner/hetzneragent.go | 33 +++++++++---- internal/hetzner/hetzneragent_test.go | 3 +- internal/woodpecker/agent.go | 67 ++++++++++++++++++++++++++- internal/woodpecker/metrics.go | 2 +- 5 files changed, 107 insertions(+), 19 deletions(-) diff --git a/cmd/woodpecker-autoscaler.go b/cmd/woodpecker-autoscaler.go index 0156cf6..ed6a46c 100644 --- a/cmd/woodpecker-autoscaler.go +++ b/cmd/woodpecker-autoscaler.go @@ -52,7 +52,13 @@ func main() { "Caller": "Main", }).Infof("Currently owning %d Agents", len(ownedNodes)) if pendingTasks > len(ownedNodes) { - server, err := hetzner.CreateNewAgent(cfg) + agent, err := woodpecker.CreateWoodpeckerAgent(cfg) + if err != nil { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Fatal(fmt.Sprintf("Error creating new agent: %s", err.Error())) + } + server, err := hetzner.CreateNewAgent(cfg, agent) if err != nil { log.WithFields(log.Fields{ "Caller": "Main", @@ -100,14 +106,17 @@ func main() { "Caller": "Main", }).Info("No tasks running. Will remove agents") for _, server := range ownedNodes { - hetzner.DecomNode(cfg, &server) - agentId, err := woodpecker.GetAgentIdByName(cfg, server.Name) + agentId, err := hetzner.DecomNode(cfg, &server) if err != nil { log.WithFields(log.Fields{ "Caller": "Main", - }).Warnf("Could not find agent %s in woodpecker. Assuming it was never added", server.Name) - } else { - woodpecker.DecomAgent(cfg, agentId) + }).Warnf("Error while deleting node %s: %s", server.Name, err.Error()) + } + err = woodpecker.DecomAgent(cfg, agentId) + if err != nil { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Warnf("Could not delete node %s in woodpecker: %s", server.Name, err.Error()) } } } diff --git a/internal/hetzner/hetzneragent.go b/internal/hetzner/hetzneragent.go index 3d83db5..f02f45b 100644 --- a/internal/hetzner/hetzneragent.go +++ b/internal/hetzner/hetzneragent.go @@ -5,10 +5,12 @@ import ( "context" "errors" "fmt" + "strconv" "strings" "text/template" "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config" + "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models" "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/utils" "github.com/hetznercloud/hcloud-go/hcloud" @@ -42,13 +44,14 @@ type UserDataConfig struct { EnvConfig map[string]interface{} } -func generateConfig(cfg *config.Config, name string) (string, error) { +func generateConfig(cfg *config.Config, name string, agentToken string) (string, error) { envConfig := map[string]interface{}{ "WOODPECKER_SERVER": fmt.Sprintf("%s", cfg.WoodpeckerGrpc), "WOODPECKER_GRPC_SECURE": true, - "WOODPECKER_AGENT_SECRET": fmt.Sprintf("%s", cfg.WoodpeckerAgentSecret), + "WOODPECKER_AGENT_SECRET": fmt.Sprintf("%s", agentToken), "WOODPECKER_FILTER_LABELS": fmt.Sprintf("%s", cfg.WoodpeckerLabelSelector), "WOODPECKER_HOSTNAME": fmt.Sprintf("%s", name), + "WOODPECKER_MAX_WORKFLOWS": 4, } config := UserDataConfig{ Image: fmt.Sprintf("woodpeckerci/woodpecker-agent:%s", cfg.WoodpeckerAgentVersion), @@ -66,10 +69,9 @@ func generateConfig(cfg *config.Config, name string) (string, error) { return buf.String(), nil } -func CreateNewAgent(cfg *config.Config) (*hcloud.Server, error) { +func CreateNewAgent(cfg *config.Config, woodpeckerAgent *models.Agent) (*hcloud.Server, error) { client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken)) - name := fmt.Sprintf("woodpecker-autoscaler-agent-%s", utils.RandStringBytes(5)) - userdata, err := generateConfig(cfg, name) + userdata, err := generateConfig(cfg, woodpeckerAgent.Name, woodpeckerAgent.Token) keys := []*hcloud.SSHKey{} for _, keyName := range strings.Split(cfg.HcloudSSHKeys, ",") { key, _, err := client.SSHKey.GetByName(context.Background(), keyName) @@ -90,6 +92,7 @@ func CreateNewAgent(cfg *config.Config) (*hcloud.Server, error) { labels := map[string]string{} labels["Role"] = "WoodpeckerAgent" labels["ControledBy"] = "WoodpeckerAutoscaler" + labels["ID"] = fmt.Sprintf("%d", woodpeckerAgent.ID) networkConf := hcloud.ServerCreatePublicNet{ EnableIPv4: !cfg.HcloudIPv6Only, @@ -97,7 +100,7 @@ func CreateNewAgent(cfg *config.Config) (*hcloud.Server, error) { } res, _, err := client.Server.Create(context.Background(), hcloud.ServerCreateOpts{ - Name: name, + Name: woodpeckerAgent.Name, ServerType: pln, Image: img, SSHKeys: keys, @@ -138,16 +141,28 @@ func ListAgents(cfg *config.Config) ([]hcloud.Server, error) { return myServers, nil } -func DecomNode(cfg *config.Config, server *hcloud.Server) error { +func DecomNode(cfg *config.Config, server *hcloud.Server) (int64, error) { client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken)) + var woodpeckerAgentID int64 + val, exists := server.Labels["ID"] + if exists { + log.WithFields(log.Fields{ + "Caller": "DecomNode", + }).Debugf("Found woodpecker agent id: %s", val) + woodpeckerAgentID, _ = strconv.ParseInt(val, 10, 64) + } else { + log.WithFields(log.Fields{ + "Caller": "DecomNode", + }).Warnf("Did not find woodpecker agent id for node %s", server.Name) + } log.WithFields(log.Fields{ "Caller": "DecomNode", }).Debugf("Deleting %s node", server.Name) _, _, err := client.Server.DeleteWithResult(context.Background(), server) if err != nil { - return errors.New(fmt.Sprintf("Could not delete Agent: %s", err.Error())) + return woodpeckerAgentID, errors.New(fmt.Sprintf("Could not delete Agent: %s", err.Error())) } - return nil + return woodpeckerAgentID, nil } func RefreshNodeInfo(cfg *config.Config, serverID int) (*hcloud.Server, error) { diff --git a/internal/hetzner/hetzneragent_test.go b/internal/hetzner/hetzneragent_test.go index 3cc53ab..be345a7 100644 --- a/internal/hetzner/hetzneragent_test.go +++ b/internal/hetzner/hetzneragent_test.go @@ -40,12 +40,13 @@ write_files: - WOODPECKER_FILTER_LABELS=uploadfilter24.eu/instance-role=WoodpeckerTest - WOODPECKER_GRPC_SECURE=true - WOODPECKER_HOSTNAME=test-instance + - WOODPECKER_MAX_WORKFLOWS=4 - WOODPECKER_SERVER=grpc-test.woodpecker.test.tld:443 path: /root/docker-compose.yml runcmd: - [ sh, -xc, "cd /root; docker run --rm --privileged multiarch/qemu-user-static --reset -p yes; docker compose up -d" ] ` - got, err := generateConfig(&cfg, "test-instance") + got, err := generateConfig(&cfg, "test-instance", "Geheim1!") if err != nil { t.Errorf("Error in generating Config: %v", err) } diff --git a/internal/woodpecker/agent.go b/internal/woodpecker/agent.go index e1c6c4f..3998ac6 100644 --- a/internal/woodpecker/agent.go +++ b/internal/woodpecker/agent.go @@ -1,6 +1,7 @@ package woodpecker import ( + "bytes" "encoding/json" "errors" "fmt" @@ -8,11 +9,12 @@ import ( "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config" "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models" + "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/utils" log "github.com/sirupsen/logrus" ) -func DecomAgent(cfg *config.Config, agentId int) error { +func DecomAgent(cfg *config.Config, agentId int64) error { apiRoute := fmt.Sprintf("%s/api/agents/%d", cfg.WoodpeckerInstance, agentId) req, err := http.NewRequest("DELETE", apiRoute, nil) if err != nil { @@ -35,7 +37,7 @@ func DecomAgent(cfg *config.Config, agentId int) error { func GetAgentIdByName(cfg *config.Config, name string) (int, error) { apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance) - req, err := http.NewRequest("GET", apiRoute, nil) + req, err := http.NewRequest(http.MethodGet, apiRoute, nil) if err != nil { return 0, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error())) } @@ -67,3 +69,64 @@ func GetAgentIdByName(cfg *config.Config, name string) (int, error) { } return 0, errors.New(fmt.Sprintf("Agent with name %s is not in server", name)) } + +func ListAgents(cfg *config.Config) (*models.AgentList, error) { + agentList := new(models.AgentList) + apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance) + req, err := http.NewRequest(http.MethodGet, apiRoute, nil) + if err != nil { + return agentList, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error())) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken)) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return agentList, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error())) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return agentList, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode)) + } + err = json.NewDecoder(resp.Body).Decode(agentList) + if err != nil { + return agentList, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error())) + } + return agentList, nil +} + +func CreateWoodpeckerAgent(cfg *config.Config) (*models.Agent, error) { + name := fmt.Sprintf("woodpecker-autoscaler-agent-%s", utils.RandStringBytes(5)) + agentRequest := models.Agent{ + Name: name, + NoSchedule: false, + } + jsonBody, _ := json.Marshal(agentRequest) + bodyReader := bytes.NewReader(jsonBody) + + apiRoute := fmt.Sprintf("%s/api/agents", cfg.WoodpeckerInstance) + req, err := http.NewRequest(http.MethodPost, apiRoute, bodyReader) + if err != nil { + return nil, errors.New(fmt.Sprintf("Could not create agent request: %s", err.Error())) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken)) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, errors.New(fmt.Sprintf("Could not create new Agent: %s", err.Error())) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode)) + } + newAgent := new(models.Agent) + err = json.NewDecoder(resp.Body).Decode(newAgent) + if err != nil { + return nil, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error())) + } + return newAgent, nil + +} diff --git a/internal/woodpecker/metrics.go b/internal/woodpecker/metrics.go index ba9a816..4880c8b 100644 --- a/internal/woodpecker/metrics.go +++ b/internal/woodpecker/metrics.go @@ -15,7 +15,7 @@ import ( func QueueInfo(cfg *config.Config, target interface{}) error { apiRoute := fmt.Sprintf("%s/api/queue/info", cfg.WoodpeckerInstance) - req, err := http.NewRequest("GET", apiRoute, nil) + req, err := http.NewRequest(http.MethodGet, apiRoute, nil) if err != nil { return errors.New(fmt.Sprintf("Could not create queue request: %s", err.Error())) }