Skip to content

Commit

Permalink
fix(k8s): create a single NodePort Service when a node port is set
Browse files Browse the repository at this point in the history
Previously we were creating two Services, which is not really the
behavior that people expect. It also apparently messed with AWS ALB
configuration.
  • Loading branch information
edvald committed Aug 1, 2019
1 parent 8d75188 commit f8d1b4b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 63 deletions.
24 changes: 7 additions & 17 deletions garden-service/src/plugins/kubernetes/container/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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: [
{
Expand Down

0 comments on commit f8d1b4b

Please sign in to comment.