forked from kyma-project/kyma
/
main.go
103 lines (85 loc) · 2.89 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
package main
import (
"context"
"flag"
"os"
"os/signal"
"syscall"
"time"
"path/filepath"
"github.com/kyma-project/kyma/components/remote-environment-broker/pkg/client/clientset/versioned"
"github.com/kyma-project/kyma/components/remote-environment-broker/pkg/client/informers/externalversions"
"github.com/sirupsen/logrus"
restclient "k8s.io/client-go/rest"
"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
func main() {
var kubeconfig *string
if home := os.Getenv("HOME"); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
log := (&logrus.Logger{
Out: os.Stdout,
Formatter: &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.StampMicro,
},
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}).WithField("service", "main")
// create sync-job
k8sConfig := newRestClientConfig(*kubeconfig)
reClient, err := versioned.NewForConfig(k8sConfig)
fatalOnError(err)
// Always prefer using an informer factory to get a shared informer instead of getting an independent
// one. This reduces memory footprint and number of connections to the server.
informerFactory := externalversions.NewSharedInformerFactory(reClient, informerResyncPeriod)
v1alpha1Interface := informerFactory.Remoteenvironment().V1alpha1()
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
stopCh := make(chan struct{})
cancelOnInterrupt(ctx, stopCh, cancelFunc)
/* protection controller */
protectionController := NewProtectionController(v1alpha1Interface.RemoteEnvironments(),
reClient.RemoteenvironmentV1alpha1().EnvironmentMappings("default"),
reClient.RemoteenvironmentV1alpha1().RemoteEnvironments(), log)
protectionController.Run(1, stopCh)
informerFactory.Start(stopCh)
<-stopCh
}
func fatalOnError(err error) {
if err != nil {
logrus.Fatal(err.Error())
}
}
// cancelOnInterrupt closes given channel and also calls cancel func when os.Interrupt or SIGTERM is received
func cancelOnInterrupt(ctx context.Context, ch chan<- struct{}, cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-ctx.Done():
close(ch)
case <-c:
close(ch)
cancel()
}
}()
}
func newRestClientConfig(kubeconfigPath string) *restclient.Config {
var config *restclient.Config
var err error
if kubeconfigPath != "" {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfigPath)
} else {
config, err = restclient.InClusterConfig()
}
fatalOnError(err)
return config
}