forked from kyma-project/kyma
/
main.go
108 lines (84 loc) · 3.8 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
package main
import (
"os"
"path/filepath"
"time"
istioAuthenticationClient "github.com/kyma-project/kyma/components/api-controller/pkg/clients/authentication.istio.io/clientset/versioned"
kyma "github.com/kyma-project/kyma/components/api-controller/pkg/clients/gateway.kyma.cx/clientset/versioned"
kymaInformers "github.com/kyma-project/kyma/components/api-controller/pkg/clients/gateway.kyma.cx/informers/externalversions"
istioNetworkingClient "github.com/kyma-project/kyma/components/api-controller/pkg/clients/networking.istio.io/clientset/versioned"
authenticationV2 "github.com/kyma-project/kyma/components/api-controller/pkg/controller/authentication/v2"
"github.com/kyma-project/kyma/components/api-controller/pkg/controller/crd"
istioNetworkingV1 "github.com/kyma-project/kyma/components/api-controller/pkg/controller/networking/v1"
serviceV1 "github.com/kyma-project/kyma/components/api-controller/pkg/controller/service/v1"
"github.com/kyma-project/kyma/components/api-controller/pkg/controller/v1alpha2"
log "github.com/sirupsen/logrus"
apiExtensionsClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
k8sClient "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
log.SetLevel(getLoggerLevel())
log.Info("Starting API controller application...")
stop := make(chan struct{})
jwtDefaultConfig := initJwtDefaultConfig()
istioGateway := getIstioGateway()
kubeConfig := initKubeConfig()
apiExtensionsClientSet := apiExtensionsClient.NewForConfigOrDie(kubeConfig)
registerer := crd.NewRegistrar(apiExtensionsClientSet)
registerer.Register(v1alpha2.Crd())
istioNetworkingClientSet := istioNetworkingClient.NewForConfigOrDie(kubeConfig)
istioNetworkingV1Interface := istioNetworkingV1.New(istioNetworkingClientSet, istioGateway)
k8sClientSet := k8sClient.NewForConfigOrDie(kubeConfig)
serviceV1Interface := serviceV1.New(k8sClientSet)
istioAuthenticationClientSet := istioAuthenticationClient.NewForConfigOrDie(kubeConfig)
authenticationV2Interface := authenticationV2.New(istioAuthenticationClientSet, jwtDefaultConfig)
kymaClientSet := kyma.NewForConfigOrDie(kubeConfig)
internalInformerFactory := kymaInformers.NewSharedInformerFactory(kymaClientSet, time.Second*30)
go internalInformerFactory.Start(stop)
v1alpha2Controller := v1alpha2.NewController(kymaClientSet, istioNetworkingV1Interface, serviceV1Interface, authenticationV2Interface, internalInformerFactory)
v1alpha2Controller.Run(2, stop)
}
func initKubeConfig() *rest.Config {
kubeConfigLocation := filepath.Join(os.Getenv("HOME"), ".kube", "config")
kubeConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigLocation)
if err != nil {
log.Warn("unable to build kube config from file. Trying in-cluster configuration")
kubeConfig, err = rest.InClusterConfig()
if err != nil {
log.Fatal("cannot find Service Account in pod to build in-cluster kube config")
}
}
return kubeConfig
}
func getLoggerLevel() log.Level {
logLevel := os.Getenv("API_CONTROLLER_LOG_LEVEL")
if logLevel != "" {
level, err := log.ParseLevel(logLevel)
if err != nil {
println("Error while setting log level: " + logLevel + ". Root cause: " + err.Error())
} else {
return level
}
}
return log.InfoLevel
}
func getIstioGateway() string {
gateway := os.Getenv("GATEWAY_FQDN")
if gateway == "" {
log.Fatal("gateway not provided. Please provide env variables GATEWAY_FQDN")
}
return gateway
}
func initJwtDefaultConfig() authenticationV2.JwtDefaultConfig {
issuer := os.Getenv("DEFAULT_ISSUER")
jwksURI := os.Getenv("DEFAULT_JWKS_URI")
if issuer == "" || jwksURI == "" {
log.Fatal("default issuer or jwksURI not provided. Please provide env variables DEFAULT_ISSUER and DEFAULT_JWKS_URI")
}
return authenticationV2.JwtDefaultConfig{
Issuer: issuer,
JwksUri: jwksURI,
}
}