This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (93 loc) · 3.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"fmt"
"strconv"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/nais/babylon/pkg/config"
"github.com/nais/babylon/pkg/criteria"
"github.com/nais/babylon/pkg/logger"
"github.com/nais/babylon/pkg/metrics"
"github.com/nais/babylon/pkg/service"
nais_io_v1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlMetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)
//nolint:funlen
func main() {
logger.Setup(config.GetEnv("LOG_LEVEL", "debug"))
cfg := config.ParseConfig()
// TODO: perhaps timeout between each tick?
ctx := context.Background()
port, err := strconv.Atoi(cfg.Port)
if err != nil {
log.Fatal(err.Error())
}
scheme := runtime.NewScheme()
_ = clientgoscheme.AddToScheme(scheme)
_ = nais_io_v1.AddToScheme(scheme)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: fmt.Sprintf(":%d", port),
HealthProbeBindAddress: fmt.Sprintf(":%d", port+1),
})
if err != nil {
log.Fatalf("error creating manager: %v", err)
return
}
log.Infof("%+v", cfg)
log.Infof("Metrics: http://localhost:%v/metrics", cfg.Port)
c := mgr.GetClient()
if !cfg.Armed {
log.Info("Not armed and dangerous! :(")
c = client.NewDryRunClient(c)
} else {
log.Info("Armed and dangerous! 🪖")
}
unleash, err := config.ConfigureUnleash()
if err != nil {
log.Fatal(err.Error())
}
influxC := influxdb2.NewClient(
cfg.InfluxdbURI,
fmt.Sprintf("%s:%s",
cfg.InfluxdbUsername.SecretString(),
cfg.InfluxdbPassword.SecretString()))
health, err := influxC.Health(ctx)
if err != nil {
log.Errorf("Influx health error: %+v", err)
}
log.Infof("InfluxDB health: %+v", health)
m := metrics.Init(unleash, c)
ctrlMetrics.Registry.MustRegister(m.RuleActivations, m.DeploymentCleanup, m.DeploymentGraceCutoff,
m.DeploymentUpdated, m.DeploymentStatusTotal, m.SlackChannelMapping)
h := metrics.NewHistory(influxC, cfg.InfluxdbDatabase, cfg.Cluster)
s := service.Service{Config: &cfg, Client: c, Metrics: &m, UnleashClient: unleash, InfluxClient: influxC, History: h}
go gardener(ctx, &s)
log.Fatal(mgr.Start(ctx))
}
func gardener(ctx context.Context, s *service.Service) {
log.Info("starting gardener")
ticker := time.Tick(s.Config.TickRate)
coreCriteriaJudge := criteria.NewCoreCriteriaJudge(s.Config, s.Client, s.Metrics, s.History,
s.UnleashClient, s.Config.Armed)
cleanUpJudge := criteria.NewCleanUpJudge(s.Config)
executioner := criteria.NewExecutioner(s.Config, s.Client, s.Metrics, s.History)
for {
<-ticker
deployments := &appsv1.DeploymentList{}
err := s.Client.List(ctx, deployments)
if logger.Logk8sError(err) {
continue
}
fails := coreCriteriaJudge.Failing(ctx, deployments)
deploymentFails := cleanUpJudge.Judge(fails)
executioner.Kill(ctx, deploymentFails)
}
}