ARO-RP/cmd/aro/rp.go

159 строки
4.1 KiB
Go
Исходник Обычный вид История

2019-10-16 06:29:17 +03:00
package main
2019-12-17 04:16:50 +03:00
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
2019-10-16 06:29:17 +03:00
import (
2019-11-18 06:43:35 +03:00
"context"
"fmt"
2019-10-16 06:29:17 +03:00
"os"
"os/signal"
"syscall"
"github.com/Azure/go-autorest/tracing"
2019-10-16 06:29:17 +03:00
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/metrics"
2019-10-16 06:29:17 +03:00
"github.com/Azure/ARO-RP/pkg/api"
2020-01-24 14:19:56 +03:00
_ "github.com/Azure/ARO-RP/pkg/api/admin"
_ "github.com/Azure/ARO-RP/pkg/api/v20191231preview"
_ "github.com/Azure/ARO-RP/pkg/api/v20200430"
_ "github.com/Azure/ARO-RP/pkg/api/v20210131preview"
2019-12-17 04:26:21 +03:00
"github.com/Azure/ARO-RP/pkg/backend"
"github.com/Azure/ARO-RP/pkg/database"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/frontend"
"github.com/Azure/ARO-RP/pkg/frontend/adminactions"
"github.com/Azure/ARO-RP/pkg/metrics/statsd"
"github.com/Azure/ARO-RP/pkg/metrics/statsd/azure"
"github.com/Azure/ARO-RP/pkg/metrics/statsd/k8s"
2020-09-17 21:42:37 +03:00
"github.com/Azure/ARO-RP/pkg/util/deployment"
"github.com/Azure/ARO-RP/pkg/util/encryption"
2019-10-16 06:29:17 +03:00
)
2019-12-22 20:47:29 +03:00
func rp(ctx context.Context, log *logrus.Entry) error {
2020-02-21 16:52:27 +03:00
_env, err := env.NewEnv(ctx, log)
2019-11-18 09:55:32 +03:00
if err != nil {
return err
}
2020-03-19 00:55:49 +03:00
var keys []string
2020-09-17 21:42:37 +03:00
if _env.DeploymentMode() == deployment.Development {
2020-03-19 00:55:49 +03:00
keys = []string{
"PULL_SECRET",
}
} else {
keys = []string{
2020-03-21 04:57:07 +03:00
"ACR_RESOURCE_ID",
"ADMIN_API_CLIENT_CERT_COMMON_NAME",
2020-02-21 16:52:27 +03:00
"MDM_ACCOUNT",
"MDM_NAMESPACE",
2020-03-19 00:55:49 +03:00
}
if _, found := os.LookupEnv("PULL_SECRET"); found {
return fmt.Errorf(`environment variable "PULL_SECRET" set`)
}
}
for _, key := range keys {
if _, found := os.LookupEnv(key); !found {
return fmt.Errorf("environment variable %q unset", key)
2020-02-21 16:52:27 +03:00
}
}
err = _env.InitializeAuthorizers()
if err != nil {
return err
}
m, err := statsd.New(ctx, log.WithField("component", "metrics"), _env, os.Getenv("MDM_ACCOUNT"), os.Getenv("MDM_NAMESPACE"))
2020-01-09 23:34:15 +03:00
if err != nil {
return err
}
tracing.Register(azure.New(m))
2020-10-31 21:45:02 +03:00
metrics.Register(metrics.RegisterOpts{
RequestResult: k8s.NewResult(m),
RequestLatency: k8s.NewLatency(m),
})
2020-11-18 00:30:57 +03:00
dbKey, err := _env.ServiceKeyvault().GetBase64Secret(ctx, env.EncryptionSecretName)
if err != nil {
return err
}
cipher, err := encryption.NewXChaCha20Poly1305(ctx, dbKey)
if err != nil {
return err
}
2020-09-26 01:07:03 +03:00
dbc, err := database.NewDatabaseClient(ctx, log.WithField("component", "database"), _env, m, cipher)
2019-11-18 07:00:07 +03:00
if err != nil {
return err
2019-11-18 06:43:35 +03:00
}
2020-09-26 01:07:03 +03:00
dbAsyncOperations, err := database.NewAsyncOperations(ctx, _env.DeploymentMode(), dbc)
if err != nil {
return err
}
dbBilling, err := database.NewBilling(ctx, _env.DeploymentMode(), dbc)
if err != nil {
return err
}
dbOpenShiftClusters, err := database.NewOpenShiftClusters(ctx, _env.DeploymentMode(), dbc)
if err != nil {
return err
}
dbSubscriptions, err := database.NewSubscriptions(ctx, _env.DeploymentMode(), dbc)
if err != nil {
return err
}
go database.EmitMetrics(ctx, log, dbOpenShiftClusters, m)
2020-04-08 02:55:53 +03:00
2020-11-18 00:30:57 +03:00
feKey, err := _env.ServiceKeyvault().GetBase64Secret(ctx, env.FrontendEncryptionSecretName)
if err != nil {
return err
}
feCipher, err := encryption.NewXChaCha20Poly1305(ctx, feKey)
2020-03-30 12:10:16 +03:00
if err != nil {
return err
}
2020-04-08 02:55:53 +03:00
2020-09-26 01:07:03 +03:00
f, err := frontend.NewFrontend(ctx, log.WithField("component", "frontend"), _env, dbAsyncOperations, dbOpenShiftClusters, dbSubscriptions, api.APIs, m, feCipher, adminactions.New)
if err != nil {
return err
}
2020-09-26 01:07:03 +03:00
b, err := backend.NewBackend(ctx, log.WithField("component", "backend"), _env, dbAsyncOperations, dbBilling, dbOpenShiftClusters, dbSubscriptions, cipher, m)
2019-10-16 06:29:17 +03:00
if err != nil {
return err
}
// This part of the code orchestrates shutdown sequence. When sigterm is
// received, it will trigger backend to stop accepting new documents and
// finish old ones. Frontend will stop advertising itself to the loadbalancer.
// When shutdown completes for frontend and backend "/healthz" endpoint
// will go dark and external observer will know that shutdown sequence is finished
sigterm := make(chan os.Signal, 1)
stop := make(chan struct{})
doneF := make(chan struct{})
doneB := make(chan struct{})
signal.Notify(sigterm, syscall.SIGTERM)
2019-10-16 06:29:17 +03:00
log.Print("listening")
go b.Run(ctx, stop, doneB)
go f.Run(ctx, stop, doneF)
2019-10-16 06:29:17 +03:00
<-sigterm
log.Print("received SIGTERM")
close(stop)
<-doneB
<-doneF
2019-10-16 06:29:17 +03:00
2019-12-10 17:58:43 +03:00
return nil
2019-10-16 06:29:17 +03:00
}