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

Add support for adhoc dependency to local chart #1765

Merged
merged 2 commits into from
Apr 10, 2021
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
88 changes: 88 additions & 0 deletions docs/advanced-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Import Configuration Parameters into Helmfile](#import-configuration-parameters-into-helmfile)
- [Deploy Kustomization with Helmfile](#deploy-kustomizations-with-helmfile)
- [Adhoc Kustomization of Helm Charts](#adhoc-kustomization-of-helm-charts)
- [Adding dependencies without forking the chart](#adding-dependencies-without-forking-the-chart)

### Import Configuration Parameters into Helmfile

Expand Down Expand Up @@ -209,3 +210,90 @@ transformers:
```

Please see https://github.com/kubernetes-sigs/kustomize/blob/master/examples/configureBuiltinPlugin.md#configuring-the-builtin-plugins-instead for more information on how to declare transformers.

### Adding dependencies without forking the chart

With Helmfile, you can add chart dependencies to a Helm chart without forking it.

An example `helmfile.yaml` that adds a `stable/envoy` dependency to the release `foo` looks like the below:

```
repositories:
- name: stable
url: https://charts.helm.sh/stable

releases:
- name: foo
chart: ./path/to/foo
dependencies:
- chart: stable/envoy
version: 1.5
```

When Helmfile encounters `releases[].dependencies`, it creates a another temporary chart from `./path/to/foo` and adds the following `dependencies` to the `Chart.yaml`, so that you don't need to fork the chart.

```
dependencies:
- name: envoy
repo: https://charts.helm.sh/stable
condition: envoy.enabled
```

A Helm chart can have two or more dependencies for the same chart with different `alias`es. To give your dependency an `alias`, defien it like you would do in a standard `Chart.yaml`:

```
repositories:
- name: stable
url: https://charts.helm.sh/stable

releases:
- name: foo
chart: ./path/to/foo
dependencies:
- chart: stable/envoy
version: 1.5
alias: bar
- chart: stable/envoy
version: 1.5
alias: baz
```

which will tweaks the temporary chart's `Chart.yaml` to have:


```
dependencies:
- alias: bar
name: envoy
repo: https://charts.helm.sh/stable
condition: bar.enabled
- alias: baz
name: envoy
repo: https://charts.helm.sh/stable
condition: baz.enabled
```

Please see #649 for more context around this feature.

After the support for adhoc dependency to local chart (#1765),
you can even write local file paths relative to `helmfile.yaml` in `chart`:

```
releases:
- name: foo
chart: ./path/to/foo
dependencies:
- chart: ./path/to/bar
```

Internally, Helmfile creates another temporary chart from the local chart `./path/to/foo`, and modifies the chart's `Chart.yaml` dependencies to look like:

```
dependencies:
- alias: bar
name: bar
repo: file:///abs/path/to/bar
condition: bar.enabled
```

Please read https://github.com/roboll/helmfile/issues/1762#issuecomment-816341251 for more details.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/spf13/cobra v1.1.1
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939
github.com/urfave/cli v1.22.5
github.com/variantdev/chartify v0.7.3
github.com/variantdev/chartify v0.8.0
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363
github.com/variantdev/vals v0.13.0
go.uber.org/multierr v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ github.com/variantdev/chartify v0.7.2 h1:H7a9xD4rfyKkLDhCO/Oj3VfYnJRb92Dqk+GR6vp
github.com/variantdev/chartify v0.7.2/go.mod h1:qF4XzQlkfH/6k2jAi1hLas+lK4zSCa8kY+r5JdmLA68=
github.com/variantdev/chartify v0.7.3 h1:uX0mN8PmYHZDWILcg41sQfasJ7TT/CYnYWSmDgsIBIA=
github.com/variantdev/chartify v0.7.3/go.mod h1:qF4XzQlkfH/6k2jAi1hLas+lK4zSCa8kY+r5JdmLA68=
github.com/variantdev/chartify v0.8.0 h1:yIBsS/dIUeMjWP8U6JWlT3PM0Lky7en9QBi+MgDu2U8=
github.com/variantdev/chartify v0.8.0/go.mod h1:qF4XzQlkfH/6k2jAi1hLas+lK4zSCa8kY+r5JdmLA68=
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363 h1:KrfQBEUn+wEOQ/6UIfoqRDvn+Q/wZridQ7t0G1vQqKE=
github.com/variantdev/dag v0.0.0-20191028002400-bb0b3c785363/go.mod h1:pH1TQsNSLj2uxMo9NNl9zdGy01Wtn+/2MT96BrKmVyE=
github.com/variantdev/vals v0.13.0 h1:zdtTBjoWKkUGdFauxETkDVjqWXdjUNwI+ggWcUmpxv8=
Expand Down
53 changes: 26 additions & 27 deletions pkg/state/helmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/roboll/helmfile/pkg/helmexec"
"github.com/roboll/helmfile/pkg/remote"
Expand Down Expand Up @@ -85,7 +84,7 @@ func (st *HelmState) goGetterChart(chart, dir, cacheDir string, force bool) (str
}

func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSpec, chart string, workerIndex int) (*Chartify, func(), error) {
chartify := &Chartify{
c := &Chartify{
Opts: &chartify.ChartifyOpts{
WorkaroundOutputDirIssue: true,
EnableKustomizeAlphaPlugins: true,
Expand All @@ -110,25 +109,25 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
}

for _, d := range release.Dependencies {
var dep string

if d.Alias != "" {
dep += d.Alias + "="
} else {
a := strings.Split(d.Chart, "/")

chart := a[len(a)-1]

dep += chart + "="
}

dep += d.Chart

if d.Version != "" {
dep += ":" + d.Version
chart := d.Chart
if st.directoryExistsAt(chart) {
var err error

// Otherwise helm-dependency-up on the temporary chart generated by chartify ends up errors like:
// Error: directory /tmp/chartify945964195/myapp-57fb4495cf/test/integration/charts/httpbin not found]
// which is due to that the temporary chart is generated outside of the current working directory/basePath,
// and therefore the relative path in `chart` points to somewhere inexistent.
chart, err = filepath.Abs(filepath.Join(st.basePath, chart))
if err != nil {
return nil, clean, err
}
}

chartify.Opts.AdhocChartDependencies = append(chartify.Opts.AdhocChartDependencies, dep)
c.Opts.AdhocChartDependencies = append(c.Opts.AdhocChartDependencies, chartify.ChartDependency{
Alias: d.Alias,
Chart: chart,
Version: d.Version,
})

shouldRun = true
}
Expand All @@ -143,7 +142,7 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
filesNeedCleaning = append(filesNeedCleaning, generatedFiles...)

for _, f := range generatedFiles {
chartify.Opts.JsonPatches = append(chartify.Opts.JsonPatches, f)
c.Opts.JsonPatches = append(c.Opts.JsonPatches, f)
}

shouldRun = true
Expand All @@ -157,7 +156,7 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
}

for _, f := range generatedFiles {
chartify.Opts.StrategicMergePatches = append(chartify.Opts.StrategicMergePatches, f)
c.Opts.StrategicMergePatches = append(c.Opts.StrategicMergePatches, f)
}

filesNeedCleaning = append(filesNeedCleaning, generatedFiles...)
Expand All @@ -173,7 +172,7 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
}

for _, f := range generatedFiles {
chartify.Opts.Transformers = append(chartify.Opts.Transformers, f)
c.Opts.Transformers = append(c.Opts.Transformers, f)
}

filesNeedCleaning = append(filesNeedCleaning, generatedFiles...)
Expand All @@ -182,7 +181,7 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp
}

if release.ForceNamespace != "" {
chartify.Opts.OverrideNamespace = release.ForceNamespace
c.Opts.OverrideNamespace = release.ForceNamespace

shouldRun = true
}
Expand All @@ -195,11 +194,11 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp

filesNeedCleaning = append(filesNeedCleaning, generatedFiles...)

chartify.Opts.ValuesFiles = generatedFiles
chartify.Opts.TemplateData = st.newReleaseTemplateData(release)
chartify.Opts.TemplateFuncs = st.newReleaseTemplateFuncMap(dir)
c.Opts.ValuesFiles = generatedFiles
c.Opts.TemplateData = st.newReleaseTemplateData(release)
c.Opts.TemplateFuncs = st.newReleaseTemplateFuncMap(dir)

return chartify, clean, nil
return c, clean, nil
}

return nil, clean, nil
Expand Down