Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Adds IngressClassName Validation (#388)
Browse files Browse the repository at this point in the history
Signed-off-by: Daneyon Hansen <daneyonhansen@gmail.com>
  • Loading branch information
danehans committed Jun 21, 2021
1 parent fb68b1e commit 892c8c1
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
retryable "github.com/projectcontour/contour-operator/internal/retryableerror"
"github.com/projectcontour/contour-operator/pkg/slice"

networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
Expand Down Expand Up @@ -65,6 +67,10 @@ func Contour(ctx context.Context, cli client.Client, contour *operatorv1alpha1.C
}
}

if err := IngressClass(ctx, cli, contour); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -169,6 +175,19 @@ func LoadBalancerProvider(contour *operatorv1alpha1.Contour) error {
return nil
}

// IngressClass validates ingressClassName of the provided contour.
func IngressClass(ctx context.Context, cli client.Client, contour *operatorv1alpha1.Contour) error {
if contour.Spec.IngressClassName != nil {
name := *contour.Spec.IngressClassName
ic := &networkingv1.IngressClass{}
key := types.NamespacedName{Name: name}
if err := cli.Get(ctx, key, ic); err != nil {
return fmt.Errorf("failed to get ingressclass %s: %w", name, err)
}
}
return nil
}

// GatewayClass returns nil if gc is a valid GatewayClass,
// otherwise an error.
func GatewayClass(gc *gatewayv1alpha1.GatewayClass) error {
Expand Down
65 changes: 65 additions & 0 deletions pkg/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/projectcontour/contour-operator/pkg/validation"

corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -476,6 +477,70 @@ func TestNodePorts(t *testing.T) {
}
}

func TestIngressClass(t *testing.T) {
ctx := context.Background()

cntr := &operatorv1alpha1.Contour{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "ns",
},
Spec: operatorv1alpha1.ContourSpec{},
}

ic := &networkingv1.IngressClass{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ic",
},
Spec: networkingv1.IngressClassSpec{},
}

testCases := map[string]struct {
class string
exists bool
expected bool
}{
"undefined ingress class name": {
expected: true,
},
"ingress class name exists": {
class: "test-ic",
expected: true,
},
"ingress class name doesn't exist": {
class: "not-exist",
expected: false,
},
}

// Build and create and instance of the client
builder := fake.NewClientBuilder()
scheme := operator.GetOperatorScheme()
builder.WithScheme(scheme)
cl := builder.Build()

// Created the referenced ingress class.
if err := cl.Create(ctx, ic); err != nil {
t.Fatalf("failed to create ingressclass: %#v", err)
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
mutated := cntr.DeepCopy()
if len(tc.class) > 0 {
mutated.Spec.IngressClassName = pointer.StringPtr(tc.class)
}
err := validation.IngressClass(ctx, cl, mutated)
if err != nil && tc.expected {
t.Fatalf("failed with error: %#v", err)
}
if err == nil && !tc.expected {
t.Fatalf("expected to fail but received no error")
}
})
}
}

func TestGatewayClass(t *testing.T) {

testCases := map[string]struct {
Expand Down

0 comments on commit 892c8c1

Please sign in to comment.