-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract webhooks to seperate package to avoid import cycle (#766)
* extract webhooks to seperate package to avoid import cycle Signed-off-by: Andreas Gerstmayr <[email protected]> * move defaults tests Signed-off-by: Andreas Gerstmayr <[email protected]> --------- Signed-off-by: Andreas Gerstmayr <[email protected]>
- Loading branch information
1 parent
de70152
commit 37ac629
Showing
12 changed files
with
605 additions
and
632 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package v1alpha1 | ||
|
||
import "k8s.io/apimachinery/pkg/api/resource" | ||
|
||
var ( | ||
tenGBQuantity = resource.MustParse("10Gi") | ||
) | ||
|
||
// Default sets all default values in a central place, instead of setting it at every place where the value is accessed. | ||
// NOTE: This function is called inside the Reconcile loop, NOT in the webhook. | ||
// We want to keep the CR as minimal as the user configures it, and not modify it in any way (except for upgrades). | ||
func (r *TempoMonolithic) Default() { | ||
if r.Spec.Storage == nil { | ||
r.Spec.Storage = &MonolithicStorageSpec{} | ||
} | ||
|
||
if r.Spec.Storage.Traces.Backend == "" { | ||
r.Spec.Storage.Traces.Backend = MonolithicTracesStorageBackendMemory | ||
} | ||
|
||
if r.Spec.Storage.Traces.Backend != MonolithicTracesStorageBackendMemory && r.Spec.Storage.Traces.WAL == nil { | ||
r.Spec.Storage.Traces.WAL = &MonolithicTracesStorageWALSpec{ | ||
Size: tenGBQuantity, | ||
} | ||
} | ||
|
||
if r.Spec.Storage.Traces.Backend == MonolithicTracesStorageBackendPV && r.Spec.Storage.Traces.PV == nil { | ||
r.Spec.Storage.Traces.PV = &MonolithicTracesStoragePVSpec{ | ||
Size: tenGBQuantity, | ||
} | ||
} | ||
|
||
if r.Spec.Ingestion == nil { | ||
r.Spec.Ingestion = &MonolithicIngestionSpec{} | ||
} | ||
if r.Spec.Ingestion.OTLP == nil { | ||
r.Spec.Ingestion.OTLP = &MonolithicIngestionOTLPSpec{} | ||
} | ||
if r.Spec.Ingestion.OTLP.GRPC == nil { | ||
r.Spec.Ingestion.OTLP.GRPC = &MonolithicIngestionOTLPProtocolsGRPCSpec{ | ||
Enabled: true, | ||
} | ||
} | ||
if r.Spec.Ingestion.OTLP.HTTP == nil { | ||
r.Spec.Ingestion.OTLP.HTTP = &MonolithicIngestionOTLPProtocolsHTTPSpec{ | ||
Enabled: true, | ||
} | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package webhooks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
tempov1alpha1 "github.com/grafana/tempo-operator/apis/tempo/v1alpha1" | ||
) | ||
|
||
// TempoMonolithicWebhook provides webhooks for TempoMonolithic CR. | ||
type TempoMonolithicWebhook struct { | ||
} | ||
|
||
// SetupWebhookWithManager will setup the manager to manage the webhooks. | ||
func (w *TempoMonolithicWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&tempov1alpha1.TempoMonolithic{}). | ||
WithValidator(&monolithicValidator{client: mgr.GetClient()}). | ||
Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/validate-tempo-grafana-com-v1alpha1-tempomonolithic,mutating=false,failurePolicy=fail,sideEffects=None,groups=tempo.grafana.com,resources=tempomonolithics,verbs=create;update,versions=v1alpha1,name=vtempomonolithic.kb.io,admissionReviewVersions=v1 | ||
|
||
type monolithicValidator struct { | ||
client client.Client | ||
} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (v *monolithicValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
return v.validate(ctx, obj) | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (v *monolithicValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { | ||
return v.validate(ctx, newObj) | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. | ||
func (v *monolithicValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. | ||
return nil, nil | ||
} | ||
|
||
func (v *monolithicValidator) validate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
tempo, ok := obj.(*tempov1alpha1.TempoMonolithic) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a TempoMonolithic object but got %T", obj)) | ||
} | ||
|
||
log := ctrl.LoggerFrom(ctx).WithName("tempomonolithic-webhook") | ||
log.V(1).Info("running validating webhook", "name", tempo.Name) | ||
|
||
// We do not modify the Kubernetes object in the defaulter webhook, | ||
// but still apply some default values in-memory. | ||
tempo.Default() | ||
|
||
allWarnings := admission.Warnings{} | ||
allErrors := field.ErrorList{} | ||
addValidation := func(warnings admission.Warnings, errors field.ErrorList) { | ||
allWarnings = append(allWarnings, warnings...) | ||
allErrors = append(allErrors, errors...) | ||
} | ||
|
||
addValidation(v.validateExtraConfig(*tempo)) | ||
|
||
if len(allErrors) == 0 { | ||
return allWarnings, nil | ||
} | ||
return allWarnings, apierrors.NewInvalid(tempo.GroupVersionKind().GroupKind(), tempo.Name, allErrors) | ||
} | ||
|
||
func (v *monolithicValidator) validateExtraConfig(tempo tempov1alpha1.TempoMonolithic) (admission.Warnings, field.ErrorList) { //nolint:unparam | ||
if tempo.Spec.ExtraConfig != nil && len(tempo.Spec.ExtraConfig.Tempo.Raw) > 0 { | ||
return admission.Warnings{"overriding Tempo configuration could potentially break the deployment, use it carefully"}, nil | ||
} | ||
return nil, nil | ||
} |
Oops, something went wrong.