forked from fluent/fluent-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluentbit_controller.go
318 lines (280 loc) · 9.75 KB
/
fluentbit_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
/*
Copyright 2021.
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"
rbacv1 "k8s.io/api/rbac/v1"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
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/controller/controllerutil"
fluentbitv1alpha2 "github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2"
"github.com/fluent/fluent-operator/v2/pkg/operator"
)
// FluentBitReconciler reconciles a FluentBit object
type FluentBitReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
ContainerLogRealPath string
Namespaced bool
}
// +kubebuilder:rbac:groups=fluentbit.fluent.io,resources=fluentbits;fluentbitconfigs;inputs;filters;outputs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterroles,verbs=create
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings,verbs=create
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the FluentBit object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *FluentBitReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("fluent-bit", req.NamespacedName)
var fb fluentbitv1alpha2.FluentBit
if err := r.Get(ctx, req.NamespacedName, &fb); err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
if fb.IsBeingDeleted() {
if err := r.handleFinalizer(ctx, &fb); err != nil {
return ctrl.Result{}, fmt.Errorf("error when handling finalizer: %v", err)
}
return ctrl.Result{}, nil
}
if !fb.HasFinalizer(fluentbitv1alpha2.FluentBitFinalizerName) {
if err := r.addFinalizer(ctx, &fb); err != nil {
return ctrl.Result{}, fmt.Errorf("error when adding finalizer: %v", err)
}
return ctrl.Result{}, nil
}
// Check if Secret exists and requeue when not found
var sec corev1.Secret
if err := r.Get(ctx, client.ObjectKey{Namespace: fb.Namespace, Name: fb.Spec.FluentBitConfigName}, &sec); err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, err
}
// Install RBAC resources for the filter plugin kubernetes
var role, sa, binding client.Object
if r.Namespaced {
role, sa, binding = operator.MakeScopedRBACObjects(fb.Name, fb.Namespace, fb.Spec.ServiceAccountAnnotations)
} else {
role, sa, binding = operator.MakeRBACObjects(fb.Name, fb.Namespace, "fluent-bit", fb.Spec.RBACRules, fb.Spec.ServiceAccountAnnotations)
}
if _, err := controllerutil.CreateOrPatch(ctx, r.Client, role, r.mutate(role, &fb)); err != nil {
return ctrl.Result{}, err
}
if _, err := controllerutil.CreateOrPatch(ctx, r.Client, sa, r.mutate(sa, &fb)); err != nil {
return ctrl.Result{}, err
}
if _, err := controllerutil.CreateOrPatch(ctx, r.Client, binding, r.mutate(binding, &fb)); err != nil {
return ctrl.Result{}, err
}
// Deploy Fluent Bit DaemonSet
logPath := r.getContainerLogPath(fb)
ds := operator.MakeDaemonSet(fb, logPath)
if _, err := controllerutil.CreateOrPatch(ctx, r.Client, ds, r.mutate(ds, &fb)); err != nil {
return ctrl.Result{}, err
}
// Deploy FluentBit Service
if !fb.Spec.DisableService {
svc := operator.MakeFluentbitService(fb)
if _, err := controllerutil.CreateOrPatch(ctx, r.Client, svc, r.mutate(svc, &fb)); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}
func (r *FluentBitReconciler) getContainerLogPath(fb fluentbitv1alpha2.FluentBit) string {
if fb.Spec.ContainerLogRealPath != "" {
return fb.Spec.ContainerLogRealPath
} else if r.ContainerLogRealPath != "" {
return r.ContainerLogRealPath
} else {
return "/var/lib/docker/containers"
}
}
func (r *FluentBitReconciler) mutate(obj client.Object, fb *fluentbitv1alpha2.FluentBit) controllerutil.MutateFn {
logPath := r.getContainerLogPath(*fb)
switch o := obj.(type) {
case *appsv1.DaemonSet:
expected := operator.MakeDaemonSet(*fb, logPath)
return func() error {
o.Labels = expected.Labels
o.Annotations = expected.Annotations
o.Spec = expected.Spec
if err := ctrl.SetControllerReference(fb, o, r.Scheme); err != nil {
return err
}
return nil
}
case *corev1.Service:
expected := operator.MakeFluentbitService(*fb)
return func() error {
o.Labels = expected.Labels
o.Spec.Selector = expected.Spec.Selector
o.Spec.Ports = expected.Spec.Ports
if err := ctrl.SetControllerReference(fb, o, r.Scheme); err != nil {
return err
}
return nil
}
case *rbacv1.Role:
expected, _, _ := operator.MakeScopedRBACObjects(fb.Name, fb.Namespace, fb.Spec.ServiceAccountAnnotations)
return func() error {
o.Rules = expected.Rules
if err := ctrl.SetControllerReference(fb, o, r.Scheme); err != nil {
return err
}
return nil
}
case *rbacv1.ClusterRole:
expected, _, _ := operator.MakeRBACObjects(fb.Name, fb.Namespace, "fluent-bit", fb.Spec.RBACRules, fb.Spec.ServiceAccountAnnotations)
return func() error {
o.Rules = expected.Rules
return nil
}
case *corev1.ServiceAccount:
_, expected, _ := operator.MakeScopedRBACObjects(fb.Name, fb.Namespace, fb.Spec.ServiceAccountAnnotations)
return func() error {
o.Annotations = expected.Annotations
if err := ctrl.SetControllerReference(fb, o, r.Scheme); err != nil {
return err
}
return nil
}
case *rbacv1.RoleBinding:
_, _, expected := operator.MakeScopedRBACObjects(fb.Name, fb.Namespace, fb.Spec.ServiceAccountAnnotations)
return func() error {
o.Subjects = expected.Subjects
o.RoleRef = expected.RoleRef
if err := ctrl.SetControllerReference(fb, o, r.Scheme); err != nil {
return err
}
return nil
}
case *rbacv1.ClusterRoleBinding:
_, _, expected := operator.MakeRBACObjects(fb.Name, fb.Namespace, "fluent-bit", fb.Spec.RBACRules, fb.Spec.ServiceAccountAnnotations)
return func() error {
o.Subjects = expected.Subjects
o.RoleRef = expected.RoleRef
return nil
}
default:
}
return nil
}
func (r *FluentBitReconciler) delete(ctx context.Context, fb *fluentbitv1alpha2.FluentBit) error {
sa := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: fb.Name,
Namespace: fb.Namespace,
},
}
if err := r.Delete(ctx, &sa); err != nil && !errors.IsNotFound(err) {
return err
}
if r.Namespaced {
roleName, _, roleBindingName := operator.MakeScopedRBACNames(fb.Name)
role := rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: roleName,
Namespace: fb.Namespace,
},
}
if err := r.Delete(ctx, &role); err != nil && !errors.IsNotFound(err) {
return err
}
rolebinding := rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: roleBindingName,
Namespace: fb.Namespace,
},
}
if err := r.Delete(ctx, &rolebinding); err != nil && !errors.IsNotFound(err) {
return err
}
}
// TODO: clusterrole, clusterrolebinding
ds := appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: fb.Name,
Namespace: fb.Namespace,
},
}
if err := r.Delete(ctx, &ds); err != nil && !errors.IsNotFound(err) {
return err
}
svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: fb.Name,
Namespace: fb.Namespace,
},
}
if err := r.Delete(ctx, &svc); err != nil && !errors.IsNotFound(err) {
return err
}
return nil
}
func (r *FluentBitReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.ServiceAccount{}, fluentbitOwnerKey, func(rawObj client.Object) []string {
// grab the job object, extract the owner.
sa := rawObj.(*corev1.ServiceAccount)
owner := metav1.GetControllerOf(sa)
if owner == nil {
return nil
}
// Make sure it's a FluentBit. If so, return it.
if owner.APIVersion != fluentbitApiGVStr || owner.Kind != "FluentBit" {
return nil
}
return []string{owner.Name}
}); err != nil {
return err
}
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &appsv1.DaemonSet{}, fluentbitOwnerKey, func(rawObj client.Object) []string {
// grab the job object, extract the owner.
ds := rawObj.(*appsv1.DaemonSet)
owner := metav1.GetControllerOf(ds)
if owner == nil {
return nil
}
// Make sure it's a FluentBit. If so, return it.
if owner.APIVersion != fluentbitApiGVStr || owner.Kind != "FluentBit" {
return nil
}
return []string{owner.Name}
}); err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&fluentbitv1alpha2.FluentBit{}).
Owns(&corev1.ServiceAccount{}).
Owns(&appsv1.DaemonSet{}).
Complete(r)
}