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"
|
2020-02-25 04:19:40 +03:00
|
|
|
"fmt"
|
2019-10-16 06:29:17 +03:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2019-11-30 03:53:25 +03:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-10-16 06:29:17 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-01-04 21:09:47 +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"
|
2020-01-04 21:09:47 +03:00
|
|
|
_ "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"
|
2020-01-08 19:39:44 +03:00
|
|
|
"github.com/Azure/ARO-RP/pkg/metrics/statsd"
|
2020-02-10 01:39:16 +03:00
|
|
|
"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-25 04:19:40 +03:00
|
|
|
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
|
|
|
|
2019-12-01 18:17:54 +03:00
|
|
|
env, err := env.NewEnv(ctx, log)
|
2019-11-18 09:55:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-26 03:51:14 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-10 01:39:16 +03:00
|
|
|
cipher, err := encryption.NewXChaCha20Poly1305(ctx, env)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-08 19:39:44 +03:00
|
|
|
f, err := frontend.NewFrontend(ctx, log.WithField("component", "frontend"), env, db, api.APIs, m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-24 13:07:43 +03:00
|
|
|
// 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
|
|
|
|
2020-02-24 13:07:43 +03:00
|
|
|
log.Print("listening")
|
2020-02-06 17:28:01 +03:00
|
|
|
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")
|
2019-10-18 21:48:09 +03:00
|
|
|
close(stop)
|
2020-02-06 17:28:01 +03:00
|
|
|
<-doneB
|
2020-02-24 13:07:43 +03:00
|
|
|
<-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
|
|
|
}
|