Skip to content

Commit

Permalink
fix(helm): filter out test pods when deploying charts
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Feb 22, 2019
1 parent 49f8115 commit b646236
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 6 deletions.
26 changes: 20 additions & 6 deletions garden-service/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ConfigurationError, PluginError } from "../../../exceptions"
import { Module } from "../../../types/module"
import { findByName } from "../../../util/util"
import { deline } from "../../../util/string"
import { getAnnotation } from "../util"

/**
* Returns true if the specified Helm module contains a template (as opposed to just referencing a remote template).
Expand Down Expand Up @@ -51,12 +52,25 @@ export async function getChartResources(ctx: PluginContext, module: Module, log:
chartPath,
))

return objects.filter(obj => obj !== null).map((obj) => {
if (!obj.metadata.annotations) {
obj.metadata.annotations = {}
}
return obj
})
return objects
.filter(obj => {
if (obj === null) {
return false
}

const helmHook = getAnnotation(obj, "helm.sh/hook")
if (helmHook && helmHook.startsWith("test-")) {
return false
}

return true
})
.map((obj) => {
if (!obj.metadata.annotations) {
obj.metadata.annotations = {}
}
return obj
})
}

/**
Expand Down
14 changes: 14 additions & 0 deletions garden-service/src/plugins/kubernetes/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { get } from "lodash"
import { KubernetesResource } from "./types"

export function getAnnotation(obj: KubernetesResource, key: string): string | null {
return get(obj, ["metadata", "annotations", key])
}
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,3 @@
module:
type: helm
name: chart-with-test-pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: chart-with-test-pod
spec:
type: {{ .Values.service.type }}
selector:
app: chart-with-test-pod
ports:
- port: 80
name: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Pod
metadata:
name: test-connection
annotations:
"helm.sh/hook": test-success
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['foo']
restartPolicy: Never
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: {}
28 changes: 28 additions & 0 deletions garden-service/test/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,34 @@ describe("Helm common functions", () => {
const module = await graph.getModule("duplicate-keys-in-template")
expect(await getChartResources(ctx, module, log)).to.not.throw
})

it("should filter out test pods", async () => {
const module = await graph.getModule("chart-with-test-pod")
const resources = await getChartResources(ctx, module, log)

expect(resources).to.eql([
{
apiVersion: "v1",
kind: "Service",
metadata: {
annotations: {},
name: "chart-with-test-pod",
},
spec: {
ports: [
{
name: "http",
port: 80,
},
],
selector: {
app: "chart-with-test-pod",
},
type: "ClusterIP",
},
},
])
})
})

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

0 comments on commit b646236

Please sign in to comment.