ARO-RP/cmd/aro/rp.go

116 строки
2.9 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"
uuid "github.com/satori/go.uuid"
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"
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/metrics/statsd"
"github.com/Azure/ARO-RP/pkg/metrics/statsd/azure"
"github.com/Azure/ARO-RP/pkg/metrics/statsd/k8s"
"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 {
for _, key := range []string{
"PULL_SECRET",
} {
if _, found := os.LookupEnv(key); !found {
return fmt.Errorf("environment variable %q unset", key)
}
}
2019-12-22 04:18:17 +03:00
uuid := uuid.NewV4().String()
2019-12-22 20:47:29 +03:00
log.Printf("uuid %s", uuid)
2019-11-18 09:24:26 +03:00
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-02-21 16:52:27 +03:00
if _, ok := _env.(env.Dev); !ok {
for _, key := range []string{
"MDM_ACCOUNT",
"MDM_NAMESPACE",
"ADMIN_API_CLIENT_CERT_COMMON_NAME",
} {
if _, found := os.LookupEnv(key); !found {
return fmt.Errorf("environment variable %q unset", key)
}
}
}
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))
metrics.Register(k8s.NewLatency(m), k8s.NewResult(m))
2020-02-21 16:52:27 +03:00
cipher, err := encryption.NewXChaCha20Poly1305(ctx, _env)
if err != nil {
return err
}
2020-02-21 16:52:27 +03:00
db, err := database.NewDatabase(ctx, log.WithField("component", "database"), _env, m, cipher, uuid)
2019-11-18 07:00:07 +03:00
if err != nil {
return err
2019-11-18 06:43:35 +03:00
}
2020-02-21 16:52:27 +03:00
f, err := frontend.NewFrontend(ctx, log.WithField("component", "frontend"), _env, db, api.APIs, m)
if err != nil {
return err
}
2020-02-21 16:52:27 +03:00
b, err := backend.NewBackend(ctx, log.WithField("component", "backend"), _env, db, 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
}