/
controller.go
366 lines (320 loc) · 13.3 KB
/
controller.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
/*
* Copyright 2022 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 consumergroup
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/IBM/sarama"
"github.com/kelseyhightower/envconfig"
"go.uber.org/multierr"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/client-go/tools/cache"
"knative.dev/eventing-kafka-broker/control-plane/pkg/kafka/offset"
"knative.dev/eventing-kafka-broker/control-plane/pkg/prober"
"knative.dev/eventing/pkg/scheduler"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/client/injection/kube/informers/apps/v1/statefulset"
configmapinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap"
nodeinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/node"
podinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod"
secretinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/secret"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"knative.dev/pkg/reconciler"
"knative.dev/pkg/resolver"
"knative.dev/pkg/system"
statefulsetscheduler "knative.dev/eventing/pkg/scheduler/statefulset"
"knative.dev/eventing-kafka-broker/control-plane/pkg/apis/config"
kafkainternals "knative.dev/eventing-kafka-broker/control-plane/pkg/apis/internals/kafka/eventing/v1alpha1"
internalsclient "knative.dev/eventing-kafka-broker/control-plane/pkg/client/internals/kafka/injection/client"
"knative.dev/eventing-kafka-broker/control-plane/pkg/client/internals/kafka/injection/informers/eventing/v1alpha1/consumer"
"knative.dev/eventing-kafka-broker/control-plane/pkg/client/internals/kafka/injection/informers/eventing/v1alpha1/consumergroup"
cgreconciler "knative.dev/eventing-kafka-broker/control-plane/pkg/client/internals/kafka/injection/reconciler/eventing/v1alpha1/consumergroup"
"knative.dev/eventing-kafka-broker/control-plane/pkg/counter"
kedaclient "knative.dev/eventing-kafka-broker/third_party/pkg/client/injection/client"
)
const (
// KafkaSourceScheduler is the key for the scheduler map for any KafkaSource.
// Keep this constant lowercase.
KafkaSourceScheduler = "kafkasource"
// KafkaTriggerScheduler is the key for the scheduler map for any Kafka Trigger.
// Keep this constant lowercase.
KafkaTriggerScheduler = "trigger"
// KafkaChannelScheduler is the key for the scheduler map for any KafkaChannel.
// Keep this constant lowercase.
KafkaChannelScheduler = "kafkachannel"
)
type envConfig struct {
SchedulerRefreshPeriod int64 `envconfig:"AUTOSCALER_REFRESH_PERIOD" required:"true"`
PodCapacity int32 `envconfig:"POD_CAPACITY" required:"true"`
SchedulerPolicyConfigMap string `envconfig:"SCHEDULER_CONFIG" required:"true"`
DeSchedulerPolicyConfigMap string `envconfig:"DESCHEDULER_CONFIG" required:"true"`
AutoscalerConfigMap string `envconfig:"AUTOSCALER_CONFIG" required:"true"`
}
type SchedulerConfig struct {
StatefulSetName string
RefreshPeriod time.Duration
Capacity int32
SchedulerPolicy *scheduler.SchedulerPolicy
DeSchedulerPolicy *scheduler.SchedulerPolicy
}
func NewController(ctx context.Context, watcher configmap.Watcher) *controller.Impl {
logger := logging.FromContext(ctx)
env := &envConfig{}
if err := envconfig.Process("", env); err != nil {
logger.Panicf("unable to process required environment variables: %v", err)
}
c := SchedulerConfig{
RefreshPeriod: time.Duration(env.SchedulerRefreshPeriod) * time.Second,
Capacity: env.PodCapacity,
SchedulerPolicy: schedulerPolicyFromConfigMapOrFail(ctx, env.SchedulerPolicyConfigMap),
DeSchedulerPolicy: schedulerPolicyFromConfigMapOrFail(ctx, env.DeSchedulerPolicyConfigMap),
}
schedulers := map[string]Scheduler{
KafkaSourceScheduler: createKafkaScheduler(ctx, c, kafkainternals.SourceStatefulSetName),
//KafkaTriggerScheduler: createKafkaScheduler(ctx, c, kafkainternals.BrokerStatefulSetName), //To be added with trigger/v2 reconciler version only
//KafkaChannelScheduler: createKafkaScheduler(ctx, c, kafkainternals.ChannelStatefulSetName), //To be added with channel/v2 reconciler version only
}
r := &Reconciler{
SchedulerFunc: func(s string) (Scheduler, bool) { sched, ok := schedulers[strings.ToLower(s)]; return sched, ok },
ConsumerLister: consumer.Get(ctx).Lister(),
InternalsClient: internalsclient.Get(ctx).InternalV1alpha1(),
SecretLister: secretinformer.Get(ctx).Lister(),
ConfigMapLister: configmapinformer.Get(ctx).Lister(),
PodLister: podinformer.Get(ctx).Lister(),
KubeClient: kubeclient.Get(ctx),
NameGenerator: names.SimpleNameGenerator,
NewKafkaClient: sarama.NewClient,
InitOffsetsFunc: offset.InitOffsets,
SystemNamespace: system.Namespace(),
NewKafkaClusterAdminClient: sarama.NewClusterAdmin,
KafkaFeatureFlags: config.DefaultFeaturesConfig(),
KedaClient: kedaclient.Get(ctx),
AutoscalerConfig: env.AutoscalerConfigMap,
DeleteConsumerGroupMetadataCounter: counter.NewExpiringCounter(ctx),
InitOffsetLatestInitialOffsetCache: prober.NewLocalExpiringCache(ctx, 20*time.Minute),
}
consumerInformer := consumer.Get(ctx)
consumerGroupInformer := consumergroup.Get(ctx)
impl := cgreconciler.NewImpl(ctx, r, func(impl *controller.Impl) controller.Options {
return controller.Options{
PromoteFunc: func(bkt reconciler.Bucket) {
for _, value := range schedulers {
if ss, ok := value.Scheduler.(*statefulsetscheduler.StatefulSetScheduler); ok {
ss.Promote(bkt, nil)
}
}
},
DemoteFunc: func(bkt reconciler.Bucket) {
for _, value := range schedulers {
if ss, ok := value.Scheduler.(*statefulsetscheduler.StatefulSetScheduler); ok {
ss.Demote(bkt)
}
}
},
}
})
r.Resolver = resolver.NewURIResolverFromTracker(ctx, impl.Tracker)
r.EnqueueKey = func(key string) {
parts := strings.SplitN(key, string(types.Separator), 3)
if len(parts) != 2 {
panic(fmt.Sprintf("Expected <namespace>/<name> format, got %s", key))
}
impl.EnqueueKey(types.NamespacedName{Namespace: parts[0], Name: parts[1]})
}
configStore := config.NewStore(ctx, func(name string, value *config.KafkaFeatureFlags) {
r.KafkaFeatureFlags.Reset(value)
impl.GlobalResync(consumerGroupInformer.Informer())
})
configStore.WatchConfigs(watcher)
consumerGroupInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: controller.PassNew(impl.Enqueue),
DeleteFunc: func(obj interface{}) {
impl.Enqueue(obj)
if cg, ok := obj.(metav1.Object); ok && cg != nil {
r.InitOffsetLatestInitialOffsetCache.Expire(keyOf(cg))
}
},
})
consumerInformer.Informer().AddEventHandler(controller.HandleAll(enqueueConsumerGroupFromConsumer(impl.EnqueueKey)))
globalResync := func(interface{}) {
impl.GlobalResync(consumerGroupInformer.Informer())
}
ResyncOnStatefulSetChange(ctx, globalResync)
//Todo: ScaledObject informer when KEDA is installed
return impl
}
func ResyncOnStatefulSetChange(ctx context.Context, handle func(interface{})) {
systemNamespace := system.Namespace()
statefulset.Get(ctx).Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
ss := obj.(*appsv1.StatefulSet)
return ss.GetNamespace() == systemNamespace && kafkainternals.IsKnownStatefulSet(ss.GetName())
},
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: handle,
UpdateFunc: func(oldObj, newObj interface{}) {
o, ok := oldObj.(*appsv1.StatefulSet)
if !ok {
return
}
n, ok := newObj.(*appsv1.StatefulSet)
if !ok {
return
}
// This should never happen, but we check for nil to be sure.
if o.Spec.Replicas == nil || n.Spec.Replicas == nil {
return
}
// Only handle when replicas change
if *o.Spec.Replicas == *n.Spec.Replicas && o.Status.ReadyReplicas == n.Status.ReadyReplicas {
return
}
handle(newObj)
},
DeleteFunc: handle,
},
})
}
func enqueueConsumerGroupFromConsumer(enqueue func(name types.NamespacedName)) func(obj interface{}) {
return func(obj interface{}) {
c, ok := obj.(*kafkainternals.Consumer)
if ok {
or := c.GetConsumerGroup()
if or != nil {
enqueue(types.NamespacedName{Namespace: c.GetNamespace(), Name: or.Name})
}
}
}
}
func createKafkaScheduler(ctx context.Context, c SchedulerConfig, ssName string) Scheduler {
lister := consumergroup.Get(ctx).Lister()
return createStatefulSetScheduler(
ctx,
SchedulerConfig{
StatefulSetName: ssName,
RefreshPeriod: c.RefreshPeriod,
Capacity: c.Capacity,
SchedulerPolicy: c.SchedulerPolicy,
DeSchedulerPolicy: c.DeSchedulerPolicy,
},
func() ([]scheduler.VPod, error) {
consumerGroups, err := lister.List(labels.SelectorFromSet(getSelectorLabel(ssName)))
if err != nil {
return nil, err
}
vpods := make([]scheduler.VPod, len(consumerGroups))
for i := 0; i < len(consumerGroups); i++ {
vpods[i] = consumerGroups[i]
}
return vpods, nil
},
)
}
func getSelectorLabel(ssName string) map[string]string {
//TODO remove hardcoded kinds
var selectorLabel map[string]string
switch ssName {
case kafkainternals.SourceStatefulSetName:
selectorLabel = map[string]string{
kafkainternals.UserFacingResourceLabelSelector: "kafkasource",
}
case kafkainternals.BrokerStatefulSetName:
selectorLabel = map[string]string{
kafkainternals.UserFacingResourceLabelSelector: "trigger",
}
case kafkainternals.ChannelStatefulSetName:
selectorLabel = map[string]string{
kafkainternals.UserFacingResourceLabelSelector: "kafkachannel",
}
}
return selectorLabel
}
func createStatefulSetScheduler(ctx context.Context, c SchedulerConfig, lister scheduler.VPodLister) Scheduler {
ss, _ := statefulsetscheduler.New(ctx, &statefulsetscheduler.Config{
StatefulSetNamespace: system.Namespace(),
StatefulSetName: c.StatefulSetName,
PodCapacity: c.Capacity,
RefreshPeriod: c.RefreshPeriod,
SchedulerPolicy: scheduler.MAXFILLUP,
SchedPolicy: c.SchedulerPolicy,
DeschedPolicy: c.DeSchedulerPolicy,
Evictor: newEvictor(ctx, zap.String("kafka.eventing.knative.dev/component", "evictor")).evict,
VPodLister: lister,
NodeLister: nodeinformer.Get(ctx).Lister(),
})
return Scheduler{
Scheduler: ss,
SchedulerConfig: c,
}
}
// schedulerPolicyFromConfigMapOrFail reads predicates and priorities data from configMap
func schedulerPolicyFromConfigMapOrFail(ctx context.Context, configMapName string) *scheduler.SchedulerPolicy {
p, err := schedulerPolicyFromConfigMap(ctx, configMapName)
if err != nil {
logging.FromContext(ctx).Fatal(zap.Error(err))
}
return p
}
// schedulerPolicyFromConfigMap reads predicates and priorities data from configMap
func schedulerPolicyFromConfigMap(ctx context.Context, configMapName string) (*scheduler.SchedulerPolicy, error) {
policyConfigMap, err := kubeclient.Get(ctx).CoreV1().ConfigMaps(system.Namespace()).Get(ctx, configMapName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("couldn't get scheduler policy config map %s/%s: %v", system.Namespace(), configMapName, err)
}
logger := logging.FromContext(ctx).
Desugar().
With(zap.String("configmap", configMapName))
policy := &scheduler.SchedulerPolicy{}
preds, found := policyConfigMap.Data["predicates"]
if !found {
return nil, fmt.Errorf("missing policy config map %s/%s value at key predicates", system.Namespace(), configMapName)
}
if err := json.NewDecoder(strings.NewReader(preds)).Decode(&policy.Predicates); err != nil {
return nil, fmt.Errorf("invalid policy %v: %v", preds, err)
}
priors, found := policyConfigMap.Data["priorities"]
if !found {
return nil, fmt.Errorf("missing policy config map value at key priorities")
}
if err := json.NewDecoder(strings.NewReader(priors)).Decode(&policy.Priorities); err != nil {
return nil, fmt.Errorf("invalid policy %v: %v", preds, err)
}
if errs := validatePolicy(policy); errs != nil {
return nil, multierr.Combine(err)
}
logger.Info("Schedulers policy registration", zap.Any("policy", policy))
return policy, nil
}
func validatePolicy(policy *scheduler.SchedulerPolicy) []error {
var validationErrors []error
for _, priority := range policy.Priorities {
if priority.Weight < scheduler.MinWeight || priority.Weight > scheduler.MaxWeight {
validationErrors = append(validationErrors, fmt.Errorf("priority %s should have a positive weight applied to it or it has overflown", priority.Name))
}
}
return validationErrors
}