convert helm inflator to builtin plugin

This commit is contained in:
Donny Xia
2020-10-30 14:53:42 -07:00
parent 981959ffcf
commit e75d4fc87d
13 changed files with 1511 additions and 103 deletions

View File

@@ -2,12 +2,12 @@
[last mile]: https://testingclouds.wordpress.com/2018/07/20/844/
[stable chart]: https://github.com/helm/charts/tree/master/stable
[Helm charts]: https://github.com/helm/charts
[helm charts]: https://github.com/helm/charts
[_minecraft_]: https://github.com/helm/charts/tree/master/stable/minecraft
[plugin]: ../docs/plugins
[Helm charts] aren't natively read by kustomize, but
kustomize has a [plugin] system that allows one to
kustomize has a built in HelmChartInflationGenerator that allows one to
access helm charts.
One pattern combining kustomize and helm is
@@ -16,17 +16,16 @@ one uses an inflated chart as a base, then
modifies it on the way to the cluster using
kustomize.
The plugin used in the example below is coded to work
only for charts found in the [stable chart] repo. The
example arbitrarily uses [_minecraft_], but should work
for any chart.
The example arbitrarily uses [_minecraft_] in [stable chart] repository,
but should work for any chart in any valid chart repository.
The following example assumes you have `helm`
on your `$PATH`.
on your `$PATH`. The plugin only supports helm V3 or later.
Make a place to work:
<!-- @makeWorkplace @helmtest -->
```
DEMO_HOME=$(mktemp -d)
mkdir -p $DEMO_HOME/base
@@ -44,6 +43,7 @@ other examples), but in this case just add the name
prefix `dev-` to all resources:
<!-- @writeKustDev @helmtest -->
```
cat <<'EOF' >$DEMO_HOME/dev/kustomization.yaml
namePrefix: dev-
@@ -56,6 +56,7 @@ Likewise define a _production_ variant, with a name
prefix `prod-`:
<!-- @writeKustProd @helmtest -->
```
cat <<'EOF' >$DEMO_HOME/prod/kustomization.yaml
namePrefix: prod-
@@ -69,6 +70,7 @@ These two variants refer to a common base.
Define this base:
<!-- @writeKustDev @helmtest -->
```
cat <<'EOF' >$DEMO_HOME/base/kustomization.yaml
generators:
@@ -84,42 +86,26 @@ and other things like a path to a values file, defaulting
to the `values.yaml` that comes with the chart.
Create the config file `chartInflator.yaml`, specifying
the arbitrarily chosen chart name _minecraft_:
the arbitrarily chosen chart name _minecraft_ and the repository
url that will be used to find the chart:
<!-- @writeGeneratorConfig @helmtest -->
```
cat <<'EOF' >$DEMO_HOME/base/chartInflator.yaml
apiVersion: someteam.example.com/v1
kind: ChartInflator
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: notImportantHere
chartName: minecraft
chartRepoUrl: https://kubernetes-charts.storage.googleapis.com
EOF
```
Because this particular YAML file is listed in the
`generators:` stanza of a kustomization file, it is
treated as the binding between a generator plugin -
identified by the _apiVersion_ and _kind_ fields - and
other fields that configure the plugin.
Download the plugin to your `DEMO_HOME` and make it
executable:
<!-- @installPlugin @helmtest -->
```
plugin=plugin/someteam.example.com/v1/chartinflator/ChartInflator
curl -s --create-dirs -o \
"$DEMO_HOME/kustomize/$plugin" \
"https://raw.githubusercontent.com/\
kubernetes-sigs/kustomize/master/$plugin"
chmod a+x $DEMO_HOME/kustomize/$plugin
```
Check the directory layout:
<!-- @tree -->
```
tree $DEMO_HOME
```
@@ -133,12 +119,6 @@ Expect something like:
> │   └── kustomization.yaml
> ├── dev
> │   └── kustomization.yaml
> ├── kustomize
> │   └── plugin
> │   └── someteam.example.com
> │   └── v1
> │   └── chartinflator
> │   └── ChartInflator
> └── prod
> └── kustomization.yaml
> ```
@@ -147,6 +127,7 @@ Define a helper function to run kustomize with the
correct environment and flags for plugins:
<!-- @defineKustomizeIt @helmtest -->
```
function kustomizeIt {
XDG_CONFIG_HOME=$DEMO_HOME \
@@ -155,10 +136,11 @@ function kustomizeIt {
}
```
Finally, build the `prod` variant. Notice that all
resource names now have the `prod-` prefix:
Finally, build the `prod` variant. Notice that all
resource names now have the `prod-` prefix:
<!-- @doProd @helmtest -->
```
clear
kustomizeIt prod
@@ -167,24 +149,25 @@ kustomizeIt prod
Compare `dev` to `prod`:
<!-- @doCompare -->
```
diff <(kustomizeIt dev) <(kustomizeIt prod) | more
```
To see the unmodified but inflated chart, run kustomize
on the base. Every invocation here is re-downloading
on the base. Every invocation here is re-downloading
and re-inflating the chart.
<!-- @showBase @helmtest -->
```
kustomizeIt base
```
## Use a local chart
The example above fetches a new copy of the chart
to a temporary directory with each kustomize
and render it with each kustomize
build, because a local chart home isn't specified
in the configuration.
@@ -195,42 +178,46 @@ there.
To demo this so that it won't interfere with your
existing helm environment, do this:
**This demo uses Helm V3**
<!-- @helmInit @helmtest -->
```
helmHome=$DEMO_HOME/dothelm
chartHome=$DEMO_HOME/base/charts
repoUrl=https://kubernetes-charts.storage.googleapis.com
function doHelm {
helm --home $helmHome $@
}
# Create helm config files in a new location.
# The init command is extremely chatty
doHelm init --client-only >& /dev/null
```
Now download a chart; again use _minecraft_
(but you could use anything):
<!-- @fetchChart @helmtest -->
```
doHelm fetch --untar \
doHelm pull --untar \
--untardir $chartHome \
stable/minecraft
--repo $repoUrl \
minecraft
```
The tree has more stuff now; helm config data
and a complete copy of the chart:
<!-- @tree -->
```
tree $DEMO_HOME
```
Add a `chartHome` field to the generator config file so
that it knows where to find the local chart:
<!-- @modifyGenConfig @helmtest -->
```
echo "chartHome: $chartHome" >>$DEMO_HOME/base/chartInflator.yaml
```
@@ -239,6 +226,7 @@ Change the values file, to show that the results
generated below are from the _locally_ stored chart:
<!-- @valueChange @helmtest -->
```
sed -i 's/CHANGEME!/SOMETHINGELSE/' $chartHome/minecraft/values.yaml
sed -i 's/LoadBalancer/NodePort/' $chartHome/minecraft/values.yaml
@@ -247,9 +235,31 @@ sed -i 's/LoadBalancer/NodePort/' $chartHome/minecraft/values.yaml
Finally, built it
<!-- @finalProd @helmtest -->
```
kustomizeIt prod
```
and observe the change from `LoadBalancer` to `NodePort`, and
and observe the change from `LoadBalancer` to `NodePort`, and
the change in the encoded password.
Clean the directory.
<!-- @showBase @helmtest -->
```
rm -r $DEMO_HOME
```
## How to migrate from old plugin to builtin plugin
Before, we have [chartinflator](https://github.com/kubernetes-sigs/kustomize/tree/master/plugin/someteam.example.com/v1/chartinflator)
plugin which servers as an external plugin. Although you can continue
using it but we recommend to migrate to builtin one if you are using
Helm V3. For Helm V2 user, we haven't implemented V2 support in builtin
plugin.
What you need to do for your generator config file:
- Change the `apiVersion` to `builtin`.
- Change the `kind` to `HelmChartInflationGenerator`