generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
222 lines (196 loc) · 6.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/kyma-project/compass-manager/api/v1beta1"
"github.com/kyma-project/compass-manager/controllers"
"github.com/kyma-project/compass-manager/controllers/metrics"
"github.com/kyma-project/compass-manager/internal/director"
"github.com/kyma-project/compass-manager/internal/graphql"
"github.com/kyma-project/compass-manager/internal/oauth"
kyma "github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
corev1 "k8s.io/api/core/v1"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)
var (
scheme = runtime.NewScheme() //nolint:gochecknoglobals
setupLog = ctrl.Log.WithName("setup") //nolint:gochecknoglobals
)
type config struct {
Address string `envconfig:"default=127.0.0.1:3000"`
APIEndpoint string `envconfig:"default=/graphql"`
SkipDirectorCertVerification bool `envconfig:"default=false"`
DirectorURL string `envconfig:"APP_DIRECTOR_URL,default=https://compass-gateway-auth-oauth.cmp-main.dev.kyma.cloud.sap/director/graphql"`
DirectorOAuthPath string `envconfig:"APP_DIRECTOR_OAUTH_PATH,default=./dev/director.yaml"`
EnabledRegistration bool `envconfig:"APP_ENABLED_REGISTRATION,default=false"`
DryRun bool `envconfig:"APP_DRYRUN,default=false"`
}
func (c *config) String() string {
return fmt.Sprintf("Address: %s, APIEndpoint: %s, DirectorURL: %s, SkipDirectorCertVerification: %v, DirectorOAuthPath: %s",
c.Address, c.APIEndpoint, c.DirectorURL,
c.SkipDirectorCertVerification, c.DirectorOAuthPath)
}
type DirectorOAuth struct {
Data struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
TokensEndpoint string `json:"tokens_endpoint"`
} `json:"data"`
}
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(kyma.AddToScheme(scheme))
utilruntime.Must(v1beta1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
cfg := config{}
err := envconfig.InitWithPrefix(&cfg, "APP")
exitOnError(err, "Failed to load application config")
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "2647ec81.kyma-project.io",
Cache: setCacheOptions(),
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
log := logrus.New()
log.SetLevel(logrus.InfoLevel)
directorClient, err := newDirectorClient(cfg)
if err != nil {
setupLog.Error(err, "unable to create Director Client")
os.Exit(1)
}
var compassRegistrator controllers.Registrator
var runtimeAgentConfigurator controllers.Configurator
if cfg.DryRun {
dry := controllers.NewDryRunner(log)
compassRegistrator = dry
runtimeAgentConfigurator = dry
} else {
compassRegistrator = controllers.NewCompassRegistrator(directorClient, log)
runtimeAgentConfigurator = controllers.NewRuntimeAgentConfigurator(directorClient, log)
}
requeueTime := time.Second * 5 //nolint:mnd
requeueTimeForKubeconfig := time.Minute * 3 //nolint:mnd
metrics := metrics.NewMetrics()
compassManagerReconciler := controllers.NewCompassManagerReconciler(
mgr,
log,
runtimeAgentConfigurator,
compassRegistrator,
requeueTime,
requeueTimeForKubeconfig,
cfg.EnabledRegistration,
cfg.DryRun,
metrics,
)
if err = compassManagerReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CompassManager")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func newDirectorClient(config config) (director.Client, error) {
file, err := os.ReadFile(config.DirectorOAuthPath)
if err != nil {
return nil, errors.Wrap(err, "Failed to open director config")
}
cfg := DirectorOAuth{}
err = yaml.Unmarshal(file, &cfg)
if err != nil {
return nil, errors.Wrap(err, "Failed to unmarshal director config")
}
gqlClient := graphql.NewGraphQLClient(config.DirectorURL, true, config.SkipDirectorCertVerification)
oauthClient := oauth.NewOauthClient(newHTTPClient(config.SkipDirectorCertVerification), cfg.Data.ClientID, cfg.Data.ClientSecret, cfg.Data.TokensEndpoint)
return director.NewDirectorClient(gqlClient, oauthClient), nil
}
func newHTTPClient(skipCertVerification bool) *http.Client {
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipCertVerification},
},
Timeout: 30 * time.Second, //nolint:mnd
}
}
func exitOnError(err error, context string) {
if err != nil {
wrappedError := errors.Wrap(err, context)
log.Fatal(wrappedError)
}
}
func setCacheOptions() cache.Options {
return cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Secret{}: {
Label: k8slabels.Everything(),
Namespaces: map[string]cache.Config{
"kcp-system": {},
},
},
&kyma.Kyma{}: {
Namespaces: map[string]cache.Config{
"kcp-system": {},
},
},
&v1beta1.CompassManagerMapping{}: {
Namespaces: map[string]cache.Config{
"kcp-system": {},
},
},
},
}
}