generated from cobaltcore-dev/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (150 loc) · 5.96 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
// Copyright 2025 SAP SE
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"time"
"github.com/cobaltcore-dev/cortex/internal/commands/checks"
"github.com/cobaltcore-dev/cortex/internal/conf"
"github.com/cobaltcore-dev/cortex/internal/db"
"github.com/cobaltcore-dev/cortex/internal/features"
"github.com/cobaltcore-dev/cortex/internal/kpis"
"github.com/cobaltcore-dev/cortex/internal/monitoring"
"github.com/cobaltcore-dev/cortex/internal/scheduler"
apihttp "github.com/cobaltcore-dev/cortex/internal/scheduler/api/http"
"github.com/cobaltcore-dev/cortex/internal/sync"
"github.com/cobaltcore-dev/cortex/internal/sync/openstack"
"github.com/cobaltcore-dev/cortex/internal/sync/prometheus"
"github.com/sapcc/go-api-declarations/bininfo"
"github.com/sapcc/go-bits/httpext"
"go.uber.org/automaxprocs/maxprocs"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Periodically fetch data from the datasources and insert it into the database.
func runSyncer(ctx context.Context, registry *monitoring.Registry, config conf.SyncConfig, db db.DB) {
monitor := sync.NewSyncMonitor(registry)
syncers := []sync.Datasource{
prometheus.NewCombinedSyncer(config.Prometheus, db, monitor),
openstack.NewCombinedSyncer(ctx, config.OpenStack, monitor, db),
}
pipeline := sync.Pipeline{Syncers: syncers}
pipeline.Init(ctx)
go pipeline.SyncPeriodic(ctx) // blocking
}
// Periodically extract features from the database.
func runExtractor(registry *monitoring.Registry, config conf.FeaturesConfig, db db.DB) {
monitor := features.NewPipelineMonitor(registry)
pipeline := features.NewPipeline(config, db, monitor)
// Selects the extractors to run based on the config.
pipeline.Init(features.SupportedExtractors)
go pipeline.ExtractOnTrigger() // blocking
}
// Run a webserver that listens for external scheduling requests.
func runScheduler(mux *http.ServeMux, registry *monitoring.Registry, config conf.SchedulerConfig, db db.DB) {
schedulerMonitor := scheduler.NewSchedulerMonitor(registry)
schedulerPipeline := scheduler.NewPipeline(config, db, schedulerMonitor)
apiMonitor := apihttp.NewSchedulerMonitor(registry)
api := apihttp.NewAPI(config.API, schedulerPipeline, apiMonitor)
api.Init(mux) // non-blocking
}
// Run a kpi service that periodically calculates kpis.
func runKPIService(registry *monitoring.Registry, config conf.KPIsConfig, db db.DB) {
pipeline := kpis.NewPipeline(config)
if err := pipeline.Init(kpis.SupportedKPIs, db, registry); err != nil {
panic("failed to initialize kpi pipeline: " + err.Error())
} // non-blocking
}
// Run the prometheus metrics server for monitoring.
func runMonitoringServer(ctx context.Context, registry *monitoring.Registry, config conf.MonitoringConfig) {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
slog.Info("metrics listening", "port", config.Port)
addr := fmt.Sprintf(":%d", config.Port)
if err := httpext.ListenAndServeContext(ctx, addr, mux); err != nil {
panic(err)
}
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
panic("no arguments provided")
}
// If called with `--version`, report version and exit (the Dockerfile
// uses this to check if the binary was built correctly)
bininfo.HandleVersionArgument()
config := conf.NewConfig()
// Set the configured logger.
config.GetLoggingConfig().SetDefaultLogger()
if err := config.Validate(); err != nil {
slog.Error("failed to validate config", "error", err)
panic(err)
}
slog.Info("config validated")
// Set runtime concurrency to match CPU limit imposed by Kubernetes
undoMaxprocs, err := maxprocs.Set(maxprocs.Logger(slog.Debug))
if err != nil {
panic(err)
}
defer undoMaxprocs()
// Override User-Agent header for all requests made by this process
// (logs will show e.g. "blueprint-api/d0c9faa" instead of "Go-http-client/2.0")
wrap := httpext.WrapTransport(&http.DefaultTransport)
wrap.SetOverrideUserAgent(bininfo.Component(), bininfo.VersionOr("rolling"))
// This context will gracefully shutdown when the process receives the
// standard shutdown signal SIGINT, with a 10-second delay to allow
// Kubernetes to stop sending new requests well before the process starts
// to shut down.
ctx := httpext.ContextWithSIGINT(context.Background(), 10*time.Second)
// Parse command line arguments.
var taskName string
if len(os.Args) == 2 {
taskName = os.Args[1]
bininfo.SetTaskName(taskName)
} else {
panic(fmt.Sprintf("usage: %s [checks | syncer | extractor | scheduler | kpis]", os.Args[0]))
}
dbInstance := db.NewPostgresDB(config.GetDBConfig())
defer dbInstance.Close()
migrater := db.NewMigrater(dbInstance)
migrater.Migrate(true)
// If we're running one-off tasks (commands), don't setup the monitoring server.
//nolint:gocritic // We may add more tasks in the future.
switch taskName {
case "checks":
checks.RunChecks(ctx, config)
return
}
monitoringConfig := config.GetMonitoringConfig()
registry := monitoring.NewRegistry(monitoringConfig)
go runMonitoringServer(ctx, registry, monitoringConfig)
// Run an api server that serves some basic endpoints and can be extended.
mux := http.NewServeMux()
mux.HandleFunc("/up", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
switch taskName {
case "syncer":
runSyncer(ctx, registry, config.GetSyncConfig(), dbInstance)
case "extractor":
runExtractor(registry, config.GetFeaturesConfig(), dbInstance)
case "scheduler":
runScheduler(mux, registry, config.GetSchedulerConfig(), dbInstance)
case "kpis":
runKPIService(registry, config.GetKPIsConfig(), dbInstance)
default:
panic("unknown task")
}
// Run the api server after all other tasks have been started and
// all http handlers have been registered to the mux.
apiConf := config.GetAPIConfig()
addr := fmt.Sprintf(":%d", apiConf.Port)
if err := httpext.ListenAndServeContext(ctx, addr, mux); err != nil {
panic(err)
}
slog.Info("api listening", "port", apiConf.Port)
select {}
}