metallb-ip-floater/cmd/main.go
Tobias Trabelsi b72c1d1edb init (#1)
Reviewed-on: #1
Co-authored-by: Tobias Trabelsi <lerentis@uploadfilter24.eu>
Co-committed-by: Tobias Trabelsi <lerentis@uploadfilter24.eu>
2023-10-15 19:50:59 +00:00

87 lines
2.4 KiB
Go

package main
import (
"fmt"
"time"
"git.uploadfilter24.eu/covidnetes/metallb-ip-floater/internal"
"git.uploadfilter24.eu/covidnetes/metallb-ip-floater/internal/utils"
log "github.com/sirupsen/logrus"
)
func main() {
cfg, err := internal.GenConfig()
utils.ConfigureLogger(cfg)
if err != nil {
log.WithFields(log.Fields{
"Caller": "Main",
}).Fatal(fmt.Sprintf("Error generating Config: %s", err.Error()))
}
go func() {
log.WithFields(log.Fields{
"Caller": "Main",
}).Info("Starting Health Endpoint")
internal.StartHealthEndpoint()
}()
log.WithFields(log.Fields{
"Caller": "Main",
}).Info("Entering main event loop")
for {
possibleCandidates, err := internal.GetSpeakableNodes(cfg)
if err != nil {
log.WithFields(log.Fields{
"Caller": "Main",
}).Fatal(fmt.Sprintf("Failed to get possible IPs: %s", err.Error()))
}
for _, ip := range possibleCandidates {
metrics, err := internal.GetMetrics(cfg, fmt.Sprintf("%s:%d/metrics", ip, 7472))
if err != nil {
log.WithFields(log.Fields{
"Caller": "Main",
}).Fatal(fmt.Sprintf("Failed to get Metrics for IP %s: %s", ip, err.Error()))
}
parsedMetrics, err := internal.ParseMetrics(metrics)
for k, v := range parsedMetrics {
if k == "metallb_speaker_announced" {
log.WithFields(log.Fields{
"Caller": "Main",
}).Debug(fmt.Sprintf("Value of key metallb_speaker_announced on %s: %s", ip, v))
metrics_ip, err := internal.GetIpFromMetrics(v)
if err != nil {
log.WithFields(log.Fields{
"Caller": "Main",
}).Fatal(fmt.Sprintf("Failed to ge IP: %s", err.Error()))
}
log.WithFields(log.Fields{
"Caller": "Main",
}).Debug(fmt.Sprintf("Ip in metrics value on %s: %s", ip, metrics_ip))
speakerNodeID, err := internal.GetNodeID(cfg, ip)
if err != nil {
log.WithFields(log.Fields{
"Caller": "Main",
}).Fatal(fmt.Sprintf("Failed to get Node ID for IP %s: %s", ip, err.Error()))
}
if cfg.DryRun {
log.WithFields(log.Fields{
"Caller": "Main",
}).Info(fmt.Sprintf("Would Assign Floating IP to Node: %s", speakerNodeID.Name))
} else {
err = internal.AttachFloatingIpToNode(cfg, speakerNodeID)
if err != nil {
log.WithFields(log.Fields{
"Caller": "Main",
}).Fatal(fmt.Sprintf("Failed to Attach Floating IP to Node: %s", err.Error()))
}
}
}
}
}
time.Sleep(15 * time.Minute)
}
}