Skip to content

Commit

Permalink
add validation
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Gerstmayr <[email protected]>
  • Loading branch information
andreasgerstmayr committed Jan 25, 2024
1 parent cbd3d7c commit 01b2106
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
39 changes: 36 additions & 3 deletions apis/tempo/v1alpha1/tempomonolithic_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,48 @@ func (tempo *TempoMonolithic) validate() (admission.Warnings, error) {
log := ctrl.Log.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{}

if tempo.Spec.ExtraConfig != nil && len(tempo.Spec.ExtraConfig.Tempo.Raw) > 0 {
allWarnings = append(allWarnings, "overriding Tempo configuration could potentially break the deployment, use it carefully")
addValidation := func(warnings admission.Warnings, errors field.ErrorList) {
allWarnings = append(allWarnings, warnings...)
allErrors = append(allErrors, errors...)
}

addValidation(tempo.validateStorage())
addValidation(tempo.validateExtraConfig())

if len(allErrors) == 0 {
return allWarnings, nil
}
return allWarnings, apierrors.NewInvalid(tempo.GroupVersionKind().GroupKind(), tempo.Name, allErrors)
}

func (tempo *TempoMonolithic) validateStorage() (admission.Warnings, field.ErrorList) {

Check failure on line 101 in apis/tempo/v1alpha1/tempomonolithic_webhook.go

View workflow job for this annotation

GitHub Actions / Code standards (linting)

(*TempoMonolithic).validateStorage - result 0 (sigs.k8s.io/controller-runtime/pkg/webhook/admission.Warnings) is always nil (unparam)
storagePath := field.NewPath("spec").Child("storage")
if tempo.Spec.Storage == nil {
return nil, field.ErrorList{field.Invalid(storagePath, tempo.Spec.Storage, "storage must be configured")}
}

switch tempo.Spec.Storage.Traces.Backend {

Check failure on line 107 in apis/tempo/v1alpha1/tempomonolithic_webhook.go

View workflow job for this annotation

GitHub Actions / Code standards (linting)

missing cases in switch of type v1alpha1.MonolithicTracesStorageBackend: v1alpha1.MonolithicTracesStorageBackendMemory, v1alpha1.MonolithicTracesStorageBackendPV, v1alpha1.MonolithicTracesStorageBackendAzure, v1alpha1.MonolithicTracesStorageBackendGCS (exhaustive)
case MonolithicTracesStorageBackendS3:
if tempo.Spec.Storage.Traces.S3 == nil {
return nil, field.ErrorList{field.Invalid(storagePath.Child("traces").Child("s3"), tempo.Spec.Storage.Traces.S3, "S3 storage must be configured")}
}
if tempo.Spec.Storage.Traces.S3.Secret == "" {
return nil, field.ErrorList{field.Invalid(storagePath.Child("traces").Child("s3").Child("secret"), tempo.Spec.Storage.Traces.S3.Secret, "S3 storage secret must be configured")}
}
}

return nil, nil
}

func (tempo *TempoMonolithic) validateExtraConfig() (admission.Warnings, field.ErrorList) {

Check failure on line 120 in apis/tempo/v1alpha1/tempomonolithic_webhook.go

View workflow job for this annotation

GitHub Actions / Code standards (linting)

(*TempoMonolithic).validateExtraConfig - result 1 (k8s.io/apimachinery/pkg/util/validation/field.ErrorList) is always nil (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
}
2 changes: 1 addition & 1 deletion controllers/tempo/tempomonolithic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (r *TempoMonolithicReconciler) getStorageParams(ctx context.Context, tempo
storageParams := manifestutils.StorageParams{}
switch tempo.Spec.Storage.Traces.Backend {

Check failure on line 46 in controllers/tempo/tempomonolithic_controller.go

View workflow job for this annotation

GitHub Actions / Code standards (linting)

missing cases in switch of type v1alpha1.MonolithicTracesStorageBackend: v1alpha1.MonolithicTracesStorageBackendAzure, v1alpha1.MonolithicTracesStorageBackendGCS (exhaustive)
case v1alpha1.MonolithicTracesStorageBackendMemory, v1alpha1.MonolithicTracesStorageBackendPV:
// no action required, return empty storageParams
// no action required, return empty StorageParams
case v1alpha1.MonolithicTracesStorageBackendS3:
if tempo.Spec.Storage.Traces.S3 == nil {
return manifestutils.StorageParams{}, fmt.Errorf(".spec.storage.traces.s3 is required")
Expand Down

0 comments on commit 01b2106

Please sign in to comment.