2019-06-17 19:10:51 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 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 servicebackend
|
|
|
|
|
|
|
|
import (
|
2020-04-29 15:30:50 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-19 03:08:56 +00:00
|
|
|
"net/http"
|
2019-06-17 19:10:51 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-09-05 11:02:36 +00:00
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework/httpexpect"
|
|
|
|
|
2022-07-31 16:16:28 +00:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-04-29 15:30:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-17 19:10:51 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2019-06-17 19:10:51 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
const echoHost = "echo"
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("[Service] Type ExternalName", func() {
|
2023-06-12 10:25:49 +00:00
|
|
|
f := framework.NewDefaultFramework("type-externalname", framework.WithHTTPBunEnabled())
|
2019-06-17 19:10:51 +00:00
|
|
|
|
2020-11-27 17:26:53 +00:00
|
|
|
ginkgo.It("works with external name set to incomplete fqdn", func() {
|
2019-08-14 19:58:02 +00:00
|
|
|
f.NewEchoDeployment()
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2019-08-14 19:58:02 +00:00
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
svc := &corev1.Service{
|
2019-08-14 19:58:02 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-06-12 10:25:49 +00:00
|
|
|
Name: framework.NIPService,
|
2019-08-14 19:58:02 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
2019-09-01 18:16:52 +00:00
|
|
|
ExternalName: framework.EchoService,
|
2019-08-14 19:58:02 +00:00
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.NIPService,
|
|
|
|
80,
|
|
|
|
nil)
|
2019-08-14 19:58:02 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2019-08-14 19:58:02 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return 200 for service type=ExternalName without a port defined", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2019-06-17 19:10:51 +00:00
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
svc := &corev1.Service{
|
2019-06-17 19:10:51 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-05-10 14:43:02 +00:00
|
|
|
Name: framework.NIPService,
|
2019-06-17 19:10:51 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
2023-06-12 10:25:49 +00:00
|
|
|
ExternalName: f.GetNIPHost(),
|
2019-06-17 19:10:51 +00:00
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
2023-06-12 10:25:49 +00:00
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": f.GetNIPHost(),
|
2020-06-16 14:37:57 +00:00
|
|
|
}
|
2023-06-12 10:25:49 +00:00
|
|
|
|
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.HTTPBunService,
|
|
|
|
80,
|
|
|
|
annotations)
|
2019-06-17 19:10:51 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2019-06-17 19:10:51 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return 200 for service type=ExternalName with a port defined", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2019-06-17 19:10:51 +00:00
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
svc := framework.BuildNIPExternalNameService(f, f.HTTPBunIP, host)
|
2019-06-17 19:10:51 +00:00
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
2023-06-12 10:25:49 +00:00
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": f.GetNIPHost(),
|
2020-06-16 14:37:57 +00:00
|
|
|
}
|
2023-06-12 10:25:49 +00:00
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.HTTPBunService,
|
|
|
|
80,
|
|
|
|
annotations)
|
2019-06-17 19:10:51 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2019-06-17 19:10:51 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return status 502 for service type=ExternalName with an invalid host", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2019-06-17 19:10:51 +00:00
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
svc := &corev1.Service{
|
2019-06-17 19:10:51 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-06-12 10:25:49 +00:00
|
|
|
Name: framework.NIPService,
|
2019-06-17 19:10:51 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "invalid.hostname",
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.NIPService,
|
|
|
|
80,
|
|
|
|
nil)
|
2019-06-17 19:10:51 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
2020-07-01 21:19:51 +00:00
|
|
|
StatusRange(httpexpect.Status5xx)
|
2019-06-17 19:10:51 +00:00
|
|
|
})
|
2019-08-15 16:09:42 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return 200 for service type=ExternalName using a port name", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2019-08-15 16:09:42 +00:00
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
svc := framework.BuildNIPExternalNameService(f, f.HTTPBunIP, host)
|
2019-08-15 16:09:42 +00:00
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
2023-06-12 10:25:49 +00:00
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": f.GetNIPHost(),
|
2020-06-16 14:37:57 +00:00
|
|
|
}
|
2023-06-12 10:25:49 +00:00
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.HTTPBunService,
|
|
|
|
80,
|
|
|
|
annotations)
|
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
namedBackend := networking.IngressBackend{
|
|
|
|
Service: &networking.IngressServiceBackend{
|
2023-05-10 14:43:02 +00:00
|
|
|
Name: framework.NIPService,
|
2021-08-21 20:42:00 +00:00
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Name: host,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-06-12 10:25:49 +00:00
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
ing.Spec.Rules[0].HTTP.Paths[0].Backend = namedBackend
|
2019-08-15 16:09:42 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2019-08-15 16:09:42 +00:00
|
|
|
})
|
2020-04-29 15:30:50 +00:00
|
|
|
|
2020-12-13 14:46:13 +00:00
|
|
|
ginkgo.It("should return 200 for service type=ExternalName using FQDN with trailing dot", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2020-12-13 14:46:13 +00:00
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
svc := &corev1.Service{
|
2020-12-13 14:46:13 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2023-05-10 14:43:02 +00:00
|
|
|
Name: framework.NIPService,
|
2020-12-13 14:46:13 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
2023-06-12 10:25:49 +00:00
|
|
|
ExternalName: f.GetNIPHost(),
|
2020-12-13 14:46:13 +00:00
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.HTTPBunService,
|
|
|
|
80,
|
|
|
|
nil)
|
2020-12-13 14:46:13 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
|
|
|
})
|
|
|
|
|
2020-04-29 15:30:50 +00:00
|
|
|
ginkgo.It("should update the external name after a service update", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2020-04-29 15:30:50 +00:00
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
svc := framework.BuildNIPExternalNameService(f, f.HTTPBunIP, host)
|
2020-04-29 15:30:50 +00:00
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
2023-06-12 10:25:49 +00:00
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": f.GetNIPHost(),
|
2020-06-16 14:37:57 +00:00
|
|
|
}
|
2023-05-10 14:43:02 +00:00
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.HTTPBunService,
|
|
|
|
80,
|
|
|
|
annotations)
|
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
namedBackend := networking.IngressBackend{
|
|
|
|
Service: &networking.IngressServiceBackend{
|
2023-05-10 14:43:02 +00:00
|
|
|
Name: framework.NIPService,
|
2021-08-21 20:42:00 +00:00
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Name: host,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ing.Spec.Rules[0].HTTP.Paths[0].Backend = namedBackend
|
2020-04-29 15:30:50 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
|
|
|
body := f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK).
|
|
|
|
Body().
|
|
|
|
Raw()
|
|
|
|
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, `"X-Forwarded-Host": "echo"`)
|
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
svc, err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
|
|
|
Services(f.Namespace).
|
|
|
|
Get(context.TODO(), framework.NIPService, metav1.GetOptions{})
|
2023-05-10 14:43:02 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error obtaining external service")
|
2020-04-29 15:30:50 +00:00
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
// Deploy a new instance to switch routing to
|
2023-06-12 10:25:49 +00:00
|
|
|
ip := f.NewHttpbunDeployment(framework.WithDeploymentName("eu-server"))
|
2023-05-10 14:43:02 +00:00
|
|
|
svc.Spec.ExternalName = framework.BuildNIPHost(ip)
|
2020-04-29 15:30:50 +00:00
|
|
|
|
2023-06-12 10:25:49 +00:00
|
|
|
_, err = f.KubeClientSet.
|
|
|
|
CoreV1().
|
|
|
|
Services(f.Namespace).
|
|
|
|
Update(context.Background(), svc, metav1.UpdateOptions{})
|
2023-05-10 14:43:02 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error updating external service")
|
2020-04-29 15:30:50 +00:00
|
|
|
|
2020-06-27 15:57:00 +00:00
|
|
|
framework.Sleep()
|
2020-04-29 15:30:50 +00:00
|
|
|
|
|
|
|
body = f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK).
|
|
|
|
Body().
|
|
|
|
Raw()
|
|
|
|
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, `"X-Forwarded-Host": "echo"`)
|
|
|
|
|
2023-05-10 14:43:02 +00:00
|
|
|
ginkgo.By("checking the service is updated to use new host")
|
2023-08-12 03:21:19 +00:00
|
|
|
dbgCmd := "/dbg backends all"
|
|
|
|
output, err := f.ExecIngressPod(dbgCmd)
|
2020-04-29 15:30:50 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
2023-06-12 10:25:49 +00:00
|
|
|
assert.Contains(
|
|
|
|
ginkgo.GinkgoT(),
|
|
|
|
output,
|
2023-08-31 07:36:48 +00:00
|
|
|
fmt.Sprintf(`"address": %q`, framework.BuildNIPHost(ip)),
|
2023-06-12 10:25:49 +00:00
|
|
|
)
|
2020-04-29 15:30:50 +00:00
|
|
|
})
|
2021-09-07 17:47:16 +00:00
|
|
|
|
|
|
|
ginkgo.It("should sync ingress on external name service addition/deletion", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := echoHost
|
2021-09-07 17:47:16 +00:00
|
|
|
|
|
|
|
// Create the Ingress first
|
2023-06-12 10:25:49 +00:00
|
|
|
ing := framework.NewSingleIngress(host,
|
|
|
|
"/",
|
|
|
|
host,
|
|
|
|
f.Namespace,
|
|
|
|
framework.NIPService,
|
|
|
|
80,
|
|
|
|
nil)
|
2021-09-07 17:47:16 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Nginx should return 503 without the underlying service being available
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusServiceUnavailable)
|
|
|
|
|
|
|
|
// Now create the service
|
2023-06-12 10:25:49 +00:00
|
|
|
svc := framework.BuildNIPExternalNameService(f, f.HTTPBunIP, host)
|
2021-09-07 17:47:16 +00:00
|
|
|
f.EnsureService(svc)
|
|
|
|
|
|
|
|
framework.Sleep()
|
|
|
|
|
|
|
|
// 503 should change to 200 OK
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
|
|
|
|
|
|
|
// And back to 503 after deleting the service
|
2023-06-12 10:25:49 +00:00
|
|
|
err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
|
|
|
Services(f.Namespace).
|
|
|
|
Delete(context.TODO(), framework.NIPService, metav1.DeleteOptions{})
|
2023-05-10 14:43:02 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error deleting external service")
|
2021-09-07 17:47:16 +00:00
|
|
|
|
|
|
|
framework.Sleep()
|
|
|
|
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET("/get").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusServiceUnavailable)
|
|
|
|
})
|
2019-06-17 19:10:51 +00:00
|
|
|
})
|