Skip to content

Commit 2333640

Browse files
authored
Merge pull request #253 from FlxPeters/feat-helm-api-versions
Add api_versions and kube_version to helm_charts block
2 parents 28b3868 + 8437240 commit 2333640

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed

docs/data-sources/overlay.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ Must enable helm support via [kustomize_options](#kustomize_options---optional)
630630
- `values_file` specify a file with helm values to use for templating. Not specifying this uses the in-chart values file, if one exists.
631631
- `values_inline` specify helm values inline from terraform, as a string
632632
- `values_merge` merge strategy if both `values_file` and `values_inline` are specified. Can be one of `override`, `replace`, `merge`. (default: `override`)
633+
- `api_versions` List of Kubernetes api versions used for Capabilities.APIVersions.
634+
- `kube_version` Allows specifying a custom kubernetes version to use when templating.
633635

634636
#### Example
635637

@@ -642,6 +644,8 @@ data "kustomization_overlay" "minecraft" {
642644
release_name = "moria"
643645
include_crds = false
644646
skip_tests = false
647+
kube_version = "1.29.0"
648+
api_versions = ["monitoring.coreos.com/v1"]
645649
values_inline = <<VALUES
646650
minecraftServer:
647651
eula: true

kustomize/data_source_kustomization_overlay.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,15 @@ func dataSourceKustomizationOverlay() *schema.Resource {
505505
Type: schema.TypeString,
506506
Optional: true,
507507
},
508+
"api_versions": {
509+
Type: schema.TypeList,
510+
Optional: true,
511+
Elem: &schema.Schema{Type: schema.TypeString},
512+
},
513+
"kube_version": {
514+
Type: schema.TypeString,
515+
Optional: true,
516+
},
508517
},
509518
},
510519
},
@@ -1010,6 +1019,8 @@ func getKustomization(d *schema.ResourceData) (k types.Kustomization) {
10101019
hca.ValuesMerge = hc["values_merge"].(string)
10111020
hca.IncludeCRDs = hc["include_crds"].(bool)
10121021
hca.SkipTests = hc["skip_tests"].(bool)
1022+
hca.KubeVersion = hc["kube_version"].(string)
1023+
hca.ApiVersions = convertListInterfaceToListString(hc["api_versions"].([]interface{}))
10131024

10141025
hc_vi := make(map[string]interface{})
10151026
if err := yaml.Unmarshal([]byte(hc["values_inline"].(string)), &hc_vi); err != nil {

kustomize/data_source_kustomization_overlay_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kustomize
22

33
import (
44
"fmt"
5+
"os"
56
"regexp"
67
"testing"
78

@@ -1185,6 +1186,9 @@ func TestDataSourceKustomizationOverlay_helm_charts_attr(t *testing.T) {
11851186
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "ids.#", "3"),
11861187
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "ids_prio.#", "3"),
11871188
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "manifests.%", "3"),
1189+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "helm_charts.0.kube_version", "1.29.0"),
1190+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "helm_charts.0.api_versions.#", "1"),
1191+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "helm_charts.0.api_versions.0", "monitoring.coreos.com/v1"),
11881192
),
11891193
},
11901194
},
@@ -1203,6 +1207,8 @@ data "kustomization_overlay" "test" {
12031207
version = "0.0.1"
12041208
namespace = "not-used"
12051209
skip_tests = true
1210+
kube_version = "1.29.0"
1211+
api_versions = ["monitoring.coreos.com/v1"]
12061212
}
12071213
12081214
namespace = "test-basic"
@@ -1681,3 +1687,104 @@ output "rolebinding" {
16811687
}
16821688
`
16831689
}
1690+
1691+
// Test the capabilites helm chart without capabilities set
1692+
func TestDataSourceKustomizationOverlay_helm_charts_capabilities_not_set(t *testing.T) {
1693+
1694+
resource.Test(t, resource.TestCase{
1695+
IsUnitTest: true,
1696+
Providers: testAccProviders,
1697+
Steps: []resource.TestStep{
1698+
{
1699+
Config: testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_not_set(),
1700+
Check: resource.ComposeAggregateTestCheckFunc(
1701+
// There should be only the default configmap in the manifests
1702+
resource.TestCheckOutput("default_configmap", "{\"apiVersion\":\"v1\",\"data\":{\"should\":\"always exist\"},\"kind\":\"ConfigMap\",\"metadata\":{\"labels\":{\"app\":\"my-release\",\"name\":\"my-release\"},\"name\":\"default-configmap\"}}"),
1703+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "manifests.%", "1"),
1704+
),
1705+
},
1706+
},
1707+
})
1708+
}
1709+
1710+
func testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_not_set() string {
1711+
return `
1712+
data "kustomization_overlay" "test" {
1713+
helm_globals {
1714+
chart_home = "./test_kustomizations/helm/initial/charts/"
1715+
}
1716+
1717+
helm_charts {
1718+
name = "test-capabilities"
1719+
version = "0.0.1"
1720+
release_name = "my-release"
1721+
}
1722+
1723+
kustomize_options {
1724+
enable_helm = true
1725+
helm_path = "helm"
1726+
}
1727+
}
1728+
1729+
output "default_configmap" {
1730+
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/default-configmap"]
1731+
}`
1732+
}
1733+
1734+
// Test the capabilites helm chart without capabilities set
1735+
func TestDataSourceKustomizationOverlay_helm_charts_capabilities_set(t *testing.T) {
1736+
1737+
foo := os.Environ()
1738+
_ = foo
1739+
1740+
resource.Test(t, resource.TestCase{
1741+
IsUnitTest: true,
1742+
Providers: testAccProviders,
1743+
Steps: []resource.TestStep{
1744+
{
1745+
Config: testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_set(),
1746+
Check: resource.ComposeAggregateTestCheckFunc(
1747+
// There should be only the default configmap in the manifests
1748+
resource.TestCheckOutput("default_configmap", "{\"apiVersion\":\"v1\",\"data\":{\"should\":\"always exist\"},\"kind\":\"ConfigMap\",\"metadata\":{\"labels\":{\"app\":\"my-release\",\"name\":\"my-release\"},\"name\":\"default-configmap\"}}"),
1749+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "manifests.%", "3"),
1750+
1751+
// Check conditional configmaps exists
1752+
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\"}}"),
1753+
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\"}}"),
1754+
),
1755+
},
1756+
},
1757+
})
1758+
}
1759+
1760+
func testDataSourceKustomizationOverlayConfig_helm_charts_capabilities_set() string {
1761+
return `
1762+
data "kustomization_overlay" "test" {
1763+
helm_globals {
1764+
chart_home = "./test_kustomizations/helm/initial/charts/"
1765+
}
1766+
1767+
helm_charts {
1768+
name = "test-capabilities"
1769+
version = "0.0.1"
1770+
release_name = "my-release"
1771+
kube_version = "v1.42.0"
1772+
api_versions = ["foo.bar/v1"]
1773+
}
1774+
1775+
kustomize_options {
1776+
enable_helm = true
1777+
helm_path = "helm"
1778+
}
1779+
}
1780+
1781+
output "default_configmap" {
1782+
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/default-configmap"]
1783+
}
1784+
output "kubeversion_configmap" {
1785+
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/kubeversion-configmap"]
1786+
}
1787+
output "apiversions_configmap" {
1788+
value = data.kustomization_overlay.test.manifests["_/ConfigMap/_/apiversions-configmap"]
1789+
}`
1790+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v2
2+
description: A Simple Helm chart to test capabilitie set by the user
3+
name: test-capabilities
4+
version: 0.0.1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{/*
2+
Expand the name of the chart. Set the chart name to nginx by default
3+
*/}}
4+
{{- define "test-capabilities.name" -}}
5+
{{- .Release.Name | default "capabilities" }}
6+
{{- end }}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
name: default-configmap
6+
labels:
7+
app: {{ include "test-capabilities.name" . }}
8+
name: {{ include "test-capabilities.name" . }}
9+
data:
10+
should: "always exist"
11+
---
12+
{{ if eq .Capabilities.KubeVersion.Version "v1.42.0" }}
13+
apiVersion: v1
14+
kind: ConfigMap
15+
metadata:
16+
name: kubeversion-configmap
17+
labels:
18+
app: {{ include "test-capabilities.name" . }}
19+
name: {{ include "test-capabilities.name" . }}
20+
data:
21+
should: "only when kubeversion is equals v1.42.0"
22+
{{- end }}
23+
---
24+
{{ if $.Capabilities.APIVersions.Has "foo.bar/v1" }}
25+
apiVersion: v1
26+
kind: ConfigMap
27+
metadata:
28+
name: apiversions-configmap
29+
labels:
30+
app: {{ include "test-capabilities.name" . }}
31+
name: {{ include "test-capabilities.name" . }}
32+
data:
33+
should: "only when a the fake apiVersion is set"
34+
{{- end }}

kustomize/test_kustomizations/helm/initial/charts/test-capabilities/values.yaml

Whitespace-only changes.

0 commit comments

Comments
 (0)