Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AV-220211 check listener protocol at start of validation #1557

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions ako-gateway-api/k8s/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ func isValidListener(key string, gateway *gatewayv1.Gateway, gatewayStatus *gate
Status(metav1.ConditionFalse).
ObservedGeneration(gateway.ObjectMeta.Generation)

// protocol validation
if listener.Protocol != gatewayv1.HTTPProtocolType &&
listener.Protocol != gatewayv1.HTTPSProtocolType {
utils.AviLog.Errorf("key: %s, msg: protocol is not supported for listener %s", key, listener.Name)
defaultCondition.
Reason(string(gatewayv1.ListenerReasonUnsupportedProtocol)).
Message("Unsupported protocol").
SetIn(&gatewayStatus.Listeners[index].Conditions)
programmedCondition.SetIn(&gatewayStatus.Listeners[index].Conditions)
gatewayStatus.Listeners[index].SupportedKinds = akogatewayapilib.SupportedKinds[gatewayv1.HTTPSProtocolType]
return false
}

// hostname should not overlap with hostname of an existing gateway
gatewayNsList, err := akogatewayapilib.AKOControlConfig().GatewayApiInformers().GatewayInformer.Lister().Gateways(gateway.Namespace).List(labels.Set(nil).AsSelector())
if err != nil {
Expand Down Expand Up @@ -208,19 +221,6 @@ func isValidListener(key string, gateway *gatewayv1.Gateway, gatewayStatus *gate
}
}

// protocol validation
if listener.Protocol != gatewayv1.HTTPProtocolType &&
listener.Protocol != gatewayv1.HTTPSProtocolType {
utils.AviLog.Errorf("key: %s, msg: protocol is not supported for listener %s", key, listener.Name)
defaultCondition.
Reason(string(gatewayv1.ListenerReasonUnsupportedProtocol)).
Message("Unsupported protocol").
SetIn(&gatewayStatus.Listeners[index].Conditions)
programmedCondition.SetIn(&gatewayStatus.Listeners[index].Conditions)
gatewayStatus.Listeners[index].SupportedKinds = akogatewayapilib.SupportedKinds[gatewayv1.HTTPSProtocolType]
return false
}

resolvedRefCondition := akogatewayapistatus.NewCondition().
Type(string(gatewayv1.ListenerConditionResolvedRefs)).
Status(metav1.ConditionFalse).
Expand Down
53 changes: 53 additions & 0 deletions tests/gatewayapitests/status/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,3 +1344,56 @@ func TestMultipleGatewayOverlappingHostname(t *testing.T) {
tests.TeardownGateway(t, gatewayName1, DEFAULT_NAMESPACE)
tests.TeardownGatewayClass(t, gatewayClassName)
}

func TestGatewayWithUnsupportedProtocolAndHostnameInListeners(t *testing.T) {

gatewayName := "gateway-neg-11"
gatewayClassName := "gateway-class-neg-11"
ports := []int32{8080, 8081}

tests.SetupGatewayClass(t, gatewayClassName, akogatewayapilib.GatewayController)
listeners := tests.GetListenersV1(ports, false, false)
listeners[0].Protocol = "GRPC"
hostName := "invalid"
listeners[0].Hostname = (*gatewayv1.Hostname)(&hostName)
tests.SetupGateway(t, gatewayName, DEFAULT_NAMESPACE, gatewayClassName, nil, listeners)

g := gomega.NewGomegaWithT(t)
g.Eventually(func() bool {
gateway, err := tests.GatewayClient.GatewayV1().Gateways(DEFAULT_NAMESPACE).Get(context.TODO(), gatewayName, metav1.GetOptions{})
if err != nil || gateway == nil {
t.Logf("Couldn't get the gateway, err: %+v", err)
return false
}
return apimeta.FindStatusCondition(gateway.Status.Conditions, string(gatewayv1.GatewayConditionAccepted)) != nil
}, 30*time.Second).Should(gomega.Equal(true))

expectedStatus := &gatewayv1.GatewayStatus{
Conditions: []metav1.Condition{
{
Type: string(gatewayv1.GatewayConditionAccepted),
Status: metav1.ConditionTrue,
Message: "Gateway contains atleast one valid listener",
ObservedGeneration: 1,
Reason: string(gatewayv1.GatewayReasonListenersNotValid),
},
},
Listeners: tests.GetListenerStatusV1(ports, []int32{0, 0}, true, false),
}
expectedStatus.Listeners[0].Conditions[0].Reason = string(gatewayv1.ListenerReasonUnsupportedProtocol)
expectedStatus.Listeners[0].Conditions[0].Status = metav1.ConditionFalse
expectedStatus.Listeners[0].Conditions[0].Message = "Unsupported protocol"

expectedStatus.Listeners[1].Conditions[0].Reason = string(gatewayv1.ListenerReasonAccepted)
expectedStatus.Listeners[1].Conditions[0].Status = metav1.ConditionTrue
expectedStatus.Listeners[1].Conditions[0].Message = "Listener is valid"

gateway, err := tests.GatewayClient.GatewayV1().Gateways(DEFAULT_NAMESPACE).Get(context.TODO(), gatewayName, metav1.GetOptions{})
if err != nil || gateway == nil {
t.Fatalf("Couldn't get the gateway, err: %+v", err)
}

tests.ValidateGatewayStatus(t, &gateway.Status, expectedStatus)
tests.TeardownGateway(t, gatewayName, DEFAULT_NAMESPACE)
tests.TeardownGatewayClass(t, gatewayClassName)
}
Loading