-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (153 loc) · 5.25 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
package main
import (
"context"
"errors"
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/equinor/radix-api/api/metrics"
"github.com/equinor/radix-api/api/secrets"
"github.com/equinor/radix-api/api/utils/tlsvalidation"
token "github.com/equinor/radix-api/api/utils/token"
"github.com/equinor/radix-api/internal/config"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/equinor/radix-api/api/environmentvariables"
"github.com/equinor/radix-api/api/buildstatus"
// Controllers
"github.com/equinor/radix-api/api/alerting"
"github.com/equinor/radix-api/api/applications"
"github.com/equinor/radix-api/api/buildsecrets"
build_models "github.com/equinor/radix-api/api/buildstatus/models"
"github.com/equinor/radix-api/api/deployments"
"github.com/equinor/radix-api/api/environments"
"github.com/equinor/radix-api/api/jobs"
"github.com/equinor/radix-api/api/privateimagehubs"
"github.com/equinor/radix-api/api/router"
"github.com/equinor/radix-api/api/utils"
_ "github.com/equinor/radix-api/docs"
"github.com/equinor/radix-api/models"
)
//go:generate swagger generate spec
func main() {
c := config.MustParse()
setupLogger(c.LogLevel, c.LogPrettyPrint)
servers := []*http.Server{
initializeServer(c),
initializeMetricsServer(c),
}
if c.UseProfiler {
log.Info().Msgf("Initializing profile server on port %d", c.ProfilePort)
servers = append(servers, &http.Server{Addr: fmt.Sprintf("localhost:%d", c.ProfilePort)})
}
startServers(servers...)
shutdownServersGracefulOnSignal(servers...)
}
func initializeServer(c config.Config) *http.Server {
jwtValidator := initializeTokenValidator(c)
controllers, err := getControllers(c)
if err != nil {
log.Fatal().Err(err).Msgf("failed to initialize controllers: %v", err)
}
handler := router.NewAPIHandler(jwtValidator, utils.NewKubeUtil(), controllers...)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", c.Port),
Handler: handler,
}
return srv
}
func initializeTokenValidator(c config.Config) token.ValidatorInterface {
azureValidator, err := token.NewValidator(c.AzureOidc.Issuer, c.AzureOidc.Audience)
if err != nil {
log.Fatal().Err(err).Msg("Error creating JWT Azure OIDC validator")
}
kubernetesValidator, err := token.NewValidator(c.KubernetesOidc.Issuer, c.KubernetesOidc.Audience)
if err != nil {
log.Fatal().Err(err).Msg("Error creating JWT Kubernetes OIDC validator")
}
chainedValidator := token.NewChainedValidator(azureValidator, kubernetesValidator)
return chainedValidator
}
func initializeMetricsServer(c config.Config) *http.Server {
log.Info().Msgf("Initializing metrics server on port %d", c.MetricsPort)
return &http.Server{
Addr: fmt.Sprintf(":%d", c.MetricsPort),
Handler: router.NewMetricsHandler(),
}
}
func startServers(servers ...*http.Server) {
for _, srv := range servers {
srv := srv
go func() {
log.Info().Msgf("Starting server on address %s", srv.Addr)
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msgf("Unable to start server on address %s", srv.Addr)
}
}()
}
}
func shutdownServersGracefulOnSignal(servers ...*http.Server) {
stopCh := make(chan os.Signal, 1)
signal.Notify(stopCh, syscall.SIGTERM, syscall.SIGINT)
s := <-stopCh
log.Info().Msgf("Received %v signal", s)
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var wg sync.WaitGroup
for _, srv := range servers {
srv := srv
wg.Add(1)
go func() {
defer wg.Done()
log.Info().Msgf("Shutting down server on address %s", srv.Addr)
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Warn().Err(err).Msgf("shutdown of server on address %s returned an error", srv.Addr)
}
}()
}
wg.Wait()
}
func setupLogger(logLevelStr string, prettyPrint bool) {
if len(logLevelStr) == 0 {
logLevelStr = zerolog.LevelInfoValue
}
logLevel, err := zerolog.ParseLevel(logLevelStr)
if err != nil {
logLevel = zerolog.InfoLevel
log.Warn().Msgf("Invalid log level '%s', fallback to '%s'", logLevelStr, logLevel.String())
}
var logWriter io.Writer = os.Stderr
if prettyPrint {
logWriter = &zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly}
}
logger := zerolog.New(logWriter).Level(logLevel).With().Timestamp().Logger()
log.Logger = logger
zerolog.DefaultContextLogger = &logger
}
func getControllers(config config.Config) ([]models.Controller, error) {
buildStatus := build_models.NewPipelineBadge()
applicatinoFactory := applications.NewApplicationHandlerFactory(config)
prometheusClient, err := metrics.NewPrometheusClient(config.PrometheusUrl)
if err != nil {
return nil, err
}
prometheusHandler := metrics.NewPrometheusHandler(prometheusClient)
return []models.Controller{
applications.NewApplicationController(nil, applicatinoFactory, prometheusHandler),
deployments.NewDeploymentController(),
jobs.NewJobController(),
environments.NewEnvironmentController(environments.NewEnvironmentHandlerFactory()),
environmentvariables.NewEnvVarsController(),
privateimagehubs.NewPrivateImageHubController(),
buildsecrets.NewBuildSecretsController(),
buildstatus.NewBuildStatusController(buildStatus),
alerting.NewAlertingController(),
secrets.NewSecretController(tlsvalidation.DefaultValidator()),
}, nil
}