Skip to content

Commit

Permalink
Merge pull request #5270 from MrFreezeex/helm-kube-api
Browse files Browse the repository at this point in the history
helm: add support for kube-version and add cli args for both kube-version and api-versions
  • Loading branch information
k8s-ci-robot authored Nov 15, 2023
2 parents 413e01e + dc29923 commit eb7f91f
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 6 deletions.
9 changes: 9 additions & 0 deletions api/internal/builtins/HelmChartInflationGenerator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

202 changes: 202 additions & 0 deletions api/krusty/helmchartinflationgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 7 additions & 0 deletions api/types/helmchartargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 2 additions & 0 deletions api/types/helmchartargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"})
Expand Down
6 changes: 4 additions & 2 deletions api/types/pluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions kustomize/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions kustomize/commands/build/flagenablehelm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eb7f91f

Please sign in to comment.