diff --git a/internal/ingress/annotations/annotations.go b/internal/ingress/annotations/annotations.go index 30a5dbfb8..66337710b 100644 --- a/internal/ingress/annotations/annotations.go +++ b/internal/ingress/annotations/annotations.go @@ -21,6 +21,7 @@ import ( "github.com/imdario/mergo" "k8s.io/ingress-nginx/internal/ingress/annotations/sslcipher" + apiv1 "k8s.io/api/core/v1" extensions "k8s.io/api/extensions/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -72,7 +73,7 @@ type Ingress struct { ConfigurationSnippet string Connection connection.Config CorsConfig cors.Config - DefaultBackend string + DefaultBackend *apiv1.Service Denied error ExternalAuth authreq.Config HealthCheck healthcheck.Config diff --git a/internal/ingress/controller/controller.go b/internal/ingress/controller/controller.go index bcf5cacf6..33c54af9c 100644 --- a/internal/ingress/controller/controller.go +++ b/internal/ingress/controller/controller.go @@ -451,6 +451,7 @@ func (n *NGINXController) getBackendServers(ingresses []*extensions.Ingress) ([] loc.GRPC = anns.GRPC loc.LuaRestyWAF = anns.LuaRestyWAF loc.InfluxDB = anns.InfluxDB + loc.DefaultBackend = anns.DefaultBackend if loc.Redirect.FromToWWW { server.RedirectFromToWWW = true @@ -488,6 +489,7 @@ func (n *NGINXController) getBackendServers(ingresses []*extensions.Ingress) ([] GRPC: anns.GRPC, LuaRestyWAF: anns.LuaRestyWAF, InfluxDB: anns.InfluxDB, + DefaultBackend: anns.DefaultBackend, } if loc.Redirect.FromToWWW { diff --git a/test/e2e/annotations/default_backend.go b/test/e2e/annotations/default_backend.go new file mode 100644 index 000000000..05fcc807c --- /dev/null +++ b/test/e2e/annotations/default_backend.go @@ -0,0 +1,58 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package annotations + +import ( + "fmt" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "k8s.io/ingress-nginx/test/e2e/framework" +) + +var _ = framework.IngressNginxDescribe("Annotations - custom default-backend", func() { + f := framework.NewDefaultFramework("default-backend") + + BeforeEach(func() { + err := f.NewEchoDeployment() + Expect(err).NotTo(HaveOccurred()) + }) + + Context("when default backend annotation is enabled", func() { + It("should use a custom default backend as upstream", func() { + host := "default-backend" + + annotations := map[string]string{ + "nginx.ingress.kubernetes.io/default-backend": "http-svc", + } + ing, err := f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.IngressController.Namespace, "invalid", 80, &annotations)) + Expect(err).NotTo(HaveOccurred()) + Expect(ing).NotTo(BeNil()) + + time.Sleep(5 * time.Second) + + err = f.WaitForNginxServer(host, + func(server string) bool { + return Expect(server).Should(ContainSubstring(fmt.Sprintf("server_name %v", host))) && + Expect(server).Should(ContainSubstring("proxy_pass http://custom-default-backend")) + }) + Expect(err).NotTo(HaveOccurred()) + }) + }) +})