Skip to content

Commit 02b422d

Browse files
authored
Added manifests to data resources providing a map for for_each usage (#132)
1 parent 1dfa7b0 commit 02b422d

13 files changed

+248
-28
lines changed

.run/Template Go Test.run.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="true" type="GoTestRunConfiguration" factoryName="Go Test">
3+
<module name="terraform-provider-kubectl" />
4+
<working_directory value="$PROJECT_DIR$" />
5+
<envs>
6+
<env name="TF_ACC" value="1" />
7+
<env name="KUBECONFIG" value="$PROJECT_DIR$/scripts/kubeconfig.yaml" />
8+
</envs>
9+
<kind value="DIRECTORY" />
10+
<package value="github.com/gavinbunney/terraform-provider-kubectl" />
11+
<directory value="$PROJECT_DIR$" />
12+
<filePath value="$PROJECT_DIR$" />
13+
<framework value="gotest" />
14+
<method v="2" />
15+
</configuration>
16+
</component>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
kind: MyAwesomeCRD
12
MyYaml: Hello, %{ if name != "" }${name}%{ else }unnamed%{ endif }!
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
apiVersion: "stable.example.com/v1"
3+
kind: CronTab
4+
metadata:
5+
name: name-here-crd
6+
spec:
7+
cronSpec: "* * * * /5"
8+
image: my-awesome-cron-image
9+
---
10+
apiVersion: apiextensions.k8s.io/v1
11+
kind: CustomResourceDefinition
12+
metadata:
13+
name: name-here-crontabs.stable.example.com
14+
spec:
15+
group: stable.example.com
16+
conversion:
17+
strategy: None
18+
scope: Namespaced
19+
names:
20+
plural: name-here-crontabs
21+
singular: crontab
22+
kind: CronTab
23+
shortNames:
24+
- ct
25+
version: v1
26+
versions:
27+
- name: v1
28+
served: true
29+
storage: true
30+
---
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
apiVersion: "stable.example.com/v1"
3+
kind: CronTab
4+
metadata:
5+
name: name-here-crd
6+
spec:
7+
cronSpec: "* * * * /5"
8+
image: my-awesome-cron-image
9+
---
10+
apiVersion: apiextensions.k8s.io/v1
11+
kind: CustomResourceDefinition
12+
metadata:
13+
name: name-here-crontabs.stable.example.com
14+
spec:
15+
group: stable.example.com
16+
conversion:
17+
strategy: None
18+
scope: Namespaced
19+
names:
20+
plural: name-here-crontabs
21+
singular: crontab
22+
kind: CronTab
23+
shortNames:
24+
- ct
25+
version: v1
26+
versions:
27+
- name: v1
28+
served: true
29+
storage: true
30+
---

_examples/manifests/multiple-templated.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
apiVersion: "stable.example.com/v1"
33
kind: CronTab
44
metadata:
5-
name: name-here-crd
5+
name: name-here-crd-templated
66
spec:
77
cronSpec: "* * * * /5"
88
image: my-awesome-cron-image
99
---
1010
apiVersion: apiextensions.k8s.io/v1
1111
kind: CustomResourceDefinition
1212
metadata:
13-
name: name-here-crontabs.stable.example.com
13+
name: name-here-templated-crontabs.stable.example.com
1414
spec:
1515
group: stable.example.com
1616
conversion:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: "stable.example.com/v1"
22
kind: ${the_kind}
33
metadata:
4-
name: name-here-crd
4+
name: name-here-crd-single-templated
55
spec:
66
cronSpec: "* * * * /5"
77
image: my-awesome-cron-image

_examples/manifests/single.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: "stable.example.com/v1"
22
kind: CronTab
33
metadata:
4-
name: name-here-crd
4+
name: name-here-crd-single
55
spec:
66
cronSpec: "* * * * /5"
77
image: my-awesome-cron-image

docs/data-sources/kubectl_file_documents.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,38 @@ This provider provides a `data` resource `kubectl_file_documents` to enable ease
44

55
## Example Usage
66

7+
### Example Usage with for_each
8+
9+
The recommended approach is to use the `manifests` attribute and a `for_each` expression to apply the found manifests.
10+
This ensures that any additional yaml documents or removals do not cause a large amount of terraform changes.
11+
12+
```hcl
13+
data "kubectl_file_documents" "docs" {
14+
content = file("multi-doc-manifest.yaml")
15+
}
16+
17+
resource "kubectl_manifest" "test" {
18+
for_each = data.kubectl_file_documents.docs.manifests
19+
yaml_body = each.value
20+
}
21+
```
22+
23+
### Example Usage via count
24+
25+
Raw documents can also be accessed via the `documents` attribute.
26+
727
```hcl
8-
data "kubectl_file_documents" "manifests" {
28+
data "kubectl_file_documents" "docs" {
929
content = file("multi-doc-manifest.yaml")
1030
}
1131
1232
resource "kubectl_manifest" "test" {
13-
count = length(data.kubectl_file_documents.manifests.documents)
14-
yaml_body = element(data.kubectl_file_documents.manifests.documents, count.index)
33+
count = length(data.kubectl_file_documents.docs.documents)
34+
yaml_body = element(data.kubectl_file_documents.docs.documents, count.index)
1535
}
1636
```
1737

1838
## Attribute Reference
1939

20-
* `documents` - List of YAML documents (string).
40+
* `manifests` - Map of YAML documents with key being the document id, and value being the document yaml. Best used with `for_each` expressions.
41+
* `documents` - List of raw YAML documents (string). Best used with `count` expressions.

docs/data-sources/kubectl_path_documents.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,34 @@ This gives you the flexibility of parameterizing your manifests, and loading & t
88

99
## Example Usage
1010

11-
### Load all manifest documents
11+
### Load all manifest documents via for_each (recommended)
12+
13+
The recommended approach is to use the `manifests` attribute and a `for_each` expression to apply the found manifests.
14+
This ensures that any additional yaml documents or removals do not cause a large amount of terraform changes.
1215

1316
```hcl
14-
data "kubectl_path_documents" "manifests" {
17+
data "kubectl_path_documents" "docs" {
18+
pattern = "./manifests/*.yaml"
19+
}
20+
21+
resource "kubectl_manifest" "test" {
22+
for_each = data.kubectl_file_documents.docs.manifests
23+
yaml_body = each.value
24+
}
25+
```
26+
27+
### Load all manifest documents via count
28+
29+
Raw documents can also be accessed via the `documents` attribute.
30+
31+
```hcl
32+
data "kubectl_path_documents" "docs" {
1533
pattern = "./manifests/*.yaml"
1634
}
1735
1836
resource "kubectl_manifest" "test" {
19-
count = length(data.kubectl_path_documents.manifests.documents)
20-
yaml_body = element(data.kubectl_path_documents.manifests.documents, count.index)
37+
count = length(data.kubectl_path_documents.docs.documents)
38+
yaml_body = element(data.kubectl_path_documents.docs.documents, count.index)
2139
}
2240
```
2341

@@ -152,4 +170,5 @@ metadata:
152170

153171
## Attribute Reference
154172

155-
* `documents` - List of YAML documents (list[string]).
173+
* `manifests` - Map of YAML documents with key being the document id, and value being the document yaml. Best used with `for_each` expressions.
174+
* `documents` - List of YAML documents (list[string]). Best used with `count` expressions.

kubernetes/data_source_kubectl_file_documents.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ func dataSourceKubectlFileDocuments() *schema.Resource {
2222
Elem: &schema.Schema{Type: schema.TypeString},
2323
Computed: true,
2424
},
25+
"manifests": &schema.Schema{
26+
Type: schema.TypeMap,
27+
Elem: &schema.Schema{
28+
Type: schema.TypeString,
29+
},
30+
Computed: true,
31+
},
2532
},
2633
}
2734
}
@@ -33,7 +40,27 @@ func dataSourceKubectlFileDocumentsRead(ctx context.Context, d *schema.ResourceD
3340
return diag.FromErr(err)
3441
}
3542

43+
manifests := make(map[string]string, 0)
44+
for _, doc := range documents {
45+
manifest, err := yaml.ParseYAML(doc)
46+
if err != nil {
47+
return diag.FromErr(fmt.Errorf("failed to parse yaml as a kubernetes yaml manifest: %v", err))
48+
}
49+
50+
parsed, err := manifest.AsYAML()
51+
if err != nil {
52+
return diag.FromErr(fmt.Errorf("failed to parse convert manifest to yaml: %v", err))
53+
}
54+
55+
if _, exists := manifests[manifest.GetSelfLink()]; exists {
56+
return diag.FromErr(fmt.Errorf("duplicate manifest found with id: %v", manifest.GetSelfLink()))
57+
}
58+
59+
manifests[manifest.GetSelfLink()] = parsed
60+
}
61+
3662
d.SetId(fmt.Sprintf("%x", sha256.Sum256([]byte(content))))
37-
d.Set("documents", documents)
63+
_ = d.Set("documents", documents)
64+
_ = d.Set("manifests", manifests)
3865
return nil
3966
}

0 commit comments

Comments
 (0)