-
Notifications
You must be signed in to change notification settings - Fork 600
/
main.go
238 lines (197 loc) · 7.83 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
/*
Copyright 2020 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"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/kelseyhightower/envconfig"
"go.opencensus.io/stats/view"
"go.uber.org/zap"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/leaderelection"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
"knative.dev/pkg/profiling"
"knative.dev/pkg/reconciler"
"knative.dev/pkg/signals"
"knative.dev/pkg/source"
"knative.dev/eventing/pkg/adapter/v2/util/crstatusevent"
)
// 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
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)
logger := env.GetLogger()
defer flush(logger)
ctx = logging.WithLogger(ctx, logger)
// Report stats on Go memory usage every 30 seconds.
msp := metrics.NewMemStatsAll()
msp.Start(ctx, 30*time.Second)
if err := view.Register(msp.DefaultViews()...); err != nil {
logger.Fatal("Error exporting go memstats view: %v", zap.Error(err))
}
var crStatusEventClient *crstatusevent.CRStatusEventClient
// Convert json metrics.ExporterOptions to metrics.ExporterOptions.
if metricsConfig, err := env.GetMetricsConfig(); err != nil {
logger.Error("failed to process metrics options", zap.Error(err))
} else if metricsConfig != nil {
if err := metrics.UpdateExporter(ctx, *metricsConfig, logger); err != nil {
logger.Error("failed to create the metrics exporter", zap.Error(err))
}
// Check if metrics config contains profiling flag
if metricsConfig.ConfigMap != nil {
if enabled, err := profiling.ReadProfilingFlag(metricsConfig.ConfigMap); err == nil && enabled {
// Start a goroutine to server profiling metrics
logger.Info("Profiling enabled")
go func() {
server := profiling.NewServer(profiling.NewHandler(logger, true))
// Don't forward ErrServerClosed as that indicates we're already shutting down.
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("profiling server failed", zap.Error(err))
}
}()
}
crStatusEventClient = crstatusevent.NewCRStatusEventClient(metricsConfig.ConfigMap)
}
}
reporter, err := source.NewStatsReporter()
if err != nil {
logger.Error("error building statsreporter", zap.Error(err))
}
if err := env.SetupTracing(logger); err != nil {
// If tracing doesn't work, we will log an error, but allow the adapter
// to continue to start.
logger.Error("Error setting up trace publishing", zap.Error(err))
}
eventsClient, err := NewCloudEventsClientCRStatus(env, reporter, crStatusEventClient)
if err != nil {
logger.Fatal("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.Error("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)
}
// 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")
go controller.StartAll(ctx, ctrl)
}
// Finally start the adapter (blocking)
if err := adapter.Start(ctx); err != nil {
logging.FromContext(ctx).Errorw("Start returned an error", zap.Error(err))
}
}
func ConstructEnvOrDie(ector EnvConfigConstructor) EnvConfigAccessor {
env := ector()
if err := envconfig.Process("", env); err != nil {
log.Fatalf("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 := sharedmain.ParseAndGetConfigOrDie()
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()
}