From 654eceda4618343f7bea4ad1d10b079a0bb9e175 Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Fri, 16 Nov 2018 17:33:56 -0300 Subject: [PATCH] Add tcp e2e test --- internal/ingress/controller/nginx.go | 12 ++- .../etc/nginx/lua/tcp_udp_configuration.lua | 4 + test/e2e/e2e.go | 1 + test/e2e/tcpudp/tcp.go | 97 +++++++++++++++++++ 4 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 test/e2e/tcpudp/tcp.go diff --git a/internal/ingress/controller/nginx.go b/internal/ingress/controller/nginx.go index 665e83da1..4f84a74e8 100644 --- a/internal/ingress/controller/nginx.go +++ b/internal/ingress/controller/nginx.go @@ -801,10 +801,10 @@ func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertif } defer conn.Close() - var streams []*ingress.Backend + streams := make([]ingress.Backend, 0) for _, ep := range pcfg.TCPEndpoints { key := fmt.Sprintf("tcp-%v-%v-%v", ep.Backend.Namespace, ep.Backend.Name, ep.Backend.Port.String()) - streams = append(streams, &ingress.Backend{ + streams = append(streams, ingress.Backend{ Name: key, Endpoints: ep.Endpoints, Port: intstr.FromInt(ep.Port), @@ -812,7 +812,7 @@ func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertif } for _, ep := range pcfg.UDPEndpoints { key := fmt.Sprintf("udp-%v-%v-%v", ep.Backend.Namespace, ep.Backend.Name, ep.Backend.Port.String()) - streams = append(streams, &ingress.Backend{ + streams = append(streams, ingress.Backend{ Name: key, Endpoints: ep.Endpoints, Port: intstr.FromInt(ep.Port), @@ -828,7 +828,11 @@ func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertif if err != nil { return err } - fmt.Fprintf(conn, "\r\n") + _, err = fmt.Fprintf(conn, "\r\n") + if err != nil { + return err + } + defer conn.Close() if isDynamicCertificatesEnabled { err = configureCertificates(pcfg, port) diff --git a/rootfs/etc/nginx/lua/tcp_udp_configuration.lua b/rootfs/etc/nginx/lua/tcp_udp_configuration.lua index fc5b402f5..8cc4111cf 100644 --- a/rootfs/etc/nginx/lua/tcp_udp_configuration.lua +++ b/rootfs/etc/nginx/lua/tcp_udp_configuration.lua @@ -25,6 +25,10 @@ function _M.call() return end + if backends == nil or backends == "" then + return + end + local success, err_conf = tcp_udp_configuration_data:set("backends", backends) if not success then ngx.log(ngx.ERR, "dynamic-configuration: error updating configuration: " .. tostring(err_conf)) diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go index ca5f7e860..ea52f9afa 100644 --- a/test/e2e/e2e.go +++ b/test/e2e/e2e.go @@ -39,6 +39,7 @@ import ( _ "k8s.io/ingress-nginx/test/e2e/settings" _ "k8s.io/ingress-nginx/test/e2e/ssl" _ "k8s.io/ingress-nginx/test/e2e/status" + _ "k8s.io/ingress-nginx/test/e2e/tcpudp" ) // RunE2ETests checks configuration parameters (specified through flags) and then runs diff --git a/test/e2e/tcpudp/tcp.go b/test/e2e/tcpudp/tcp.go new file mode 100644 index 000000000..f5f481c8f --- /dev/null +++ b/test/e2e/tcpudp/tcp.go @@ -0,0 +1,97 @@ +/* +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 ( + "fmt" + "strings" + + "github.com/parnurzeal/gorequest" + 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" +) + +var _ = framework.IngressNginxDescribe("TCP Feature", func() { + f := framework.NewDefaultFramework("tcp") + + BeforeEach(func() { + }) + + AfterEach(func() { + }) + + It("should expose a TCP service", func() { + f.NewEchoDeploymentWithReplicas(1) + + config, err := f.KubeClientSet. + CoreV1(). + ConfigMaps(f.IngressController.Namespace). + 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{} + } + + config.Data["8080"] = fmt.Sprintf("%v/http-svc:80", f.IngressController.Namespace) + _, err = f.KubeClientSet. + CoreV1(). + ConfigMaps(f.IngressController.Namespace). + Update(config) + Expect(err).NotTo(HaveOccurred(), "unexpected error updating configmap") + + svc, err := f.KubeClientSet. + CoreV1(). + Services(f.IngressController.Namespace). + Get("ingress-nginx", metav1.GetOptions{}) + 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: "http-svc", + Port: 8080, + TargetPort: intstr.FromInt(8080), + }) + _, err = f.KubeClientSet. + CoreV1(). + Services(f.IngressController.Namespace). + Update(svc) + Expect(err).NotTo(HaveOccurred(), "unexpected error updating service") + + f.WaitForNginxConfiguration( + func(cfg string) bool { + return strings.Contains(cfg, fmt.Sprintf(`ngx.var.proxy_upstream_name="tcp-%v-http-svc-80"`, f.IngressController.Namespace)) + }) + + ip := f.GetNginxIP() + port, err := f.GetNginxPort("http-svc") + Expect(err).NotTo(HaveOccurred(), "unexpected error obtaning service port") + + resp, _, errs := gorequest.New(). + Get(fmt.Sprintf("http://%v:%v", ip, port)). + End() + Expect(len(errs)).Should(BeNumerically("==", 0)) + Expect(resp.StatusCode).Should(Equal(200)) + }) +})