One plugin per dir.

This commit is contained in:
jregan
2019-05-19 12:00:07 -07:00
parent 31534fe47d
commit 5653ae69e4
42 changed files with 612 additions and 626 deletions

View File

@@ -0,0 +1,91 @@
#!/bin/bash
# Copyright 2019 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0
# Helm chart inflator
#
# Reads a file like this
#
# apiVersion: kustomize.config.k8s.io/v1
# kind: ChartInflator
# metadata:
# name: notImportantHere
# chartName: nameOfStableChart
# values: /abs/path/to/local/values/file
# chartHome: /abs/path/local/chart/storage
# helmHome: /abs/path/to/helm/config
# helmBin: /abs/path/to/helmBin
#
# fetches the given chart from stable/$chartName,
# and inflates it to stdout, using the given values file.
#
# chartDir default: $TMP_DIR/charts
#
# Example execution:
# ./plugin/someteam.example.com/v1/ChartInflator configFile.yaml
set -e
# Yaml parsing is a ridiculous thing to do in bash,
# but let's try:
function parseYaml {
local file=$1
while read -r line
do
local k=${line%:*}
local v=${line#*:}
[ "$k" == "chartName" ] && chartName=$v
[ "$k" == "chartHome" ] && chartHome=$v
[ "$k" == "values" ] && valuesFile=$v
[ "$k" == "helmHome" ] && helmHome=$v
[ "$k" == "helmBin" ] && helmBin=$v
done <"$file"
# Trim leading space
chartName="${chartName#"${chartName%%[![:space:]]*}"}"
chartHome="${chartHome#"${chartHome%%[![:space:]]*}"}"
valuesFile="${valuesFile#"${valuesFile%%[![:space:]]*}"}"
helmBin="${helmBin#"${helmBin%%[![:space:]]*}"}"
}
TMP_DIR=$(mktemp -d)
parseYaml $1
# Where all the files generated by 'helm init' live.
if [ -z "$helmHome" ]; then
helmHome=$TMP_DIR/dotHelm
fi
# Where helm charts are unpacked.
if [ -z "$chartHome" ]; then
chartHome=$TMP_DIR/charts
fi
if [ -z "$helmBin" ]; then
helmBin=helm
fi
if [ -z "$valuesFile" ]; then
valuesFile=$chartHome/$chartName/values.yaml
fi
function doHelm {
$helmBin --home $helmHome $@
}
# The init command is extremely chatty
doHelm init --client-only >& /dev/null
if [ ! -d "$chartHome/$chartName" ]; then
doHelm fetch --untar \
--untardir $chartHome \
stable/$chartName
fi
doHelm template \
--values $valuesFile \
$chartHome/$chartName
/bin/rm -rf $TMP_DIR

View File

@@ -0,0 +1,88 @@
// +build notravis
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Disabled on travis, because don't want to install helm on travis.
package main_test
import (
"testing"
"sigs.k8s.io/kustomize/pkg/kusttest"
"sigs.k8s.io/kustomize/plugin"
)
// 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) {
tc := plugin.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildExecPlugin(
"someteam.example.com", "v1", "ChartInflator")
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
m := th.LoadAndRunGenerator(`
apiVersion: someteam.example.com/v1
kind: ChartInflator
metadata:
name: notImportantHere
chartName: minecraft`)
th.AssertActualEqualsExpected(m, `
apiVersion: v1
data:
rcon-password: Q0hBTkdFTUUh
kind: Secret
metadata:
labels:
app: release-name-minecraft
chart: minecraft-1.0.0
heritage: Tiller
release: release-name
name: release-name-minecraft
type: Opaque
---
apiVersion: v1
kind: Service
metadata:
labels:
app: release-name-minecraft
chart: minecraft-1.0.0
heritage: Tiller
release: release-name
name: release-name-minecraft
spec:
ports:
- name: minecraft
port: 25565
protocol: TCP
targetPort: minecraft
selector:
app: release-name-minecraft
type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
annotations:
volume.alpha.kubernetes.io/storage-class: default
labels:
app: release-name-minecraft
chart: minecraft-1.0.0
heritage: Tiller
release: release-name
name: release-name-minecraft-datadir
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
`)
}