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 fa3f744
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions controller/dataplane/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,21 @@ func applyExtensions(ctx context.Context, cl client.Client, logger logr.Logger,
return false, false, nil
}
condition := k8sutils.NewConditionWithGeneration(consts.ResolvedRefsType, metav1.ConditionTrue, consts.ResolvedRefsReason, "", dataplane.GetGeneration())
err = applyDataPlaneKonnectExtension(ctx, cl, dataplane)
message, err := applyDataPlaneKonnectExtension(ctx, cl, dataplane)
if err != nil {
switch {
case errors.Is(err, ErrCrossNamespaceReference):
condition.Status = metav1.ConditionFalse
condition.Reason = string(consts.RefNotPermittedReason)
condition.Message = consts.RefNotPermittedMessage
condition.Message = message
case errors.Is(err, ErrKonnectExtensionNotFound):
condition.Status = metav1.ConditionFalse
condition.Reason = string(consts.InvalidExtensionRefReason)
condition.Message = consts.InvalidExtensionRefMessage
condition.Message = message
case errors.Is(err, ErrClusterCertificateNotFound):
condition.Status = metav1.ConditionFalse
condition.Reason = string(consts.InvalidSecretRefReason)
condition.Message = consts.InvalidSecretRefMessage
condition.Message = message
default:
return patched, true, err
}
Expand Down
15 changes: 8 additions & 7 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 All @@ -29,14 +30,14 @@ var (

// applyDataPlaneKonnectExtension gets the DataPlane as argument, and in case it references a KonnectExtension, it
// fetches the referenced extension and applies the necessary changes to the DataPlane spec.
func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, dataplane *v1beta1.DataPlane) error {
func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, dataplane *v1beta1.DataPlane) (string, error) {
for _, extensionRef := range dataplane.Spec.Extensions {
if extensionRef.Group != operatorv1alpha1.SchemeGroupVersion.Group || extensionRef.Kind != operatorv1alpha1.DataPlaneKonnectExtensionKind {
continue
}
namespace := dataplane.Namespace
if extensionRef.Namespace != nil && *extensionRef.Namespace != namespace {
return ErrCrossNamespaceReference
return fmt.Sprintf("The cross-namespace reference to the extension %s/%s is not permitted", *extensionRef.Namespace, extensionRef.Name), ErrCrossNamespaceReference
}

konnectExt := operatorv1alpha1.DataPlaneKonnectExtension{}
Expand All @@ -45,9 +46,9 @@ func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, datap
Name: extensionRef.Name,
}, &konnectExt); err != nil {
if k8serrors.IsNotFound(err) {
return ErrKonnectExtensionNotFound
return fmt.Sprintf("The extension %s/%s referenced by the DataPlane is not found", namespace, extensionRef.Name), ErrKonnectExtensionNotFound
} else {
return err
return "", err
}
}

Expand All @@ -57,9 +58,9 @@ 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 fmt.Sprintf("The cluster certificate secret %s/%s referenced by the extension %s/%s is not found", namespace, konnectExt.Spec.AuthConfiguration.ClusterCertificateSecretRef.Name, namespace, extensionRef.Name), ErrClusterCertificateNotFound
} else {
return err
return "", err
}
}

Expand Down Expand Up @@ -93,7 +94,7 @@ func applyDataPlaneKonnectExtension(ctx context.Context, cl client.Client, datap
dputils.FillDataPlaneProxyContainerEnvs(nil, &d.Spec.Template, envSet)
dataplane.Spec.Deployment.PodTemplateSpec = &d.Spec.Template
}
return nil
return "", nil
}

func kongInKonnectClusterCertVolume(secretName string) corev1.Volume {
Expand Down
7 changes: 6 additions & 1 deletion controller/dataplane/konnect_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestApplyDataPlaneKonnectExtension(t *testing.T) {
konnectExt *operatorv1alpha1.DataPlaneKonnectExtension
secret *corev1.Secret
expectedError error
message string
}{
{
name: "no extensions",
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestApplyDataPlaneKonnectExtension(t *testing.T) {
},
},
expectedError: ErrCrossNamespaceReference,
message: "The cross-namespace reference to the extension other/konnect-ext is not permitted",
},
{
name: "Extension not found",
Expand Down Expand Up @@ -117,6 +119,7 @@ func TestApplyDataPlaneKonnectExtension(t *testing.T) {
},
},
expectedError: ErrKonnectExtensionNotFound,
message: "The extension default/konnect-ext referenced by the DataPlane is not found",
},
{
name: "Extension properly referenced, secret not found",
Expand Down Expand Up @@ -162,6 +165,7 @@ func TestApplyDataPlaneKonnectExtension(t *testing.T) {
},
},
expectedError: ErrClusterCertificateNotFound,
message: "The cluster certificate secret default/cluster-cert-secret referenced by the extension default/konnect-ext is not found",
},
{
name: "Extension properly referenced, no deployment Options set.",
Expand Down Expand Up @@ -291,7 +295,8 @@ func TestApplyDataPlaneKonnectExtension(t *testing.T) {
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build()

dataplane := tt.dataplane.DeepCopy()
err := applyDataPlaneKonnectExtension(context.Background(), cl, dataplane)
message, err := applyDataPlaneKonnectExtension(context.Background(), cl, dataplane)
require.Equal(t, tt.message, message)
if tt.expectedError != nil {
require.ErrorIs(t, err, tt.expectedError)
} else {
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 fa3f744

Please sign in to comment.