-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (58 loc) · 1.7 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/kvvit/deployment_cleaner/pkg/clientset"
"github.com/kvvit/deployment_cleaner/pkg/deleteobjects"
"github.com/kvvit/deployment_cleaner/pkg/loadvars"
"github.com/kvvit/deployment_cleaner/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
deploymentMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "deployment_live_time_seconds",
Help: "Live time of deployments in seconds",
},
[]string{"deployment"},
)
)
func main() {
ctx := context.Background()
envvars := loadvars.LoadVars()
clientset := clientset.GetClientset()
port := "8080"
log.Println("Cleaning service has been started")
prometheus.MustRegister(deploymentMetrics)
go func() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
})
log.Println("Web server start listening on", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("Error starting HTTP server:", err)
}
}()
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
timeNow := metav1.Now()
timeWork := timeNow.Hour()
for {
select {
case <-ticker.C:
metrics.FetchAndUpdateDeploymentMetrics(ctx, clientset, envvars.NameSpace, deploymentMetrics)
if timeWork >= envvars.WorkStart && timeWork < envvars.WorkEnd {
log.Println("Now is working time, pass changes")
} else {
deleteobjects.DeleteOldHelmReleases(ctx, clientset, envvars.TimeToDelete, envvars.DeploymentName, envvars.NameSpace, envvars.IsDryRun)
}
}
}
}