forked from kyma-incubator/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (85 loc) · 3.12 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
package main
import (
"log"
"path/filepath"
"sync"
"github.com/kyma-incubator/compass/components/connector/config"
"github.com/kyma-incubator/compass/components/connector/internal/api"
"github.com/kyma-incubator/compass/components/connector/internal/authentication"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
cfg := config.Config{}
err := envconfig.InitWithPrefix(&cfg, "APP")
exitOnError(err, "Error while loading app Config")
log.Println("Starting Connector Service")
log.Printf("Config: %s", cfg.String())
k8sClientSet, appErr := newCoreClientSet()
exitOnError(appErr, "Failed to initialize Kubernetes client.")
internalComponents := config.InitInternalComponents(cfg, k8sClientSet)
tokenResolver := api.NewTokenResolver(internalComponents.TokenService)
certificateResolver := api.NewCertificateResolver(
internalComponents.Authenticator,
internalComponents.TokenService,
internalComponents.CertificateService,
internalComponents.SubjectConsts,
cfg.DirectorURL,
cfg.CertificateSecuredConnectorURL,
internalComponents.RevocationsRepository)
authContextMiddleware := authentication.NewAuthenticationContextMiddleware()
externalGqlServer := config.PrepareExternalGraphQLServer(cfg, certificateResolver, authContextMiddleware.PropagateAuthentication)
internalGqlServer := config.PrepareInternalGraphQLServer(cfg, tokenResolver)
hydratorServer := config.PrepareHydratorServer(cfg, internalComponents.TokenService, internalComponents.SubjectConsts, internalComponents.RevocationsRepository)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
log.Printf("External GraphQL API listening on %s...", cfg.ExternalAddress)
if err := externalGqlServer.ListenAndServe(); err != nil {
panic(err)
}
}()
go func() {
log.Printf("Internal GraphQL API listening on %s...", cfg.InternalAddress)
if err := internalGqlServer.ListenAndServe(); err != nil {
panic(err)
}
}()
go func() {
log.Printf("Hydrator API listening on %s...", cfg.HydratorAddress)
if err := hydratorServer.ListenAndServe(); err != nil {
panic(err)
}
}()
wg.Wait()
}
func exitOnError(err error, context string) {
if err != nil {
wrappedError := errors.Wrap(err, context)
log.Fatal(wrappedError)
}
}
func newCoreClientSet() (*kubernetes.Clientset, error) {
k8sConfig, err := restclient.InClusterConfig()
if err != nil {
logrus.Warnf("Failed to read in cluster Config: %s", err.Error())
logrus.Info("Trying to initialize with local Config")
home := homedir.HomeDir()
k8sConfPath := filepath.Join(home, ".kube", "Config")
k8sConfig, err = clientcmd.BuildConfigFromFlags("", k8sConfPath)
if err != nil {
return nil, errors.Errorf("failed to read k8s in-cluster configuration, %s", err.Error())
}
}
coreClientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
return nil, errors.Errorf("failed to create k8s core client, %s", err.Error())
}
return coreClientset, nil
}