Skip to content

Commit

Permalink
Add testing for frontendConfig->IR and IR->GatewayExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
sawsa307 committed Sep 28, 2024
1 parent 287470a commit e123362
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 312 deletions.
70 changes: 64 additions & 6 deletions pkg/i2gw/providers/gce/gateway_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ import (
)

const (
testGatewayName = "test-gateway"
testHTTPRouteName = "test-http-route"
testSaBackendPolicyName = testServiceName
testGatewayName = "test-gateway"
testHTTPRouteName = "test-http-route"
testSaGCPBackendPolicyName = testServiceName
testSslGCPGatewayPolicyName = testGatewayName
)

var (
Expand Down Expand Up @@ -96,7 +97,7 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: testSaBackendPolicyName,
Name: testSaGCPBackendPolicyName,
},
Spec: gkegatewayv1.GCPBackendPolicySpec{
Default: &gkegatewayv1.GCPBackendPolicyConfig{
Expand All @@ -120,7 +121,7 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: testSaBackendPolicyName,
Name: testSaGCPBackendPolicyName,
},
Spec: gkegatewayv1.GCPBackendPolicySpec{
Default: &gkegatewayv1.GCPBackendPolicyConfig{
Expand All @@ -143,7 +144,7 @@ var (
},
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: testSaBackendPolicyName,
Name: testSaGCPBackendPolicyName,
},
Spec: gkegatewayv1.GCPBackendPolicySpec{
Default: &gkegatewayv1.GCPBackendPolicyConfig{
Expand All @@ -156,6 +157,27 @@ var (
},
},
}

testSslGCPGatewayPolicy = gkegatewayv1.GCPGatewayPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "networking.gke.io/v1",
Kind: "GCPGatewayPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: testSslGCPGatewayPolicyName,
},
Spec: gkegatewayv1.GCPGatewayPolicySpec{
Default: &gkegatewayv1.GCPGatewayPolicyConfig{
SslPolicy: testSslPolicy,
},
TargetRef: v1alpha2.NamespacedPolicyTargetReference{
Group: "gateway.networking.k8s.io",
Kind: "Gateway",
Name: gatewayv1.ObjectName(testGatewayName),
},
},
}
)

func Test_irToGateway(t *testing.T) {
Expand All @@ -171,6 +193,10 @@ func Test_irToGateway(t *testing.T) {
if err != nil {
t.Errorf("Failed to generate unstructured GCP Backend Policy with Security Policy feature: %v", err)
}
testSslGCPGatewayPolicyUnstructured, err := i2gw.CastToUnstructured(&testSslGCPGatewayPolicy)
if err != nil {
t.Errorf("Failed to generate unstructured GCP Gateway Policy with Ssl Policy feature: %v", err)
}

testCases := []struct {
name string
Expand Down Expand Up @@ -287,6 +313,38 @@ func Test_irToGateway(t *testing.T) {
},
expectedErrors: field.ErrorList{},
},
{
name: "ingress with a Frontend Config specifying Ssl Policy",
ir: intermediate.IR{
Gateways: map[types.NamespacedName]intermediate.GatewayContext{
{Namespace: testNamespace, Name: testGatewayName}: {
Gateway: testGateway,
ProviderSpecificIR: intermediate.ProviderSpecificGatewayIR{
Gce: &intermediate.GceGatewayIR{
SslPolicy: &intermediate.SslPolicyConfig{Name: testSslPolicy},
},
},
},
},
HTTPRoutes: map[types.NamespacedName]intermediate.HTTPRouteContext{
{Namespace: testNamespace, Name: testHTTPRouteName}: {
HTTPRoute: testHTTPRoute,
},
},
},
expectedGatewayResources: i2gw.GatewayResources{
Gateways: map[types.NamespacedName]gatewayv1.Gateway{
{Namespace: testNamespace, Name: testGatewayName}: testGateway,
},
HTTPRoutes: map[types.NamespacedName]gatewayv1.HTTPRoute{
{Namespace: testNamespace, Name: testHTTPRouteName}: testHTTPRoute,
},
GatewayExtensions: []unstructured.Unstructured{
*testSslGCPGatewayPolicyUnstructured,
},
},
expectedErrors: field.ErrorList{},
},
}

for _, tc := range testCases {
Expand Down
18 changes: 10 additions & 8 deletions pkg/i2gw/providers/gce/ir_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ func buildGceGatewayIR(ctx context.Context, storage *storage, ir *intermediate.I
ir.Gateways = make(map[types.NamespacedName]intermediate.GatewayContext)
}

feConfigToIngs := getFrontendConfigMapping(ctx, storage)
feConfigToGwys := getFrontendConfigMapping(ctx, storage)
for feConfigKey, feConfig := range storage.FrontendConfigs {
if feConfig == nil {
continue
}
gceGatewayIR := feConfigToGceGatewayIR(feConfig)
gateways := feConfigToIngs[feConfigKey]
gateways := feConfigToGwys[feConfigKey]

for _, gwyKey := range gateways {
gatewayContext := ir.Gateways[gwyKey]
gatewayContext.ProviderSpecificIR.Gce = &gceGatewayIR
Expand All @@ -108,24 +109,25 @@ func buildGceGatewayIR(ctx context.Context, storage *storage, ir *intermediate.I
}
}

type ingressNames []types.NamespacedName
type gatewayNames []types.NamespacedName

func getFrontendConfigMapping(ctx context.Context, storage *storage) map[types.NamespacedName]ingressNames {
feConfigToIngs := make(map[types.NamespacedName]ingressNames)
func getFrontendConfigMapping(ctx context.Context, storage *storage) map[types.NamespacedName]gatewayNames {
feConfigToGwys := make(map[types.NamespacedName]gatewayNames)

for _, ingress := range storage.Ingresses {
ing := types.NamespacedName{Namespace: ingress.Namespace, Name: ingress.Name}
gwyKey := types.NamespacedName{Namespace: ingress.Namespace, Name: common.GetIngressClass(*ingress)}
// ing := types.NamespacedName{Namespace: ingress.Namespace, Name: ingress.Name}
ctx = context.WithValue(ctx, serviceKey, ingress)

feConfigName, exists := getFrontendConfigAnnotation(ingress)
if exists {
feConfigKey := types.NamespacedName{Namespace: ingress.Namespace, Name: feConfigName}
feConfigToIngs[feConfigKey] = append(feConfigToIngs[feConfigKey], ing)
feConfigToGwys[feConfigKey] = append(feConfigToGwys[feConfigKey], gwyKey)
continue
}

}
return feConfigToIngs
return feConfigToGwys
}

// Get names of the FrontendConfig in the cluster based on the FrontendConfig
Expand Down
Loading

0 comments on commit e123362

Please sign in to comment.