From f8d1b4b326a32795cc562bfee3ef0be4baff8bb2 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Thu, 1 Aug 2019 12:33:44 +0200 Subject: [PATCH] fix(k8s): create a single NodePort Service when a node port is set Previously we were creating two Services, which is not really the behavior that people expect. It also apparently messed with AWS ALB configuration. --- .../plugins/kubernetes/container/service.ts | 24 +++------- .../plugins/kubernetes/container/service.ts | 46 ------------------- 2 files changed, 7 insertions(+), 63 deletions(-) diff --git a/garden-service/src/plugins/kubernetes/container/service.ts b/garden-service/src/plugins/kubernetes/container/service.ts index d890374ee9..5efe21e26c 100644 --- a/garden-service/src/plugins/kubernetes/container/service.ts +++ b/garden-service/src/plugins/kubernetes/container/service.ts @@ -35,37 +35,27 @@ export async function createServiceResources(service: ContainerService, namespac const ports = service.spec.ports if (ports.length) { - addService(service.name, "ClusterIP", ports.map(portSpec => ({ - name: portSpec.name, - protocol: portSpec.protocol, - targetPort: portSpec.containerPort, - port: portSpec.servicePort, - }))) - } - - // optionally add a NodePort service for externally open ports, if applicable - const exposedPorts = ports.filter(portSpec => portSpec.nodePort) + const serviceType = ports.filter(portSpec => !!portSpec.nodePort).length > 0 ? "NodePort" : "ClusterIP" - if (exposedPorts.length > 0) { - const nodePorts = exposedPorts.map(portSpec => { + addService(service.name, serviceType, ports.map(portSpec => { const port: V1ServicePort = { name: portSpec.name, protocol: portSpec.protocol, port: portSpec.servicePort, targetPort: portSpec.containerPort, } - if (portSpec.nodePort !== true) { + + if (portSpec.nodePort && portSpec.nodePort !== true) { port.nodePort = portSpec.nodePort } - return port - }) - addService(service.name + "-nodeport", "NodePort", nodePorts) + return port + })) } return services } -export function rsyncPortName(serviceName) { +export function rsyncPortName(serviceName: string) { return `garden-rsync-${serviceName}` } diff --git a/garden-service/test/unit/src/plugins/kubernetes/container/service.ts b/garden-service/test/unit/src/plugins/kubernetes/container/service.ts index 76d361c407..beda6d62d9 100644 --- a/garden-service/test/unit/src/plugins/kubernetes/container/service.ts +++ b/garden-service/test/unit/src/plugins/kubernetes/container/service.ts @@ -101,29 +101,6 @@ describe("createServiceResources", () => { namespace: "my-namespace", annotations: {}, }, - spec: { - ports: [ - { - name: "http", - protocol: "TCP", - targetPort: 8080, - port: 8080, - }, - ], - selector: { - service: "service-a", - }, - type: "ClusterIP", - }, - }, - { - apiVersion: "v1", - kind: "Service", - metadata: { - name: "service-a-nodeport", - namespace: "my-namespace", - annotations: {}, - }, spec: { ports: [ { @@ -160,29 +137,6 @@ describe("createServiceResources", () => { namespace: "my-namespace", annotations: {}, }, - spec: { - ports: [ - { - name: "http", - protocol: "TCP", - targetPort: 8080, - port: 8080, - }, - ], - selector: { - service: "service-a", - }, - type: "ClusterIP", - }, - }, - { - apiVersion: "v1", - kind: "Service", - metadata: { - name: "service-a-nodeport", - namespace: "my-namespace", - annotations: {}, - }, spec: { ports: [ {