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"
|
|
|
|
|
2020-07-01 21:19:51 +00:00
|
|
|
"github.com/gavv/httpexpect/v2"
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/onsi/ginkgo"
|
2020-04-29 15:30:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-17 19:10:51 +00:00
|
|
|
core "k8s.io/api/core/v1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
|
2020-04-29 15:30:50 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/nginx"
|
2019-06-17 19:10:51 +00:00
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("[Service] Type ExternalName", func() {
|
2019-06-17 19:10:51 +00:00
|
|
|
f := framework.NewDefaultFramework("type-externalname")
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("works with external name set to incomplete fdqn", func() {
|
2019-08-14 19:58:02 +00:00
|
|
|
f.NewEchoDeployment()
|
|
|
|
|
|
|
|
host := "echo"
|
|
|
|
|
|
|
|
svc := &core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2019-09-01 18:16:52 +00:00
|
|
|
Name: framework.HTTPBinService,
|
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)
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.HTTPBinService, 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() {
|
2019-06-17 19:10:51 +00:00
|
|
|
host := "echo"
|
|
|
|
|
|
|
|
svc := &core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2019-09-01 18:16:52 +00:00
|
|
|
Name: framework.HTTPBinService,
|
2019-06-17 19:10:51 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "httpbin.org",
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": "httpbin.org",
|
|
|
|
}
|
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.HTTPBinService, 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() {
|
2019-06-17 19:10:51 +00:00
|
|
|
host := "echo"
|
|
|
|
|
|
|
|
svc := &core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2019-09-01 18:16:52 +00:00
|
|
|
Name: framework.HTTPBinService,
|
2019-06-17 19:10:51 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "httpbin.org",
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: host,
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(80),
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": "httpbin.org",
|
|
|
|
}
|
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.HTTPBinService, 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() {
|
2019-06-17 19:10:51 +00:00
|
|
|
host := "echo"
|
|
|
|
|
|
|
|
svc := &core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2019-09-01 18:16:52 +00:00
|
|
|
Name: framework.HTTPBinService,
|
2019-06-17 19:10:51 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "invalid.hostname",
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.HTTPBinService, 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() {
|
2019-08-15 16:09:42 +00:00
|
|
|
host := "echo"
|
|
|
|
|
|
|
|
svc := &core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2019-09-01 18:16:52 +00:00
|
|
|
Name: framework.HTTPBinService,
|
2019-08-15 16:09:42 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "httpbin.org",
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: host,
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(80),
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": "httpbin.org",
|
|
|
|
}
|
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.HTTPBinService, 80, annotations)
|
2019-08-15 16:09:42 +00:00
|
|
|
ing.Spec.Rules[0].HTTP.Paths[0].Backend.ServicePort = intstr.FromString(host)
|
|
|
|
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() {
|
|
|
|
host := "echo"
|
|
|
|
|
|
|
|
svc := &core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: framework.HTTPBinService,
|
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "httpbin.org.",
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.HTTPBinService, 80, nil)
|
|
|
|
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() {
|
|
|
|
host := "echo"
|
|
|
|
|
|
|
|
svc := &core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: framework.HTTPBinService,
|
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "httpbin.org",
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: host,
|
|
|
|
Port: 80,
|
|
|
|
TargetPort: intstr.FromInt(80),
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2020-06-16 14:37:57 +00:00
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/upstream-vhost": "httpbin.org",
|
|
|
|
}
|
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.HTTPBinService, 80, annotations)
|
2020-04-29 15:30:50 +00:00
|
|
|
ing.Spec.Rules[0].HTTP.Paths[0].Backend.ServicePort = intstr.FromString(host)
|
|
|
|
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"`)
|
|
|
|
|
|
|
|
svc, err := f.KubeClientSet.CoreV1().Services(f.Namespace).Get(context.TODO(), framework.HTTPBinService, metav1.GetOptions{})
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error obtaining httpbin service")
|
|
|
|
|
|
|
|
svc.Spec.ExternalName = "eu.httpbin.org"
|
|
|
|
|
|
|
|
_, err = f.KubeClientSet.CoreV1().Services(f.Namespace).Update(context.Background(), svc, metav1.UpdateOptions{})
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "unexpected error updating httpbin service")
|
|
|
|
|
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"`)
|
|
|
|
|
|
|
|
ginkgo.By("checking the service is updated to use eu.httpbin.org")
|
|
|
|
curlCmd := fmt.Sprintf("curl --fail --silent http://localhost:%v/configuration/backends", nginx.StatusPort)
|
|
|
|
output, err := f.ExecIngressPod(curlCmd)
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), output, `{"address":"eu.httpbin.org"`)
|
|
|
|
})
|
2019-06-17 19:10:51 +00:00
|
|
|
})
|