From d5dd5f4567b32fabd290eaad577c161ca428f785 Mon Sep 17 00:00:00 2001 From: Arthur Outhenin-Chalandre Date: Mon, 14 Aug 2023 19:42:06 +0200 Subject: [PATCH 1/3] helm: add kube-version Signed-off-by: Arthur Outhenin-Chalandre --- api/types/helmchartargs.go | 7 +++++++ api/types/helmchartargs_test.go | 2 ++ 2 files changed, 9 insertions(+) diff --git a/api/types/helmchartargs.go b/api/types/helmchartargs.go index 15ea7178fb..1ed89c01ce 100644 --- a/api/types/helmchartargs.go +++ b/api/types/helmchartargs.go @@ -88,6 +88,9 @@ type HelmChart struct { // ApiVersions is the kubernetes apiversions used for Capabilities.APIVersions ApiVersions []string `json:"apiVersions,omitempty" yaml:"apiVersions,omitempty"` + // KubeVersion is the kubernetes version used by Helm for Capabilities.KubeVersion" + KubeVersion string `json:"kubeVersion,omitempty" yaml:"kubeVersion,omitempty"` + // NameTemplate is for specifying the name template used to name the release. NameTemplate string `json:"nameTemplate,omitempty" yaml:"nameTemplate,omitempty"` @@ -172,6 +175,10 @@ func (h HelmChart) AsHelmArgs(absChartHome string) []string { for _, apiVer := range h.ApiVersions { args = append(args, "--api-versions", apiVer) } + if h.KubeVersion != "" { + args = append(args, "--kube-version", h.KubeVersion) + } + if h.IncludeCRDs { args = append(args, "--include-crds") } diff --git a/api/types/helmchartargs_test.go b/api/types/helmchartargs_test.go index 765a28a05f..9827360d9b 100644 --- a/api/types/helmchartargs_test.go +++ b/api/types/helmchartargs_test.go @@ -17,6 +17,7 @@ func TestAsHelmArgs(t *testing.T) { Version: "1.0.0", Repo: "https://helm.releases.hashicorp.com", ApiVersions: []string{"foo", "bar"}, + KubeVersion: "1.27", NameTemplate: "template", SkipTests: true, IncludeCRDs: true, @@ -33,6 +34,7 @@ func TestAsHelmArgs(t *testing.T) { "-f", "values", "-f", "values1", "-f", "values2", "--api-versions", "foo", "--api-versions", "bar", + "--kube-version", "1.27", "--include-crds", "--skip-tests", "--no-hooks"}) From 790ca0e7b66aae32c4ab6258709f15be471b15b4 Mon Sep 17 00:00:00 2001 From: Arthur Outhenin-Chalandre Date: Mon, 14 Aug 2023 19:42:45 +0200 Subject: [PATCH 2/3] helm: add kube-version and api-versions on CLI args It makes sense to add that as a CLI args since you could use one single kustomization file/helm chart for multiple clusters. Also it's easier to have those on the CLI if the user has some kind of tooling that will end up calling kustomize and that could pass those (i.e.: ArgoCD is doing that for Helm so it could do that for Kustomize as well that will end up calling Helm as well). Signed-off-by: Arthur Outhenin-Chalandre --- api/internal/builtins/HelmChartInflationGenerator.go | 9 +++++++++ api/types/pluginconfig.go | 6 ++++-- kustomize/commands/build/build.go | 12 ++++++++---- kustomize/commands/build/flagenablehelm.go | 10 ++++++++++ .../HelmChartInflationGenerator.go | 9 +++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/api/internal/builtins/HelmChartInflationGenerator.go b/api/internal/builtins/HelmChartInflationGenerator.go index 40437a2155..d0380f0c55 100644 --- a/api/internal/builtins/HelmChartInflationGenerator.go +++ b/api/internal/builtins/HelmChartInflationGenerator.go @@ -53,6 +53,15 @@ func (p *HelmChartInflationGeneratorPlugin) Config( if h.GeneralConfig().HelmConfig.Command == "" { return fmt.Errorf("must specify --helm-command") } + + // CLI args takes precedence + if h.GeneralConfig().HelmConfig.KubeVersion != "" { + p.HelmChart.KubeVersion = h.GeneralConfig().HelmConfig.KubeVersion + } + if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 { + p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions + } + p.h = h if err = yaml.Unmarshal(config, p); err != nil { return diff --git a/api/types/pluginconfig.go b/api/types/pluginconfig.go index 741e5debc4..aa511ae79c 100644 --- a/api/types/pluginconfig.go +++ b/api/types/pluginconfig.go @@ -4,8 +4,10 @@ package types type HelmConfig struct { - Enabled bool - Command string + Enabled bool + Command string + ApiVersions []string + KubeVersion string } // PluginConfig holds plugin configuration. diff --git a/kustomize/commands/build/build.go b/kustomize/commands/build/build.go index 3e884a6127..5914f89593 100644 --- a/kustomize/commands/build/build.go +++ b/kustomize/commands/build/build.go @@ -27,10 +27,12 @@ var theFlags struct { managedByLabel bool helm bool } - helmCommand string - loadRestrictor string - reorderOutput string - fnOptions types.FnPluginLoadingOptions + helmCommand string + helmApiVersions []string + helmKubeVersion string + loadRestrictor string + reorderOutput string + fnOptions types.FnPluginLoadingOptions } type Help struct { @@ -153,6 +155,8 @@ func HonorKustomizeFlags(kOpts *krusty.Options, flags *flag.FlagSet) *krusty.Opt kOpts.PluginConfig.HelmConfig.Enabled = theFlags.enable.helm } kOpts.PluginConfig.HelmConfig.Command = theFlags.helmCommand + kOpts.PluginConfig.HelmConfig.ApiVersions = theFlags.helmApiVersions + kOpts.PluginConfig.HelmConfig.KubeVersion = theFlags.helmKubeVersion kOpts.AddManagedbyLabel = isManagedByLabelEnabled() return kOpts } diff --git a/kustomize/commands/build/flagenablehelm.go b/kustomize/commands/build/flagenablehelm.go index 1a328ce671..a379d180a5 100644 --- a/kustomize/commands/build/flagenablehelm.go +++ b/kustomize/commands/build/flagenablehelm.go @@ -21,4 +21,14 @@ func AddFlagEnableHelm(set *pflag.FlagSet) { "helm-command", "helm", // default "helm command (path to executable)") + set.StringArrayVar( + &theFlags.helmApiVersions, + "helm-api-versions", + []string{}, // default + "Kubernetes api versions used by Helm for Capabilities.APIVersions") + set.StringVar( + &theFlags.helmKubeVersion, + "helm-kube-version", + "", // default + "Kubernetes version used by Helm for Capabilities.KubeVersion") } diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go index 01adb29f5b..f72047c418 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go @@ -59,6 +59,15 @@ func (p *plugin) Config( if h.GeneralConfig().HelmConfig.Command == "" { return fmt.Errorf("must specify --helm-command") } + + // CLI args takes precedence + if h.GeneralConfig().HelmConfig.KubeVersion != "" { + p.HelmChart.KubeVersion = h.GeneralConfig().HelmConfig.KubeVersion + } + if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 { + p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions + } + p.h = h if err = yaml.Unmarshal(config, p); err != nil { return From dc29923a08853177ffe477ec9ec9242fe52d5f16 Mon Sep 17 00:00:00 2001 From: Arthur Outhenin-Chalandre Date: Thu, 2 Nov 2023 18:35:25 +0100 Subject: [PATCH 3/3] helm: add tests with different kubeVersion Signed-off-by: Arthur Outhenin-Chalandre --- .../helmchartinflationgenerator_test.go | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) diff --git a/api/krusty/helmchartinflationgenerator_test.go b/api/krusty/helmchartinflationgenerator_test.go index daffa16a0a..7fc3f76e7f 100644 --- a/api/krusty/helmchartinflationgenerator_test.go +++ b/api/krusty/helmchartinflationgenerator_test.go @@ -684,6 +684,208 @@ spec: `) } +func TestHelmChartInflationGeneratorForMultipleKubeVersions(t *testing.T) { + th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t) + defer th.Reset() + if err := th.ErrIfNoHelm(); err != nil { + t.Skip("skipping: " + err.Error()) + } + + copyValuesFilesTestChartsIntoHarness(t, th) + + th.WriteK(th.GetRoot(), ` +namespace: default +helmCharts: + - name: minecraft + repo: https://itzg.github.io/minecraft-server-charts + version: 4.11.0 + releaseName: test + kubeVersion: "1.16" + valuesInline: + minecraftServer: + extraPorts: + - name: map + containerPort: 8123 + protocol: TCP + service: + enabled: false + ingress: + enabled: true +`) + + m := th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled()) + th.AssertActualEqualsExpected(m, ` +apiVersion: v1 +data: + rcon-password: Q0hBTkdFTUUh +kind: Secret +metadata: + labels: + app: test-minecraft + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft-rcon + namespace: default +type: Opaque +--- +apiVersion: v1 +data: + cf-api-key: Q0hBTkdFTUUh +kind: Secret +metadata: + labels: + app: test-minecraft + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft-curseforge + namespace: default +type: Opaque +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: test-minecraft + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft + namespace: default +spec: + ports: + - name: minecraft + port: 25565 + protocol: TCP + targetPort: minecraft + selector: + app: test-minecraft + type: ClusterIP +--- +apiVersion: networking.k8s.io/v1beta1 +kind: Ingress +metadata: + labels: + app: test-minecraft-map + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft-map + namespace: default +spec: + rules: null +`) + + th.WriteK(th.GetRoot(), ` +namespace: default +helmCharts: + - name: minecraft + repo: https://itzg.github.io/minecraft-server-charts + version: 4.11.0 + releaseName: test + kubeVersion: "1.27" + valuesInline: + minecraftServer: + extraPorts: + - name: map + containerPort: 8123 + protocol: TCP + service: + enabled: false + ingress: + enabled: true +`) + + m = th.Run(th.GetRoot(), th.MakeOptionsPluginsEnabled()) + th.AssertActualEqualsExpected(m, ` +apiVersion: v1 +data: + rcon-password: Q0hBTkdFTUUh +kind: Secret +metadata: + labels: + app: test-minecraft + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft-rcon + namespace: default +type: Opaque +--- +apiVersion: v1 +data: + cf-api-key: Q0hBTkdFTUUh +kind: Secret +metadata: + labels: + app: test-minecraft + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft-curseforge + namespace: default +type: Opaque +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: test-minecraft + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft + namespace: default +spec: + ports: + - name: minecraft + port: 25565 + protocol: TCP + targetPort: minecraft + selector: + app: test-minecraft + type: ClusterIP +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + labels: + app: test-minecraft-map + app.kubernetes.io/instance: test-minecraft + app.kubernetes.io/name: minecraft + app.kubernetes.io/version: 4.11.0 + chart: minecraft-4.11.0 + heritage: Helm + release: test + name: test-minecraft-map + namespace: default +spec: + rules: null +`) +} + func copyValuesFilesTestChartsIntoHarness(t *testing.T, th *kusttest_test.HarnessEnhanced) { t.Helper()