diff --git a/README.md b/README.md index 28aaf46f..434b172b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ The k8s Terraform provider enables Terraform to deploy Kubernetes resources. Unl [official Kubernetes provider][kubernetes-provider] it handles raw manifests, leveraging `kubectl` directly to allow developers to work with any Kubernetes resource natively. -This provider is published in the [Terraform Registry Provider](https://registry.terraform.io/providers/fiveai/k8s) +This provider is published in the [Terraform Registry](https://registry.terraform.io/providers/fiveai/k8s) ## ToC @@ -14,20 +14,28 @@ This provider is published in the [Terraform Registry Provider](https://registry ## Usage -Use `go get` to install the provider: +### Provider and Version control -``` -go get -u github.com/ericchiang/terraform-provider-k8s -``` - -Register the plugin in `~/.terraformrc`: +This provider is automatically pushed to the TF Registry. You can pull it into your project uses the standard provider +and versions notation. ```hcl -providers { - k8s = "/$GOPATH/bin/terraform-provider-k8s" +terraform { + required_providers { + kubernetes = { + source = "fiveai/k8s" + version = "0.2.1" + } + } +} + +provider "kubernetes" { + # Configuration options } ``` +#### Provider Configuration options + The provider takes the following optional configuration parameters: * If you have a kubeconfig available on the file system you can configure the provider as: @@ -47,49 +55,48 @@ provider "k8s" { ``` **WARNING:** Configuration from the variable will be recorded into a temporary file and the file will be removed as -soon as call is completed. This may impact performance if the code runs on a shared system because and the global +soon as call is completed. This may impact performance if the code runs on a shared system because the global tempdir is used. -The k8s Terraform provider introduces a single Terraform resource, a `k8s_manifest`. The resource contains a `content` -field, which contains a raw manifest. +### Resource Definition -```hcl -variable "replicas" { - type = "string" - default = 3 -} +An example of the k8s_manifest might look like. -data "template_file" "nginx-deployment" { - template = "${file("manifests/nginx-deployment.yaml")}" +The terraform resource definition should include: - vars { - replicas = "${var.replicas}" - } -} +* `name` of the resource +* `namespace` to be deployed to (namespaced resources only, not cluster level) +* `content` resource to be deployed +* `kind` of resource being deployed, e.g. `Deployment`, `ConfigMap` -resource "k8s_manifest" "nginx-deployment" { - content = "${data.template_file.nginx-deployment.rendered}" +```hcl +resource "k8s_manifest" "dummy-deployment" { + name = "dummyvalue" + namespace = "default" + kind = "Deployment" + + content = templatefile("${path.module}/manifest/dummy-deployment.yml.tpl", { + app = "dummyvalue" + }) } ``` -In this case `manifests/nginx-deployment.yaml` is a templated deployment manifest. +The templated resource definition should then resemble the following example. ```yaml -apiVersion: apps/v1beta2 -kind: Deployment +apiVersion: apps/v1 metadata: - name: nginx-deployment labels: - app: nginx + app: ${app} spec: - replicas: ${replicas} + replicas: 1 selector: matchLabels: - app: nginx + app: ${app} template: metadata: labels: - app: nginx + app: ${app} spec: containers: - name: nginx @@ -98,30 +105,6 @@ spec: - containerPort: 80 ``` -The Kubernetes resources can then be managed through Terraform. - -```terminal -$ terraform apply -# ... -Apply complete! Resources: 1 added, 1 changed, 0 destroyed. -$ kubectl get deployments -NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE -nginx-deployment 3 3 3 3 1m -$ terraform apply -var 'replicas=5' -# ... -Apply complete! Resources: 0 added, 1 changed, 0 destroyed. -$ kubectl get deployments -NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE -nginx-deployment 5 5 5 3 3m -$ terraform destroy -force -# ... -Destroy complete! Resources: 2 destroyed. -$ kubectl get deployments -No resources found. -``` - -[kubernetes-provider]: https://www.terraform.io/docs/providers/kubernetes/index.html - ## Build To build please use [goreleaser](https://goreleaser.com/intro/). You can edit the automated Github Actions by the diff --git a/examples/example.tf b/examples/example.tf index 482caee3..4ce126a1 100644 --- a/examples/example.tf +++ b/examples/example.tf @@ -1,23 +1,18 @@ -data "template_file" "my-configmap" { - template = "${file("manifests/my-configmap.yaml")}" - - vars { - greeting = "${var.greeting}" - } -} - resource "k8s_manifest" "my-configmap" { - content = "${data.template_file.my-configmap.rendered}" -} - -data "template_file" "nginx-deployment" { - template = "${file("manifests/nginx-deployment.yaml")}" - - vars { - replicas = "${var.replicas}" - } + name = "my-configmap" + namespace = "default" + kind = "ConfigMap" + content = templatefile("${path.module}/manifests/my-configmap.yml.tpl", { + app = "dummyvalue" + }) } -resource "k8s_manifest" "nginx-deployment" { - content = "${data.template_file.nginx-deployment.rendered}" +resource "k8s_manifest" "ngix-deployment" { + name = "nginx-deployment-test" + namespace = "default" + kind = "Deployment" + content = templatefile("${path.module}/manifests/nginx-deployment.yml.tpl", { + replicas = 1 + app = "dummyvalue" + }) } diff --git a/examples/manifests/my-configmap.yaml b/examples/manifests/my-configmap.yaml deleted file mode 100644 index f25a145d..00000000 --- a/examples/manifests/my-configmap.yaml +++ /dev/null @@ -1,7 +0,0 @@ -kind: ConfigMap -apiVersion: v1 -metadata: - name: my-configmap - namespace: default -data: - greeting: ${greeting} diff --git a/examples/manifests/my-configmap.yml.tpl b/examples/manifests/my-configmap.yml.tpl new file mode 100644 index 00000000..d7a2ab47 --- /dev/null +++ b/examples/manifests/my-configmap.yml.tpl @@ -0,0 +1,5 @@ +apiVersion: v1 +metadata: + name: my-configmap +data: + app: ${app} diff --git a/examples/manifests/nginx-deployment.yaml b/examples/manifests/nginx-deployment.yml.tpl similarity index 66% rename from examples/manifests/nginx-deployment.yaml rename to examples/manifests/nginx-deployment.yml.tpl index 750b9f66..a75714b0 100644 --- a/examples/manifests/nginx-deployment.yaml +++ b/examples/manifests/nginx-deployment.yml.tpl @@ -1,18 +1,16 @@ -apiVersion: apps/v1beta2 -kind: Deployment +apiVersion: apps/v1 metadata: - name: nginx-deployment labels: - app: nginx + app: ${app} spec: replicas: ${replicas} selector: matchLabels: - app: nginx + app: ${app} template: metadata: labels: - app: nginx + app: ${app} spec: containers: - name: nginx diff --git a/examples/variables.tf b/examples/variables.tf deleted file mode 100644 index 22e0b6fd..00000000 --- a/examples/variables.tf +++ /dev/null @@ -1,9 +0,0 @@ -variable "greeting" { - type = "string" - default = "Hello, world!" -} - -variable "replicas" { - type = "string" - default = 3 -} diff --git a/examples/versions.tf b/examples/versions.tf new file mode 100644 index 00000000..b506f81d --- /dev/null +++ b/examples/versions.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + k8s = { + source = "fiveai/k8s" + version = "0.2.1" + } + } +} + +provider "k8s" { + # Configuration options +}