Skip to content

Commit

Permalink
[backport release/3.0.x] fix: Ingress with default backend targeting …
Browse files Browse the repository at this point in the history
…the same Service (#5188) (#5219)
  • Loading branch information
programmer04 authored Nov 21, 2023
1 parent bf8cd6f commit dd24f50
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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

0 comments on commit dd24f50

Please sign in to comment.