-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
288 lines (258 loc) · 10.6 KB
/
main.go
File metadata and controls
288 lines (258 loc) · 10.6 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"context"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/krateoplatformops/unstructured-runtime/pkg/controller"
"github.com/krateoplatformops/unstructured-runtime/pkg/controller/builder"
ctrlevent "github.com/krateoplatformops/unstructured-runtime/pkg/controller/event"
"github.com/krateoplatformops/unstructured-runtime/pkg/metrics/server"
"github.com/krateoplatformops/unstructured-runtime/pkg/telemetry"
"github.com/go-logr/logr"
"github.com/krateoplatformops/composition-dynamic-controller/internal/composition"
"github.com/krateoplatformops/composition-dynamic-controller/internal/metrics"
"github.com/krateoplatformops/composition-dynamic-controller/internal/tools/archive"
"github.com/krateoplatformops/composition-dynamic-controller/internal/tools/dynamic"
"github.com/krateoplatformops/composition-dynamic-controller/pkg/meta"
"github.com/krateoplatformops/plumbing/env"
"github.com/krateoplatformops/plumbing/kubeutil/event"
"github.com/krateoplatformops/plumbing/kubeutil/eventrecorder"
"github.com/krateoplatformops/plumbing/ptr"
prettylog "github.com/krateoplatformops/plumbing/slogs/pretty"
"github.com/krateoplatformops/unstructured-runtime/pkg/logging"
"github.com/krateoplatformops/unstructured-runtime/pkg/pluralizer"
"github.com/krateoplatformops/unstructured-runtime/pkg/workqueue"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
serviceName = "composition-dynamic-controller"
defaultOtelExportInterval = 30 * time.Second
)
func main() {
// Flags
kubeconfig := flag.String("kubeconfig", env.String("KUBECONFIG", ""),
"absolute path to the kubeconfig file")
debug := flag.Bool("debug",
env.Bool("COMPOSITION_CONTROLLER_DEBUG", false), "dump verbose output")
workers := flag.Int("workers", env.Int("COMPOSITION_CONTROLLER_WORKERS", 1), "number of workers")
resyncInterval := flag.Duration("resync-interval",
env.Duration("COMPOSITION_CONTROLLER_RESYNC_INTERVAL", time.Minute*3), "resync interval")
resourceGroup := flag.String("group",
env.String("COMPOSITION_CONTROLLER_GROUP", ""), "resource api group")
resourceVersion := flag.String("version",
env.String("COMPOSITION_CONTROLLER_VERSION", ""), "resource api version")
resourceName := flag.String("resource",
env.String("COMPOSITION_CONTROLLER_RESOURCE", ""), "resource plural name")
namespace := flag.String("namespace",
env.String("COMPOSITION_CONTROLLER_NAMESPACE", ""), "namespace to watch, empty for all namespaces")
chart := flag.String("chart",
env.String("COMPOSITION_CONTROLLER_CHART", ""), "chart")
urlChartInspector := flag.String("urlChartInspector",
env.String("URL_CHART_INSPECTOR", "http://chart-inspector.krateo-system.svc.cluster.local:8081/"), "url chart inspector")
saName := flag.String("saName",
env.String("COMPOSITION_CONTROLLER_SA_NAME", ""), "service account name")
saNamespace := flag.String("saNamespace",
env.String("COMPOSITION_CONTROLLER_SA_NAMESPACE", ""), "service account namespace")
maxErrorRetryInterval := flag.Duration("max-error-retry-interval",
env.Duration("COMPOSITION_CONTROLLER_MAX_ERROR_RETRY_INTERVAL", 60*time.Second), "The maximum interval between retries when an error occurs. This should be less than the half of the poll interval.")
minErrorRetryInterval := flag.Duration("min-error-retry-interval",
env.Duration("COMPOSITION_CONTROLLER_MIN_ERROR_RETRY_INTERVAL", 1*time.Second), "The minimum interval between retries when an error occurs. This should be less than max-error-retry-interval.")
maxErrorRetry := flag.Int("max-error-retries",
env.Int("COMPOSITION_CONTROLLER_MAX_ERROR_RETRIES", 5), "How many times to retry the processing of a resource when an error occurs before giving up and dropping the resource.")
metricsServerPort := flag.Int("metrics-server-port",
env.Int("COMPOSITION_CONTROLLER_METRICS_SERVER_PORT", 0), "The address to bind the metrics server to. If empty, metrics server is disabled.")
safeReleaseName := flag.Bool("safe-release-name",
env.Bool("COMPOSITION_CONTROLLER_SAFE_RELEASE_NAME", true), "If disabled the randmom suffix is not appended in the Helm release name. This can be useful for avoid having problems with complex helm charts. The use of this option is highly discouraged, as it can lead to release name collisions.")
otelEnabled := flag.Bool("otel-enabled", env.Bool("OTEL_ENABLED", false), "Enable OTLP metrics export for provider-runtime telemetry.")
otelServiceName := flag.String("otel-service-name", serviceName, "The service name attached to exported OTLP metrics.")
otelExportInterval := flag.Duration("otel-export-interval", env.Duration("OTEL_EXPORT_INTERVAL", defaultOtelExportInterval), "The interval used to export OTLP metrics.")
deploymentName := flag.String("deployment-name", env.String("DEPLOYMENT_NAME", ""), "The deployment name for stable resource identification in metrics.")
flag.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "Flags:")
flag.PrintDefaults()
}
flag.Parse()
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
logLevel := slog.LevelInfo
if *debug {
logLevel = slog.LevelDebug
}
lh := prettylog.New(&slog.HandlerOptions{
Level: logLevel,
AddSource: false,
},
prettylog.WithDestinationWriter(os.Stderr),
prettylog.WithColor(),
prettylog.WithOutputEmptyAttrs(),
)
log := logging.NewLogrLogger(logr.FromSlogHandler(slog.New(lh).Handler()))
// Kubernetes configuration
var cfg *rest.Config
var err error
if len(*kubeconfig) > 0 {
cfg, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
} else {
cfg, err = builder.GetConfig()
}
if err != nil {
log.Error(err, "Building kubeconfig.")
os.Exit(1)
}
ctx, cancel := signal.NotifyContext(context.Background(), []os.Signal{
os.Interrupt,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGKILL,
syscall.SIGHUP,
syscall.SIGQUIT,
}...)
defer cancel()
rec, err := eventrecorder.Create(ctx, cfg, "composition-dynamic-controller", nil)
if err != nil {
log.Error(err, "Creating event recorder.")
os.Exit(1)
}
pluralizer := pluralizer.New()
var pig archive.Getter
if len(*chart) > 0 {
pig = archive.Static(*chart)
} else {
pig, err = archive.Dynamic(cfg, pluralizer)
if err != nil {
log.Error(err, "Creating chart url info getter.")
os.Exit(1)
}
}
log.WithValues("debug", *debug).
WithValues("resyncInterval", *resyncInterval).
WithValues("group", *resourceGroup).
WithValues("version", *resourceVersion).
WithValues("resource", *resourceName).
WithValues("namespace", *namespace).
WithValues("workers", *workers).
WithValues("minErrorRetryInterval", *minErrorRetryInterval).
WithValues("maxErrorRetryInterval", *maxErrorRetryInterval).
WithValues("maxErrorRetry", *maxErrorRetry).
WithValues("metricsServerPort", *metricsServerPort).
WithValues("safeReleaseName", *safeReleaseName).
Info("Starting composition dynamic controller.")
log.WithValues("deploymentName", *deploymentName).
WithValues("otelEnabled", *otelEnabled).
WithValues("otelServiceName", *otelServiceName).
WithValues("otelExportInterval", *otelExportInterval).
Info("[FLAG PARSE DEBUG] Telemetry configuration loaded from flags/env.")
telemetryEnabled := *otelEnabled
telemetryExportInterval := *otelExportInterval
telemetryMetrics, telemetryShutdown, err := telemetry.Setup(context.Background(), log, telemetry.Config{
Enabled: telemetryEnabled,
ServiceName: *otelServiceName,
ExportInterval: telemetryExportInterval,
DeploymentName: *deploymentName,
})
if err != nil {
log.Error(err, "Cannot initialize OpenTelemetry metrics")
os.Exit(1)
}
defer telemetryShutdown(context.Background())
// Initialize CDC-specific metrics
if err := metrics.InitMetrics(context.Background(), log, telemetryEnabled, *otelServiceName, telemetryExportInterval, *deploymentName); err != nil {
slog.Warn("Cannot initialize CDC metrics, continuing without metrics", "error", err)
}
metrics.DebugStatus()
// Periodic metrics status logging for monitoring during stress tests
go func() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
metrics.DebugStatus()
}
}
}()
// Create a label requirement for the composition version
labelreq, err := labels.NewRequirement(meta.CompositionVersionLabel, selection.Equals, []string{*resourceVersion})
if err != nil {
log.Error(err, "Creating label requirement.")
os.Exit(1)
}
labelselector := labels.NewSelector().Add(*labelreq)
mapper, err := dynamic.NewRESTMapper(cfg)
if err != nil {
log.Error(err, "Creating RESTMapper.")
os.Exit(1)
}
apiRecorder := event.NewAPIRecorder(rec)
if apiRecorder == nil {
log.Error(fmt.Errorf("creating API recorder"), "API recorder is nil, events will not be recorded.")
os.Exit(1)
}
handler := composition.NewHandler(&composition.HandlerOptions{
Kubeconfig: cfg,
Pluralizer: pluralizer,
PackageInfoGetter: pig,
EventRecorder: *apiRecorder,
ChartInspectorUrl: *urlChartInspector,
SaName: *saName,
SaNamespace: *saNamespace,
SafeReleaseName: *safeReleaseName,
Mapper: mapper,
})
opts := []builder.FuncOption{
builder.WithLogger(log),
builder.WithNamespace(*namespace),
builder.WithResyncInterval(*resyncInterval),
builder.WithGlobalRateLimiter(workqueue.NewExponentialTimedFailureRateLimiter[any](*minErrorRetryInterval, *maxErrorRetryInterval)),
builder.WithMaxRetries(*maxErrorRetry),
builder.WithListWatcher(controller.ListWatcherConfiguration{
LabelSelector: ptr.To(labelselector.String()),
}),
builder.WithActionEvent(ctrlevent.CRUpdated, ctrlevent.Observe),
builder.WithTelemetryMetrics(telemetryMetrics),
}
metricsServerBindAddress := ""
if ptr.Deref(metricsServerPort, 0) != 0 {
log.Info("Metrics server enabled", "bindAddress", fmt.Sprintf(":%d", *metricsServerPort))
metricsServerBindAddress = fmt.Sprintf(":%d", *metricsServerPort)
opts = append(opts, builder.WithMetrics(server.Options{
BindAddress: metricsServerBindAddress,
}))
} else {
log.Info("Metrics server disabled")
}
controller, err := builder.Build(ctx, builder.Configuration{
Config: cfg,
GVR: schema.GroupVersionResource{
Group: *resourceGroup,
Version: *resourceVersion,
Resource: *resourceName,
},
ProviderName: serviceName,
}, opts...)
if err != nil {
log.Error(err, "Creating controller.")
os.Exit(1)
}
controller.SetExternalClient(handler)
err = controller.Run(ctx, *workers)
if err != nil {
log.Error(err, "Running controller.")
os.Exit(1)
}
}