-
Notifications
You must be signed in to change notification settings - Fork 577
/
main.go
442 lines (365 loc) · 14.7 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
Copyright 2021 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package adapter
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"sync"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"knative.dev/pkg/tracing"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/configmap"
cminformer "knative.dev/pkg/configmap/informer"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/leaderelection"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
"knative.dev/pkg/reconciler"
"knative.dev/pkg/signals"
"knative.dev/eventing/pkg/adapter/v2/util/crstatusevent"
"knative.dev/eventing/pkg/metrics/source"
)
// Adapter is the interface receive adapters are expected to implement
type Adapter interface {
Start(ctx context.Context) error
}
type AdapterConstructor func(ctx context.Context, env EnvConfigAccessor, client cloudevents.Client) Adapter
// ControllerConstructor is the function signature for creating controllers synchronizing
// the multi-tenant receive adapter state
type ControllerConstructor func(ctx context.Context, adapter Adapter) *controller.Impl
// LoggerConfigurator configures the logger for an adapter.
type LoggerConfigurator interface {
CreateLogger(ctx context.Context) *zap.SugaredLogger
}
// MetricsExporterConfigurator configures the metrics exporter for an adapter.
type MetricsExporterConfigurator interface {
SetupMetricsExporter(ctx context.Context)
}
// TracingConfiguration for adapters.
type TracingConfiguration struct {
InstanceName string
}
// TracingConfigurator configures the tracing settings for an adapter.
type TracingConfigurator interface {
SetupTracing(ctx context.Context, cfg *TracingConfiguration) tracing.Tracer
}
// ObservabilityConfigurator groups the observability related methods
// that configure an adapter.
type ObservabilityConfigurator interface {
LoggerConfigurator
MetricsExporterConfigurator
TracingConfigurator
}
// ProfilerConfigurator configures the profiling settings for an adapter.
type ProfilerConfigurator interface {
CreateProfilingServer(ctx context.Context) *http.Server
}
// CloudEventsStatusReporterConfigurator configures the CloudEvents client reporting
// settings for an adapter.
type CloudEventsStatusReporterConfigurator interface {
CreateCloudEventsStatusReporter(ctx context.Context) *crstatusevent.CRStatusEventClient
}
// AdapterConfigurator exposes methods for configuring the adapter.
type AdapterConfigurator interface {
ObservabilityConfigurator
ProfilerConfigurator
CloudEventsStatusReporterConfigurator
}
type injectorEnabledKey struct{}
// WithInjectorEnabled signals to MainWithInjectors that it should try to run injectors.
// TODO: deprecated. Use WithController instead
func WithInjectorEnabled(ctx context.Context) context.Context {
return context.WithValue(ctx, injectorEnabledKey{}, struct{}{})
}
// IsInjectorEnabled checks the context for the desire to enable injectors
// TODO: deprecated.
func IsInjectorEnabled(ctx context.Context) bool {
val := ctx.Value(injectorEnabledKey{})
return val != nil
}
func Main(component string, ector EnvConfigConstructor, ctor AdapterConstructor) {
ctx := signals.NewContext()
MainWithContext(ctx, component, ector, ctor)
}
func MainWithContext(ctx context.Context, component string, ector EnvConfigConstructor, ctor AdapterConstructor) {
MainWithEnv(ctx, component, ConstructEnvOrDie(ector), ctor)
}
func MainWithEnv(ctx context.Context, component string, env EnvConfigAccessor, ctor AdapterConstructor) {
if flag.Lookup("disable-ha") == nil {
flag.Bool("disable-ha", false, "Whether to disable high-availability functionality for this component.")
}
if ControllerFromContext(ctx) != nil || IsInjectorEnabled(ctx) {
ictx, informers := SetupInformers(ctx, env.GetLogger())
if informers != nil {
StartInformers(ctx, informers) // none-blocking
}
ctx = ictx
}
if !flag.Parsed() {
flag.Parse()
}
b, err := strconv.ParseBool(flag.Lookup("disable-ha").Value.String())
if err != nil || b {
ctx = withHADisabledFlag(ctx)
}
MainWithInformers(ctx, component, env, ctor)
}
func MainWithInformers(ctx context.Context, component string, env EnvConfigAccessor, ctor AdapterConstructor) {
if !flag.Parsed() {
flag.Parse()
}
env.SetComponent(component)
// If not explicitly set, use the namespace from the environment variable.
if NamespaceFromContext(ctx) == "" {
ctx = WithNamespace(ctx, env.GetNamespace())
}
// If required a ConfigMap watcher is made available for configuration, either at this
// shared main function or at the downstream adapter's code.
if IsConfigWatcherEnabled(ctx) {
if cmw := ConfigWatcherFromContext(ctx); cmw == nil {
ctx = WithConfigWatcher(ctx, SetupConfigMapWatch(ctx))
}
}
// The adapter configurator is used to setup and customize the adapter behavior
configurator := newConfigurator(env, ConfiguratorOptionsFromContext(ctx)...)
logger := configurator.CreateLogger(ctx)
defer flush(logger)
// Flush any previous initial logger and replace with the
// one created from the configurator.
prev := logging.FromContext(ctx)
_ = prev.Sync()
ctx = logging.WithLogger(ctx, logger)
configurator.SetupMetricsExporter(ctx)
// Report stats on Go memory usage.
metrics.MemStatsOrDie(ctx)
// Create a profiling server based on configuration.
if ps := configurator.CreateProfilingServer(ctx); ps != nil {
go func() {
// Don't forward ErrServerClosed as that indicates we're already shutting down.
if err := ps.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Errorw("profiling server failed", zap.Error(err))
}
}()
}
tracer := configurator.SetupTracing(ctx, &TracingConfiguration{InstanceName: env.GetName()})
defer tracer.Shutdown(context.Background())
crStatusEventClient := configurator.CreateCloudEventsStatusReporter(ctx)
reporter, err := source.NewStatsReporter()
if err != nil {
logger.Errorw("Error building statsreporter", zap.Error(err))
}
eventsClient, err := NewCloudEventsClientCRStatus(env, reporter, crStatusEventClient)
if err != nil {
logger.Fatalw("Error building cloud event client", zap.Error(err))
}
// Configuring the adapter
adapter := ctor(ctx, env, eventsClient)
// Build the leader elector
leConfig, err := env.GetLeaderElectionConfig()
if err != nil {
logger.Errorw("Error loading the leader election configuration", zap.Error(err))
}
if !isHADisabledFlag(ctx) && IsHAEnabled(ctx) {
// Signal that we are executing in a context with leader election.
logger.Info("Leader election mode enabled")
ctx = leaderelection.WithStandardLeaderElectorBuilder(ctx, kubeclient.Get(ctx), *leConfig)
}
wg := sync.WaitGroup{}
// Create and start controller is needed
if ctor := ControllerFromContext(ctx); ctor != nil {
ctrl := ctor(ctx, adapter)
if leaderelection.HasLeaderElection(ctx) {
// the reconciler MUST implement LeaderAware.
if _, ok := ctrl.Reconciler.(reconciler.LeaderAware); !ok {
log.Fatalf("%T is not leader-aware, all reconcilers must be leader-aware to enable fine-grained leader election.", ctrl.Reconciler)
}
}
logger.Info("Starting controller")
wg.Add(1)
go func() {
defer wg.Done()
controller.StartAll(ctx, ctrl)
}()
}
// Finally start the adapter (blocking)
if err := adapter.Start(ctx); err != nil {
logger.Fatalw("Start returned an error", zap.Error(err))
}
wg.Wait()
}
func ConstructEnvOrDie(ector EnvConfigConstructor) EnvConfigAccessor {
env := ector()
if err := envconfig.Process("", env); err != nil {
log.Panicf("Error processing env var: %s", err)
}
return env
}
func SetupInformers(ctx context.Context, logger *zap.SugaredLogger) (context.Context, []controller.Informer) {
// Run the injectors, but only if strictly necessary to relax the dependency on kubeconfig.
if len(injection.Default.GetInformers()) > 0 || len(injection.Default.GetClients()) > 0 ||
len(injection.Default.GetDucks()) > 0 || len(injection.Default.GetInformerFactories()) > 0 {
logger.Infof("Registering %d clients", len(injection.Default.GetClients()))
logger.Infof("Registering %d informer factories", len(injection.Default.GetInformerFactories()))
logger.Infof("Registering %d informers", len(injection.Default.GetInformers()))
logger.Infof("Registering %d ducks", len(injection.Default.GetDucks()))
cfg := injection.ParseAndGetRESTConfigOrDie()
return injection.Default.SetupInformers(ctx, cfg)
}
return ctx, nil
}
func StartInformers(ctx context.Context, informers []controller.Informer) {
go func() {
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
panic(fmt.Sprint("Failed to start informers - ", err))
}
<-ctx.Done()
}()
}
func flush(logger *zap.SugaredLogger) {
_ = logger.Sync()
metrics.FlushExporter()
}
// GetConfigMapByPolling retrieves a ConfigMap.
// If an error other than NotFound is returned, the operation will be repeated
// each second up to 5 seconds.
// These timeout and retry interval are set by heuristics.
// e.g. istio sidecar needs a few seconds to configure the pod network.
//
// The context is expected to be initialized with injection and namespace.
func GetConfigMapByPolling(ctx context.Context, name string) (cm *corev1.ConfigMap, err error) {
err = wait.PollImmediate(1*time.Second, 5*time.Second, func() (bool, error) {
cm, err = kubeclient.Get(ctx).
CoreV1().ConfigMaps(NamespaceFromContext(ctx)).
Get(ctx, name, metav1.GetOptions{})
return err == nil || apierrors.IsNotFound(err), nil
})
if err != nil {
err = fmt.Errorf("timed out waiting trying to retrieve ConfigMap: %w", err)
}
return cm, err
}
// ConfigMapWatchOptions are the options that can be set for
// the ConfigMapWatch informer.
type ConfigMapWatchOptions struct {
LabelsFilter []labels.Requirement
}
// ConfigMapWatchOption modifies setup for a ConfigMap informer.
type ConfigMapWatchOption func(*ConfigMapWatchOptions)
// ConfigMapWatchWithLabels sets the labels filter to be
// configured at the ConfigMap watcher informer.
func ConfigMapWatchWithLabels(ls []labels.Requirement) ConfigMapWatchOption {
return func(opts *ConfigMapWatchOptions) {
opts.LabelsFilter = ls
}
}
// SetupConfigMapWatch establishes a watch on a namespace's configmaps.
func SetupConfigMapWatch(ctx context.Context, opts ...ConfigMapWatchOption) configmap.Watcher {
o := &ConfigMapWatchOptions{}
for _, opt := range opts {
opt(o)
}
return cminformer.NewInformedWatcher(kubeclient.Get(ctx), NamespaceFromContext(ctx), o.LabelsFilter...)
}
// SecretFetcher provides a helper function to fetch individual Kubernetes
// Secrets (for example, a key for client-side TLS). Note that this is not
// intended for high-volume usage; the current use is when establishing a
// metrics client connection in WatchObservabilityConfigOrDie.
// This method requires that the Namespace has been added to the context.
func SecretFetcher(ctx context.Context) metrics.SecretFetcher {
// NOTE: Do not use secrets.Get(ctx) here to get a SecretLister, as it will register
// a *global* SecretInformer and require cluster-level `secrets.list` permission,
// even if you scope down the Lister to a given namespace after requesting it. Instead,
// we package up a function from kubeclient.
// TODO(evankanderson): If this direct request to the apiserver on each TLS connection
// to the opencensus agent is too much load, switch to a cached Secret.
return func(name string) (*corev1.Secret, error) {
return kubeclient.Get(ctx).CoreV1().Secrets(NamespaceFromContext(ctx)).Get(ctx, name, metav1.GetOptions{})
}
}
// adapterConfigurator hosts the range of configurators that
// will be used when setting up the adapter.
type adapterConfigurator struct {
LoggerConfigurator
MetricsExporterConfigurator
TracingConfigurator
ProfilerConfigurator
CloudEventsStatusReporterConfigurator
}
// ConfiguratorOption enables customizing the adapter configuration.
type ConfiguratorOption func(*adapterConfigurator)
// WithLoggerConfigurator sets the adapter configurator with
// a custom logger option.
func WithLoggerConfigurator(c LoggerConfigurator) ConfiguratorOption {
return func(acfg *adapterConfigurator) {
acfg.LoggerConfigurator = c
}
}
// WithMetricsExporterConfigurator sets the adapter configurator with
// a custom metrics exporter option.
func WithMetricsExporterConfigurator(c MetricsExporterConfigurator) ConfiguratorOption {
return func(acfg *adapterConfigurator) {
acfg.MetricsExporterConfigurator = c
}
}
// WithTracingConfigurator sets the adapter configurator with
// a custom tracing option.
func WithTracingConfigurator(c TracingConfigurator) ConfiguratorOption {
return func(acfg *adapterConfigurator) {
acfg.TracingConfigurator = c
}
}
// WithProfilerConfigurator sets the adapter configurator with
// a custom profiler option.
func WithProfilerConfigurator(c ProfilerConfigurator) ConfiguratorOption {
return func(acfg *adapterConfigurator) {
acfg.ProfilerConfigurator = c
}
}
// WithCloudEventsStatusReporterConfigurator sets the adapter configurator with
// a CloudEvents status reporter option.
func WithCloudEventsStatusReporterConfigurator(c CloudEventsStatusReporterConfigurator) ConfiguratorOption {
return func(acfg *adapterConfigurator) {
acfg.CloudEventsStatusReporterConfigurator = c
}
}
// newConfigurator creates an adapter configurator that defaults to environment variable based
// internal configurators, and can be overridden to use custom ones.
func newConfigurator(env EnvConfigAccessor, opts ...ConfiguratorOption) AdapterConfigurator {
// default to environment variable based configurators
acfg := &adapterConfigurator{
LoggerConfigurator: NewLoggerConfiguratorFromEnvironment(env),
MetricsExporterConfigurator: NewMetricsExporterConfiguratorFromEnvironment(env),
TracingConfigurator: NewTracingConfiguratorFromEnvironment(env),
ProfilerConfigurator: NewProfilerConfiguratorFromEnvironment(env),
CloudEventsStatusReporterConfigurator: NewCloudEventsStatusReporterConfiguratorFromEnvironment(env),
}
// override with user defined options
for _, opt := range opts {
opt(acfg)
}
return acfg
}
var _ AdapterConfigurator = (*adapterConfigurator)(nil)