Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Adding docs folder and acceptance tests (#18)
Browse files Browse the repository at this point in the history
* release binaries in commit script

* rewrite of resource, adding acceptance tests

* add make target for acceptance tests

* make targets

* remove import block, as update resource is not implemented yet

* add documentation and more tests

* update docs

* proper check destroy method

* make for ci
  • Loading branch information
Daniel Roth authored Aug 5, 2020
1 parent cc37ab9 commit ad2500c
Show file tree
Hide file tree
Showing 14 changed files with 536 additions and 241 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.PHONY: build
build:
./before-commit.sh

.PHONY: ci-build
ci-build:
./before-commit.sh ci

.PHONY: ci-pr
Expand All @@ -13,4 +17,10 @@ ci-release: build

.PHONY: clean
clean:
rm -rf bin
rm -rf bin

ci-testacc:
./before-commit.sh ci testacc

testacc:
./before-commit.sh testacc
77 changes: 7 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,19 @@

The Terraform Provider for kind enables [Terraform](https://www.terraform.io) to provision local [Kubernetes](https://kubernetes.io) clusters on base of [Kubernetes IN Docker (kind)](https://github.com/kubernetes-sigs/kind).

## Prerequisites
## Quick Starts
- [Using the provider](./docs/USAGE.md)
- [Provider development](./docs/DEVELOPMENT.md)

- [Terraform](https://www.terraform.io/downloads.html) 0.12+
- [Go](https://golang.org/doc/install) 1.13 or higher

## Development

Perform the following steps to build the providers:

1. Build the provider:
```bash
go build -o terraform-provider-kind
```
2. Move the provider binary into the terraform plugins folder.

>**NOTE**: For details on Terraform plugins see [this](https://www.terraform.io/docs/plugins/basics.html#installing-plugins) document.

## Usage

Perform the following steps to use the provider:

1. Go to the provider [example](https://github.com/kyma-incubator/terraform-provider-kind/tree/master/example) folder:
```bash
cd example
```
2. Edit the `main.tf` file and provide the following kind configuration:

```hcl
provider "kind" {}
# creating a cluster with kind of the name "test-cluster" with kubernetes version hardcoded in kind defaults https://github.com/kubernetes-sigs/kind/blob/master/pkg/apis/config/defaults/image.go#L21
resource "kind" "my-cluster" {
name = "test-cluster"
}
```

To override the node image used, you can specify the `node_image` like so:

```hcl
provider "kind" {}
# creating a cluster with kind of the name "test-cluster" with kubernetes version v1.16.1
resource "kind" "my-cluster" {
name = "test-cluster"
node_image = "kindest/node:v1.16.1"
}
```

To override the default kind config, you can specify the `kind_config` with HEREDOC:
## Example Usage

Copy the following code into a file with the extension `.tf` to create a kind cluster with only default values.
```hcl
provider "kind" {}
# creating a cluster with kind of the name "test-cluster" with kubernetes version v1.18.4 and two nodes
resource "kind" "my-cluster" {
resource "kind_cluster" "default" {
name = "test-cluster"
node_image = "kindest/node:v1.18.4"
kind_config =<<KIONF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
KIONF
}
```

1. Initialize Terraform:
```bash
terraform init
```
2. Plan the provisioning:
```bash
terraform plan
```
3. Deploy the cluster:
```bash
terraform apply
```
Then run `terraform init`, `terraform plan` & `terraform apply` and follow the on screen instructions. For more details on how to influence creation of the kind resource check out the Quick Start section above.
53 changes: 42 additions & 11 deletions before-commit.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash

readonly CI_FLAG=ci
readonly TEST_ACC_FLAG=testacc

RED='\033[0;31m'
GREEN='\033[0;32m'
Expand All @@ -27,21 +28,43 @@ fi
##
# GO BUILD
##
buildEnv=""
if [ "$1" == "$CI_FLAG" ]; then
# build binary statically
buildEnv="env CGO_ENABLED=0"
fi
if [ "$1" == "$CI_FLAG" ] || [ "$2" == "$CI_FLAG" ]; then
# build all binaries
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o bin/terraform-provider-kind-windows-amd64
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (windows)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (windows)${NC}"
fi

${buildEnv} go build -o bin/terraform-provider-kind
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/terraform-provider-kind-linux-amd64
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (linux)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (linux)${NC}"
fi

goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build${NC}"
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o bin/terraform-provider-kind-darwin-amd64
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (mac)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (mac)${NC}"
fi
else
# build just current arch
CGO_ENABLED=0 go build -o bin/terraform-provider-kind
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (dev)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (dev)${NC}"
fi
fi


##
# Verify dependencies
##
Expand All @@ -67,6 +90,14 @@ fi

goFilesToCheck=$(find . -type f -name "*.go" | egrep -v "\/vendor\/|_*/automock/|_*/testdata/|_*export_test.go")

##
# TF ACCEPTANCE TESTS
##
if [ "$1" == "$TEST_ACC_FLAG" ] || [ "$2" == "$TEST_ACC_FLAG" ]; then
# run terraform acceptance tests
TF_ACC=1 go test ./kind -v -count 1 -parallel 20 -timeout 120m
fi

#
# GO FMT
#
Expand Down
25 changes: 25 additions & 0 deletions docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Development Environment Setup

## Requirements

- [Terraform](https://www.terraform.io/downloads.html) 0.12+
- [Go](https://golang.org/doc/install) 1.13 or higher
- Make sure that your Docker Engine has enough memory assigned to run multi-node kind clusters.

## Development

Perform the following steps to build the providers:

1. Build the provider:
```bash
go build -o terraform-provider-kind
```
2. Move the provider binary into the terraform plugins folder.

>**NOTE**: For details on Terraform plugins see [this](https://www.terraform.io/docs/plugins/basics.html#installing-plugins) document.

## Testing

In order to test the provider you can run `go test ./...` for the unit tests as well as `make testacc` for the Acceptance Tests. If you prefer to only run tests and skip linting and formatting when running Acceptance Tests start them by running `TF_ACC=1 go test ./kind -v -count 1 -parallel 20 -timeout 120m`.

*Note:* Acceptance tests create real resources, and will consume significant resources on the machine they run on.
3 changes: 3 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Frequently Asked Questions

Add questions as they arise...
64 changes: 64 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Using The Provider

## Usage

Perform the following steps to use the provider:

1. Go to the provider [example](https://github.com/kyma-incubator/terraform-provider-kind/tree/master/example) folder:
```bash
cd example
```
2. Edit the `main.tf` file and provide the following kind configuration:

```hcl
provider "kind" {}
# creating a cluster with kind of the name "test-cluster" with kubernetes version hardcoded in kind defaults https://github.com/kubernetes-sigs/kind/blob/master/pkg/apis/config/defaults/image.go#L21
resource "kind_cluster" "default" {
name = "test-cluster"
}
```

To override the node image used, you can specify the `node_image` like so:

```hcl
provider "kind" {}
# creating a cluster with kind of the name "test-cluster" with kubernetes version v1.16.1
resource "kind_cluster" "default" {
name = "test-cluster"
node_image = "kindest/node:v1.16.1"
}
```

To override the default kind config, you can specify the `kind_config` with HEREDOC:

```hcl
provider "kind" {}
# creating a cluster with kind of the name "test-cluster" with kubernetes version v1.18.4 and two nodes
resource "kind_cluster" "default" {
name = "test-cluster"
node_image = "kindest/node:v1.18.4"
kind_config =<<KIONF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
KIONF
}
```

1. Initialize Terraform:
```bash
terraform init
```
2. Plan the provisioning:
```bash
terraform plan
```
3. Deploy the cluster:
```bash
terraform apply
```
2 changes: 1 addition & 1 deletion example/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
provider "kind" {}

resource "kind" "my-cluster" {
resource "kind_cluster" "default" {
name = "test-cluster"
node_image = "kindest/node:v1.18.4"
wait_for_ready = true
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ module github.com/kyma-incubator/terraform-provider-kind
go 1.13

require (
github.com/hashicorp/terraform v0.12.28
github.com/aws/aws-sdk-go v1.30.12 // indirect
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 // indirect
github.com/hashicorp/go-plugin v1.3.0 // indirect
github.com/hashicorp/hcl/v2 v2.3.0 // indirect
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.15.0
github.com/imdario/mergo v0.3.9 // indirect
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
k8s.io/client-go v12.0.0+incompatible
k8s.io/utils v0.0.0-20200619165400-6e3d28b6ed19 // indirect
sigs.k8s.io/kind v0.8.1
Expand Down
Loading

0 comments on commit ad2500c

Please sign in to comment.