2023-10-29 21:44:39 +00:00
|
|
|
package woodpecker
|
|
|
|
|
2023-10-30 21:46:17 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-10-31 22:18:08 +00:00
|
|
|
"strings"
|
2023-10-29 21:44:39 +00:00
|
|
|
|
2023-10-30 21:46:17 +00:00
|
|
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
|
|
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
|
2023-10-29 21:44:39 +00:00
|
|
|
|
2023-10-30 21:46:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func QueueInfo(cfg *config.Config, target interface{}) error {
|
|
|
|
apiRoute := fmt.Sprintf("%s/api/queue/info", cfg.WoodpeckerInstance)
|
|
|
|
req, err := http.NewRequest("GET", apiRoute, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Could not create queue 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 errors.New(fmt.Sprintf("Could not query queue info: %s", err.Error()))
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return errors.New(fmt.Sprintf("Error from queue info api: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewDecoder(resp.Body).Decode(target)
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:18:08 +00:00
|
|
|
func CheckPending(cfg *config.Config) (bool, error) {
|
2023-11-01 19:48:45 +00:00
|
|
|
expectedKV := strings.Split(cfg.WoodpeckerLabelSelector, "=")
|
2023-10-30 21:46:17 +00:00
|
|
|
queueInfo := new(models.QueueInfo)
|
|
|
|
err := QueueInfo(cfg, queueInfo)
|
|
|
|
if err != nil {
|
2023-10-31 22:18:08 +00:00
|
|
|
return false, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
|
2023-10-30 21:46:17 +00:00
|
|
|
}
|
|
|
|
if queueInfo.Stats.PendingCount > 0 {
|
2023-10-31 22:18:08 +00:00
|
|
|
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
|
|
|
|
}
|
2023-10-30 21:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 22:18:08 +00:00
|
|
|
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
|
2023-10-29 21:44:39 +00:00
|
|
|
}
|