Skip to content

Commit

Permalink
feat: add api_versions and kube_version to helm block
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Peters committed May 5, 2024
1 parent 28b3868 commit 8437240
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/data-sources/overlay.md
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ Must enable helm support via [kustomize_options](#kustomize_options---optional)
- `values_file` specify a file with helm values to use for templating. Not specifying this uses the in-chart values file, if one exists.
- `values_inline` specify helm values inline from terraform, as a string
- `values_merge` merge strategy if both `values_file` and `values_inline` are specified. Can be one of `override`, `replace`, `merge`. (default: `override`)
- `api_versions` List of Kubernetes api versions used for Capabilities.APIVersions.
- `kube_version` Allows specifying a custom kubernetes version to use when templating.

#### Example

Expand All @@ -642,6 +644,8 @@ data "kustomization_overlay" "minecraft" {
release_name = "moria"
include_crds = false
skip_tests = false
kube_version = "1.29.0"
api_versions = ["monitoring.coreos.com/v1"]
values_inline = <<VALUES
minecraftServer:
eula: true
Expand Down
11 changes: 11 additions & 0 deletions kustomize/data_source_kustomization_overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ func dataSourceKustomizationOverlay() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"api_versions": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"kube_version": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -1010,6 +1019,8 @@ func getKustomization(d *schema.ResourceData) (k types.Kustomization) {
hca.ValuesMerge = hc["values_merge"].(string)
hca.IncludeCRDs = hc["include_crds"].(bool)
hca.SkipTests = hc["skip_tests"].(bool)
hca.KubeVersion = hc["kube_version"].(string)
hca.ApiVersions = convertListInterfaceToListString(hc["api_versions"].([]interface{}))

hc_vi := make(map[string]interface{})
if err := yaml.Unmarshal([]byte(hc["values_inline"].(string)), &hc_vi); err != nil {
Expand Down
107 changes: 107 additions & 0 deletions kustomize/data_source_kustomization_overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kustomize

import (
"fmt"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -1185,6 +1186,9 @@ func TestDataSourceKustomizationOverlay_helm_charts_attr(t *testing.T) {
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "ids.#", "3"),
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "ids_prio.#", "3"),
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "manifests.%", "3"),
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "helm_charts.0.kube_version", "1.29.0"),
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "helm_charts.0.api_versions.#", "1"),
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "helm_charts.0.api_versions.0", "monitoring.coreos.com/v1"),
),
},
},
Expand All @@ -1203,6 +1207,8 @@ data "kustomization_overlay" "test" {
version = "0.0.1"
namespace = "not-used"
skip_tests = true
kube_version = "1.29.0"
api_versions = ["monitoring.coreos.com/v1"]
}
namespace = "test-basic"
Expand Down Expand Up @@ -1681,3 +1687,104 @@ output "rolebinding" {
}
`
}

// Test the capabilites helm chart without capabilities set
func TestDataSourceKustomizationOverlay_helm_charts_capabilities_not_set(t *testing.T) {

resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_not_set(),
Check: resource.ComposeAggregateTestCheckFunc(
// There should be only the default configmap in the manifests
resource.TestCheckOutput("default_configmap", "{\"apiVersion\":\"v1\",\"data\":{\"should\":\"always exist\"},\"kind\":\"ConfigMap\",\"metadata\":{\"labels\":{\"app\":\"my-release\",\"name\":\"my-release\"},\"name\":\"default-configmap\"}}"),
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "manifests.%", "1"),
),
},
},
})
}

func testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_not_set() string {
return `
data "kustomization_overlay" "test" {
helm_globals {
chart_home = "./test_kustomizations/helm/initial/charts/"
}
helm_charts {
name = "test-capabilities"
version = "0.0.1"
release_name = "my-release"
}
kustomize_options {
enable_helm = true
helm_path = "helm"
}
}
output "default_configmap" {
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/default-configmap"]
}`
}

// Test the capabilites helm chart without capabilities set
func TestDataSourceKustomizationOverlay_helm_charts_capabilities_set(t *testing.T) {

foo := os.Environ()
_ = foo

resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_set(),
Check: resource.ComposeAggregateTestCheckFunc(
// There should be only the default configmap in the manifests
resource.TestCheckOutput("default_configmap", "{\"apiVersion\":\"v1\",\"data\":{\"should\":\"always exist\"},\"kind\":\"ConfigMap\",\"metadata\":{\"labels\":{\"app\":\"my-release\",\"name\":\"my-release\"},\"name\":\"default-configmap\"}}"),
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "manifests.%", "3"),

// Check conditional configmaps exists
resource.TestCheckOutput("kubeversion_configmap", "{\"apiVersion\":\"v1\",\"data\":{\"should\":\"only when kubeversion is equals v1.42.0\"},\"kind\":\"ConfigMap\",\"metadata\":{\"labels\":{\"app\":\"my-release\",\"name\":\"my-release\"},\"name\":\"kubeversion-configmap\"}}"),
resource.TestCheckOutput("apiversions_configmap", "{\"apiVersion\":\"v1\",\"data\":{\"should\":\"only when a the fake apiVersion is set\"},\"kind\":\"ConfigMap\",\"metadata\":{\"labels\":{\"app\":\"my-release\",\"name\":\"my-release\"},\"name\":\"apiversions-configmap\"}}"),
),
},
},
})
}

func testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_set() string {
return `
data "kustomization_overlay" "test" {
helm_globals {
chart_home = "./test_kustomizations/helm/initial/charts/"
}
helm_charts {
name = "test-capabilities"
version = "0.0.1"
release_name = "my-release"
kube_version = "v1.42.0"
api_versions = ["foo.bar/v1"]
}
kustomize_options {
enable_helm = true
helm_path = "helm"
}
}
output "default_configmap" {
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/default-configmap"]
}
output "kubeversion_configmap" {
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/kubeversion-configmap"]
}
output "apiversions_configmap" {
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/apiversions-configmap"]
}`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v2
description: A Simple Helm chart to test capabilitie set by the user
name: test-capabilities
version: 0.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{/*
Expand the name of the chart. Set the chart name to nginx by default
*/}}
{{- define "test-capabilities.name" -}}
{{- .Release.Name | default "capabilities" }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: default-configmap
labels:
app: {{ include "test-capabilities.name" . }}
name: {{ include "test-capabilities.name" . }}
data:
should: "always exist"
---
{{ if eq .Capabilities.KubeVersion.Version "v1.42.0" }}
apiVersion: v1
kind: ConfigMap
metadata:
name: kubeversion-configmap
labels:
app: {{ include "test-capabilities.name" . }}
name: {{ include "test-capabilities.name" . }}
data:
should: "only when kubeversion is equals v1.42.0"
{{- end }}
---
{{ if $.Capabilities.APIVersions.Has "foo.bar/v1" }}
apiVersion: v1
kind: ConfigMap
metadata:
name: apiversions-configmap
labels:
app: {{ include "test-capabilities.name" . }}
name: {{ include "test-capabilities.name" . }}
data:
should: "only when a the fake apiVersion is set"
{{- end }}
Empty file.

0 comments on commit 8437240

Please sign in to comment.