implemented spawning of new agents
All checks were successful
ci/woodpecker/pr/pr Pipeline was successful

split pipeline
This commit is contained in:
2023-10-31 23:18:08 +01:00
parent bb1c7fb1d3
commit 7f2c465391
13 changed files with 412 additions and 91 deletions

View File

@ -0,0 +1,60 @@
package woodpecker
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
)
func DecomAgent(cfg *config.Config, agentId int) error {
apiRoute := fmt.Sprintf("%s/api/agents/%d", cfg.WoodpeckerInstance, agentId)
req, err := http.NewRequest("DELETE", apiRoute, nil)
if err != nil {
return errors.New(fmt.Sprintf("Could not create delete request: %s", err.Error()))
}
req.Header.Set("Accept", "text/plain")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New(fmt.Sprintf("Could not delete agent: %s", err.Error()))
}
defer resp.Body.Close()
return nil
}
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)
if err != nil {
return 0, 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 0, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error()))
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
}
agentList := new(models.AgentList)
err = json.NewDecoder(resp.Body).Decode(agentList)
if err != nil {
return 0, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
}
for _, agent := range agentList.Agents {
if agent.Name == name {
return int(agent.ID), nil
}
}
return 0, errors.New(fmt.Sprintf("Agent with name %s is not in server", name))
}

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
@ -34,27 +35,43 @@ func QueueInfo(cfg *config.Config, target interface{}) error {
return json.NewDecoder(resp.Body).Decode(target)
}
func CheckPending(cfg *config.Config) error {
func CheckPending(cfg *config.Config) (bool, error) {
expectedKV := strings.Split(cfg.LabelSelector, "=")
queueInfo := new(models.QueueInfo)
err := QueueInfo(cfg, queueInfo)
if err != nil {
return errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
return false, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
}
if queueInfo.Stats.PendingCount > 0 {
// TODO: queueInfo.Pending may be empty
for _, pendingJobs := range queueInfo.Pending {
// TODO: separate key and value from LabelSelector and compare them deeply
_, exists := pendingJobs.Labels[cfg.LabelSelector]
if exists {
log.WithFields(log.Fields{
"Caller": "CheckPending",
}).Info("Found pending job for us. Requesting new Agent")
} else {
log.WithFields(log.Fields{
"Caller": "CheckPending",
}).Info("No Jobs for us in Queue")
if queueInfo.Pending != nil {
for _, pendingJobs := range queueInfo.Pending {
val, exists := pendingJobs.Labels[expectedKV[0]]
if exists && val == expectedKV[1] {
log.WithFields(log.Fields{
"Caller": "CheckPending",
}).Info("Found pending job for us")
return true, nil
} else {
log.WithFields(log.Fields{
"Caller": "CheckPending",
}).Info("No Jobs for us in Queue")
return false, nil
}
}
}
}
return nil
return false, nil
}
func CheckRunning(cfg *config.Config) (bool, error) {
queueInfo := new(models.QueueInfo)
err := QueueInfo(cfg, queueInfo)
if err != nil {
return false, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
}
// TODO: create and parse running object. there may be jobs that are not for us
if queueInfo.Stats.RunningCount > 0 {
return true, nil
}
return false, nil
}