Merge pull request 'change agent creation logic to use agentToken instead of systemToken' (#5) from feature/tt/fix-agent-decom into main
All checks were successful
Pipeline was successful

Reviewed-on: #5
This commit is contained in:
lerentis 2023-11-13 19:29:05 +00:00
commit c431131d5f
5 changed files with 107 additions and 19 deletions

View File

@ -52,7 +52,13 @@ func main() {
"Caller": "Main", "Caller": "Main",
}).Infof("Currently owning %d Agents", len(ownedNodes)) }).Infof("Currently owning %d Agents", len(ownedNodes))
if pendingTasks > 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 { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"Caller": "Main", "Caller": "Main",
@ -100,14 +106,17 @@ func main() {
"Caller": "Main", "Caller": "Main",
}).Info("No tasks running. Will remove agents") }).Info("No tasks running. Will remove agents")
for _, server := range ownedNodes { for _, server := range ownedNodes {
hetzner.DecomNode(cfg, &server) agentId, err := hetzner.DecomNode(cfg, &server)
agentId, err := woodpecker.GetAgentIdByName(cfg, server.Name)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"Caller": "Main", "Caller": "Main",
}).Warnf("Could not find agent %s in woodpecker. Assuming it was never added", server.Name) }).Warnf("Error while deleting node %s: %s", server.Name, err.Error())
} else { }
woodpecker.DecomAgent(cfg, agentId) 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())
} }
} }
} }

View File

@ -5,10 +5,12 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strconv"
"strings" "strings"
"text/template" "text/template"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config" "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/utils" "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/utils"
"github.com/hetznercloud/hcloud-go/hcloud" "github.com/hetznercloud/hcloud-go/hcloud"
@ -42,13 +44,14 @@ type UserDataConfig struct {
EnvConfig map[string]interface{} 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{}{ envConfig := map[string]interface{}{
"WOODPECKER_SERVER": fmt.Sprintf("%s", cfg.WoodpeckerGrpc), "WOODPECKER_SERVER": fmt.Sprintf("%s", cfg.WoodpeckerGrpc),
"WOODPECKER_GRPC_SECURE": true, "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_FILTER_LABELS": fmt.Sprintf("%s", cfg.WoodpeckerLabelSelector),
"WOODPECKER_HOSTNAME": fmt.Sprintf("%s", name), "WOODPECKER_HOSTNAME": fmt.Sprintf("%s", name),
"WOODPECKER_MAX_WORKFLOWS": 4,
} }
config := UserDataConfig{ config := UserDataConfig{
Image: fmt.Sprintf("woodpeckerci/woodpecker-agent:%s", cfg.WoodpeckerAgentVersion), 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 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)) client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
name := fmt.Sprintf("woodpecker-autoscaler-agent-%s", utils.RandStringBytes(5)) userdata, err := generateConfig(cfg, woodpeckerAgent.Name, woodpeckerAgent.Token)
userdata, err := generateConfig(cfg, name)
keys := []*hcloud.SSHKey{} keys := []*hcloud.SSHKey{}
for _, keyName := range strings.Split(cfg.HcloudSSHKeys, ",") { for _, keyName := range strings.Split(cfg.HcloudSSHKeys, ",") {
key, _, err := client.SSHKey.GetByName(context.Background(), keyName) 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 := map[string]string{}
labels["Role"] = "WoodpeckerAgent" labels["Role"] = "WoodpeckerAgent"
labels["ControledBy"] = "WoodpeckerAutoscaler" labels["ControledBy"] = "WoodpeckerAutoscaler"
labels["ID"] = fmt.Sprintf("%d", woodpeckerAgent.ID)
networkConf := hcloud.ServerCreatePublicNet{ networkConf := hcloud.ServerCreatePublicNet{
EnableIPv4: !cfg.HcloudIPv6Only, EnableIPv4: !cfg.HcloudIPv6Only,
@ -97,7 +100,7 @@ func CreateNewAgent(cfg *config.Config) (*hcloud.Server, error) {
} }
res, _, err := client.Server.Create(context.Background(), hcloud.ServerCreateOpts{ res, _, err := client.Server.Create(context.Background(), hcloud.ServerCreateOpts{
Name: name, Name: woodpeckerAgent.Name,
ServerType: pln, ServerType: pln,
Image: img, Image: img,
SSHKeys: keys, SSHKeys: keys,
@ -138,16 +141,28 @@ func ListAgents(cfg *config.Config) ([]hcloud.Server, error) {
return myServers, nil 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)) 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{ log.WithFields(log.Fields{
"Caller": "DecomNode", "Caller": "DecomNode",
}).Debugf("Deleting %s node", server.Name) }).Debugf("Deleting %s node", server.Name)
_, _, err := client.Server.DeleteWithResult(context.Background(), server) _, _, err := client.Server.DeleteWithResult(context.Background(), server)
if err != nil { 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) { func RefreshNodeInfo(cfg *config.Config, serverID int) (*hcloud.Server, error) {

View File

@ -40,12 +40,13 @@ write_files:
- WOODPECKER_FILTER_LABELS=uploadfilter24.eu/instance-role=WoodpeckerTest - WOODPECKER_FILTER_LABELS=uploadfilter24.eu/instance-role=WoodpeckerTest
- WOODPECKER_GRPC_SECURE=true - WOODPECKER_GRPC_SECURE=true
- WOODPECKER_HOSTNAME=test-instance - WOODPECKER_HOSTNAME=test-instance
- WOODPECKER_MAX_WORKFLOWS=4
- WOODPECKER_SERVER=grpc-test.woodpecker.test.tld:443 - WOODPECKER_SERVER=grpc-test.woodpecker.test.tld:443
path: /root/docker-compose.yml path: /root/docker-compose.yml
runcmd: runcmd:
- [ sh, -xc, "cd /root; docker run --rm --privileged multiarch/qemu-user-static --reset -p yes; docker compose up -d" ] - [ 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 { if err != nil {
t.Errorf("Error in generating Config: %v", err) t.Errorf("Error in generating Config: %v", err)
} }

View File

@ -1,6 +1,7 @@
package woodpecker package woodpecker
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -8,11 +9,12 @@ import (
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config" "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models" "git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/utils"
log "github.com/sirupsen/logrus" 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) apiRoute := fmt.Sprintf("%s/api/agents/%d", cfg.WoodpeckerInstance, agentId)
req, err := http.NewRequest("DELETE", apiRoute, nil) req, err := http.NewRequest("DELETE", apiRoute, nil)
if err != 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) { func GetAgentIdByName(cfg *config.Config, name string) (int, error) {
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance) 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 { if err != nil {
return 0, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error())) 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)) 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
}

View File

@ -15,7 +15,7 @@ import (
func QueueInfo(cfg *config.Config, target interface{}) error { func QueueInfo(cfg *config.Config, target interface{}) error {
apiRoute := fmt.Sprintf("%s/api/queue/info", cfg.WoodpeckerInstance) 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 { if err != nil {
return errors.New(fmt.Sprintf("Could not create queue request: %s", err.Error())) return errors.New(fmt.Sprintf("Could not create queue request: %s", err.Error()))
} }