Skip to content

Commit

Permalink
customize the error message
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed Oct 10, 2024
1 parent 7fc841c commit 9b78974
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
9 changes: 5 additions & 4 deletions controller/dataplane/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
7 changes: 4 additions & 3 deletions controller/dataplane/konnect_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dataplane
import (
"context"
"errors"
"fmt"

"github.com/samber/lo"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -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{}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/consts/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit 9b78974

Please sign in to comment.