| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package types |
| 5 |
|
| 6 |
import "path/filepath" |
| 7 |
|
| 8 |
const HelmDefaultHome = "charts" |
| 9 |
|
| 10 |
type HelmGlobals struct { |
| 11 |
// ChartHome is a file path, relative to the kustomization root, |
| 12 |
// to a directory containing a subdirectory for each chart to be |
| 13 |
// included in the kustomization. |
| 14 |
// The default value of this field is "charts". |
| 15 |
// So, for example, kustomize looks for the minecraft chart |
| 16 |
// at {kustomizationRoot}/{ChartHome}/minecraft. |
| 17 |
// If the chart is there at build time, kustomize will use it as found, |
| 18 |
// and not check version numbers or dates. |
| 19 |
// If the chart is not there, kustomize will attempt to pull it |
| 20 |
// using the version number specified in the kustomization file, |
| 21 |
// and put it there. To suppress the pull attempt, simply assure |
| 22 |
// that the chart is already there. |
| 23 |
ChartHome string `json:"chartHome,omitempty" yaml:"chartHome,omitempty"` |
| 24 |
|
| 25 |
// ConfigHome defines a value that kustomize should pass to helm via |
| 26 |
// the HELM_CONFIG_HOME environment variable. kustomize doesn't attempt |
| 27 |
// to read or write this directory. |
| 28 |
// If omitted, {tmpDir}/helm is used, where {tmpDir} is some temporary |
| 29 |
// directory created by kustomize for the benefit of helm. |
| 30 |
// Likewise, kustomize sets |
| 31 |
// HELM_CACHE_HOME={ConfigHome}/.cache |
| 32 |
// HELM_DATA_HOME={ConfigHome}/.data |
| 33 |
// for the helm subprocess. |
| 34 |
ConfigHome string `json:"configHome,omitempty" yaml:"configHome,omitempty"` |
| 35 |
} |
| 36 |
|
| 37 |
type HelmChart struct { |
| 38 |
// Name is the name of the chart, e.g. 'minecraft'. |
| 39 |
Name string `json:"name,omitempty" yaml:"name,omitempty"` |
| 40 |
|
| 41 |
// Version is the version of the chart, e.g. '3.1.3' |
| 42 |
Version string `json:"version,omitempty" yaml:"version,omitempty"` |
| 43 |
|
| 44 |
// Repo is a URL locating the chart on the internet. |
| 45 |
// This is the argument to helm's `--repo` flag, e.g. |
| 46 |
// `https://itzg.github.io/minecraft-server-charts`. |
| 47 |
Repo string `json:"repo,omitempty" yaml:"repo,omitempty"` |
| 48 |
|
| 49 |
// ReleaseName replaces RELEASE-NAME in chart template output, |
| 50 |
// making a particular inflation of a chart unique with respect to |
| 51 |
// other inflations of the same chart in a cluster. It's the first |
| 52 |
// argument to the helm `install` and `template` commands, i.e. |
| 53 |
// helm install {RELEASE-NAME} {chartName} |
| 54 |
// helm template {RELEASE-NAME} {chartName} |
| 55 |
// If omitted, the flag --generate-name is passed to 'helm template'. |
| 56 |
ReleaseName string `json:"releaseName,omitempty" yaml:"releaseName,omitempty"` |
| 57 |
|
| 58 |
// Namespace set the target namespace for a release. It is .Release.Namespace |
| 59 |
// in the helm template |
| 60 |
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` |
| 61 |
|
| 62 |
// AdditionalValuesFiles are local file paths to values files to be used in |
| 63 |
// addition to either the default values file or the values specified in ValuesFile. |
| 64 |
AdditionalValuesFiles []string `json:"additionalValuesFiles,omitempty" yaml:"additionalValuesFiles,omitempty"` |
| 65 |
|
| 66 |
// ValuesFile is a local file path to a values file to use _instead of_ |
| 67 |
// the default values that accompanied the chart. |
| 68 |
// The default values are in '{ChartHome}/{Name}/values.yaml'. |
| 69 |
ValuesFile string `json:"valuesFile,omitempty" yaml:"valuesFile,omitempty"` |
| 70 |
|
| 71 |
// ValuesInline holds value mappings specified directly, |
| 72 |
// rather than in a separate file. |
| 73 |
ValuesInline map[string]interface{} `json:"valuesInline,omitempty" yaml:"valuesInline,omitempty"` |
| 74 |
|
| 75 |
// ValuesMerge specifies how to treat ValuesInline with respect to Values. |
| 76 |
// Legal values: 'merge', 'override', 'replace'. |
| 77 |
// Defaults to 'override'. |
| 78 |
ValuesMerge string `json:"valuesMerge,omitempty" yaml:"valuesMerge,omitempty"` |
| 79 |
|
| 80 |
// IncludeCRDs specifies if Helm should also generate CustomResourceDefinitions. |
| 81 |
// Defaults to 'false'. |
| 82 |
IncludeCRDs bool `json:"includeCRDs,omitempty" yaml:"includeCRDs,omitempty"` //nolint: tagliatelle |
| 83 |
|
| 84 |
// SkipHooks sets the --no-hooks flag when calling helm template. This prevents |
| 85 |
// helm from erroneously rendering test templates. |
| 86 |
SkipHooks bool `json:"skipHooks,omitempty" yaml:"skipHooks,omitempty"` |
| 87 |
|
| 88 |
// ApiVersions is the kubernetes apiversions used for Capabilities.APIVersions |
| 89 |
ApiVersions []string `json:"apiVersions,omitempty" yaml:"apiVersions,omitempty"` |
| 90 |
|
| 91 |
// KubeVersion is the kubernetes version used by Helm for Capabilities.KubeVersion" |
| 92 |
KubeVersion string `json:"kubeVersion,omitempty" yaml:"kubeVersion,omitempty"` |
| 93 |
|
| 94 |
// NameTemplate is for specifying the name template used to name the release. |
| 95 |
NameTemplate string `json:"nameTemplate,omitempty" yaml:"nameTemplate,omitempty"` |
| 96 |
|
| 97 |
// SkipTests skips tests from templated output. |
| 98 |
SkipTests bool `json:"skipTests,omitempty" yaml:"skipTests,omitempty"` |
| 99 |
|
| 100 |
// debug enables debug output from the Helm chart inflator generator. |
| 101 |
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"` |
| 102 |
|
| 103 |
// allow for devel release to be used. |
| 104 |
Devel bool `json:"devel,omitempty" yaml:"devel,omitempty"` |
| 105 |
} |
| 106 |
|
| 107 |
// HelmChartArgs contains arguments to helm. |
| 108 |
// Deprecated. Use HelmGlobals and HelmChart instead. |
| 109 |
type HelmChartArgs struct { |
| 110 |
ChartName string `json:"chartName,omitempty" yaml:"chartName,omitempty"` |
| 111 |
ChartVersion string `json:"chartVersion,omitempty" yaml:"chartVersion,omitempty"` |
| 112 |
ChartRepoURL string `json:"chartRepoUrl,omitempty" yaml:"chartRepoUrl,omitempty"` |
| 113 |
ChartHome string `json:"chartHome,omitempty" yaml:"chartHome,omitempty"` |
| 114 |
ChartRepoName string `json:"chartRepoName,omitempty" yaml:"chartRepoName,omitempty"` |
| 115 |
HelmBin string `json:"helmBin,omitempty" yaml:"helmBin,omitempty"` |
| 116 |
HelmHome string `json:"helmHome,omitempty" yaml:"helmHome,omitempty"` |
| 117 |
Values string `json:"values,omitempty" yaml:"values,omitempty"` |
| 118 |
ValuesLocal map[string]interface{} `json:"valuesLocal,omitempty" yaml:"valuesLocal,omitempty"` |
| 119 |
ValuesMerge string `json:"valuesMerge,omitempty" yaml:"valuesMerge,omitempty"` |
| 120 |
ReleaseName string `json:"releaseName,omitempty" yaml:"releaseName,omitempty"` |
| 121 |
ReleaseNamespace string `json:"releaseNamespace,omitempty" yaml:"releaseNamespace,omitempty"` |
| 122 |
ExtraArgs []string `json:"extraArgs,omitempty" yaml:"extraArgs,omitempty"` |
| 123 |
} |
| 124 |
|
| 125 |
// SplitHelmParameters splits helm parameters into |
| 126 |
// per-chart params and global chart-independent parameters. |
| 127 |
func SplitHelmParameters( |
| 128 |
oldArgs []HelmChartArgs) (charts []HelmChart, globals HelmGlobals) { |
| 129 |
for i, old := range oldArgs { |
| 130 |
charts = append(charts, makeHelmChartFromHca(&oldArgs[i])) |
| 131 |
if old.HelmHome != "" { |
| 132 |
// last non-empty wins |
| 133 |
globals.ConfigHome = old.HelmHome |
| 134 |
} |
| 135 |
if old.ChartHome != "" { |
| 136 |
// last non-empty wins |
| 137 |
globals.ChartHome = old.ChartHome |
| 138 |
} |
| 139 |
} |
| 140 |
return charts, globals |
| 141 |
} |
| 142 |
|
| 143 |
func makeHelmChartFromHca(old *HelmChartArgs) (c HelmChart) { |
| 144 |
c.Name = old.ChartName |
| 145 |
c.Version = old.ChartVersion |
| 146 |
c.Repo = old.ChartRepoURL |
| 147 |
c.ValuesFile = old.Values |
| 148 |
c.ValuesInline = old.ValuesLocal |
| 149 |
c.ValuesMerge = old.ValuesMerge |
| 150 |
c.ReleaseName = old.ReleaseName |
| 151 |
return |
| 152 |
} |
| 153 |
|
| 154 |
func (h HelmChart) AsHelmArgs(absChartHome string) []string { |
| 155 |
args := []string{"template"} |
| 156 |
if h.ReleaseName != "" { |
| 157 |
args = append(args, h.ReleaseName) |
| 158 |
} else { |
| 159 |
// AFAICT, this doesn't work as intended due to a bug in helm. |
| 160 |
// See https://github.com/helm/helm/issues/6019 |
| 161 |
// I've tried placing the flag before and after the name argument. |
| 162 |
args = append(args, "--generate-name") |
| 163 |
} |
| 164 |
if h.Name != "" { |
| 165 |
args = append(args, filepath.Join(absChartHome, h.Name)) |
| 166 |
} |
| 167 |
if h.Namespace != "" { |
| 168 |
args = append(args, "--namespace", h.Namespace) |
| 169 |
} |
| 170 |
if h.NameTemplate != "" { |
| 171 |
args = append(args, "--name-template", h.NameTemplate) |
| 172 |
} |
| 173 |
|
| 174 |
if h.ValuesFile != "" { |
| 175 |
args = append(args, "-f", h.ValuesFile) |
| 176 |
} |
| 177 |
for _, valuesFile := range h.AdditionalValuesFiles { |
| 178 |
args = append(args, "-f", valuesFile) |
| 179 |
} |
| 180 |
|
| 181 |
for _, apiVer := range h.ApiVersions { |
| 182 |
args = append(args, "--api-versions", apiVer) |
| 183 |
} |
| 184 |
if h.KubeVersion != "" { |
| 185 |
args = append(args, "--kube-version", h.KubeVersion) |
| 186 |
} |
| 187 |
|
| 188 |
if h.IncludeCRDs { |
| 189 |
args = append(args, "--include-crds") |
| 190 |
} |
| 191 |
if h.SkipTests { |
| 192 |
args = append(args, "--skip-tests") |
| 193 |
} |
| 194 |
if h.SkipHooks { |
| 195 |
args = append(args, "--no-hooks") |
| 196 |
} |
| 197 |
if h.Debug { |
| 198 |
args = append(args, "--debug") |
| 199 |
} |
| 200 |
if h.Devel { |
| 201 |
args = append(args, "--devel") |
| 202 |
} |
| 203 |
return args |
| 204 |
} |