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
All checks were successful
Pipeline was successful
Reviewed-on: #5
This commit is contained in:
commit
c431131d5f
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
}
|
||||
|
@ -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()))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user