-
Notifications
You must be signed in to change notification settings - Fork 30
/
tempostack_controller.go
469 lines (410 loc) · 16 KB
/
tempostack_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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package controllers
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
configv1alpha1 "github.com/os-observability/tempo-operator/apis/config/v1alpha1"
"github.com/os-observability/tempo-operator/apis/tempo/v1alpha1"
"github.com/os-observability/tempo-operator/internal/certrotation/handlers"
"github.com/os-observability/tempo-operator/internal/handlers/gateway"
"github.com/os-observability/tempo-operator/internal/manifests"
"github.com/os-observability/tempo-operator/internal/manifests/manifestutils"
"github.com/os-observability/tempo-operator/internal/status"
"github.com/os-observability/tempo-operator/internal/tlsprofile"
)
const (
storageSecretField = ".spec.storage.secret.name" // nolint #nosec
)
// TempoStackReconciler reconciles a TempoStack object.
type TempoStackReconciler struct {
client.Client
Scheme *runtime.Scheme
FeatureGates configv1alpha1.FeatureGates
}
// +kubebuilder:rbac:groups="",resources=services;configmaps;serviceaccounts;secrets;pods,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments;statefulsets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings;clusterroles,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes;routes/custom-host,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=operator.openshift.io,resources=ingresscontrollers,verbs=get;list;watch
// +kubebuilder:rbac:groups=config.openshift.io,resources=dnses,verbs=get;list;watch
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors;prometheusrules,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=tempo.grafana.com,resources=tempostacks,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=tempo.grafana.com,resources=tempostacks/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=tempo.grafana.com,resources=tempostacks/finalizers,verbs=update
// 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 TempoStack 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/[email protected]/pkg/reconcile
func (r *TempoStackReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
log = log.WithValues("tempo", req.NamespacedName)
tempo := v1alpha1.TempoStack{}
if err := r.Get(ctx, req.NamespacedName, &tempo); err != nil {
if !apierrors.IsNotFound(err) {
log.Error(err, "unable to fetch TempoTempoStack")
return ctrl.Result{}, fmt.Errorf("could not fetch tempo: %w", err)
}
// we'll ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
// on deleted requests.
return ctrl.Result{}, nil
}
if r.FeatureGates.BuiltInCertManagement.Enabled {
err := handlers.CreateOrRotateCertificates(ctx, log, req, r.Client, r.Scheme, r.FeatureGates)
if err != nil {
return ctrl.Result{}, fmt.Errorf("built in cert manager error: %w", err)
}
}
err := r.reconcileManifests(ctx, log, req, tempo)
return r.handleStatus(ctx, tempo, err)
}
// handleStatus set all components status, then verify if an error is type status.DegradedError, if that is the case, it will update the CR
// status conditions to Degraded. if not it will throw an error as usual.
func (r *TempoStackReconciler) handleStatus(ctx context.Context, tempo v1alpha1.TempoStack, err error) (ctrl.Result, error) {
// First refresh components
newStatus, rerr := status.GetComponetsStatus(ctx, r, tempo)
requeue := false
if rerr != nil {
return ctrl.Result{
Requeue: requeue,
RequeueAfter: time.Second,
}, err
}
// If is not degraded error, refresh and return error
var degraded *status.DegradedError
if !errors.As(err, °raded) {
requeue, rerr := status.Refresh(ctx, r, tempo, &newStatus)
// Error refreshing components status
if rerr != nil {
return ctrl.Result{
Requeue: requeue,
RequeueAfter: time.Second,
}, err
}
// Return original error
return ctrl.Result{
Requeue: false,
}, err
}
// Degraded error
newStatus.Conditions = status.DegradedCondition(tempo, degraded.Message, degraded.Reason)
// Refresh status
requeue, err = status.Refresh(ctx, r, tempo, &newStatus)
// Error refreshing status
if err != nil {
return ctrl.Result{
Requeue: requeue || degraded.Requeue,
RequeueAfter: time.Second,
}, err
}
// No errors at all.
return ctrl.Result{}, nil
}
func (r *TempoStackReconciler) reconcileManifests(ctx context.Context, log logr.Logger, req ctrl.Request, tempo v1alpha1.TempoStack) error {
storageConfig, err := r.getStorageConfig(ctx, tempo)
if err != nil {
return &status.DegradedError{
Reason: v1alpha1.ReasonInvalidStorageConfig,
Message: err.Error(),
Requeue: false,
}
}
if err = v1alpha1.ValidateTenantConfigs(tempo); err != nil {
return &status.DegradedError{
Message: fmt.Sprintf("Invalid tenants configuration: %s", err),
Reason: v1alpha1.ReasonInvalidTenantsConfiguration,
Requeue: false,
}
}
if tempo.Spec.Tenants != nil && tempo.Spec.Tenants.Mode == v1alpha1.OpenShift && r.FeatureGates.OpenShift.BaseDomain == "" {
domain, err := gateway.GetOpenShiftBaseDomain(ctx, r.Client)
if err != nil {
return err
}
log.Info("OpenShift base domain set", "openshift-base-domain", domain)
r.FeatureGates.OpenShift.BaseDomain = domain
}
tlsProfile, err := tlsprofile.Get(ctx, r.FeatureGates, r.Client, log)
if err != nil {
switch err {
case tlsprofile.ErrGetProfileFromCluster:
case tlsprofile.ErrGetInvalidProfile:
return &status.DegradedError{
Message: err.Error(),
Reason: v1alpha1.ReasonCouldNotGetOpenShiftTLSPolicy,
Requeue: false,
}
default:
return err
}
}
// Collect all objects owned by the operator, to be able to prune objects
// which exist in the cluster but are not managed by the operator anymore.
// For example, when the Jaeger Query Ingress is enabled and later disabled,
// the Ingress object should be removed from the cluster.
pruneObjects, err := r.findObjectsOwnedByTempoOperator(ctx, tempo)
if err != nil {
return err
}
var tenantSecrets []*manifestutils.GatewayTenantOIDCSecret
if tempo.Spec.Tenants != nil && tempo.Spec.Tenants.Mode == v1alpha1.Static {
tenantSecrets, err = gateway.GetOIDCTenantSecrets(ctx, r.Client, tempo)
if err != nil {
return err
}
}
var gatewayTenantsData []*manifestutils.GatewayTenantsData
if tempo.Spec.Tenants != nil && tempo.Spec.Tenants.Mode == v1alpha1.OpenShift {
gatewayTenantsData, err = gateway.GetGatewayTenantsData(ctx, r.Client, tempo)
if err != nil {
// just log the error the secret is not created if the loop for an instance runs for the first time.
log.Info("Failed to get gateway secret and/or tenants.yaml", "error", err)
}
}
managedObjects, err := manifests.BuildAll(manifestutils.Params{
Tempo: tempo,
StorageParams: *storageConfig,
Gates: r.FeatureGates,
TLSProfile: tlsProfile,
GatewayTenantSecret: tenantSecrets,
GatewayTenantsData: gatewayTenantsData,
})
// TODO (pavolloffay) check error type and change return appropriately
if err != nil {
return fmt.Errorf("error building manifests: %w", err)
}
errCount := 0
for _, obj := range managedObjects {
l := log.WithValues(
"object_name", obj.GetName(),
"object_kind", obj.GetObjectKind(),
)
if isNamespaceScoped(obj) {
obj.SetNamespace(req.Namespace)
if err := ctrl.SetControllerReference(&tempo, obj, r.Scheme); err != nil {
l.Error(err, "failed to set controller owner reference to resource")
errCount++
continue
}
}
desired := obj.DeepCopyObject().(client.Object)
mutateFn := manifests.MutateFuncFor(obj, desired)
op, err := ctrl.CreateOrUpdate(ctx, r.Client, obj, mutateFn)
if err != nil {
l.Error(err, "failed to configure resource")
errCount++
continue
}
l.Info(fmt.Sprintf("resource has been %s", op))
// This object is still managed by the operator, remove it from the list of objects to prune
delete(pruneObjects, obj.GetUID())
}
if errCount > 0 {
return fmt.Errorf("failed to create objects for TempoStack %s", req.NamespacedName)
}
// Prune owned objects in the cluster which are not managed anymore.
pruneErrCount := 0
for _, obj := range pruneObjects {
l := log.WithValues(
"object_name", obj.GetName(),
"object_kind", obj.GetObjectKind(),
)
l.Info("pruning unmanaged resource")
err = r.Delete(ctx, obj)
if err != nil {
l.Error(err, "failed to delete resource")
pruneErrCount++
}
}
if pruneErrCount > 0 {
return fmt.Errorf("failed to prune objects of TempoStack %s", req.NamespacedName)
}
return nil
}
func (r *TempoStackReconciler) getStorageConfig(ctx context.Context, tempo v1alpha1.TempoStack) (*manifestutils.StorageParams, error) {
storageSecret := &corev1.Secret{}
err := r.Get(ctx, types.NamespacedName{Namespace: tempo.Namespace, Name: tempo.Spec.Storage.Secret.Name}, storageSecret)
if err != nil {
return nil, fmt.Errorf("could not fetch storage secret: %w", err)
}
fieldErrs := v1alpha1.ValidateStorageSecret(tempo, *storageSecret)
if len(fieldErrs) > 0 {
msgs := make([]string, len(fieldErrs))
for i, fieldErr := range fieldErrs {
msgs[i] = fieldErr.Detail
}
return nil, fmt.Errorf("invalid storage secret: %s", strings.Join(msgs, ", "))
}
params := manifestutils.StorageParams{
AzureStorage: &manifestutils.AzureStorage{},
GCS: &manifestutils.GCS{},
S3: &manifestutils.S3{},
}
switch tempo.Spec.Storage.Secret.Type {
case v1alpha1.ObjectStorageSecretAzure:
params.AzureStorage = getAzureParams(storageSecret)
case v1alpha1.ObjectStorageSecretGCS:
params.GCS = getGCSParams(storageSecret)
case v1alpha1.ObjectStorageSecretS3:
params.S3 = getS3Params(storageSecret)
default:
return ¶ms, fmt.Errorf("storage secret type is not recognized")
}
return ¶ms, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *TempoStackReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Add an index to the storage secret field in the TempoStack CRD.
// If the content of any secret in the cluster changes, the watcher can identify related TempoStack CRs
// and reconcile them (i.e. update the tempo configuration file and restart the pods)
err := mgr.GetFieldIndexer().IndexField(context.Background(), &v1alpha1.TempoStack{}, storageSecretField, func(rawObj client.Object) []string {
tempostacks := rawObj.(*v1alpha1.TempoStack)
if tempostacks.Spec.Storage.Secret.Name == "" {
return nil
}
return []string{tempostacks.Spec.Storage.Secret.Name}
})
if err != nil {
return err
}
builder := ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.TempoStack{}).
Owns(&corev1.ConfigMap{}).
Owns(&corev1.ServiceAccount{}).
Owns(&corev1.Service{}).
Owns(&corev1.Secret{}).
Owns(&appsv1.StatefulSet{}).
Owns(&appsv1.Deployment{}).
Owns(&networkingv1.Ingress{}).
Watches(
&source.Kind{Type: &corev1.Secret{}},
handler.EnqueueRequestsFromMapFunc(r.findTempoStackForStorageSecret),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
)
if r.FeatureGates.PrometheusOperator {
builder = builder.Owns(&monitoringv1.ServiceMonitor{})
builder = builder.Owns(&monitoringv1.PrometheusRule{})
}
if r.FeatureGates.OpenShift.OpenShiftRoute {
builder = builder.Owns(&routev1.Route{})
}
return builder.Complete(r)
}
func isNamespaceScoped(obj client.Object) bool {
switch obj.(type) {
case *rbacv1.ClusterRole, *rbacv1.ClusterRoleBinding:
return false
default:
return true
}
}
func (r *TempoStackReconciler) findTempoStackForStorageSecret(secret client.Object) []reconcile.Request {
tempostacks := &v1alpha1.TempoStackList{}
listOps := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(storageSecretField, secret.GetName()),
Namespace: secret.GetNamespace(),
}
err := r.List(context.TODO(), tempostacks, listOps)
if err != nil {
return []reconcile.Request{}
}
requests := make([]reconcile.Request, len(tempostacks.Items))
for i, item := range tempostacks.Items {
requests[i] = reconcile.Request{
NamespacedName: types.NamespacedName{
Name: item.GetName(),
Namespace: item.GetNamespace(),
},
}
}
return requests
}
func (r *TempoStackReconciler) findObjectsOwnedByTempoOperator(ctx context.Context, tempo v1alpha1.TempoStack) (map[types.UID]client.Object, error) {
ownedObjects := map[types.UID]client.Object{}
listOps := &client.ListOptions{
Namespace: tempo.GetNamespace(),
LabelSelector: labels.SelectorFromSet(manifestutils.CommonLabels(tempo.Name)),
}
// Add all resources where the operator can conditionally create an object.
// For example, Ingress and Route can be enabled or disabled in the CR.
ingressList := &networkingv1.IngressList{}
err := r.List(ctx, ingressList, listOps)
if err != nil {
return nil, fmt.Errorf("error listing ingress: %w", err)
}
for i := range ingressList.Items {
ownedObjects[ingressList.Items[i].GetUID()] = &ingressList.Items[i]
}
if r.FeatureGates.PrometheusOperator {
servicemonitorList := &monitoringv1.ServiceMonitorList{}
err := r.List(ctx, servicemonitorList, listOps)
if err != nil {
return nil, fmt.Errorf("error listing service monitors: %w", err)
}
for i := range servicemonitorList.Items {
ownedObjects[servicemonitorList.Items[i].GetUID()] = servicemonitorList.Items[i]
}
prometheusRulesList := &monitoringv1.PrometheusRuleList{}
err = r.List(ctx, prometheusRulesList, listOps)
if err != nil {
return nil, fmt.Errorf("error listing prometheus rules: %w", err)
}
for i := range prometheusRulesList.Items {
ownedObjects[prometheusRulesList.Items[i].GetUID()] = prometheusRulesList.Items[i]
}
}
if r.FeatureGates.OpenShift.OpenShiftRoute {
routesList := &routev1.RouteList{}
err := r.List(ctx, routesList, listOps)
if err != nil {
return nil, fmt.Errorf("error listing routes: %w", err)
}
for i := range routesList.Items {
ownedObjects[routesList.Items[i].GetUID()] = &routesList.Items[i]
}
}
return ownedObjects, nil
}
// GetPodsComponent is used for fetching component pod status and refreshing the status of the CR.
func (r *TempoStackReconciler) GetPodsComponent(ctx context.Context, componentName string, stack v1alpha1.TempoStack) (*corev1.PodList, error) {
pods := &corev1.PodList{}
opts := []client.ListOption{
client.MatchingLabels(manifestutils.ComponentLabels(componentName, stack.Name)),
client.InNamespace(stack.Namespace),
}
err := r.Client.List(ctx, pods, opts...)
return pods, err
}
// PatchStatus patches the status field of the CR.
func (r *TempoStackReconciler) PatchStatus(ctx context.Context, changed, original *v1alpha1.TempoStack) error {
statusPatch := client.MergeFrom(original)
return r.Client.Status().Patch(ctx, changed, statusPatch)
}