Skip to content

Commit 2341cf8

Browse files
committed
WIP: Add kustomization_overlay data source fix #18
1 parent 685d849 commit 2341cf8

8 files changed

+170
-2
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ require (
1111
k8s.io/apimachinery v0.20.2
1212
k8s.io/client-go v0.20.2
1313
sigs.k8s.io/kustomize/api v0.7.3
14+
sigs.k8s.io/kustomize/kyaml v0.10.8
1415
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
174174
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
175175
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
176176
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
177+
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
177178
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
178179
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
179180
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
@@ -1089,6 +1090,8 @@ sigs.k8s.io/kustomize/api v0.7.3 h1:5ZlS24VT7Tm1BXNeqkdC/UsX+ahdMVVojt5aP+5S3ME=
10891090
sigs.k8s.io/kustomize/api v0.7.3/go.mod h1:4RLLidX0T3BWrC6gsRqz6uCJ6OXFecXfh/3QDHMauOg=
10901091
sigs.k8s.io/kustomize/kyaml v0.10.7 h1:r0r8UEL0bL7X56HKUmhJZ+TP+nvRNGrDHHSLO7izlcQ=
10911092
sigs.k8s.io/kustomize/kyaml v0.10.7/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
1093+
sigs.k8s.io/kustomize/kyaml v0.10.8 h1:z5BAm9ZCfaXGnZOLIF7p9cYZMUoftUmYhDziESumVoI=
1094+
sigs.k8s.io/kustomize/kyaml v0.10.8/go.mod h1:K9yg1k/HB/6xNOf5VH3LhTo1DK9/5ykSZO5uIv+Y/1k=
10921095
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e h1:4Z09Hglb792X0kfOBBJUPFEyvVfQWrYT/l8h5EKA6JQ=
10931096
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
10941097
sigs.k8s.io/structured-merge-diff/v4 v4.0.2 h1:YHQV7Dajm86OuqnIR6zAelnDWBRjo+YhYV9PmGrh1s8=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package kustomize
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"io/ioutil"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
11+
"sigs.k8s.io/kustomize/api/filesys"
12+
"sigs.k8s.io/kustomize/api/krusty"
13+
"sigs.k8s.io/kustomize/api/resmap"
14+
"sigs.k8s.io/kustomize/api/types"
15+
"sigs.k8s.io/kustomize/kyaml/yaml"
16+
)
17+
18+
func dataSourceKustomizationOverlay() *schema.Resource {
19+
return &schema.Resource{
20+
Read: kustomizationOverlay,
21+
22+
Schema: map[string]*schema.Schema{
23+
"namespace": &schema.Schema{
24+
Type: schema.TypeString,
25+
Optional: true,
26+
},
27+
"resources": &schema.Schema{
28+
Type: schema.TypeList,
29+
Required: true,
30+
Elem: &schema.Schema{
31+
Type: schema.TypeString,
32+
},
33+
},
34+
"ids": &schema.Schema{
35+
Type: schema.TypeSet,
36+
Computed: true,
37+
Elem: &schema.Schema{Type: schema.TypeString},
38+
Set: idSetHash,
39+
},
40+
"manifests": &schema.Schema{
41+
Type: schema.TypeMap,
42+
Computed: true,
43+
Elem: &schema.Schema{Type: schema.TypeString},
44+
},
45+
},
46+
}
47+
}
48+
49+
func buildKustomizeOverlay(kustomization types.Kustomization) (rm resmap.ResMap, err error) {
50+
fSys := filesys.MakeFsOnDisk()
51+
opts := krusty.MakeDefaultOptions()
52+
53+
var b bytes.Buffer
54+
ye := yaml.NewEncoder(io.Writer(&b))
55+
ye.Encode(kustomization)
56+
ye.Close()
57+
data, _ := ioutil.ReadAll(io.Reader(&b))
58+
59+
fSys.WriteFile("Kustomization", data)
60+
defer fSys.RemoveAll("Kustomization")
61+
62+
k := krusty.MakeKustomizer(fSys, opts)
63+
64+
rm, err = k.Run(".")
65+
if err != nil {
66+
return nil, fmt.Errorf("Kustomizer Run failed: %s", err)
67+
}
68+
69+
return rm, nil
70+
}
71+
72+
func kustomizationOverlay(d *schema.ResourceData, m interface{}) error {
73+
74+
var res []string
75+
for _, v := range d.Get("resources").([]interface{}) {
76+
res = append(res, v.(string))
77+
}
78+
79+
kustomization := types.Kustomization{
80+
TypeMeta: types.TypeMeta{
81+
APIVersion: "kustomize.config.k8s.io/v1beta1",
82+
Kind: "Kustomization",
83+
},
84+
Namespace: d.Get("namespace").(string),
85+
Resources: res,
86+
}
87+
88+
rm, err := buildKustomizeOverlay(kustomization)
89+
if err != nil {
90+
return fmt.Errorf("buildKustomizeOverlay: %s", err)
91+
}
92+
93+
d.Set("ids", flattenKustomizationIDs(rm))
94+
95+
resources, err := flattenKustomizationResources(rm)
96+
if err != nil {
97+
return fmt.Errorf("buildKustomizeOverlay: %s", err)
98+
}
99+
d.Set("manifests", resources)
100+
101+
id, err := getIDFromResources(rm)
102+
if err != nil {
103+
return fmt.Errorf("buildKustomizeOverlay: %s", err)
104+
}
105+
d.SetId(id)
106+
107+
return nil
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package kustomize
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccDataSourceKustomizationOverlay_basic(t *testing.T) {
10+
11+
resource.Test(t, resource.TestCase{
12+
//PreCheck: func() { testAccPreCheck(t) },
13+
Providers: testAccProviders,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccDataSourceKustomizationOverlayConfig_basic(),
17+
Check: resource.ComposeAggregateTestCheckFunc(
18+
resource.TestCheckResourceAttrSet("data.kustomization_overlay.test", "id"),
19+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "namespace", "test-overlay"),
20+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "resources.#", "1"),
21+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "resources.0", "../test_kustomizations/basic/initial"),
22+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "ids.#", "4"),
23+
resource.TestCheckResourceAttr("data.kustomization_overlay.test", "manifests.%", "4"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccDataSourceKustomizationOverlayConfig_basic() string {
31+
return `
32+
data "kustomization_overlay" "test" {
33+
namespace = "test-overlay"
34+
35+
resources = [
36+
"../test_kustomizations/basic/initial",
37+
]
38+
}
39+
`
40+
}

kustomize/provider.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ func Provider() *schema.Provider {
3737
DataSourcesMap: map[string]*schema.Resource{
3838
// legacy name of the data source
3939
"kustomization": dataSourceKustomization(),
40-
4140
// new name for the data source
4241
"kustomization_build": dataSourceKustomization(),
42+
43+
// define overlay from TF
44+
"kustomization_overlay": dataSourceKustomizationOverlay(),
4345
},
4446

4547
Schema: map[string]*schema.Schema{

test.tf

+15-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@ data "kustomization_build" "test" {
1414
path = "test_kustomizations/basic/initial"
1515
}
1616

17-
resource "kustomization_resource" "test" {
17+
resource "kustomization_resource" "from_build" {
1818
for_each = data.kustomization_build.test.ids
1919

2020
manifest = data.kustomization_build.test.manifests[each.value]
2121
}
22+
23+
data "kustomization_overlay" "test" {
24+
namespace = "test-overlay"
25+
26+
resources = [
27+
"test_kustomizations/basic/initial"
28+
]
29+
}
30+
31+
resource "kustomization_resource" "from_overlay" {
32+
for_each = data.kustomization_overlay.test.ids
33+
34+
manifest = data.kustomization_overlay.test.manifests[each.value]
35+
}

0 commit comments

Comments
 (0)