Skip to content

Commit

Permalink
updated README and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Graeme Davidson committed Feb 3, 2021
1 parent cacd171 commit f75375d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 98 deletions.
97 changes: 40 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
33 changes: 14 additions & 19 deletions examples/example.tf
Original file line number Diff line number Diff line change
@@ -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"
})
}
7 changes: 0 additions & 7 deletions examples/manifests/my-configmap.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions examples/manifests/my-configmap.yml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
metadata:
name: my-configmap
data:
app: ${app}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 0 additions & 9 deletions examples/variables.tf

This file was deleted.

12 changes: 12 additions & 0 deletions examples/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
k8s = {
source = "fiveai/k8s"
version = "0.2.1"
}
}
}

provider "k8s" {
# Configuration options
}

0 comments on commit f75375d

Please sign in to comment.