Skip to content

Commit f947c93

Browse files
authored
Merge pull request #152 from BlueOwlDev/bradon/add-helm-support
Allow enabling helm templating in kustomize base
2 parents 0732a0b + d57b42b commit f947c93

14 files changed

+216
-0
lines changed

.github/workflows/main.yml

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ jobs:
3333
with:
3434
go-version: "${{ env.GO_VERSION }}"
3535

36+
- name: Set up Helm
37+
uses: azure/setup-helm@v1
38+
with:
39+
version: '3.7.1'
40+
3641
- name: 'Run TF acceptance tests (${{ matrix.runner }})'
3742
if: startsWith(matrix.runner, 'ubuntu-')
3843
env:

kustomize/data_source_kustomization.go

+10
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,15 @@ func getKustomizeOptions(d *schema.ResourceData) (opts *krusty.Options) {
128128
}
129129
}
130130

131+
if kOpts["enable_helm"] != nil {
132+
if kOpts["enable_helm"].(string) == "true" {
133+
opts.PluginConfig = types.EnabledPluginConfig(types.BploUseStaticallyLinked)
134+
135+
if kOpts["helm_path"] != nil {
136+
opts.PluginConfig.HelmConfig.Command = kOpts["helm_path"].(string)
137+
}
138+
}
139+
}
140+
131141
return opts
132142
}

kustomize/data_source_kustomization_build.go

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func dataSourceKustomization() *schema.Resource {
2626
Type: schema.TypeString,
2727
Optional: true,
2828
},
29+
"enable_helm": {
30+
Type: schema.TypeBool,
31+
Optional: true,
32+
},
33+
"helm_path": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
},
2937
},
3038
},
3139
},

kustomize/data_source_kustomization_build_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,52 @@ data "kustomization" "test" {
6969
}
7070
`, path)
7171
}
72+
73+
func TestAccDataSourceKustomization_helmChart(t *testing.T) {
74+
75+
resource.Test(t, resource.TestCase{
76+
//PreCheck: func() { testAccPreCheck(t) },
77+
Providers: testAccProviders,
78+
Steps: []resource.TestStep{
79+
{
80+
Config: testAccDataSourceKustomizationConfig_helm("test_kustomizations/helm/initial", false),
81+
Check: resource.ComposeAggregateTestCheckFunc(
82+
resource.TestCheckResourceAttrSet("data.kustomization_build.test", "id"),
83+
resource.TestCheckResourceAttrSet("data.kustomization_build.test", "path"),
84+
resource.TestCheckResourceAttr("data.kustomization_build.test", "path", "test_kustomizations/helm/initial"),
85+
resource.TestCheckResourceAttr("data.kustomization_build.test", "ids.#", "4"),
86+
resource.TestCheckResourceAttr("data.kustomization_build.test", "ids_prio.#", "3"),
87+
resource.TestCheckResourceAttr("data.kustomization_build.test", "manifests.%", "4"),
88+
resource.TestCheckOutput("service", "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"creationTimestamp\":null,\"labels\":{\"app\":\"nginx\"},\"name\":\"nginx\",\"namespace\":\"test-basic\"},\"spec\":{\"ports\":[{\"name\":\"http\",\"port\":80,\"protocol\":\"TCP\",\"targetPort\":80}],\"selector\":{\"app\":\"nginx\"},\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}"),
89+
resource.TestCheckOutput("deployment", "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"creationTimestamp\":null,\"labels\":{\"app\":\"nginx\"},\"name\":\"nginx\",\"namespace\":\"test-basic\"},\"spec\":{\"replicas\":1,\"selector\":{\"matchLabels\":{\"app\":\"nginx\"}},\"strategy\":{},\"template\":{\"metadata\":{\"creationTimestamp\":null,\"labels\":{\"app\":\"nginx\"}},\"spec\":{\"containers\":[{\"image\":\"nginx:6.0.10\",\"name\":\"test-basic\",\"resources\":{}}]}}},\"status\":{}}"),
90+
),
91+
},
92+
},
93+
})
94+
}
95+
96+
func testAccDataSourceKustomizationConfig_helm(path string, legacy bool) string {
97+
return fmt.Sprintf(`
98+
99+
provider "kustomization" {
100+
legacy_id_format = %v
101+
}
102+
103+
data "kustomization_build" "test" {
104+
path = "%s"
105+
106+
kustomize_options = {
107+
enable_helm = true
108+
helm_path = "helm"
109+
}
110+
}
111+
112+
output "service" {
113+
value = data.kustomization_build.test.manifests["_/Service/test-basic/nginx"]
114+
}
115+
116+
output "deployment" {
117+
value = data.kustomization_build.test.manifests["apps/Deployment/test-basic/nginx"]
118+
}
119+
`, legacy, path)
120+
}

kustomize/data_source_kustomization_overlay.go

+8
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ func dataSourceKustomizationOverlay() *schema.Resource {
424424
Type: schema.TypeString,
425425
Optional: true,
426426
},
427+
"enable_helm": {
428+
Type: schema.TypeBool,
429+
Optional: true,
430+
},
431+
"helm_path": {
432+
Type: schema.TypeString,
433+
Optional: true,
434+
},
427435
},
428436
},
429437
},

kustomize/data_source_kustomization_overlay_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,43 @@ output "check_ns" {
999999
}
10001000
`
10011001
}
1002+
1003+
// Test helm with common_annotations attr
1004+
func TestDataSourceKustomizationOverlay_commonAnnotations_helm(t *testing.T) {
1005+
1006+
resource.Test(t, resource.TestCase{
1007+
IsUnitTest: true,
1008+
Providers: testAccProviders,
1009+
Steps: []resource.TestStep{
1010+
{
1011+
Config: testDataSourceKustomizationOverlayConfig_commonAnnotations_helm(),
1012+
Check: resource.ComposeAggregateTestCheckFunc(
1013+
resource.TestCheckOutput("check", "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{\"test-annotation\":\"true\"},\"creationTimestamp\":null,\"labels\":{\"app\":\"nginx\"},\"name\":\"nginx\",\"namespace\":\"test-basic\"},\"spec\":{\"ports\":[{\"name\":\"http\",\"port\":80,\"protocol\":\"TCP\",\"targetPort\":80}],\"selector\":{\"app\":\"nginx\"},\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}"),
1014+
),
1015+
},
1016+
},
1017+
})
1018+
}
1019+
1020+
func testDataSourceKustomizationOverlayConfig_commonAnnotations_helm() string {
1021+
return `
1022+
data "kustomization_overlay" "test" {
1023+
common_annotations = {
1024+
test-annotation: true
1025+
}
1026+
1027+
resources = [
1028+
"test_kustomizations/helm/initial",
1029+
]
1030+
1031+
kustomize_options = {
1032+
enable_helm = true
1033+
helm_path = "helm"
1034+
}
1035+
}
1036+
1037+
output "check" {
1038+
value = data.kustomization_overlay.test.manifests["_/Service/test-basic/nginx"]
1039+
}
1040+
`
1041+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v2
2+
description: A Simple Helm chart
3+
name: test-basic
4+
version: 0.0.1
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-basic.name" -}}
5+
{{- default "nginx" }}
6+
{{- end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
creationTimestamp: null
5+
labels:
6+
app: {{ include "test-basic.name" . }}
7+
name: {{ include "test-basic.name" . }}
8+
spec:
9+
replicas: {{ .Values.replicaCount }}
10+
selector:
11+
matchLabels:
12+
app: {{ include "test-basic.name" . }}
13+
strategy: {}
14+
template:
15+
metadata:
16+
creationTimestamp: null
17+
labels:
18+
app: {{ include "test-basic.name" . }}
19+
spec:
20+
containers:
21+
- image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
22+
name : {{ .Chart.Name }}
23+
resources: {}
24+
status: {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: networking.k8s.io/v1beta1
2+
kind: Ingress
3+
metadata:
4+
name: {{ include "test-basic.name" . }}
5+
annotations:
6+
nginx.ingress.kubernetes.io/rewrite-target: /
7+
spec:
8+
rules:
9+
- http:
10+
paths:
11+
- path: /testpath
12+
backend:
13+
serviceName: {{ include "test-basic.name" . }}
14+
servicePort: {{ .Values.nginx.port }}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
creationTimestamp: null
5+
labels:
6+
app: {{ include "test-basic.name" . }}
7+
name: {{ include "test-basic.name" . }}
8+
spec:
9+
ports:
10+
- name: http
11+
port: {{ .Values.nginx.port }}
12+
protocol: TCP
13+
targetPort: {{ .Values.nginx.port }}
14+
selector:
15+
app: {{ include "test-basic.name" . }}
16+
type: ClusterIP
17+
status:
18+
loadBalancer: {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Default values for test-basic
2+
# This is a YAML-formatted file.
3+
# Declare variables to be passed into your templates.
4+
5+
replicaCount: 1
6+
7+
image:
8+
repository: nginx
9+
# Overrides the image tag whose default is the chart appVersion.
10+
tag: "6.0.10"
11+
12+
nginx:
13+
port: 80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
namespace: test-basic
5+
6+
resources:
7+
- namespace.yaml
8+
9+
helmCharts:
10+
- name: test-basic
11+
version: 0.0.1
12+
namespace: not-used
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: test-basic

0 commit comments

Comments
 (0)