-
Notifications
You must be signed in to change notification settings - Fork 24
/
tempostack_webhook.go
490 lines (420 loc) · 18.7 KB
/
tempostack_webhook.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package v1alpha1
import (
"context"
"errors"
"fmt"
"math"
"net"
"strconv"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/grafana/tempo-operator/apis/config/v1alpha1"
"github.com/grafana/tempo-operator/internal/manifests/naming"
)
var (
zeroQuantity = resource.MustParse("0Gi")
tenGBQuantity = resource.MustParse("10Gi")
errNoDefaultTempoImage = errors.New("please specify a tempo image in the CR or in the operator configuration")
errNoDefaultTempoGatewayImage = errors.New("please specify a tempo-gateway image in the CR or in the operator configuration")
errNoDefaultGatewayOPAImage = errors.New("please specify a opa image in the CR or in the operator configuration")
errNoDefaultTempoQueryImage = errors.New("please specify a tempo-query image in the CR or in the operator configuration")
)
const maxLabelLength = 63
const defaultRouteGatewayTLSTermination = TLSRouteTerminationTypePassthrough
const defaultUITLSTermination = TLSRouteTerminationTypeEdge
// SetupWebhookWithManager initializes the webhook.
func (r *TempoStack) SetupWebhookWithManager(mgr ctrl.Manager, ctrlConfig v1alpha1.ProjectConfig) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithDefaulter(NewDefaulter(ctrlConfig)).
WithValidator(&validator{client: mgr.GetClient(), ctrlConfig: ctrlConfig}).
Complete()
}
//+kubebuilder:webhook:path=/mutate-tempo-grafana-com-v1alpha1-tempostack,mutating=true,failurePolicy=fail,sideEffects=None,groups=tempo.grafana.com,resources=tempostacks,verbs=create;update,versions=v1alpha1,name=mtempostack.tempo.grafana.com,admissionReviewVersions=v1
// NewDefaulter creates a new instance of Defaulter, which implements functions for setting defaults on the Tempo CR.
func NewDefaulter(ctrlConfig v1alpha1.ProjectConfig) *Defaulter {
return &Defaulter{
ctrlConfig: ctrlConfig,
}
}
// Defaulter implements the CustomDefaulter interface.
type Defaulter struct {
ctrlConfig v1alpha1.ProjectConfig
}
// Default applies default values to a Kubernetes object.
func (d *Defaulter) Default(ctx context.Context, obj runtime.Object) error {
r, ok := obj.(*TempoStack)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a TempoStack object but got %T", obj))
}
log := ctrl.LoggerFrom(ctx).WithName("tempostack-webhook")
log.V(1).Info("running defaulter webhook", "name", r.Name)
if r.Labels == nil {
r.Labels = map[string]string{}
}
if r.Labels["app.kubernetes.io/managed-by"] == "" {
r.Labels["app.kubernetes.io/managed-by"] = "tempo-operator"
}
r.Labels["tempo.grafana.com/distribution"] = d.ctrlConfig.Distribution
if r.Spec.Images.Tempo == "" {
if d.ctrlConfig.DefaultImages.Tempo == "" {
return errNoDefaultTempoImage
}
r.Spec.Images.Tempo = d.ctrlConfig.DefaultImages.Tempo
}
if r.Spec.Images.TempoQuery == "" {
if d.ctrlConfig.DefaultImages.TempoQuery == "" {
return errNoDefaultTempoQueryImage
}
r.Spec.Images.TempoQuery = d.ctrlConfig.DefaultImages.TempoQuery
}
if r.Spec.Images.TempoGateway == "" {
if d.ctrlConfig.DefaultImages.TempoGateway == "" {
return errNoDefaultTempoGatewayImage
}
r.Spec.Images.TempoGateway = d.ctrlConfig.DefaultImages.TempoGateway
}
if r.Spec.Images.TempoGatewayOpa == "" {
if d.ctrlConfig.DefaultImages.TempoGatewayOpa == "" {
return errNoDefaultGatewayOPAImage
}
r.Spec.Images.TempoGatewayOpa = d.ctrlConfig.DefaultImages.TempoGatewayOpa
}
if r.Spec.ServiceAccount == "" {
r.Spec.ServiceAccount = naming.DefaultServiceAccountName(r.Name)
}
if r.Spec.Retention.Global.Traces.Duration == 0 {
r.Spec.Retention.Global.Traces.Duration = 48 * time.Hour
}
if r.Spec.StorageSize.Cmp(zeroQuantity) <= 0 {
r.Spec.StorageSize = tenGBQuantity
}
if r.Spec.SearchSpec.DefaultResultLimit == nil {
defaultDefaultResultLimit := 20
r.Spec.SearchSpec.DefaultResultLimit = &defaultDefaultResultLimit
}
defaultComponentReplicas := pointer.Int32(1)
defaultReplicationFactor := 1
// Default replicas for ingester if not specified.
if r.Spec.Template.Ingester.Replicas == nil {
r.Spec.Template.Ingester.Replicas = defaultComponentReplicas
}
// Default replicas for distributor if not specified.
if r.Spec.Template.Distributor.Replicas == nil {
r.Spec.Template.Distributor.Replicas = defaultComponentReplicas
}
// Default replication factor if not specified.
if r.Spec.ReplicationFactor == 0 {
r.Spec.ReplicationFactor = defaultReplicationFactor
}
// if tenant mode is Openshift, ingress type should be route by default.
if r.Spec.Tenants != nil && r.Spec.Tenants.Mode == ModeOpenShift && r.Spec.Template.Gateway.Ingress.Type == "" {
r.Spec.Template.Gateway.Ingress.Type = IngressTypeRoute
}
if r.Spec.Template.Gateway.Ingress.Type == IngressTypeRoute && r.Spec.Template.Gateway.Ingress.Route.Termination == "" {
r.Spec.Template.Gateway.Ingress.Route.Termination = defaultRouteGatewayTLSTermination
}
// Terminate TLS of the JaegerQuery Route on the Edge by default
if r.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Type == IngressTypeRoute && r.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Route.Termination == "" {
r.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Route.Termination = defaultUITLSTermination
}
return nil
}
//+kubebuilder:webhook:path=/validate-tempo-grafana-com-v1alpha1-tempostack,mutating=false,failurePolicy=fail,sideEffects=None,groups=tempo.grafana.com,resources=tempostacks,verbs=create;update,versions=v1alpha1,name=vtempostack.tempo.grafana.com,admissionReviewVersions=v1
type validator struct {
client client.Client
ctrlConfig v1alpha1.ProjectConfig
}
func (v *validator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return v.validate(ctx, obj)
}
func (v *validator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
return v.validate(ctx, newObj)
}
func (v *validator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
// NOTE(agerstmayr): change verbs in +kubebuilder:webhook to "verbs=create;update;delete" if you want to enable deletion validation.
return nil, nil
}
func (v *validator) validateServiceAccount(ctx context.Context, tempo TempoStack) field.ErrorList {
var allErrs field.ErrorList
// the default service account gets created later in the reconciliation loop
if tempo.Spec.ServiceAccount != naming.DefaultServiceAccountName(tempo.Name) {
// check if custom service account exists
serviceAccount := &corev1.ServiceAccount{}
err := v.client.Get(ctx, types.NamespacedName{Namespace: tempo.Namespace, Name: tempo.Spec.ServiceAccount}, serviceAccount)
if err != nil {
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec").Child("serviceAccount"),
tempo.Spec.ServiceAccount,
err.Error(),
))
}
}
return allErrs
}
func (v *validator) validateStorageSecret(ctx context.Context, tempo TempoStack) (admission.Warnings, field.ErrorList) {
storageSecret := &corev1.Secret{}
err := v.client.Get(ctx, types.NamespacedName{Namespace: tempo.Namespace, Name: tempo.Spec.Storage.Secret.Name}, storageSecret)
if err != nil {
// Do not fail the validation here, the user can create the storage secret later.
// The operator will remain in a ConfigurationError status condition until the storage secret is set.
return admission.Warnings{fmt.Sprintf("Secret '%s' does not exist", tempo.Spec.Storage.Secret.Name)}, field.ErrorList{}
}
return admission.Warnings{}, ValidateStorageSecret(tempo, *storageSecret)
}
func (v *validator) validateStorageCA(ctx context.Context, tempo TempoStack) (admission.Warnings, field.ErrorList) {
caConfigMap := &corev1.ConfigMap{}
err := v.client.Get(ctx, types.NamespacedName{Namespace: tempo.Namespace, Name: tempo.Spec.Storage.TLS.CA}, caConfigMap)
if err != nil {
// Do not fail the validation here, the user can create the ConfigMap later.
// The operator will remain in a ConfigurationError status condition until the ConfigMap is created.
return admission.Warnings{fmt.Sprintf("ConfigMap '%s' does not exist", tempo.Spec.Storage.TLS.CA)}, field.ErrorList{}
}
return admission.Warnings{}, ValidateStorageCAConfigMap(*caConfigMap)
}
func (v *validator) validateReplicationFactor(tempo TempoStack) field.ErrorList {
// Validate minimum quorum on ingestors according to replicas and replication factor
replicatonFactor := tempo.Spec.ReplicationFactor
// Ingester replicas should not be nil at this point, due defauler.
ingesterReplicas := int(*tempo.Spec.Template.Ingester.Replicas)
quorum := int(math.Floor(float64(replicatonFactor)/2.0) + 1)
// if ingester replicas less than quorum (which depends on replication factor), then doesn't allow to deploy as it is an
// invalid configuration. Quorum equal to replicas doesn't allow you to lose ingesters but is a valid configuration.
if ingesterReplicas < quorum {
path := field.NewPath("spec").Child("ReplicationFactor")
return field.ErrorList{
field.Invalid(path, tempo.Spec.ReplicationFactor,
fmt.Sprintf("replica factor of %d requires at least %d ingester replicas", replicatonFactor, quorum),
)}
}
return nil
}
func (v *validator) validateQueryFrontend(tempo TempoStack) field.ErrorList {
path := field.NewPath("spec").Child("template").Child("queryFrontend").Child("jaegerQuery").Child("ingress").Child("type")
if tempo.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Type != IngressTypeNone && !tempo.Spec.Template.QueryFrontend.JaegerQuery.Enabled {
return field.ErrorList{field.Invalid(
path,
tempo.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Type,
"Ingress cannot be enabled if jaegerQuery is disabled",
)}
}
if tempo.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Type == IngressTypeRoute && !v.ctrlConfig.Gates.OpenShift.OpenShiftRoute {
return field.ErrorList{field.Invalid(
path,
tempo.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Type,
"Please enable the featureGates.openshift.openshiftRoute feature gate to use Routes",
)}
}
if tempo.Spec.Template.QueryFrontend.JaegerQuery.MonitorTab.Enabled {
prometheusEndpointPath := field.NewPath("spec").Child("template").Child("queryFrontend").Child("jaegerQuery").Child("monitorTab").Child("prometheusEndpoint")
if tempo.Spec.Template.QueryFrontend.JaegerQuery.MonitorTab.PrometheusEndpoint == "" {
return field.ErrorList{field.Invalid(
prometheusEndpointPath,
tempo.Spec.Template.QueryFrontend.JaegerQuery.MonitorTab.PrometheusEndpoint,
"Prometheus endpoint must be set when monitoring is enabled",
)}
}
}
return nil
}
func (v *validator) validateGateway(tempo TempoStack) field.ErrorList {
path := field.NewPath("spec").Child("template").Child("gateway").Child("enabled")
if tempo.Spec.Template.Gateway.Enabled {
if !tempo.Spec.Template.QueryFrontend.JaegerQuery.Enabled {
return field.ErrorList{
field.Invalid(path, tempo.Spec.Template.Gateway.Enabled,
"to use the gateway, please enable jaegerQuery",
)}
}
if tempo.Spec.Template.QueryFrontend.JaegerQuery.Ingress.Type != IngressTypeNone {
return field.ErrorList{
field.Invalid(path, tempo.Spec.Template.Gateway.Enabled,
"cannot enable gateway and jaeger query ingress at the same time, please use the Jaeger UI from the gateway",
)}
}
if tempo.Spec.Tenants == nil {
return field.ErrorList{
field.Invalid(path, tempo.Spec.Template.Gateway.Enabled,
"to enable the gateway, please configure tenants",
)}
}
if tempo.Spec.Template.Gateway.Ingress.Type == IngressTypeRoute && !v.ctrlConfig.Gates.OpenShift.OpenShiftRoute {
return field.ErrorList{field.Invalid(
field.NewPath("spec").Child("template").Child("gateway").Child("ingress").Child("type"),
tempo.Spec.Template.Gateway.Ingress.Type,
"please enable the featureGates.openshift.openshiftRoute feature gate to use Routes",
)}
}
}
return nil
}
func (v *validator) validateObservability(tempo TempoStack) field.ErrorList {
observabilityBase := field.NewPath("spec").Child("observability")
metricsBase := observabilityBase.Child("metrics")
if tempo.Spec.Observability.Metrics.CreateServiceMonitors && !v.ctrlConfig.Gates.PrometheusOperator {
return field.ErrorList{
field.Invalid(metricsBase.Child("createServiceMonitors"), tempo.Spec.Observability.Metrics.CreateServiceMonitors,
"the prometheusOperator feature gate must be enabled to create ServiceMonitors for Tempo components",
)}
}
if tempo.Spec.Observability.Metrics.CreatePrometheusRules && !v.ctrlConfig.Gates.PrometheusOperator {
return field.ErrorList{
field.Invalid(metricsBase.Child("createPrometheusRules"), tempo.Spec.Observability.Metrics.CreatePrometheusRules,
"the prometheusOperator feature gate must be enabled to create PrometheusRules for Tempo components",
)}
}
if tempo.Spec.Observability.Metrics.CreatePrometheusRules && !tempo.Spec.Observability.Metrics.CreateServiceMonitors {
return field.ErrorList{
field.Invalid(metricsBase.Child("createPrometheusRules"), tempo.Spec.Observability.Metrics.CreatePrometheusRules,
"the Prometheus rules alert based on collected metrics, therefore the createServiceMonitors feature must be enabled when enabling the createPrometheusRules feature",
)}
}
tracingBase := observabilityBase.Child("tracing")
if tempo.Spec.Observability.Tracing.SamplingFraction == "" {
return nil
}
if _, err := strconv.ParseFloat(tempo.Spec.Observability.Tracing.SamplingFraction, 64); err != nil {
return field.ErrorList{
field.Invalid(
tracingBase.Child("sampling_fraction"),
tempo.Spec.Observability.Tracing.SamplingFraction,
err.Error(),
)}
}
if tempo.Spec.Observability.Tracing.JaegerAgentEndpoint != "" {
_, _, err := net.SplitHostPort(tempo.Spec.Observability.Tracing.JaegerAgentEndpoint)
if err != nil {
return field.ErrorList{
field.Invalid(
tracingBase.Child("jaeger_agent_endpoint"),
tempo.Spec.Observability.Tracing.JaegerAgentEndpoint,
err.Error(),
)}
}
}
return nil
}
func (v *validator) validateTenantConfigs(tempo TempoStack) field.ErrorList {
if err := ValidateTenantConfigs(tempo); err != nil {
return field.ErrorList{
field.Invalid(
field.NewPath("spec").Child("template").Child("tenants"),
tempo.Spec.Template.Gateway.Enabled,
err.Error(),
)}
}
return nil
}
func (v *validator) validateStackName(tempo TempoStack) field.ErrorList {
// We need to check this because the name is used as a label value for app.kubernetes.io/instance
// Only validate the length, because the DNS rules are enforced by the functions in the `naming` package.
if len(tempo.Name) > maxLabelLength {
return field.ErrorList{
field.Invalid(
field.NewPath("metadata").Child("name"),
tempo.Name,
fmt.Sprintf("must be no more than %d characters", maxLabelLength),
)}
}
return nil
}
func (v *validator) validateDeprecatedFields(tempo TempoStack) field.ErrorList {
if tempo.Spec.LimitSpec.Global.Query.MaxSearchBytesPerTrace != nil {
return field.ErrorList{
field.Invalid(
field.NewPath("spec").Child("limits").Child("global").Child("query").Child("maxSearchBytesPerTrace"),
tempo.Spec.LimitSpec.Global.Query.MaxSearchBytesPerTrace,
"this field is deprecated and must be unset",
)}
}
for tenant, limits := range tempo.Spec.LimitSpec.PerTenant {
if limits.Query.MaxSearchBytesPerTrace != nil {
return field.ErrorList{
field.Invalid(
field.NewPath("spec").Child("limits").Child("perTenant").Key(tenant).Child("query").Child("maxSearchBytesPerTrace"),
limits.Query.MaxSearchBytesPerTrace,
"this field is deprecated and must be unset",
)}
}
}
return nil
}
func (v *validator) validate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
tempo, ok := obj.(*TempoStack)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a TempoStack object but got %T", obj))
}
log := ctrl.LoggerFrom(ctx).WithName("tempostack-webhook")
log.V(1).Info("running validating webhook", "name", tempo.Name)
allWarnings := admission.Warnings{}
allErrors := field.ErrorList{}
var warnings admission.Warnings
var errors field.ErrorList
allErrors = append(allErrors, v.validateStackName(*tempo)...)
allErrors = append(allErrors, v.validateServiceAccount(ctx, *tempo)...)
warnings, errors = v.validateStorageSecret(ctx, *tempo)
allWarnings = append(allWarnings, warnings...)
allErrors = append(allErrors, errors...)
if tempo.Spec.Storage.TLS.CA != "" {
warnings, errors = v.validateStorageCA(ctx, *tempo)
allWarnings = append(allWarnings, warnings...)
allErrors = append(allErrors, errors...)
}
allErrors = append(allErrors, v.validateReplicationFactor(*tempo)...)
allErrors = append(allErrors, v.validateQueryFrontend(*tempo)...)
allErrors = append(allErrors, v.validateGateway(*tempo)...)
allErrors = append(allErrors, v.validateTenantConfigs(*tempo)...)
allErrors = append(allErrors, v.validateObservability(*tempo)...)
allErrors = append(allErrors, v.validateDeprecatedFields(*tempo)...)
if len(allErrors) == 0 {
return allWarnings, nil
}
return allWarnings, apierrors.NewInvalid(tempo.GroupVersionKind().GroupKind(), tempo.Name, allErrors)
}
// ValidateTenantConfigs validates the tenants mode specification.
func ValidateTenantConfigs(tempo TempoStack) error {
if tempo.Spec.Tenants == nil {
return nil
}
tenants := tempo.Spec.Tenants
if tenants.Mode == ModeStatic {
// If the static mode is combined with the gateway, we will need the following fields
// otherwise this will just enable tempo multitenancy without the gateway
if tempo.Spec.Template.Gateway.Enabled {
if tenants.Authentication == nil {
return fmt.Errorf("spec.tenants.authentication is required in static mode")
}
if tenants.Authorization == nil {
return fmt.Errorf("spec.tenants.authorization is required in static mode")
}
if tenants.Authorization.Roles == nil {
return fmt.Errorf("spec.tenants.authorization.roles is required in static mode")
}
if tenants.Authorization.RoleBindings == nil {
return fmt.Errorf("spec.tenants.authorization.roleBindings is required in static mode")
}
}
} else if tenants.Mode == ModeOpenShift {
if !tempo.Spec.Template.Gateway.Enabled {
return fmt.Errorf("openshift mode requires gateway enabled")
}
if tenants.Authorization != nil {
return fmt.Errorf("spec.tenants.authorization should not be defined in openshift mode")
}
for _, auth := range tenants.Authentication {
if auth.OIDC != nil {
return fmt.Errorf("spec.tenants.authentication.oidc should not be defined in openshift mode")
}
}
}
return nil
}