Skip to content

Commit

Permalink
feat: add servicePort config option
Browse files Browse the repository at this point in the history
in k8s plugin this becomes the port the service is exposed on defaults to containerPort
  • Loading branch information
drubin authored and eysi09 committed Jan 11, 2019
1 parent 50f24da commit 57b23f3
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 21 deletions.
16 changes: 14 additions & 2 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,16 +643,28 @@ module:
# Required.
name:

# The protocol of the service container port.
# The protocol of the port.
#
# Optional.
protocol: TCP

# The port number on the service container.
# The port exposed on the container by the running procces. This will also be the
# default value for `servicePort`.
# `servicePort:80 -> containerPort:8080 -> process:8080`
#
# Example: "8080"
#
# Required.
containerPort:

# The port exposed on the service. Defaults to `containerPort` if not specified.
# `servicePort:80 -> containerPort:8080 -> process:8080`
#
# Example: "80"
#
# Optional.
servicePort: <containerPort>

hostPort:

# Set this to expose the service on the specified port on the host node (may not be
Expand Down
6 changes: 4 additions & 2 deletions examples/simple-project/services/go-service/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module:
- name: go-service
ports:
- name: http
containerPort: 80
containerPort: 8080
# Maps service:80 -> container:8080
servicePort: 80
ingresses:
- path: /hello-go
port: http
port: http
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func main() {
http.HandleFunc("/hello-go", handler)
fmt.Println("Server running...")

http.ListenAndServe(":80", nil)
http.ListenAndServe(":8080", nil)
}
6 changes: 3 additions & 3 deletions garden-service/src/docs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export function getDefaultValue(description: Joi.Description) {
if (defaultSpec === undefined) {
return
} else if (defaultSpec && defaultSpec.function) {
const value = defaultSpec.function({})
if (value === undefined){
const value = defaultSpec.function({})
if (value === undefined) {
return defaultSpec.description
}else {
} else {
return value
}
} else {
Expand Down
19 changes: 16 additions & 3 deletions garden-service/src/plugins/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface ServicePortSpec {
name: string
protocol: ServicePortProtocol
containerPort: number
// Defaults to containerPort
servicePort: number
hostPort?: number
nodePort?: number | null
}
Expand Down Expand Up @@ -164,18 +166,29 @@ const healthCheckSchema = Joi.object()
.description("Set this to check the service's health by checking if this TCP port is accepting connections."),
}).xor("httpGet", "command", "tcpPort")

const portSchema = Joi.object()
export const portSchema = Joi.object()
.keys({
name: joiIdentifier()
.required()
.description("The name of the port (used when referencing the port elsewhere in the service configuration)."),
protocol: Joi.string()
.allow("TCP", "UDP")
.default(DEFAULT_PORT_PROTOCOL)
.description("The protocol of the service container port."),
.description("The protocol of the port."),
containerPort: Joi.number()
.required()
.description("The port number on the service container."),
.example("8080")
.description(deline`
The port exposed on the container by the running procces. This will also be the default value
for \`servicePort\`.
\`servicePort:80 -> containerPort:8080 -> process:8080\``),
servicePort: Joi.number().default((context) => context.containerPort, "<containerPort>")
.example("80")
.description(deline`The port exposed on the service.
Defaults to \`containerPort\` if not specified.
\`servicePort:80 -> containerPort:8080 -> process:8080\``),
hostPort: Joi.number()
.meta({ deprecated: true }),
nodePort: Joi.number()
Expand Down
1 change: 1 addition & 0 deletions garden-service/src/plugins/kubernetes/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export async function createDeployment(

for (const port of ports) {
container.ports.push({
name: port.name,
protocol: port.protocol,
containerPort: port.containerPort,
})
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/plugins/kubernetes/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function createIngresses(api: KubeApi, namespace: string, service:
path: ingress.path,
backend: {
serviceName: service.name,
servicePort: findByName(service.spec.ports, ingress.spec.port)!.containerPort,
servicePort: findByName(service.spec.ports, ingress.spec.port)!.servicePort,
},
})),
},
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/plugins/kubernetes/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function createServices(service: ContainerService, namespace: strin
name: portSpec.name,
protocol: portSpec.protocol,
targetPort: portSpec.containerPort,
port: portSpec.containerPort,
port: portSpec.servicePort,
})
}

Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/plugins/local/local-docker-swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const gardenPlugin = (): GardenPlugin => ({
}

if (p.hostPort) {
port.PublishedPort = p.hostPort
port.PublishedPort = p.servicePort
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const gardenPlugin = (): GardenPlugin => ({
name: "http",
protocol: <ServicePortProtocol>"TCP",
containerPort: emulatorPort,
servicePort: emulatorPort,
},
],
volumes: [],
Expand Down
10 changes: 5 additions & 5 deletions garden-service/test/src/docs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import dedent = require("dedent")

describe("config", () => {
const serivcePortSchema = Joi.number().default((context) => context.containerPort, "default value")
.example("8080")
.description("description")
.example("8080")
.description("description")

const testDefaultSchema = Joi.number().default(() => "result", "default value")
.description("description")
.description("description")

const portSchema = Joi.object()
.keys({
Expand All @@ -22,8 +22,8 @@ describe("config", () => {

describe("renderSchemaDescription", () => {
it("should render correct markdown", () => {
const yaml = renderSchemaDescription(portSchema.describe(), { required: true })
expect(yaml).to.equal(dedent`\n# description
const yaml = renderSchemaDescription(portSchema.describe(), { required: true })
expect(yaml).to.equal(dedent`\n# description
#
# Required.
containerPort:
Expand Down
5 changes: 3 additions & 2 deletions garden-service/test/src/plugins/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ describe("plugins.container", () => {
name: "http",
protocol: "TCP",
containerPort: 8080,
servicePort: 8080,
}],
outputs: {},
volumes: [],
Expand Down Expand Up @@ -275,7 +276,7 @@ describe("plugins.container", () => {
},
healthCheck:
{ httpGet: { path: "/health", port: "http", scheme: "HTTP" } },
ports: [{ name: "http", protocol: "TCP", containerPort: 8080 }],
ports: [{ name: "http", protocol: "TCP", containerPort: 8080, servicePort: 8080 }],
outputs: {},
volumes: [],
}],
Expand Down Expand Up @@ -315,7 +316,7 @@ describe("plugins.container", () => {
},
healthCheck:
{ httpGet: { path: "/health", port: "http", scheme: "HTTP" } },
ports: [{ name: "http", protocol: "TCP", containerPort: 8080 }],
ports: [{ name: "http", protocol: "TCP", containerPort: 8080, servicePort: 8080 }],
outputs: {},
volumes: [],
},
Expand Down
1 change: 1 addition & 0 deletions garden-service/test/src/plugins/kubernetes/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const ports = [{
name: "http",
protocol: <ServicePortProtocol>"http",
containerPort: 123,
servicePort: 123,
}]

const basicConfig: KubernetesConfig = {
Expand Down
22 changes: 22 additions & 0 deletions garden-service/test/src/types/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect } from "chai"
import { validate } from "../../../src/config/common"
import { portSchema } from "../../../src/plugins/container"

describe("portSchema", () => {
it("should default servicePort to containerPorts value", async () => {
const containerPort = 8080
const obj = { name: "a", containerPort }

const value = validate(obj, portSchema)
expect(value["servicePort"]).to.equal(containerPort)
})

it("should not default servicePort to containerPorts when configured", async () => {
const containerPort = 8080
const servicePort = 9090
const obj = { name: "a", containerPort, servicePort }

const value = validate(obj, portSchema)
expect(value["servicePort"]).to.equal(servicePort)
})
})

0 comments on commit 57b23f3

Please sign in to comment.