diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..085e2277 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,22 @@ +# EditorConfig is awesome: http://EditorConfig.org +# Uses editorconfig to maintain consistent coding styles + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 2 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +end_of_line = lf + +[*.md] +trim_trailing_whitespace = false +indent_size = 4 +max_line_length=120 + +[*.{tf,tfvars}] +indent_size = 2 +indent_style = space diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..0538f676 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,37 @@ +# https://github.com/hashicorp/terraform-provider-scaffolding/blob/main/.github/workflows/release.yml +name: release +on: + push: + tags: + - 'v*' +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2 + - + name: Unshallow + run: git fetch --prune --unshallow + - + name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.15 + - + name: Import GPG key + id: import_gpg + uses: crazy-max/ghaction-import-gpg@v3 + with: + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} + passphrase: ${{ secrets.PASSPHRASE }} + - + name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + version: latest + args: release --parallelism 2 --rm-dist + env: + GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 22d0d82f..691cb6b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vendor +dist diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 00000000..e7f76e33 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,51 @@ +# https://github.com/hashicorp/terraform-provider-scaffolding/blob/main/.goreleaser.yml +# Visit https://goreleaser.com for documentation on how to customize this +# behavior. +before: + hooks: + # this is just an example and not a requirement for provider building/publishing + - go mod tidy +builds: +- env: + # goreleaser does not work with CGO, it could also complicate + # usage by users in CI/CD systems like Terraform Cloud where + # they are unable to install libraries. + - CGO_ENABLED=0 + mod_timestamp: '{{ .CommitTimestamp }}' + flags: + - -trimpath + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + goos: + - freebsd + - linux + - darwin + goarch: + - amd64 + - '386' + - arm + - arm64 + ignore: + - goos: darwin + goarch: '386' + binary: '{{ .ProjectName }}_v{{ .Version }}' +archives: +- format: zip + name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' +checksum: + name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS' + algorithm: sha256 +signs: + - artifacts: checksum + args: + # if you are using this in a GitHub action or some other automated pipeline, you + # need to pass the batch flag to indicate its not interactive. + - "--batch" + - "--local-user" + - "{{ .Env.GPG_FINGERPRINT }}" # set this environment variable for your signing key + - "--output" + - "${signature}" + - "--detach-sign" + - "${artifact}" +changelog: + skip: true diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index effe0d0a..00000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: go -go: -- '1.12' - -script: -- make dist - -deploy: - provider: releases - api_key: $GITHUB_TOKEN - file_glob: - file: terraform-provider-k8s_$TRAVIS_TAG* - skip_cleanup: true - on: - tags: true diff --git a/Makefile b/Makefile deleted file mode 100644 index 82d77120..00000000 --- a/Makefile +++ /dev/null @@ -1,84 +0,0 @@ -GOVARS := CGO_ENABLED=0 -VERSION := $(shell git describe --tags --always --dirty="-dev") - -release: clean github-release dist - github-release release \ - --user fiveai \ - --repo terraform-provider-k8s \ - --tag $(VERSION) \ - --name $(VERSION) - --security-token $$GITHUB_TOKEN - - #======================================================================== - # GNU/Linux - #======================================================================== - # X86 - #------------------------------------------------------------------------ - github-release upload \ - --user fiveai \ - --repo terraform-provider-k8s \ - --tag $(VERSION) \ - --name terraform-provider-k8s_$(VERSION)-linux-amd64 \ - --file terraform-provider-k8s_$(VERSION)-linux-amd64 \ - --security-token $$GITHUB_TOKEN - - #------------------------------------------------------------------------ - # arm - #------------------------------------------------------------------------ - github-release upload \ - --user fiveai \ - --repo terraform-provider-k8s \ - --tag $(VERSION) \ - --name terraform-provider-k8s_$(VERSION)-linux-arm \ - --file terraform-provider-k8s_$(VERSION)-linux-arm \ - --security-token $$GITHUB_TOKEN - - github-release upload \ - --user fiveai \ - --repo terraform-provider-k8s \ - --tag $(VERSION) \ - --name terraform-provider-k8s_$(VERSION)-linux-arm64 \ - --file terraform-provider-k8s_$(VERSION)-linux-arm64 \ - --security-token $$GITHUB_TOKEN - - #======================================================================== - # macOS - #======================================================================== - github-release upload \ - --user fiveai \ - --repo terraform-provider-k8s \ - --tag $(VERSION) \ - --name terraform-provider-k8s_$(VERSION)-darwin-amd64 \ - --file terraform-provider-k8s_$(VERSION)-darwin-amd64 \ - --security-token $$GITHUB_TOKEN - - -dist: goget - #======================================================================== - # GNU/Linux - #======================================================================== - # X86 - #------------------------------------------------------------------------ - $(GOVARS) GOOS=linux GOARCH=amd64 go build -o terraform-provider-k8s_$(VERSION)-linux-amd64 - - #------------------------------------------------------------------------ - # arm - #------------------------------------------------------------------------ - $(GOVARS) GOOS=linux GOARCH=arm go build -o terraform-provider-k8s_$(VERSION)-linux-arm - $(GOVARS) GOOS=linux GOARCH=arm64 go build -o terraform-provider-k8s_$(VERSION)-linux-arm64 - - #======================================================================== - # macOS - #======================================================================== - $(GOVARS) GOOS=darwin GOARCH=amd64 go build -o terraform-provider-k8s_$(VERSION)-darwin-amd64 - -goget: - go get - -clean: - rm -rf terraform-provider-k8s* - -github-release: - go get -u github.com/aktau/github-release - -.PHONY: clean github-release diff --git a/README.md b/README.md index 8f6e65ff..434b172b 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,41 @@ # Kubernetes Terraform Provider -The k8s Terraform provider enables Terraform to deploy Kubernetes resources. Unlike the [official Kubernetes provider][kubernetes-provider] it handles raw manifests, leveraging `kubectl` directly to allow developers to work with any Kubernetes resource natively. +The k8s Terraform provider enables Terraform to deploy Kubernetes resources. Unlike the +[official Kubernetes provider][kubernetes-provider] it handles raw manifests, leveraging `kubectl` directly to allow +developers to work with any Kubernetes resource natively. -## Usage +This provider is published in the [Terraform Registry](https://registry.terraform.io/providers/fiveai/k8s) -Use `go get` to install the provider: +## ToC -``` -go get -u github.com/ericchiang/terraform-provider-k8s -``` +* [Usage](#usage) +* [Build](#build) +* [Publishing](#publishing) + +## Usage + +### Provider and Version control -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: @@ -37,48 +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 tempdir is used. +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 @@ -87,26 +105,20 @@ 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. +## Build + +To build please use [goreleaser](https://goreleaser.com/intro/). You can edit the automated Github Actions by the +`./.goreleaser.yml` and `./github/workflows/release.yml` files. + +To create a test build run the following command: + +``` +goreleaser build --snapshot ``` -[kubernetes-provider]: https://www.terraform.io/docs/providers/kubernetes/index.html +To push up a new build add a tag based on [semantic versioning](https://semver.org/) with a `v` prefix. + +## Publishing + +The configuration is a terraform and github integration with manual configuration. The provider has been setup and will +automatically pickup new version releases in github. 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 +} diff --git a/go.mod b/go.mod index 107d61b7..f2436714 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,15 @@ module github.com/fiveai/terraform-provider-k8s +go 1.15 + require ( - github.com/aktau/github-release v0.7.2 // indirect github.com/hashicorp/go-cleanhttp v0.5.1 // indirect github.com/hashicorp/hcl v0.0.0-20171017181929-23c074d0eceb // indirect github.com/hashicorp/terraform v0.12.1 github.com/kylelemons/godebug v1.1.0 // indirect github.com/mattn/go-isatty v0.0.8 // indirect github.com/stretchr/testify v1.3.0 - github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect github.com/ulikunitz/xz v0.5.6 // indirect - github.com/voxelbrain/goptions v0.0.0-20180630082107-58cddc247ea2 // indirect golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 // indirect golang.org/x/net v0.0.0-20190606173856-1492cefac77f // indirect golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444 // indirect diff --git a/go.sum b/go.sum index 95c8451f..c9005118 100644 --- a/go.sum +++ b/go.sum @@ -19,8 +19,6 @@ github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE= github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agl/ed25519 v0.0.0-20150830182803-278e1ec8e8a6/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0= -github.com/aktau/github-release v0.7.2 h1:la7AnShr2MQPIlBEcRA9MPbI8av0YFmpFP9WM5EoqJs= -github.com/aktau/github-release v0.7.2/go.mod h1:cPkP83iRnV8pAJyQlQ4vjLJoC+JE+aT5sOrYz3sTsX0= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/antchfx/xpath v0.0.0-20190129040759-c8489ed3251e/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk= github.com/antchfx/xquery v0.0.0-20180515051857-ad5b8c7a47b0/go.mod h1:LzD22aAzDP8/dyiCKFp31He4m2GPjl0AFyzDtZzUu9M= @@ -297,8 +295,6 @@ github.com/svanharmelen/jsonapi v0.0.0-20180618144545-0c0828c3f16d/go.mod h1:BST github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/terraform-providers/terraform-provider-openstack v1.15.0/go.mod h1:2aQ6n/BtChAl1y2S60vebhyJyZXBsuAI5G4+lHrT1Ew= github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y= -github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80/go.mod h1:iFyPdL66DjUD96XmzVL3ZntbzcflLnznH0fr99w5VqE= github.com/ugorji/go v0.0.0-20180813092308-00b869d2f4a5/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ= github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= github.com/ulikunitz/xz v0.5.6 h1:jGHAfXawEGZQ3blwU5wnWKQJvAraT7Ftq9EXjnXYgt8= @@ -306,8 +302,6 @@ github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4A github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU= github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= -github.com/voxelbrain/goptions v0.0.0-20180630082107-58cddc247ea2 h1:txplJASvd6b/hrE0s/Ixfpp2cuwH9IO9oZBAN9iYa4A= -github.com/voxelbrain/goptions v0.0.0-20180630082107-58cddc247ea2/go.mod h1:DGCIhurYgnLz8J9ga1fMV/fbLDyUvTyrWXVWUIyJon4= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=