From 9b789746aa202808fed9a4238433317d68c38270 Mon Sep 17 00:00:00 2001 From: Mattia Lavacca Date: Thu, 10 Oct 2024 14:42:43 +0200 Subject: [PATCH] customize the error message Signed-off-by: Mattia Lavacca --- controller/dataplane/controller_utils.go | 9 +++++---- controller/dataplane/konnect_extension.go | 7 ++++--- pkg/consts/status.go | 7 ------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/controller/dataplane/controller_utils.go b/controller/dataplane/controller_utils.go index fe1f25a77..4cbd65a57 100644 --- a/controller/dataplane/controller_utils.go +++ b/controller/dataplane/controller_utils.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "os" + "strings" "github.com/go-logr/logr" appsv1 "k8s.io/api/apps/v1" @@ -332,15 +333,15 @@ func applyExtensions(ctx context.Context, cl client.Client, logger logr.Logger, case errors.Is(err, ErrCrossNamespaceReference): condition.Status = metav1.ConditionFalse condition.Reason = string(consts.RefNotPermittedReason) - condition.Message = consts.RefNotPermittedMessage + condition.Message = strings.ReplaceAll(err.Error(), "\n", " - ") case errors.Is(err, ErrKonnectExtensionNotFound): condition.Status = metav1.ConditionFalse condition.Reason = string(consts.InvalidExtensionRefReason) - condition.Message = consts.InvalidExtensionRefMessage + condition.Message = strings.ReplaceAll(err.Error(), "\n", " - ") case errors.Is(err, ErrClusterCertificateNotFound): condition.Status = metav1.ConditionFalse condition.Reason = string(consts.InvalidSecretRefReason) - condition.Message = consts.InvalidSecretRefMessage + condition.Message = strings.ReplaceAll(err.Error(), "\n", " - ") default: return patched, true, err } @@ -351,5 +352,5 @@ func applyExtensions(ctx context.Context, cl client.Client, logger logr.Logger, if patchErr != nil { return false, true, fmt.Errorf("failed patching status for DataPlane %s/%s: %w", dataplane.Namespace, dataplane.Name, patchErr) } - return patched, false, nil + return patched, false, err } diff --git a/controller/dataplane/konnect_extension.go b/controller/dataplane/konnect_extension.go index 6365c037b..f46db4ec7 100644 --- a/controller/dataplane/konnect_extension.go +++ b/controller/dataplane/konnect_extension.go @@ -3,6 +3,7 @@ package dataplane import ( "context" "errors" + "fmt" "github.com/samber/lo" appsv1 "k8s.io/api/apps/v1" @@ -36,7 +37,7 @@ func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, datap } namespace := dataplane.Namespace if extensionRef.Namespace != nil && *extensionRef.Namespace != namespace { - return ErrCrossNamespaceReference + return errors.Join(ErrCrossNamespaceReference, fmt.Errorf("the cross-namespace reference to the extension %s/%s is not permitted", *extensionRef.Namespace, extensionRef.Name)) } konnectExt := operatorv1alpha1.DataPlaneKonnectExtension{} @@ -45,7 +46,7 @@ func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, datap Name: extensionRef.Name, }, &konnectExt); err != nil { if k8serrors.IsNotFound(err) { - return ErrKonnectExtensionNotFound + return errors.Join(ErrKonnectExtensionNotFound, fmt.Errorf("the extension %s/%s referenced by the DataPlane is not found", namespace, extensionRef.Name)) } else { return err } @@ -57,7 +58,7 @@ func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, datap Name: konnectExt.Spec.AuthConfiguration.ClusterCertificateSecretRef.Name, }, &secret); err != nil { if k8serrors.IsNotFound(err) { - return ErrClusterCertificateNotFound + return errors.Join(ErrClusterCertificateNotFound, fmt.Errorf("the cluster certificate secret %s/%s referenced by the extension %s/%s is not found", namespace, konnectExt.Spec.AuthConfiguration.ClusterCertificateSecretRef.Name, namespace, extensionRef.Name)) } else { return err } diff --git a/pkg/consts/status.go b/pkg/consts/status.go index fd9e4bf8f..469243da3 100644 --- a/pkg/consts/status.go +++ b/pkg/consts/status.go @@ -51,11 +51,4 @@ const ( InvalidExtensionRefReason ConditionReason = "InvalidExtension" // InvalidSecretRefReason is a generic reason describing that the secret reference is invalid. It must be used when the ResolvedRefs condition is set to False. InvalidSecretRefReason ConditionReason = "InvalidSecret" - - // RefNotPermittedMessage indicates the reference is not permitted. It must be used when the ResolvedRefs condition is set to False. - RefNotPermittedMessage = "The extension cross-namespace reference is not permitted" - // InvalidExtensionRefMessage indicates the extension reference is invalid. It must be used when the ResolvedRefs condition is set to False. - InvalidExtensionRefMessage = "The referenced extension is invalid" - // InvalidSecretRefMessage indicates the secret reference is invalid. It must be used when the ResolvedRefs condition is set to False. - InvalidSecretRefMessage = "The secret referenced by the konnectExtension is invalid" )