/
kafkacluster_controller.go
429 lines (390 loc) · 16.2 KB
/
kafkacluster_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
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
// Copyright © 2019 Banzai Cloud
//
// 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 controllers
import (
"context"
"fmt"
"reflect"
"time"
"emperror.dev/errors"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"github.com/banzaicloud/k8s-objectmatcher/patch"
"github.com/banzaicloud/kafka-operator/api/v1alpha1"
"github.com/banzaicloud/kafka-operator/api/v1beta1"
"github.com/banzaicloud/kafka-operator/pkg/errorfactory"
"github.com/banzaicloud/kafka-operator/pkg/k8sutil"
"github.com/banzaicloud/kafka-operator/pkg/kafkaclient"
"github.com/banzaicloud/kafka-operator/pkg/pki"
"github.com/banzaicloud/kafka-operator/pkg/resources"
"github.com/banzaicloud/kafka-operator/pkg/resources/cruisecontrol"
"github.com/banzaicloud/kafka-operator/pkg/resources/cruisecontrolmonitoring"
"github.com/banzaicloud/kafka-operator/pkg/resources/envoy"
"github.com/banzaicloud/kafka-operator/pkg/resources/istioingress"
"github.com/banzaicloud/kafka-operator/pkg/resources/kafka"
"github.com/banzaicloud/kafka-operator/pkg/resources/kafkamonitoring"
"github.com/banzaicloud/kafka-operator/pkg/resources/nodeportexternalaccess"
"github.com/banzaicloud/kafka-operator/pkg/util"
)
var clusterFinalizer = "finalizer.kafkaclusters.kafka.banzaicloud.io"
var clusterTopicsFinalizer = "topics.kafkaclusters.kafka.banzaicloud.io"
var clusterUsersFinalizer = "users.kafkaclusters.kafka.banzaicloud.io"
// KafkaClusterReconciler reconciles a KafkaCluster object
type KafkaClusterReconciler struct {
client.Client
DirectClient client.Reader
Log logr.Logger
Scheme *runtime.Scheme
Namespaces []string
KafkaClientProvider kafkaclient.Provider
}
// Reconcile reads that state of the cluster for a KafkaCluster object and makes changes based on the state read
// and what is in the KafkaCluster.Spec
// Automatically generate RBAC rules to allow the Controller to read and write Deployments
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;delete;patch
// +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
// +kubebuilder:rbac:groups="policy",resources=poddisruptionbudgets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kafka.banzaicloud.io,resources=kafkaclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kafka.banzaicloud.io,resources=kafkaclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=istio.banzaicloud.io,resources=meshgateways,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.istio.io,resources=*,verbs=*
func (r *KafkaClusterReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("Request.Namespace", request.NamespacedName, "Request.Name", request.Name)
log.Info("Reconciling KafkaCluster")
// Fetch the KafkaCluster instance
instance := &v1beta1.KafkaCluster{}
err := r.Get(ctx, request.NamespacedName, instance)
if err != nil {
if apiErrors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
return reconciled()
}
// Error reading the object - requeue the request.
return requeueWithError(log, err.Error(), err)
}
// Check if marked for deletion and run finalizers
if k8sutil.IsMarkedForDeletion(instance.ObjectMeta) {
return r.checkFinalizers(ctx, log, instance)
}
if instance.Status.State != v1beta1.KafkaClusterRollingUpgrading {
if err := k8sutil.UpdateCRStatus(r.Client, instance, v1beta1.KafkaClusterReconciling, log); err != nil {
return requeueWithError(log, err.Error(), err)
}
}
reconcilers := []resources.ComponentReconciler{
envoy.New(r.Client, instance),
istioingress.New(r.Client, instance),
nodeportexternalaccess.New(r.Client, instance),
kafkamonitoring.New(r.Client, instance),
cruisecontrolmonitoring.New(r.Client, instance),
kafka.New(r.Client, r.DirectClient, r.Scheme, instance, r.KafkaClientProvider),
cruisecontrol.New(r.Client, instance),
}
for _, rec := range reconcilers {
err = rec.Reconcile(log)
if err != nil {
switch errors.Cause(err).(type) {
case errorfactory.BrokersUnreachable:
log.Info("Brokers unreachable, may still be starting up", "error", err.Error())
return ctrl.Result{
RequeueAfter: time.Duration(15) * time.Second,
}, nil
case errorfactory.BrokersNotReady:
log.Info("Brokers not ready, may still be starting up", "error", err.Error())
return ctrl.Result{
RequeueAfter: time.Duration(15) * time.Second,
}, nil
case errorfactory.ResourceNotReady:
log.Info("A new resource was not found or may not be ready", "error", err.Error())
return ctrl.Result{
RequeueAfter: time.Duration(7) * time.Second,
}, nil
case errorfactory.ReconcileRollingUpgrade:
log.Info("Rolling Upgrade in Progress")
return ctrl.Result{
RequeueAfter: time.Duration(15) * time.Second,
}, nil
case errorfactory.CruiseControlNotReady:
return ctrl.Result{
RequeueAfter: time.Duration(15) * time.Second,
}, nil
case errorfactory.CruiseControlTaskRunning:
return ctrl.Result{
RequeueAfter: time.Duration(20) * time.Second,
}, nil
case errorfactory.CruiseControlTaskTimeout, errorfactory.CruiseControlTaskFailure:
return ctrl.Result{
RequeueAfter: time.Duration(20) * time.Second,
}, nil
case errorfactory.PerBrokerConfigNotReady:
log.V(1).Info("dynamically updated broker configuration hasn't propagated through yet")
// for exponential backoff
return ctrl.Result{}, err
case errorfactory.LoadBalancerIPNotReady:
return ctrl.Result{
RequeueAfter: time.Duration(30) * time.Second,
}, nil
default:
return requeueWithError(log, err.Error(), err)
}
}
}
log.Info("ensuring finalizers on kafkacluster")
if instance, err = r.ensureFinalizers(ctx, instance); err != nil {
return requeueWithError(log, "failed to ensure finalizers on kafkacluster instance", err)
}
//Update rolling upgrade last successful state
if instance.Status.State == v1beta1.KafkaClusterRollingUpgrading {
if err := k8sutil.UpdateRollingUpgradeState(r.Client, instance, time.Now(), log); err != nil {
return requeueWithError(log, err.Error(), err)
}
}
if err := k8sutil.UpdateCRStatus(r.Client, instance, v1beta1.KafkaClusterRunning, log); err != nil {
return requeueWithError(log, err.Error(), err)
}
return reconciled()
}
func (r *KafkaClusterReconciler) checkFinalizers(ctx context.Context, log logr.Logger, cluster *v1beta1.KafkaCluster) (ctrl.Result, error) {
log.Info("KafkaCluster is marked for deletion, checking for children")
// If the main finalizer is gone then we've already finished up
if !util.StringSliceContains(cluster.GetFinalizers(), clusterFinalizer) {
return reconciled()
}
var err error
var namespaces []string
if r.Namespaces == nil {
// Fetch a list of all namespaces for DeleteAllOf requests
namespaces = make([]string, 0)
var namespaceList corev1.NamespaceList
if err := r.Client.List(ctx, &namespaceList); err != nil {
return requeueWithError(log, "failed to get namespace list", err)
}
for _, ns := range namespaceList.Items {
namespaces = append(namespaces, ns.Name)
}
} else {
// use configured namespaces
namespaces = r.Namespaces
}
// If we haven't deleted all kafkatopics yet, iterate namespaces and delete all kafkatopics
// with the matching label.
if util.StringSliceContains(cluster.GetFinalizers(), clusterTopicsFinalizer) {
log.Info(fmt.Sprintf("Sending delete kafkatopics request to all namespaces for cluster %s/%s", cluster.Namespace, cluster.Name))
for _, ns := range namespaces {
if err := r.Client.DeleteAllOf(
ctx,
&v1alpha1.KafkaTopic{},
client.InNamespace(ns),
client.MatchingLabels{clusterRefLabel: clusterLabelString(cluster)},
); err != nil {
if client.IgnoreNotFound(err) != nil {
return requeueWithError(log, "failed to send delete request for children kafkatopics", err)
}
log.Info(fmt.Sprintf("No matching kafkatopics in namespace: %s", ns))
}
}
if cluster, err = r.removeFinalizer(ctx, cluster, clusterTopicsFinalizer); err != nil {
return requeueWithError(log, "failed to remove topics finalizer from kafkacluster", err)
}
}
// If any of the topics still exist, it means their finalizer is still running.
// Wait to make sure we have fully cleaned up zookeeper. Also if we delete
// our kafkausers before all topics are finished cleaning up, we will lose
// our controller certificate.
log.Info("Ensuring all topics have finished cleaning up")
var childTopics v1alpha1.KafkaTopicList
if err = r.Client.List(
ctx,
&childTopics,
client.InNamespace(metav1.NamespaceAll),
client.MatchingLabels{clusterRefLabel: clusterLabelString(cluster)},
); err != nil {
return requeueWithError(log, "failed to list kafkatopics", err)
}
if len(childTopics.Items) > 0 {
log.Info(fmt.Sprintf("Still waiting for the following topics to be deleted: %v", topicListToStrSlice(childTopics)))
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Duration(3) * time.Second,
}, nil
}
// If we haven't deleted all kafkausers yet, iterate namespaces and delete all kafkausers
// with the matching label.
if util.StringSliceContains(cluster.GetFinalizers(), clusterUsersFinalizer) {
log.Info(fmt.Sprintf("Sending delete kafkausers request to all namespaces for cluster %s/%s", cluster.Namespace, cluster.Name))
for _, ns := range namespaces {
if err := r.Client.DeleteAllOf(
ctx,
&v1alpha1.KafkaUser{},
client.InNamespace(ns),
client.MatchingLabels{clusterRefLabel: clusterLabelString(cluster)},
); err != nil {
if client.IgnoreNotFound(err) != nil {
return requeueWithError(log, "failed to send delete request for children kafkausers", err)
}
log.Info(fmt.Sprintf("No matching kafkausers in namespace: %s", ns))
}
}
if cluster, err = r.removeFinalizer(ctx, cluster, clusterUsersFinalizer); err != nil {
return requeueWithError(log, "failed to remove users finalizer from kafkacluster", err)
}
}
if cluster.Spec.ListenersConfig.SSLSecrets != nil {
// Do any necessary PKI cleanup - a PKI backend should make sure any
// user finalizations are done before it does its final cleanup
log.Info("Tearing down any PKI resources for the kafkacluster")
if err = pki.GetPKIManager(r.Client, cluster, v1beta1.PKIBackendProvided, r.Log).FinalizePKI(ctx, log); err != nil {
switch err.(type) {
case errorfactory.ResourceNotReady:
log.Info("The PKI is not ready to be torn down")
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Duration(5) * time.Second,
}, nil
default:
return requeueWithError(log, "failed to finalize PKI", err)
}
}
}
log.Info("Finalizing deletion of kafkacluster instance")
if _, err = r.removeFinalizer(ctx, cluster, clusterFinalizer); err != nil {
if client.IgnoreNotFound(err) == nil {
// We may have been a requeue from earlier with all conditions met - but with
// the state of the finalizer not yet reflected in the response we got.
return reconciled()
}
return requeueWithError(log, "failed to remove main finalizer", err)
}
return ctrl.Result{}, nil
}
func topicListToStrSlice(list v1alpha1.KafkaTopicList) []string {
names := make([]string, 0)
for _, topic := range list.Items {
names = append(names, fmt.Sprintf("%s/%s", topic.Namespace, topic.Name))
}
return names
}
func (r *KafkaClusterReconciler) ensureFinalizers(ctx context.Context, cluster *v1beta1.KafkaCluster) (updated *v1beta1.KafkaCluster, err error) {
finalizers := []string{clusterFinalizer, clusterTopicsFinalizer, clusterUsersFinalizer}
for _, finalizer := range finalizers {
if util.StringSliceContains(cluster.GetFinalizers(), finalizer) {
continue
}
cluster.SetFinalizers(append(cluster.GetFinalizers(), finalizer))
}
return r.updateAndFetchLatest(ctx, cluster)
}
func (r *KafkaClusterReconciler) removeFinalizer(ctx context.Context, cluster *v1beta1.KafkaCluster, finalizer string) (updated *v1beta1.KafkaCluster, err error) {
cluster.SetFinalizers(util.StringSliceRemove(cluster.GetFinalizers(), finalizer))
return r.updateAndFetchLatest(ctx, cluster)
}
func (r *KafkaClusterReconciler) updateAndFetchLatest(ctx context.Context, cluster *v1beta1.KafkaCluster) (*v1beta1.KafkaCluster, error) {
typeMeta := cluster.TypeMeta
err := r.Client.Update(ctx, cluster)
if err != nil {
return nil, err
}
cluster.TypeMeta = typeMeta
return cluster, nil
}
// SetupKafkaClusterWithManager registers kafka cluster controller to the manager
func SetupKafkaClusterWithManager(mgr ctrl.Manager, log logr.Logger) *ctrl.Builder {
builder := ctrl.NewControllerManagedBy(mgr).
For(&v1beta1.KafkaCluster{}).Named("KafkaCluster")
kafkaWatches(builder)
envoyWatches(builder)
cruiseControlWatches(builder)
builder.WithEventFilter(
predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
object, err := meta.Accessor(e.Object)
if err != nil {
return false
}
if _, ok := object.(*v1beta1.KafkaCluster); ok {
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
object, err := meta.Accessor(e.ObjectNew)
if err != nil {
return false
}
switch object.(type) {
case *corev1.Pod, *corev1.ConfigMap, *corev1.PersistentVolumeClaim:
patchResult, err := patch.DefaultPatchMaker.Calculate(e.ObjectOld, e.ObjectNew)
if err != nil {
log.Error(err, "could not match objects", "kind", e.ObjectOld.GetObjectKind())
} else if patchResult.IsEmpty() {
return false
}
case *v1beta1.KafkaCluster:
old := e.ObjectOld.(*v1beta1.KafkaCluster)
new := e.ObjectNew.(*v1beta1.KafkaCluster)
if !reflect.DeepEqual(old.Spec, new.Spec) ||
old.GetDeletionTimestamp() != new.GetDeletionTimestamp() ||
old.GetGeneration() != new.GetGeneration() ||
!reflect.DeepEqual(old.Status.BrokersState, new.Status.BrokersState) {
return true
}
return false
}
return true
},
})
return builder
}
func kafkaWatches(builder *ctrl.Builder) *ctrl.Builder {
return builder.
Owns(&corev1.Service{}).
Owns(&corev1.ConfigMap{}).
Owns(&policyv1beta1.PodDisruptionBudget{}).
Owns(&corev1.PersistentVolumeClaim{}).
Owns(&corev1.Pod{})
}
func envoyWatches(builder *ctrl.Builder) *ctrl.Builder {
return builder.
Owns(&corev1.Service{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.ConfigMap{})
}
func cruiseControlWatches(builder *ctrl.Builder) *ctrl.Builder {
return builder.
Owns(&corev1.Service{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.ConfigMap{})
}