-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (149 loc) · 5.27 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
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/equinor/radix-job-scheduler/api"
batchApi "github.com/equinor/radix-job-scheduler/api/v1/batches"
batchControllers "github.com/equinor/radix-job-scheduler/api/v1/controllers/batches"
jobControllers "github.com/equinor/radix-job-scheduler/api/v1/controllers/jobs"
jobApi "github.com/equinor/radix-job-scheduler/api/v1/jobs"
"github.com/equinor/radix-job-scheduler/models"
"github.com/equinor/radix-job-scheduler/pkg/batch"
"github.com/equinor/radix-job-scheduler/pkg/notifications"
"github.com/equinor/radix-job-scheduler/pkg/watcher"
"github.com/equinor/radix-job-scheduler/router"
_ "github.com/equinor/radix-job-scheduler/swaggerui"
"github.com/equinor/radix-job-scheduler/utils/radix"
"github.com/equinor/radix-operator/pkg/apis/kube"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
)
const (
defaultProfilePort = "7070"
)
func main() {
ctx := context.Background()
env := models.NewEnv()
initLogger(env)
kubeUtil := getKubeUtil(ctx)
radixDeployJobComponent, err := radix.GetRadixDeployJobComponentByName(ctx, kubeUtil.RadixClient(), env.RadixDeploymentNamespace, env.RadixDeploymentName, env.RadixComponentName)
if err != nil {
log.Fatal().Err(err).Msg("failed to get job specification")
}
jobHistory := batch.NewHistory(kubeUtil, env, radixDeployJobComponent)
notifier := notifications.NewWebhookNotifier(radixDeployJobComponent)
radixBatchWatcher, err := watcher.NewRadixBatchWatcher(ctx, kubeUtil.RadixClient(), env.RadixDeploymentNamespace, jobHistory, notifier)
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize job watcher")
}
defer radixBatchWatcher.Stop()
runApiServer(ctx, kubeUtil, env, radixDeployJobComponent)
}
func initLogger(env *models.Env) {
logLevelStr := env.LogLevel
if len(logLevelStr) == 0 {
logLevelStr = zerolog.LevelInfoValue
}
logLevel, err := zerolog.ParseLevel(logLevelStr)
if err != nil {
logLevel = zerolog.InfoLevel
}
zerolog.SetGlobalLevel(logLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
zerolog.DefaultContextLogger = &log.Logger
}
func runApiServer(ctx context.Context, kubeUtil *kube.Kube, env *models.Env, radixDeployJobComponent *radixv1.RadixDeployJobComponent) {
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
fs := initializeFlagSet()
port := fs.StringP("port", "p", env.RadixPort, "Port where API will be served")
parseFlagsFromArgs(fs)
var servers []*http.Server
if env.UseProfiler {
log.Info().Msgf("Initializing a profile server on a port %s", defaultProfilePort)
servers = append(servers, &http.Server{Addr: fmt.Sprintf(":%s", defaultProfilePort)})
}
apiServer := &http.Server{
Addr: fmt.Sprintf(":%s", *port),
Handler: router.NewServer(env, getControllers(kubeUtil, env, radixDeployJobComponent)...),
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
servers = append(servers, apiServer)
startServers(servers...)
<-ctx.Done()
shutdownServersGracefulOnSignal(servers...)
}
func startServers(servers ...*http.Server) {
for _, srv := range servers {
go func() {
log.Debug().Msgf("Starting a server on address %s", srv.Addr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msgf("Unable to start server on address %s", srv.Addr)
return
}
log.Info().Msgf("Started a server on address %s", srv.Addr)
}()
}
}
func getKubeUtil(ctx context.Context) *kube.Kube {
kubeClient, radixClient, kedaClient, _, secretProviderClient, _ := utils.GetKubernetesClient(ctx)
kubeUtil, _ := kube.New(kubeClient, radixClient, kedaClient, secretProviderClient)
return kubeUtil
}
func getControllers(kubeUtil *kube.Kube, env *models.Env, radixDeployJobComponent *radixv1.RadixDeployJobComponent) []api.Controller {
return []api.Controller{
jobControllers.New(jobApi.New(kubeUtil, env, radixDeployJobComponent)),
batchControllers.New(batchApi.New(kubeUtil, env, radixDeployJobComponent)),
}
}
func initializeFlagSet() *pflag.FlagSet {
// Flag domain.
fs := pflag.NewFlagSet("default", pflag.ContinueOnError)
fs.Usage = func() {
fmt.Fprint(os.Stderr, "DESCRIPTION\n")
fmt.Fprint(os.Stderr, "Radix job scheduler API server.\n")
fmt.Fprint(os.Stderr, "\n")
fmt.Fprint(os.Stderr, "FLAGS\n")
fs.PrintDefaults()
}
return fs
}
func parseFlagsFromArgs(fs *pflag.FlagSet) {
err := fs.Parse(os.Args[1:])
switch {
case err == pflag.ErrHelp:
os.Exit(0)
case err != nil:
log.Error().Err(err).Msg("Failed to parse flags")
fs.Usage()
os.Exit(2)
}
}
func shutdownServersGracefulOnSignal(servers ...*http.Server) {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var wg sync.WaitGroup
for _, srv := range servers {
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()
}