From 98414e8bf66cf168be6835d2b5366ba71ac523f3 Mon Sep 17 00:00:00 2001 From: jregan Date: Tue, 21 Apr 2020 19:31:53 -0700 Subject: [PATCH] Start adding helm3 to ChartInflator and its coverage. --- Makefile | 31 +++++--- hack/testUnitKustomizePlugins.sh | 15 +++- .../v1/chartinflator/ChartInflator | 76 +++++++++++++++---- .../v1/chartinflator/ChartInflator_test.go | 69 ++++++++++++----- 4 files changed, 142 insertions(+), 49 deletions(-) diff --git a/Makefile b/Makefile index bb1b6c0de..677a7c460 100644 --- a/Makefile +++ b/Makefile @@ -261,18 +261,31 @@ $(MYGOBIN)/kubeval: ) # linux only. -# This is for testing an example plugin that -# uses helm to inflate a chart for subsequent kustomization. -# Don't want to add a hard dependence in go.mod file -# to helm. -# Instead, download the binary. -$(MYGOBIN)/helm: +# This is for testing an example plugin that uses helm to inflate a chart +# for subsequent kustomization. +# Don't want to add a hard dependence in go.mod file to helm. +# Instead, download the binaries. +$(MYGOBIN)/helmV2: ( \ set -e; \ d=$(shell mktemp -d); cd $$d; \ - wget https://storage.googleapis.com/kubernetes-helm/helm-v2.13.1-linux-amd64.tar.gz; \ - tar -xvzf helm-v2.13.1-linux-amd64.tar.gz; \ - mv linux-amd64/helm $(MYGOBIN); \ + tgzFile=helm-v2.13.1-linux-amd64.tar.gz; \ + wget https://storage.googleapis.com/kubernetes-helm/$$tgzFile; \ + tar -xvzf $$tgzFile; \ + mv linux-amd64/helm $(MYGOBIN)/helmV2; \ + rm -rf $$d \ + ) + +# Helm V3 differs from helm V2; downloading it to provide coverage for the +# chart inflator plugin under helm v3. +$(MYGOBIN)/helmV3: + ( \ + set -e; \ + d=$(shell mktemp -d); cd $$d; \ + tgzFile=helm-v3.2.0-rc.1-linux-amd64.tar.gz; \ + wget https://get.helm.sh/$$tgzFile; \ + tar -xvzf $$tgzFile; \ + mv linux-amd64/helm $(MYGOBIN)/helmV3; \ rm -rf $$d \ ) diff --git a/hack/testUnitKustomizePlugins.sh b/hack/testUnitKustomizePlugins.sh index 99ba361ac..649baeb5c 100755 --- a/hack/testUnitKustomizePlugins.sh +++ b/hack/testUnitKustomizePlugins.sh @@ -14,6 +14,14 @@ set -o pipefail rcAccumulator=0 +# hack. We used to run test on travis, and disable certain tests +# when running on travis. Now we run on Prow, so look for that. +# https://github.com/kubernetes/test-infra/blob/master/prow/jobs.md +# TODO: Make the code ignorant of the CI environment "brand name". +# brand name of the CI environment (replace "travis" and "prow" with "CI_env" +# or something). +TRAVIS=$PROW_JOB_ID + function onLinuxAndNotOnTravis { [[ ("linux" == "$(go env GOOS)") && (-z ${TRAVIS+x}) ]] && return false @@ -24,7 +32,7 @@ function runTest { local code=0 if grep -q "// +build notravis" "$file"; then if onLinuxAndNotOnTravis; then - go test -v -tags=notravis $file + go test -v -tags=notravis $file code=$? else # TODO: make work for non-linux @@ -51,7 +59,8 @@ function scanDir { if onLinuxAndNotOnTravis; then # Some of these tests have special deps. - make $(go env GOPATH)/bin/helm + make $(go env GOPATH)/bin/helmV2 + make $(go env GOPATH)/bin/helmV3 make $(go env GOPATH)/bin/kubeval fi @@ -60,7 +69,7 @@ for goMod in $(find ./plugin -name 'go.mod'); do if [[ "$d" == "./plugin/someteam.example.com/v1/gogetter" ]]; then echo "Skipping broken $d" else - scanDir $d + scanDir $d fi done diff --git a/plugin/someteam.example.com/v1/chartinflator/ChartInflator b/plugin/someteam.example.com/v1/chartinflator/ChartInflator index 7bf69be3c..aba44f26e 100755 --- a/plugin/someteam.example.com/v1/chartinflator/ChartInflator +++ b/plugin/someteam.example.com/v1/chartinflator/ChartInflator @@ -115,25 +115,69 @@ if [ -z "$releaseNamespace" ]; then releaseNamespace=default fi -function doHelm { - $helmBin --home $helmHome $@ +function v2RunHelm { + $helmBin --home $helmHome "$@" } -# The init command is extremely chatty -doHelm init --client-only >& /dev/null +function v3RunHelm { + XDG_CONFIG_DIR=$helmHome + $helmBin "$@" +} -if [ ! -d "$chartHome/$chartName" ]; then - doHelm fetch $chartVersionArg \ - $chartRepoArg \ - --untar \ - --untardir $chartHome \ - $chartNameArg -fi +function v2InitHelm { + # The init command is extremely chatty + v2RunHelm init --client-only >& /dev/null +} -doHelm template \ - --name $releaseName \ - --namespace $releaseNamespace \ - --values $valuesFile \ - $chartHome/$chartName +function v3InitHelm { + # Do nothing - there's no init command in helm v3 + true +} + +function v2PullChart { + if [ ! -d "$chartHome/$chartName" ]; then + v2RunHelm fetch $chartVersionArg \ + $chartRepoArg \ + --untar \ + --untardir $chartHome \ + $chartNameArg + fi +} + +function v3PullChart { + # TODO implement + echo "?? helmV3 pull --repo https://hub.helm.sh/charts/ stable/minecraft --destination /tmp/junk" +} + +function v2InflateChart { + v2RunHelm template \ + --name $releaseName \ + --namespace $releaseNamespace \ + --values $valuesFile \ + $chartHome/$chartName +} + +function v3InflateChart { + # TODO implement + true +} + +HELM_VERSION=$($helmBin version -c --short) + +case $HELM_VERSION in + 'Client: v2'*) + v2InitHelm + v2PullChart + v2InflateChart + ;; + v3*) + v3InitHelm + v3PullChart + v3InflateChart + ;; + *) + echo "[!] Unsupported 'helm' version '${HELM_VERSION}'" 1>&2 && exit 1 + ;; +esac /bin/rm -rf $TMP_DIR diff --git a/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go b/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go index 2571a4321..3d1fa79dc 100644 --- a/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go +++ b/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go @@ -14,27 +14,7 @@ import ( kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) -// This test requires having the helm binary on the PATH. -// -// TODO: Download and inflate the chart, and check that -// in for the test. -func TestChartInflator(t *testing.T) { - th := kusttest_test.MakeEnhancedHarness(t). - PrepExecPlugin("someteam.example.com", "v1", "ChartInflator") - defer th.Reset() - - m := th.LoadAndRunGenerator(` -apiVersion: someteam.example.com/v1 -kind: ChartInflator -metadata: - name: notImportantHere -chartName: minecraft`) - - chartName := regexp.MustCompile("chart: minecraft-[0-9.]+") - th.AssertActualEqualsExpectedWithTweak(m, - func(x []byte) []byte { - return chartName.ReplaceAll(x, []byte("chart: minecraft-SOMEVERSION")) - }, ` +const expectedResources=` apiVersion: v1 data: rcon-password: Q0hBTkdFTUUh @@ -84,5 +64,52 @@ spec: selector: app: release-name-minecraft type: LoadBalancer +` + +// This test requires having "helmV2" (presumably helm V2 series) on the PATH. +// +// TODO: Download and inflate the chart, and check that +// in for the test. +func TestHelmV2ChartInflator(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepExecPlugin("someteam.example.com", "v1", "ChartInflator") + defer th.Reset() + + m := th.LoadAndRunGenerator(` +apiVersion: someteam.example.com/v1 +kind: ChartInflator +metadata: + name: notImportantHere +chartName: minecraft +helmBin: helmV2 `) + + chartName := regexp.MustCompile("chart: minecraft-[0-9.]+") + th.AssertActualEqualsExpectedWithTweak(m, + func(x []byte) []byte { + return chartName.ReplaceAll(x, []byte("chart: minecraft-SOMEVERSION")) + }, expectedResources) +} + +// This test requires having "helmV3" (presumably helm V3 series) on the PATH. +// +func disabled_TestHelmV3ChartInflator(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepExecPlugin("someteam.example.com", "v1", "ChartInflator") + defer th.Reset() + + m := th.LoadAndRunGenerator(` +apiVersion: someteam.example.com/v1 +kind: ChartInflator +metadata: + name: notImportantHere +chartName: minecraft +helmBin: helmV3 +`) + + chartName := regexp.MustCompile("chart: minecraft-[0-9.]+") + th.AssertActualEqualsExpectedWithTweak(m, + func(x []byte) []byte { + return chartName.ReplaceAll(x, []byte("chart: minecraft-SOMEVERSION")) + }, expectedResources) }