From f771cae54560a4bdb56acae6a59dc9e6ebfd07e3 Mon Sep 17 00:00:00 2001 From: Craig Willis Date: Tue, 24 Oct 2017 17:10:19 -0500 Subject: [PATCH] NDS-1069: Do not include port in single-port spec ingress hosts (#217) * Check that service from event still exists * Only add port to ingress/host if > 1 * Removed port from endpoints --- apiserver/cmd/server/server.go | 12 +++++++- apiserver/pkg/kube/kube.go | 53 ++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/apiserver/cmd/server/server.go b/apiserver/cmd/server/server.go index 80d6261f..d7e8f336 100644 --- a/apiserver/cmd/server/server.go +++ b/apiserver/cmd/server/server.go @@ -2224,7 +2224,12 @@ func (s *Server) getStackWithStatus(userId string, sid string) (*api.Stack, erro endpoint.Protocol = specPort.Protocol endpoint.NodePort = k8port.NodePort if s.useLoadBalancer() && spec.Access == api.AccessExternal { - endpoint.Host = fmt.Sprintf("%s-%d.%s", stackService.Id, specPort.Port, s.domain) + if len(spec.Ports) == 1 { + endpoint.Host = fmt.Sprintf("%s.%s", stackService.Id, s.domain) + } else { + endpoint.Host = fmt.Sprintf("%s-%d.%s", stackService.Id, specPort.Port, s.domain) + } + endpoint.Path = specPort.ContextPath endpoint.URL = endpoint.Host + specPort.ContextPath } @@ -2505,6 +2510,11 @@ func (s *Server) HandlePodEvent(eventType watch.EventType, event *k8api.Event, p } } + if stackService == nil { + glog.Errorf("No such stack service: %s\n", ssid) + return + } + if event != nil { // This is a general Event if event.Reason == "MissingClusterDNS" { diff --git a/apiserver/pkg/kube/kube.go b/apiserver/pkg/kube/kube.go index 062eb9be..474942d4 100644 --- a/apiserver/pkg/kube/kube.go +++ b/apiserver/pkg/kube/kube.go @@ -855,8 +855,8 @@ func (k *KubeHelper) CreateControllerTemplate(ns string, name string, stack stri } else if len(spec.Image.Tags) > 0 { tag = spec.Image.Tags[0] } else { - tag = "latest" - } + tag = "latest" + } k8template := api.PodTemplateSpec{ ObjectMeta: api.ObjectMeta{ Labels: map[string]string{ @@ -1181,27 +1181,18 @@ func (k *KubeHelper) CreateIngress(pid string, domain string, service string, po } hosts := []string{} - for _, port := range ports { - - host := fmt.Sprintf("%s-%d.%s", service, port.Port, domain) - rule := extensions.IngressRule{ - Host: host, - IngressRuleValue: extensions.IngressRuleValue{ - HTTP: &extensions.HTTPIngressRuleValue{ - Paths: []extensions.HTTPIngressPath{ - extensions.HTTPIngressPath{ - Path: "/", - Backend: extensions.IngressBackend{ - ServiceName: service, - ServicePort: intstr.FromInt(int(port.Port)), - }, - }, - }, - }, - }, - } + if len(ports) == 1 { + host := fmt.Sprintf("%s.%s", service, domain) + rule := k.createIngressRule(service, host, int(ports[0].Port)) ingress.Spec.Rules = append(ingress.Spec.Rules, rule) hosts = append(hosts, host) + } else { + for _, port := range ports { + host := fmt.Sprintf("%s-%d.%s", service, port.Port, domain) + rule := k.createIngressRule(service, host, int(port.Port)) + ingress.Spec.Rules = append(ingress.Spec.Rules, rule) + hosts = append(hosts, host) + } } annotations := map[string]string{} @@ -1229,6 +1220,26 @@ func (k *KubeHelper) CreateIngress(pid string, domain string, service string, po return k.CreateUpdateIngress(pid, ingress, update) } +func (k *KubeHelper) createIngressRule(service string, host string, port int) extensions.IngressRule { + rule := extensions.IngressRule{ + Host: host, + IngressRuleValue: extensions.IngressRuleValue{ + HTTP: &extensions.HTTPIngressRuleValue{ + Paths: []extensions.HTTPIngressPath{ + extensions.HTTPIngressPath{ + Path: "/", + Backend: extensions.IngressBackend{ + ServiceName: service, + ServicePort: intstr.FromInt(port), + }, + }, + }, + }, + }, + } + return rule +} + func (k *KubeHelper) CreateUpdateIngress(pid string, ingress *extensions.Ingress, update bool) (*extensions.Ingress, error) { glog.V(4).Info("Updating ingress " + ingress.Name)