-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new resource 'kubectl_kustomize_documents' (#113)
This will allow the use of https://kustomize.io/ to generate a series of k8s YAML docs, then apply them to a cluster in much the same way as 'kubectl_kustomize_documents' Co-authored-by: Gavin Bunney <[email protected]>
- Loading branch information
1 parent
cb35f13
commit 0ad0c82
Showing
11 changed files
with
292 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Data Source: kubectl_kustomize_documents | ||
|
||
This provider provides a `data` resource `kubectl_kustomize_documents` to | ||
render a kustomize target to a series of yaml documents. See https://kustomize.io/ | ||
for more info. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "kubectl_kustomize_documents" "manifests" { | ||
target = "https://github.com/kubernetes-sigs/kustomize/examples/multibases?ref=v1.0.6" | ||
} | ||
resource "kubectl_manifest" "test" { | ||
count = length(data.kubectl_file_documents.manifests.documents) | ||
yaml_body = element(data.kubectl_file_documents.manifests.documents, count.index) | ||
} | ||
``` | ||
|
||
## Attribute Reference | ||
|
||
* `documents` - List of YAML documents (string). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"sigs.k8s.io/kustomize/api/krusty" | ||
"sigs.k8s.io/kustomize/api/resmap" | ||
"sigs.k8s.io/kustomize/api/types" | ||
"sigs.k8s.io/kustomize/api/filesys" | ||
) | ||
|
||
func dataSourceKubectlKustomizeDocuments() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceKubectlKustomizeDocumentsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"target": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"load_restrictor": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "rootOnly", | ||
}, | ||
"add_managed_by_label": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"documents": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceKubectlKustomizeDocumentsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
target := d.Get("target").(string) | ||
|
||
opts, err := makeKustOpts(d) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
k := krusty.MakeKustomizer(opts) | ||
memFS := filesys.MakeFsOnDisk() | ||
|
||
rm, err := k.Run(memFS, target) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error rendering kustomization: %w", err)) | ||
} | ||
|
||
documents, err := readFromResMap(rm) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error reading documents: %w", err)) | ||
} | ||
|
||
d.SetId(target) | ||
d.Set("documents", documents) | ||
return nil | ||
} | ||
|
||
func makeKustOpts(d *schema.ResourceData) (*krusty.Options, error) { | ||
opts := &krusty.Options{} | ||
|
||
rName := d.Get("load_restrictor").(string) | ||
switch rName { | ||
case "none": | ||
opts.LoadRestrictions = types.LoadRestrictionsNone | ||
case "rootOnly": | ||
opts.LoadRestrictions = types.LoadRestrictionsRootOnly | ||
default: | ||
return nil, fmt.Errorf("invalid restrictor '%s'", rName) | ||
} | ||
|
||
opts.AddManagedbyLabel = d.Get("add_managed_by_label").(bool) | ||
|
||
return opts, nil | ||
} | ||
|
||
func readFromResMap(rm resmap.ResMap) ([]string, error) { | ||
docs := make([]string, 0) | ||
|
||
for _, res := range rm.Resources() { | ||
b, err := res.AsYAML() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
docs = append(docs, string(b)) | ||
} | ||
|
||
return docs, nil | ||
} |
43 changes: 43 additions & 0 deletions
43
kubernetes/data_source_kubectl_kustomize_documents_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"testing" | ||
) | ||
|
||
var kustTargetUrl = "https://github.com/kubernetes-sigs/kustomize/examples/multibases?ref=v1.0.6" | ||
|
||
func TestAccKubectlDataSourceKustomizeDocuments_url(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: nil, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: kubectlKustomizeDocumentsConfig(kustTargetUrl), | ||
Check: resource.TestCheckResourceAttr("data.kubectl_kustomize_documents.test", "documents.#", "3"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccKubectlDataSourceKustomizeDocuments_localDir(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: nil, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: kubectlKustomizeDocumentsConfig("../test/data/kustomize/helloWorld"), | ||
Check: resource.TestCheckResourceAttr("data.kubectl_kustomize_documents.test", "documents.#", "3"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func kubectlKustomizeDocumentsConfig(target string) string { | ||
return fmt.Sprintf(` | ||
data "kubectl_kustomize_documents" "test" { | ||
target = "%s" | ||
} | ||
`, target) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Example sourced from https://github.com/kubernetes-sigs/kustomize/tree/v3.3.1/examples/helloWorld |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: the-map | ||
data: | ||
altGreeting: "Good Morning!" | ||
enableRisky: "false" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: the-deployment | ||
spec: | ||
replicas: 3 | ||
template: | ||
metadata: | ||
labels: | ||
deployment: hello | ||
spec: | ||
containers: | ||
- name: the-container | ||
image: monopole/hello:1 | ||
command: ["/hello", | ||
"--port=8080", | ||
"--enableRiskyFeature=$(ENABLE_RISKY)"] | ||
ports: | ||
- containerPort: 8080 | ||
env: | ||
- name: ALT_GREETING | ||
valueFrom: | ||
configMapKeyRef: | ||
name: the-map | ||
key: altGreeting | ||
- name: ENABLE_RISKY | ||
valueFrom: | ||
configMapKeyRef: | ||
name: the-map | ||
key: enableRisky |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Example configuration for the webserver | ||
# at https://github.com/monopole/hello | ||
commonLabels: | ||
app: hello | ||
|
||
resources: | ||
- deployment.yaml | ||
- service.yaml | ||
- configMap.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
kind: Service | ||
apiVersion: v1 | ||
metadata: | ||
name: the-service | ||
spec: | ||
selector: | ||
deployment: hello | ||
type: LoadBalancer | ||
ports: | ||
- protocol: TCP | ||
port: 8666 | ||
targetPort: 8080 |