Skip to content

Commit

Permalink
feat(k8s): allow overriding release name in Helm modules
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jan 30, 2019
1 parent 37fe9c3 commit 1530105
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions garden-service/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ export function getValuesPath(chartPath: string) {
}

/**
* Get the release name to use for the module/chart (currently just the module name).
* Get the release name to use for the module/chart (the module name, unless overridden in config).
*/
export function getReleaseName(module: HelmModule) {
return module.name
return module.spec.releaseName || module.name
}

/**
Expand Down
3 changes: 3 additions & 0 deletions garden-service/src/plugins/kubernetes/helm/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export interface HelmServiceSpec extends ServiceSpec {
chart?: string
chartPath: string
dependencies: string[]
releaseName?: string
repo?: string
serviceResource?: HelmResourceSpec
skipDeploy: boolean
Expand Down Expand Up @@ -173,6 +174,8 @@ export const helmModuleSpecSchema = Joi.object().keys({
.default("."),
dependencies: joiArray(joiIdentifier())
.description("List of names of services that should be deployed before this chart."),
releaseName: joiIdentifier()
.description("Optionally override the release name used when installing (defaults to the module name)."),
repo: Joi.string()
.description("The repository URL to fetch the chart from."),
serviceResource: resourceSchema
Expand Down
1 change: 1 addition & 0 deletions garden-service/test/data/test-projects/helm/api/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module:
description: The API backend for the voting UI
type: helm
name: api
releaseName: api-release
serviceResource:
kind: Deployment
containerModule: api-image
Expand Down
32 changes: 19 additions & 13 deletions garden-service/test/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ describe("Helm common functions", () => {
apiVersion: "v1",
kind: "Service",
metadata: {
name: "api",
name: "api-release",
labels: {
"app.kubernetes.io/name": "api",
"helm.sh/chart": "api-0.1.0",
"app.kubernetes.io/instance": "api",
"app.kubernetes.io/instance": "api-release",
"app.kubernetes.io/managed-by": "Tiller",
},
annotations: {},
Expand All @@ -96,19 +96,19 @@ describe("Helm common functions", () => {
],
selector: {
"app.kubernetes.io/name": "api",
"app.kubernetes.io/instance": "api",
"app.kubernetes.io/instance": "api-release",
},
},
},
{
apiVersion: "apps/v1",
kind: "Deployment",
metadata: {
name: "api",
name: "api-release",
labels: {
"app.kubernetes.io/name": "api",
"helm.sh/chart": "api-0.1.0",
"app.kubernetes.io/instance": "api",
"app.kubernetes.io/instance": "api-release",
"app.kubernetes.io/managed-by": "Tiller",
},
annotations: {},
Expand All @@ -118,14 +118,14 @@ describe("Helm common functions", () => {
selector: {
matchLabels: {
"app.kubernetes.io/name": "api",
"app.kubernetes.io/instance": "api",
"app.kubernetes.io/instance": "api-release",
},
},
template: {
metadata: {
labels: {
"app.kubernetes.io/name": "api",
"app.kubernetes.io/instance": "api",
"app.kubernetes.io/instance": "api-release",
},
},
spec: {
Expand Down Expand Up @@ -156,11 +156,11 @@ describe("Helm common functions", () => {
apiVersion: "extensions/v1beta1",
kind: "Ingress",
metadata: {
name: "api",
name: "api-release",
labels: {
"app.kubernetes.io/name": "api",
"helm.sh/chart": "api-0.1.0",
"app.kubernetes.io/instance": "api",
"app.kubernetes.io/instance": "api-release",
"app.kubernetes.io/managed-by": "Tiller",
},
annotations: {},
Expand All @@ -174,7 +174,7 @@ describe("Helm common functions", () => {
{
path: "/",
backend: {
serviceName: "api",
serviceName: "api-release",
servicePort: "http",
},
},
Expand Down Expand Up @@ -509,10 +509,16 @@ describe("Helm common functions", () => {
})

describe("getReleaseName", () => {
it("should return the module name", async () => {
it("should return the module name if not overridden in config", async () => {
const module = await garden.getModule("api")
delete module.spec.releaseName
expect(getReleaseName(module)).to.equal("api")
})

it("should return the configured release name if any", async () => {
const module = await garden.getModule("api")
expect(getReleaseName(module)).to.equal("api-release")
})
})

describe("getServiceResourceSpec", () => {
Expand Down Expand Up @@ -671,15 +677,15 @@ describe("Helm common functions", () => {
deployment.spec.template.spec.containers = []
await expectError(
() => getResourceContainer(deployment),
err => expect(err.message).to.equal("Deployment api has no containers configured."),
err => expect(err.message).to.equal("Deployment api-release has no containers configured."),
)
})

it("should throw if name is specified and no containers match", async () => {
const deployment = await getDeployment()
await expectError(
() => getResourceContainer(deployment, "foo"),
err => expect(err.message).to.equal("Could not find container 'foo' in Deployment 'api'"),
err => expect(err.message).to.equal("Could not find container 'foo' in Deployment 'api-release'"),
)
})
})
Expand Down
2 changes: 2 additions & 0 deletions garden-service/test/src/plugins/kubernetes/helm/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe("validateHelmModule", () => {
spec: {
chartPath: ".",
dependencies: [],
releaseName: "api-release",
serviceResource: {
kind: "Deployment",
containerModule: "api-image",
Expand Down Expand Up @@ -87,6 +88,7 @@ describe("validateHelmModule", () => {
spec: {
chartPath: ".",
dependencies: [],
releaseName: "api-release",
serviceResource: {
kind: "Deployment",
containerModule: "api-image",
Expand Down
2 changes: 1 addition & 1 deletion garden-service/test/src/plugins/kubernetes/helm/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ describe("getServiceOutputs", () => {

const result = await getServiceOutputs({ ctx, module, service, log: garden.log })

expect(result).to.eql({ "release-name": "api" })
expect(result).to.eql({ "release-name": "api-release" })
})
})

0 comments on commit 1530105

Please sign in to comment.