forked from kyma-incubator/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
360 lines (312 loc) · 13.6 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/appinfo"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/avs"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/broker"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/director"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/director/oauth"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/edp"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/gardener"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/health"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/httputil"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/hyperscaler"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/hyperscaler/azure"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/ias"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/lms"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/middleware"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/process"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/process/deprovisioning"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/process/provisioning"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/process/provisioning/input"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/provisioner"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/runtime"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/storage"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/storage/dbsession/dbmodel"
"code.cloudfoundry.org/lager"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
gcli "github.com/machinebox/graphql"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
// Config holds configuration for the whole application
type Config struct {
Auth struct {
Username string
Password string
}
DbInMemory bool `envconfig:"default=false"`
// DisableProcessOperationsInProgress allows to disable processing operations
// which are in progress on starting application. Set to true if you are
// running in a separate testing deployment but with the production DB.
DisableProcessOperationsInProgress bool `envconfig:"default=false"`
// DevelopmentMode if set to true then errors are returned in http
// responses, otherwise errors are only logged and generic message
// is returned to client.
// Currently works only with /info endpoints.
DevelopmentMode bool `envconfig:"default=false"`
// DumpProvisionerRequests enables dumping Provisioner requests. Must be disabled on Production environments
// because some data must not be visible in the log file.
DumpProvisionerRequests bool `envconfig:"default=false"`
Host string `envconfig:"optional"`
Port string `envconfig:"default=8080"`
StatusPort string `envconfig:"default=8071"`
Provisioning input.Config
Director director.Config
Database storage.Config
Gardener gardener.Config
ServiceManager provisioning.ServiceManagerOverrideConfig
KymaVersion string
EnableOnDemandVersion bool `envconfig:"default=false"`
ManagedRuntimeComponentsYAMLFilePath string
DefaultRequestRegion string `envconfig:"default=cf-eu10"`
Broker broker.Config
Avs avs.Config
LMS lms.Config
IAS ias.Config
EDP edp.Config
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create and fill config
var cfg Config
err := envconfig.InitWithPrefix(&cfg, "APP")
fatalOnError(err)
// create logger
logger := lager.NewLogger("kyma-env-broker")
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.ERROR))
logger.Info("Starting Kyma Environment Broker")
logs := logrus.New()
logger.Info("Registering healthz endpoint for health probes")
health.NewServer(cfg.Host, cfg.StatusPort, logs).ServeAsync()
// create provisioner client
provisionerClient := provisioner.NewProvisionerClient(cfg.Provisioning.URL, cfg.DumpProvisionerRequests)
// create kubernetes client
k8sCfg, err := config.GetConfig()
fatalOnError(err)
cli, err := client.New(k8sCfg, client.Options{})
fatalOnError(err)
// create director client on the base of graphQL client and OAuth client
httpClient := httputil.NewClient(30, cfg.Director.SkipCertVerification)
graphQLClient := gcli.NewClient(cfg.Director.URL, gcli.WithHTTPClient(httpClient))
oauthClient := oauth.NewOauthClient(httpClient, cli, cfg.Director.OauthCredentialsSecretName, cfg.Director.Namespace)
fatalOnError(oauthClient.WaitForCredentials())
directorClient := director.NewDirectorClient(oauthClient, graphQLClient)
// create storage
var db storage.BrokerStorage
if cfg.DbInMemory {
db = storage.NewMemoryStorage()
} else {
db, err = storage.NewFromConfig(cfg.Database, logs)
fatalOnError(err)
}
// LMS
fatalOnError(cfg.LMS.Validate())
lmsClient := lms.NewClient(cfg.LMS, logs)
lmsTenantManager := lms.NewTenantManager(db.LMSTenants(), lmsClient, logs.WithField("service", "lmsTenantManager"))
// Register disabler. Convention:
// {component-name} : {component-disabler-service}
//
// Using map is intentional - we ensure that component name is not duplicated.
optionalComponentsDisablers := runtime.ComponentsDisablers{
"Kiali": runtime.NewGenericComponentDisabler("kiali", "kyma-system"),
"Tracing": runtime.NewGenericComponentDisabler("tracing", "kyma-system"),
// TODO(workaround until #1049): following components should be always disabled and user should not be able to enable them in provisioning request. This implies following components cannot be specified under the plan schema definition.
"BackupInt": runtime.NewGenericComponentDisabler("backup-init", "kyma-system"),
"Backup": runtime.NewGenericComponentDisabler("backup", "kyma-system"),
"KnativeProvisionerNatss": runtime.NewGenericComponentDisabler("knative-provisioner-natss", "knative-eventing"),
"NatssStreaming": runtime.NewGenericComponentDisabler("nats-streaming", "natss"),
}
optComponentsSvc := runtime.NewOptionalComponentsService(optionalComponentsDisablers)
runtimeProvider := runtime.NewComponentsListProvider(cfg.ManagedRuntimeComponentsYAMLFilePath)
gardenerClusterConfig, err := gardener.NewGardenerClusterConfig(cfg.Gardener.KubeconfigPath)
fatalOnError(err)
//
gardenerSecrets, err := gardener.NewGardenerSecretsInterface(gardenerClusterConfig, cfg.Gardener.Project)
fatalOnError(err)
gardenerAccountPool := hyperscaler.NewAccountPool(gardenerSecrets)
accountProvider := hyperscaler.NewAccountProvider(nil, gardenerAccountPool)
inputFactory, err := input.NewInputBuilderFactory(optComponentsSvc, runtimeProvider, cfg.Provisioning, cfg.KymaVersion)
fatalOnError(err)
edpClient := edp.NewClient(cfg.EDP, logs)
avsDel := avs.NewDelegator(cfg.Avs, db.Operations())
externalEvalAssistant := avs.NewExternalEvalAssistant(cfg.Avs)
internalEvalAssistant := avs.NewInternalEvalAssistant(cfg.Avs)
externalEvalCreator := provisioning.NewExternalEvalCreator(avsDel, cfg.Avs.Disabled, externalEvalAssistant)
bundleBuilder := ias.NewBundleBuilder(httpClient, cfg.IAS)
iasTypeSetter := provisioning.NewIASType(bundleBuilder, cfg.IAS.Disabled)
// setup operation managers
provisionManager := provisioning.NewManager(db.Operations(), logs)
deprovisionManager := deprovisioning.NewManager(db.Operations(), logs)
// define steps
provisioningInit := provisioning.NewInitialisationStep(db.Operations(), db.Instances(),
provisionerClient, directorClient, inputFactory, externalEvalCreator, iasTypeSetter, cfg.Provisioning.Timeout)
provisionManager.InitStep(provisioningInit)
provisioningSteps := []struct {
disabled bool
weight int
step provisioning.Step
}{
{
weight: 1,
step: provisioning.NewResolveCredentialsStep(db.Operations(), accountProvider),
},
{
weight: 1,
step: provisioning.NewInternalEvaluationStep(avsDel, internalEvalAssistant),
disabled: cfg.Avs.Disabled,
},
{
weight: 1,
step: provisioning.NewProvideLmsTenantStep(lmsTenantManager, db.Operations(), cfg.LMS.Region),
disabled: cfg.LMS.Disabled,
},
{
weight: 1,
step: provisioning.NewEDPRegistrationStep(db.Operations(), edpClient, cfg.EDP),
disabled: cfg.EDP.Disabled,
},
{
weight: 2,
step: provisioning.NewProvisionAzureEventHubStep(db.Operations(), azure.NewAzureProvider(), accountProvider, ctx),
},
{
weight: 2,
step: provisioning.NewOverridesFromSecretsAndConfigStep(ctx, cli, db.Operations()),
},
{
weight: 2,
step: provisioning.NewServiceManagerOverridesStep(db.Operations(), cfg.ServiceManager),
},
{
weight: 4,
step: provisioning.NewLmsCertificatesStep(lmsClient, db.Operations()),
disabled: cfg.LMS.Disabled,
},
{
weight: 5,
step: provisioning.NewIASRegistrationStep(db.Operations(), bundleBuilder),
disabled: cfg.IAS.Disabled,
},
{
weight: 10,
step: provisioning.NewCreateRuntimeStep(db.Operations(), db.Instances(), provisionerClient),
},
}
for _, step := range provisioningSteps {
if !step.disabled {
provisionManager.AddStep(step.weight, step.step)
}
}
deprovisioningInit := deprovisioning.NewInitialisationStep(db.Operations(), db.Instances(), provisionerClient)
deprovisionManager.InitStep(deprovisioningInit)
deprovisioningSteps := []struct {
disabled bool
weight int
step deprovisioning.Step
}{
{
weight: 1,
step: deprovisioning.NewAvsEvaluationsRemovalStep(avsDel, db.Operations(), externalEvalAssistant, internalEvalAssistant),
},
{
weight: 1,
step: deprovisioning.NewDeprovisionAzureEventHubStep(db.Operations(), azure.NewAzureProvider(), accountProvider, ctx),
},
{
weight: 1,
step: deprovisioning.NewEDPDeregistrationStep(edpClient, cfg.EDP),
disabled: cfg.EDP.Disabled,
},
{
weight: 1,
step: deprovisioning.NewIASDeregistrationStep(db.Operations(), bundleBuilder),
disabled: cfg.IAS.Disabled,
},
{
weight: 10,
step: deprovisioning.NewRemoveRuntimeStep(db.Operations(), db.Instances(), provisionerClient),
},
}
for _, step := range deprovisioningSteps {
if !step.disabled {
deprovisionManager.AddStep(step.weight, step.step)
}
}
// run queues
provisionQueue := process.NewQueue(provisionManager)
provisionQueue.Run(ctx.Done())
deprovisionQueue := process.NewQueue(deprovisionManager)
deprovisionQueue.Run(ctx.Done())
if !cfg.DisableProcessOperationsInProgress {
err = processOperationsInProgressByType(dbmodel.OperationTypeProvision, db.Operations(), provisionQueue, logs)
fatalOnError(err)
err = processOperationsInProgressByType(dbmodel.OperationTypeDeprovision, db.Operations(), deprovisionQueue, logs)
fatalOnError(err)
} else {
logger.Info("Skipping processing operation in progress on start")
}
plansValidator, err := broker.NewPlansSchemaValidator()
fatalOnError(err)
// create KymaEnvironmentBroker endpoints
kymaEnvBroker := &broker.KymaEnvironmentBroker{
broker.NewServices(cfg.Broker, optComponentsSvc, logs),
broker.NewProvision(cfg.Broker, db.Operations(), db.Instances(), provisionQueue, inputFactory, plansValidator, cfg.EnableOnDemandVersion, logs),
broker.NewDeprovision(db.Instances(), db.Operations(), deprovisionQueue, logs),
broker.NewUpdate(logs),
broker.NewGetInstance(db.Instances(), logs),
broker.NewLastOperation(db.Operations(), logs),
broker.NewBind(logs),
broker.NewUnbind(logs),
broker.NewGetBinding(logs),
broker.NewLastBindingOperation(logs),
}
// create server
router := mux.NewRouter()
// create info endpoints
respWriter := httputil.NewResponseWriter(logs, cfg.DevelopmentMode)
runtimesInfoHandler := appinfo.NewRuntimeInfoHandler(db.Instances(), respWriter)
router.Handle("/info/runtimes", runtimesInfoHandler)
// create OSB API endpoints
basicAuth := &broker.Credentials{
Username: cfg.Auth.Username,
Password: cfg.Auth.Password,
}
router.Use(middleware.AddRegionToContext(cfg.DefaultRequestRegion))
for prefix, creds := range map[string]*broker.Credentials{
"/": basicAuth, // legacy basic auth
"/oauth/": nil, // oauth2 handled by Ory
"/oauth/{region}/": nil, // oauth2 handled by Ory with region
} {
route := router.PathPrefix(prefix).Subrouter()
broker.AttachRoutes(route, kymaEnvBroker, logger, creds)
}
svr := handlers.LoggingHandler(os.Stdout, router)
fatalOnError(http.ListenAndServe(cfg.Host+":"+cfg.Port, svr))
}
// queues all in progress provision operations existing in the database
func processOperationsInProgressByType(opType dbmodel.OperationType, op storage.Operations, queue *process.Queue, log logrus.FieldLogger) error {
operations, err := op.GetOperationsInProgressByType(opType)
if err != nil {
return errors.Wrap(err, "while getting in progress operations from storage")
}
for _, operation := range operations {
queue.Add(operation.ID)
log.Infof("Resuming the processing of %s operation ID: %s", opType, operation.ID)
}
return nil
}
func fatalOnError(err error) {
if err != nil {
log.Fatal(err)
}
}