2018-11-16 20:33:56 +00:00
|
|
|
/*
|
|
|
|
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 settings
|
|
|
|
|
|
|
|
import (
|
2019-02-17 22:12:10 +00:00
|
|
|
"context"
|
2018-11-16 20:33:56 +00:00
|
|
|
"fmt"
|
2019-02-17 22:12:10 +00:00
|
|
|
"net"
|
2018-11-16 20:33:56 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
"github.com/parnurzeal/gorequest"
|
|
|
|
|
2018-11-16 20:33:56 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("[TCP] tcp-services", func() {
|
2018-11-16 20:33:56 +00:00
|
|
|
f := framework.NewDefaultFramework("tcp")
|
|
|
|
|
|
|
|
It("should expose a TCP service", func() {
|
|
|
|
f.NewEchoDeploymentWithReplicas(1)
|
|
|
|
|
|
|
|
config, err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
ConfigMaps(f.Namespace).
|
2018-11-16 20:33:56 +00:00
|
|
|
Get("tcp-services", metav1.GetOptions{})
|
|
|
|
Expect(err).To(BeNil(), "unexpected error obtaining tcp-services configmap")
|
|
|
|
Expect(config).NotTo(BeNil(), "expected a configmap but none returned")
|
|
|
|
|
|
|
|
if config.Data == nil {
|
|
|
|
config.Data = map[string]string{}
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
config.Data["8080"] = fmt.Sprintf("%v/%v:80", f.Namespace, framework.EchoService)
|
2019-02-17 22:12:10 +00:00
|
|
|
|
2018-11-16 20:33:56 +00:00
|
|
|
_, err = f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
ConfigMaps(f.Namespace).
|
2018-11-16 20:33:56 +00:00
|
|
|
Update(config)
|
|
|
|
Expect(err).NotTo(HaveOccurred(), "unexpected error updating configmap")
|
|
|
|
|
|
|
|
svc, err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
Services(f.Namespace).
|
2020-02-16 14:58:37 +00:00
|
|
|
Get("nginx-ingress-controller", metav1.GetOptions{})
|
2018-11-16 20:33:56 +00:00
|
|
|
Expect(err).To(BeNil(), "unexpected error obtaining ingress-nginx service")
|
|
|
|
Expect(svc).NotTo(BeNil(), "expected a service but none returned")
|
|
|
|
|
|
|
|
svc.Spec.Ports = append(svc.Spec.Ports, corev1.ServicePort{
|
2019-09-01 18:16:52 +00:00
|
|
|
Name: framework.EchoService,
|
2018-11-16 20:33:56 +00:00
|
|
|
Port: 8080,
|
|
|
|
TargetPort: intstr.FromInt(8080),
|
|
|
|
})
|
|
|
|
_, err = f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
Services(f.Namespace).
|
2018-11-16 20:33:56 +00:00
|
|
|
Update(svc)
|
|
|
|
Expect(err).NotTo(HaveOccurred(), "unexpected error updating service")
|
|
|
|
|
|
|
|
f.WaitForNginxConfiguration(
|
|
|
|
func(cfg string) bool {
|
2019-09-01 18:16:52 +00:00
|
|
|
return strings.Contains(cfg, fmt.Sprintf(`ngx.var.proxy_upstream_name="tcp-%v-%v-80"`, f.Namespace, framework.EchoService))
|
2018-11-16 20:33:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ip := f.GetNginxIP()
|
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(fmt.Sprintf("http://%v:8080", ip)).
|
2018-11-16 20:33:56 +00:00
|
|
|
End()
|
2018-11-18 13:53:05 +00:00
|
|
|
Expect(errs).Should(BeEmpty())
|
2018-11-16 20:33:56 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(200))
|
|
|
|
})
|
2019-02-17 22:12:10 +00:00
|
|
|
|
|
|
|
It("should expose an ExternalName TCP service", func() {
|
|
|
|
// Setup:
|
|
|
|
// - Create an external name service for DNS lookups on port 5353. Point it to google's DNS server
|
|
|
|
// - Expose port 5353 on the nginx ingress NodePort service to open a hole for this test
|
|
|
|
// - Update the `tcp-services` configmap to proxy traffic to the configured external name service
|
|
|
|
|
|
|
|
// Create an external service for DNS
|
|
|
|
externalService := &corev1.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "dns-external-name-svc",
|
2019-02-22 14:03:42 +00:00
|
|
|
Namespace: f.Namespace,
|
2019-02-17 22:12:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: "google-public-dns-a.google.com",
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "dns-external-name-svc",
|
|
|
|
Port: 5353,
|
|
|
|
TargetPort: intstr.FromInt(53),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(externalService)
|
|
|
|
|
|
|
|
// Expose the `external name` port on the `ingress-nginx` service
|
|
|
|
svc, err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
Services(f.Namespace).
|
2020-02-16 14:58:37 +00:00
|
|
|
Get("nginx-ingress-controller", metav1.GetOptions{})
|
2019-02-17 22:12:10 +00:00
|
|
|
Expect(err).To(BeNil(), "unexpected error obtaining ingress-nginx service")
|
|
|
|
Expect(svc).NotTo(BeNil(), "expected a service but none returned")
|
|
|
|
|
|
|
|
svc.Spec.Ports = append(svc.Spec.Ports, corev1.ServicePort{
|
|
|
|
Name: "dns-svc",
|
|
|
|
Port: 5353,
|
|
|
|
TargetPort: intstr.FromInt(5353),
|
|
|
|
})
|
|
|
|
_, err = f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
Services(f.Namespace).
|
2019-02-17 22:12:10 +00:00
|
|
|
Update(svc)
|
|
|
|
Expect(err).NotTo(HaveOccurred(), "unexpected error updating service")
|
|
|
|
|
|
|
|
// Update the TCP configmap to link port 5353 to the DNS external name service
|
|
|
|
config, err := f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
ConfigMaps(f.Namespace).
|
2019-02-17 22:12:10 +00:00
|
|
|
Get("tcp-services", metav1.GetOptions{})
|
|
|
|
Expect(err).To(BeNil(), "unexpected error obtaining tcp-services configmap")
|
|
|
|
Expect(config).NotTo(BeNil(), "expected a configmap but none returned")
|
|
|
|
|
|
|
|
if config.Data == nil {
|
|
|
|
config.Data = map[string]string{}
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:03:42 +00:00
|
|
|
config.Data["5353"] = fmt.Sprintf("%v/dns-external-name-svc:5353", f.Namespace)
|
2019-02-17 22:12:10 +00:00
|
|
|
|
|
|
|
_, err = f.KubeClientSet.
|
|
|
|
CoreV1().
|
2019-02-22 14:03:42 +00:00
|
|
|
ConfigMaps(f.Namespace).
|
2019-02-17 22:12:10 +00:00
|
|
|
Update(config)
|
|
|
|
Expect(err).NotTo(HaveOccurred(), "unexpected error updating configmap")
|
|
|
|
|
|
|
|
// Validate that the generated nginx config contains the expected `proxy_upstream_name` value
|
|
|
|
f.WaitForNginxConfiguration(
|
|
|
|
func(cfg string) bool {
|
2019-02-22 14:03:42 +00:00
|
|
|
return strings.Contains(cfg, fmt.Sprintf(`ngx.var.proxy_upstream_name="tcp-%v-dns-external-name-svc-5353"`, f.Namespace))
|
2019-02-17 22:12:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Execute the test. Use the `external name` service to resolve a domain name.
|
|
|
|
ip := f.GetNginxIP()
|
|
|
|
resolver := net.Resolver{
|
|
|
|
PreferGo: true,
|
|
|
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
d := net.Dialer{}
|
2019-02-22 14:03:42 +00:00
|
|
|
return d.DialContext(ctx, "tcp", fmt.Sprintf("%v:5353", ip))
|
2019-02-17 22:12:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
ips, err := resolver.LookupHost(context.Background(), "google-public-dns-b.google.com")
|
2019-02-18 04:32:27 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "unexpected error from DNS resolver")
|
2019-02-17 22:12:10 +00:00
|
|
|
Expect(ips).Should(ContainElement("8.8.4.4"))
|
|
|
|
|
|
|
|
})
|
2018-11-16 20:33:56 +00:00
|
|
|
})
|