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

[backport release/3.0.x] fix: Ingress with default backend targeting the same Service (#5188) #5219

Merged
merged 1 commit into from
Nov 21, 2023
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
5 changes: 5 additions & 0 deletions internal/dataplane/parser/translate_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (p *Parser) ingressRulesFromIngressV1() ingressRules {
// Add a default backend if it exists.
defaultBackendService, ok := getDefaultBackendService(allDefaultBackends, p.featureFlags.ExpressionRoutes)
if ok {
// When such service would overwrite an existing service, merge the routes.
if svc, ok := result.ServiceNameToServices[*defaultBackendService.Name]; ok {
svc.Routes = append(svc.Routes, defaultBackendService.Routes...)
defaultBackendService = svc
}
result.ServiceNameToServices[*defaultBackendService.Name] = defaultBackendService
result.ServiceNameToParent[*defaultBackendService.Name] = defaultBackendService.Parent
}
Expand Down
42 changes: 42 additions & 0 deletions test/integration/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,48 @@ func TestIngressEssentials(t *testing.T) {
helpers.EventuallyExpectHTTP404WithNoRoute(t, proxyURL, proxyURL.Host, "/test_ingress_essentials", ingressWait, waitTick, nil)
}

func TestIngressDefaultBackend(t *testing.T) {
ctx := context.Background()
ns, cleaner := helpers.Setup(ctx, t, env)

t.Log("deploying a minimal HTTP container deployment to test Ingress routes")
container := generators.NewContainer("httpbin", test.HTTPBinImage, test.HTTPBinPort)
deployment := generators.NewDeploymentForContainer(container)
deployment, err := env.Cluster().Client().AppsV1().Deployments(ns.Name).Create(ctx, deployment, metav1.CreateOptions{})
require.NoError(t, err)
cleaner.Add(deployment)

t.Logf("exposing deployment %s via service", deployment.Name)
service := generators.NewServiceForDeployment(deployment, corev1.ServiceTypeLoadBalancer)
_, err = env.Cluster().Client().CoreV1().Services(ns.Name).Create(ctx, service, metav1.CreateOptions{})
require.NoError(t, err)
cleaner.Add(service)

t.Logf("creating an ingress for service %s with ingress.class %s", service.Name, consts.IngressClass)
ingress := generators.NewIngressForService("/foo", map[string]string{
"konghq.com/strip-path": "true",
}, service)
ingress.Spec.IngressClassName = kong.String(consts.IngressClass)
ingress.Spec.DefaultBackend = &netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: service.Name,
Port: netv1.ServiceBackendPort{
Number: service.Spec.Ports[0].Port,
},
},
}
require.NoError(t, clusters.DeployIngress(ctx, env.Cluster(), ns.Name, ingress))
cleaner.Add(ingress)

t.Log("matching path")
helpers.EventuallyGETPath(t, nil, proxyURL.String(), "/foo", http.StatusOK, "<title>httpbin.org</title>", nil, ingressWait, waitTick)

t.Log("non matching path - use default backend")
helpers.EventuallyGETPath(
t, nil, proxyURL.String(), fmt.Sprintf("/status/%d", http.StatusTeapot), http.StatusTeapot, "", nil, ingressWait, waitTick,
)
}

func TestGRPCIngressEssentials(t *testing.T) {
t.Parallel()

Expand Down