This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
main.go
181 lines (155 loc) · 6.26 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
/*
Copyright 2019 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 main
import (
"flag"
"log"
"github.com/knative/eventing/pkg/tracing"
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters).
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
informers "github.com/knative/eventing-contrib/kafka/channel/pkg/client/informers/externalversions"
"github.com/knative/eventing-contrib/kafka/channel/pkg/dispatcher"
"github.com/knative/eventing-contrib/kafka/channel/pkg/reconciler"
kafkachannel "github.com/knative/eventing-contrib/kafka/channel/pkg/reconciler/dispatcher"
"github.com/knative/eventing-contrib/kafka/channel/pkg/utils"
"github.com/knative/eventing/pkg/logconfig"
"go.uber.org/zap"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"knative.dev/pkg/configmap"
kncontroller "knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"
)
var (
hardcodedLoggingConfig = flag.Bool("hardCodedLoggingConfig", false, "If true, use the hard coded logging config. It is intended to be used only when debugging outside a Kubernetes cluster.")
masterURL = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
)
func main() {
flag.Parse()
logger, atomicLevel := setupLogger()
defer logger.Sync()
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
cfg, err := clientcmd.BuildConfigFromFlags(*masterURL, *kubeconfig)
if err != nil {
logger.Fatalw("Error building kubeconfig", zap.Error(err))
}
kafkaConfig, err := utils.GetKafkaConfig("/etc/config-kafka")
if err != nil {
logger.Fatalw("Error loading kafka config", zap.Error(err))
}
args := &dispatcher.KafkaDispatcherArgs{
ClientID: "kafka-ch-dispatcher",
Brokers: kafkaConfig.Brokers,
ConsumerMode: kafkaConfig.ConsumerMode,
TopicFunc: utils.TopicName,
Logger: logger.Desugar(),
}
kafkaDispatcher, err := dispatcher.NewDispatcher(args)
if err != nil {
logger.Fatalw("Unable to create kafka dispatcher", zap.Error(err))
}
logger = logger.With(zap.String("controller/impl", "pkg"))
logger.Info("Starting the Kafka dispatcher")
const numControllers = 1
cfg.QPS = numControllers * rest.DefaultQPS
cfg.Burst = numControllers * rest.DefaultBurst
opt := reconciler.NewOptionsOrDie(cfg, logger, stopCh)
messagingInformerFactory := informers.NewSharedInformerFactory(opt.KafkaClientSet, opt.ResyncPeriod)
// Messaging
kafkaChannelInformer := messagingInformerFactory.Messaging().V1alpha1().KafkaChannels()
// Build all of our controllers, with the clients constructed above.
// Add new controllers to this array.
// You also need to modify numControllers above to match this.
controllers := [...]*kncontroller.Impl{
kafkachannel.NewController(
opt,
kafkaDispatcher,
kafkaChannelInformer,
),
}
// This line asserts at compile time that the length of controllers is equal to numControllers.
// It is based on https://go101.org/article/tips.html#assert-at-compile-time, which notes that
// var _ [N-M]int
// asserts at compile time that N >= M, which we can use to establish equality of N and M:
// (N >= M) && (M >= N) => (N == M)
var _ [numControllers - len(controllers)][len(controllers) - numControllers]int
// Watch the logging config map and dynamically update logging levels.
opt.ConfigMapWatcher.Watch(logconfig.ConfigMapName(), logging.UpdateLevelFromConfigMap(logger, atomicLevel, logconfig.Controller))
// TODO: Watch the observability config map and dynamically update metrics exporter.
//opt.ConfigMapWatcher.Watch(metrics.ObservabilityConfigName, metrics.UpdateExporterFromConfigMap(component, logger))
// Setup zipkin tracing.
if err = tracing.SetupDynamicZipkinPublishing(logger, opt.ConfigMapWatcher, "kafka-ch-dispatcher"); err != nil {
logger.Fatalw("Error setting up Zipkin publishing", zap.Error(err))
}
if err := opt.ConfigMapWatcher.Start(stopCh); err != nil {
logger.Fatalw("failed to start configuration manager", zap.Error(err))
}
// Start all of the informers and wait for them to sync.
logger.Info("Starting informers.")
if err := kncontroller.StartInformers(
stopCh,
// Messaging
kafkaChannelInformer.Informer(),
); err != nil {
logger.Fatalf("Failed to start informers: %v", err)
}
logger.Info("Starting dispatcher.")
go kafkaDispatcher.Start(stopCh)
logger.Info("Starting controllers.")
kncontroller.StartAll(stopCh, controllers[:]...)
}
func setupLogger() (*zap.SugaredLogger, zap.AtomicLevel) {
// Set up our logger.
loggingConfigMap := getLoggingConfigOrDie()
loggingConfig, err := logging.NewConfigFromMap(loggingConfigMap)
if err != nil {
log.Fatalf("Error parsing logging configuration: %v", err)
}
return logging.NewLoggerFromConfig(loggingConfig, logconfig.Controller)
}
func getLoggingConfigOrDie() map[string]string {
if hardcodedLoggingConfig != nil && *hardcodedLoggingConfig {
return map[string]string{
"loglevel.controller": "info",
"zap-logger-config": `
{
"level": "info",
"development": false,
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"encoding": "json",
"encoderConfig": {
"timeKey": "ts",
"levelKey": "level",
"nameKey": "logger",
"callerKey": "caller",
"messageKey": "msg",
"stacktraceKey": "stacktrace",
"lineEnding": "",
"levelEncoder": "",
"timeEncoder": "iso8601",
"durationEncoder": "",
"callerEncoder": ""
}`,
}
} else {
cm, err := configmap.Load("/etc/config-logging")
if err != nil {
log.Fatalf("Error loading logging configuration: %v", err)
}
return cm
}
}