Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(helm): allow duplicate keys in template #559

Merged
merged 1 commit into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions garden-service/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { PluginContext } from "../../../plugin-context"
import { LogEntry } from "../../../logger/log-entry"
import { getNamespace } from "../namespace"
import { KubernetesResource } from "../types"
import { safeLoadAll } from "js-yaml"
import { loadAll } from "js-yaml"
import { helm } from "./helm-cli"
import { HelmModule, HelmModuleConfig, HelmResourceSpec } from "./config"
import { HotReloadableResource } from "../hot-reload"
Expand All @@ -43,7 +43,7 @@ export async function getChartResources(ctx: PluginContext, module: Module, log:
const context = ctx.provider.config.context
const releaseName = getReleaseName(module)

const objects = <KubernetesResource[]>safeLoadAll(await helm(namespace, context, log,
const objects = <KubernetesResource[]>loadTemplate(await helm(namespace, context, log,
"template",
"--name", releaseName,
"--namespace", namespace,
Expand Down Expand Up @@ -269,7 +269,7 @@ async function renderHelmTemplateString(
try {
await writeFile(tempFilePath, value)

const objects = safeLoadAll(await helm(namespace, context, log,
const objects = loadTemplate(await helm(namespace, context, log,
"template",
"--name", releaseName,
"--namespace", namespace,
Expand All @@ -284,3 +284,16 @@ async function renderHelmTemplateString(
await remove(tempFilePath)
}
}

/**
* Helm templates can include duplicate keys, e.g. because of a mistake in the remote chart repo.
* We therefore load the template with `{ json: true }`, so that duplicate keys in a mapping will override values rather
* than throwing an error.
*
* However, this behavior is broken for the `safeLoadAll` function, see: https://github.com/nodeca/js-yaml/issues/456.
* We therefore need to use the `loadAll` function. See the following link for a conversation on using
* `loadAll` in this context: https://github.com/kubeapps/kubeapps/issues/636.
*/
function loadTemplate(template: string) {
return loadAll(template, undefined, { json: true })
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: duplicate-keys-in-template
version: 0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module:
description: Helm chart for the duplicate-keys-in-template container
type: helm
name: duplicate-keys-in-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: duplicate-keys-in-template
spec:
type: {{ .Values.service.type }}
type: {{ .Values.service.type }}
selector:
app: duplicate-keys-in-template
ports:
- port: 80
name: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Default values for node-service.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

nameOverride: ""
fullnameOverride: ""

service:
type: ClusterIP

resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}
5 changes: 5 additions & 0 deletions garden-service/test/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ describe("Helm common functions", () => {
},
])
})

it("should handle duplicate keys in template", async () => {
const module = await graph.getModule("duplicate-keys-in-template")
expect(await getChartResources(ctx, module, log)).to.not.throw
})
})

describe("getBaseModule", () => {
Expand Down