forked from robusta-dev/kubewatch
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
102 lines (91 loc) · 2.9 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/julienschmidt/httprouter"
"github.com/marvasgit/kubernetes-statemonitor/pkg/client"
"github.com/marvasgit/kubernetes-statemonitor/pkg/utils"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
var list = utils.NewTTLList()
func main() {
// Create a context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
router := httprouter.New()
router.GET("/metrics", Metrics)
router.PUT("/deploy/:namespace/:duration", namespaceDeployment)
router.PUT("/deploy/:namespace", namespaceDeployment)
router.DELETE("/deploy/:namespace", deletenamespaceDeployment)
router.POST("/reset", reset)
go func() {
http.ListenAndServe(":80", router)
}()
initLogger()
client.Start(ctx, list)
}
func Metrics(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
promhttp.Handler().ServeHTTP(w, r)
}
func deletenamespaceDeployment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
namespace := ps.ByName("namespace")
list.Remove(namespace)
response := fmt.Sprintf("Namespace -%s was removed ", namespace)
w.Write([]byte(response))
}
func reset(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
list.Reset()
w.Write([]byte("Deployment List was reset "))
}
func namespaceDeployment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
namespace := ps.ByName("namespace")
durationString := ps.ByName("duration")
//set default time to 5 minutes if not provided
if durationString == "" {
durationString = "2"
}
// Parse JSON request body
// Parse durationInMinutes from string to int
durationInMinutes, err := strconv.Atoi(durationString)
if err != nil {
http.Error(w, "Invalid time value", http.StatusBadRequest)
return
}
er := list.Add(namespace, time.Duration(durationInMinutes)*time.Minute)
if er != nil {
http.Error(w, er.Error(), http.StatusBadRequest)
return
}
response := fmt.Sprintf("Namespace -%s added to deployment list", namespace)
// change status code to 201
w.WriteHeader(http.StatusCreated)
w.Write([]byte(response))
}
func initLogger() {
logLevel := os.Getenv("LOG_LEVEL")
if logLevel != "" {
logrus.Printf("Custom log level: %s", logLevel)
parsedLevel, err := logrus.ParseLevel(logLevel)
if err == nil {
logrus.Printf("Setting custom log level to: %s", logLevel)
logrus.SetLevel(parsedLevel)
} else {
logrus.Errorf("Illegal custom log level: %s. Ignoring custom log level", logLevel)
}
}
logFormatter := os.Getenv("LOG_FORMATTER")
if logFormatter != "" {
logrus.Printf("Custom log formatter: %s", logFormatter)
if logFormatter == "json" {
logrus.Printf("Setting custom log formatter to: %s", logFormatter)
logrus.SetFormatter(new(logrus.JSONFormatter))
} else {
logrus.Errorf("Illegal custom log formatter: %s. Ignoring custom log formatter", logFormatter)
}
}
}