This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
115 lines (92 loc) · 3.95 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
114
115
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/kyma-incubator/service-catalog-tester/internal/collector"
"github.com/kyma-incubator/service-catalog-tester/internal/monitoring"
"github.com/kyma-incubator/service-catalog-tester/internal/notifier"
"github.com/kyma-incubator/service-catalog-tester/internal/platform/logger"
"github.com/kyma-incubator/service-catalog-tester/internal/platform/signal"
"github.com/kyma-incubator/service-catalog-tester/internal/runner"
"github.com/kyma-incubator/service-catalog-tester/internal/tests"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
"k8s.io/client-go/informers"
k8sClientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// informerResyncPeriod defines how often informer will execute relist action. Setting to zero disable resync.
// BEWARE: too short period time will increase the CPU load.
const informerResyncPeriod = 30 * time.Minute
// Config holds application configuration
type Config struct {
Logger logger.Config
Port int `envconfig:"default=8080"`
KubeconfigPath string `envconfig:"optional"`
SlackClient notifier.SlackClientConfig
ClusterName string
ObservableDeployments collector.DeploymentConfig
E2EServiceCatalogHappyPath tests.E2EServiceCatalogHappyPathTestConfig
}
func main() {
var cfg Config
err := envconfig.InitWithPrefix(&cfg, "APP")
fatalOnError(err, "while reading configuration from environment variables")
log := logger.New(&cfg.Logger)
// set up signals so we can handle the first shutdown signal gracefully
stopCh := signal.SetupChannel()
k8sConfig, err := clientcmd.BuildConfigFromFlags("", cfg.KubeconfigPath)
fatalOnError(err, "while creating k8s config")
// k8s informers
k8sCli, err := k8sClientset.NewForConfig(k8sConfig)
fatalOnError(err, "while creating k8s clientset")
k8sInformersFactory := informers.NewSharedInformerFactoryWithOptions(k8sCli, informerResyncPeriod)
// Slack Notifier
slackClient := notifier.NewSlackClient(cfg.SlackClient)
msgRenderer, err := notifier.NewMessageRenderer()
fatalOnError(err, "while creating Slack message renderer")
sNotifier := notifier.New(cfg.ClusterName, slackClient, msgRenderer)
// Ecosystem Monitor
observableDeploys, err := collector.CollectPodLabelsFromDeployments(k8sCli.AppsV1(), cfg.ObservableDeployments)
fatalOnError(err, "while collecting Pod labels from requested Deployments")
watchSvc := monitoring.NewWatcherService(k8sCli.CoreV1(), sNotifier, log)
monitor := monitoring.NewPodDetector(k8sInformersFactory.Core().V1().Pods(), watchSvc, log, observableDeploys)
// Test Runner
testRunner := runner.NewStressTestRunner(sNotifier, log)
E2EServiceCatalogHappyPath := tests.NewE2EServiceCatalogHappyPathTest(cfg.E2EServiceCatalogHappyPath, k8sConfig)
// Start services
err = monitor.Start()
fatalOnError(err, "while starting resources monitoring")
go testRunner.Run(stopCh, cfg.E2EServiceCatalogHappyPath.TestThrottle, E2EServiceCatalogHappyPath)
// Start informers
k8sInformersFactory.Start(stopCh)
// Wait for cache sync
k8sInformersFactory.WaitForCacheSync(stopCh)
runStatuszHTTPServer(stopCh, fmt.Sprintf(":%d", cfg.Port), log)
}
func fatalOnError(err error, context string) {
if err != nil {
logrus.Fatal(errors.Wrap(err, context).Error())
}
}
func runStatuszHTTPServer(stop <-chan struct{}, addr string, log logrus.FieldLogger) {
mux := http.NewServeMux()
mux.HandleFunc("/statusz", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "OK")
})
srv := &http.Server{Addr: addr, Handler: mux}
go func() {
<-stop
// We received an interrupt signal, shut down.
if err := srv.Shutdown(context.Background()); err != nil {
log.Errorf("HTTP server Shutdown: %v", err)
}
}()
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Errorf("HTTP server ListenAndServe: %v", err)
}
}