forked from kyma-project/kyma
/
main.go
107 lines (86 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/kyma-project/kyma/tools/stability-checker/internal/notifier"
"github.com/kyma-project/kyma/tools/stability-checker/internal/runner"
"github.com/kyma-project/kyma/tools/stability-checker/platform/logger"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// Config holds application configuration
type Config struct {
Logger logger.Config
Port int `envconfig:"default=8080"`
KubeConfigPath string `envconfig:"optional"`
TestThrottle time.Duration `envconfig:"default=5m"`
WorkingNamespace string `envconfig:"default=kyma-system"`
PodName string `envconfig:"HOSTNAME"`
TestConfigMapName string `envconfig:"default=stability-checker"`
PathToTestingScript string
TestResultWindowTime time.Duration `envconfig:"default=6h"`
SlackClient notifier.SlackClientConfig
}
func main() {
var cfg Config
err := envconfig.InitWithPrefix(&cfg, "APP")
fatalOnError(errors.Wrap(err, "while reading configuration from environment variables"))
log := logger.New(&cfg.Logger)
k8sConfig, err := newRestClientConfig(cfg.KubeConfigPath)
fatalOnError(err)
ctx := contextCanceledOnInterrupt()
// k8s client
k8sCli, err := kubernetes.NewForConfig(k8sConfig)
fatalOnError(err)
cfgMapClient := k8sCli.CoreV1().ConfigMaps(cfg.WorkingNamespace)
// statusz server
go func() {
http.HandleFunc("/statusz", func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
})
fatalOnError(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil))
}()
// Slack notifier
slackClient := notifier.NewSlackClient(cfg.SlackClient)
testRenderer, err := notifier.NewTestRenderer()
fatalOnError(err)
sNotifier := notifier.New(slackClient, testRenderer, cfgMapClient, cfg.TestConfigMapName, cfg.TestResultWindowTime, cfg.PodName, cfg.WorkingNamespace, log)
go sNotifier.Run(ctx)
// Test runner
tRunner := runner.NewTestRunner(cfg.TestThrottle, cfg.TestConfigMapName, cfg.PathToTestingScript, cfgMapClient, log)
tRunner.Run(ctx)
}
func fatalOnError(err error) {
if err != nil {
logrus.Fatal(err.Error())
}
}
// copied from https://github.com/kyma-project/binding-usage-controller/tree/v0.1.3/cmd/controller/main.go:143
func newRestClientConfig(kubeConfigPath string) (*restclient.Config, error) {
if kubeConfigPath != "" {
return clientcmd.BuildConfigFromFlags("", kubeConfigPath)
}
return restclient.InClusterConfig()
}
// contextCanceledOnInterrupt returns context which is canceled when os.Interrupt or SIGTERM is received
func contextCanceledOnInterrupt() context.Context {
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}()
return ctx
}