### Example Usage of kubectl_filename_list Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/docs/data-sources/kubectl_filename_list.md Demonstrates how to use the kubectl_filename_list data source to find YAML files and then apply them using the kubectl_manifest resource. ```hcl data "kubectl_filename_list" "manifests" { pattern = "./manifests/*.yaml" } resource "kubectl_manifest" "test" { count = length(data.kubectl_filename_list.manifests.matches) yaml_body = file(element(data.kubectl_filename_list.manifests.matches, count.index)) } ``` -------------------------------- ### Example Template with Looping Directive Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/docs/data-sources/kubectl_path_documents.md Illustrates using a directive with `split` to generate multiple manifests, such as creating a PersistentVolumeClaim for each namespace in a comma-separated list. ```yaml # # Given the following YAML template # %{ for namespace in split(",", namespaces) } --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: myvolume-claim namespace: ${namespace} spec: accessModes: - ReadWriteMany volumeMode: Filesystem resources: requests: storage: 100Gi %{ endfor } ``` ```hcl # # Loading the document is a comma-separated list of namespace # data "kubectl_path_documents" "manifests" { pattern = "./manifests/*.yaml" vars = { namespaces = "dev,test,prod" } } # # Results in 3 documents: # --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: myvolume-claim namespace: dev --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: myvolume-claim namespace: test --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: myvolume-claim namespace: prod ``` -------------------------------- ### Example Template with Variable Substitution Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/docs/data-sources/kubectl_path_documents.md Demonstrates loading a YAML file and parsing a variable like `${docker_image}` using the `vars` attribute for template rendering. ```yaml # # Given the following YAML template # apiVersion: v1 kind: Pod metadata: name: nginx labels: name: nginx spec: containers: - name: nginx image: ${docker_image} ports: - containerPort: 80 ``` ```hcl # # Load the yaml file, parsing the ${docker_image} variable # data "kubectl_path_documents" "manifests" { pattern = "./manifests/*.yaml" vars = { docker_image = "https://myregistry.example.com/nginx" } } ``` -------------------------------- ### Kubectl Manifest Resource Example Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/README.md Define a Kubernetes resource using the kubectl_manifest resource. The yaml_body accepts a YAML string for resource definition. ```hcl resource "kubectl_manifest" "test" { yaml_body = < 0 error_message = "cluster CA ConfigMap is missing from kube-system" } } ``` -------------------------------- ### Read a Cluster-Scoped Object Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/docs/data-sources/kubectl_manifest.md Fetches a cluster-scoped object like a Namespace. The 'namespace' argument is omitted for such resources. ```hcl data "kubectl_manifest" "ns" { api_version = "v1" kind = "Namespace" name = "kube-system" fields = { phase = "status.phase" } } ``` -------------------------------- ### Importing a Kubectl Manifest CRD Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/docs/resources/kubectl_manifest.md Import an existing cert-manager Issuer CRD into Terraform state. Ensure the provider and resource configuration are correctly set up before running the import command. ```bash $ terraform import -provider kubectl module.kubernetes.kubectl_manifest.crd-example certmanager.k8s.io/v1alpha1//Issuer//cluster-selfsigned-issuer-root-ca//my-namespace ``` -------------------------------- ### Read a CRD Object and Extract Nested Array Elements Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/docs/data-sources/kubectl_manifest.md Fetches a Deployment object and extracts specific fields, including an element from a nested array and metadata labels. The labels are JSON-decoded for use. ```hcl data "kubectl_manifest" "dep" { api_version = "apps/v1" kind = "Deployment" name = "nginx" namespace = "web" fields = { replicas = "spec.replicas" first_image = "spec.template.spec.containers.[0].image" labels = "metadata.labels" } } # `labels` is a JSON-encoded object string. `jsondecode()` recovers it. output "labels" { value = jsondecode(data.kubectl_manifest.dep.results["labels"]) } ``` -------------------------------- ### Ignoring Fields During Manifest Changes Source: https://github.com/alekc/terraform-provider-kubectl/blob/master/docs/resources/kubectl_manifest.md Configures a list of YAML keys to ignore changes for, useful for fields managed by external processes. Supports dot-separator syntax for nested fields and indexed access for arrays. ```hcl resource "kubectl_manifest" "test" { yaml_body = <