/terraform-provider-kubernetes

Terraform kubernetes provider

Primary LanguageGoMozilla Public License 2.0MPL-2.0

Kubernetes Terraform Provider

This provider is a fork of the official Kubernetes provider developed by HashiCorp. This fork supports the following resources in addition to the official provider:

  • DaemonSets
  • Deployments
  • Ingress
  • StatefulSets

Supported Kubernetes Versions

The latest build of this provider uses v6.0 of the kubernetes client-go library, and has been tested with the following Kubernetes versions:

  • 1.7.x
  • 1.8.x
  • 1.9.x

Requirements

  • Terraform 0.11.x
  • Go 1.10 (to build the provider plugin)

Building The Provider

Clone repository to: $GOPATH/src/github.com/sl1pm4t/terraform-provider-kubernetes

$ mkdir -p $GOPATH/src/github.com/sl1pm4t; cd $GOPATH/src/github.com/sl1pm4t
$ git clone git@github.com:sl1pm4t/terraform-provider-kubernetes

Enter the provider directory and build the provider

$ cd $GOPATH/src/github.com/sl1pm4t/terraform-provider-kubernetes
$ make build

Using the provider

Provider Configuration

Simplest - kubectl configuration
provider kubernetes {
  # leave blank to pickup config from kubectl config of local system
}
Explicit configuration
provider "kubernetes" {
  host     = "https://104.196.242.174"
  username = "ClusterMaster"
  password = "MindTheGap"

  client_certificate     = "${file("~/.kube/client-cert.pem")}"
  client_key             = "${file("~/.kube/client-key.pem")}"
  cluster_ca_certificate = "${file("~/.kube/cluster-ca-cert.pem")}"
}

Deployment Resource

resource "kubernetes_deployment" "nginx" {

  metadata {
    name      = "nginx"
    namespace = "web"
  }

  spec {
    selector {
      app = "nginx"
    }

    template {
      metadata {
        labels {
          app = "nginx"
        }
      }

      spec {
        container {
          image = "nginx:1.8"
          name  = "app"

          resources {
            requests {
              memory = "1Gi"
              cpu    = "1"
            }

            limits {
              memory = "2Gi"
              cpu    = "2"
            }
          }

          readiness_probe {
            http_get {
              path = "/health"
              port = "90"
            }

            initial_delay_seconds = 10
            period_seconds        = 10
          }

          liveness_probe {
            exec {
              command = ["/bin/health"]
            }

            initial_delay_seconds = 120
            period_seconds        = 15
          }

          env {
            name  = "CONFIG_FILE_LOCATION"
            value = "/etc/app/config"
          }

          port {
            container_port = 80
          }

          volume_mount {
            name       = "config"
            mount_path = "/etc/app/config"
          }
        }

        init_container {
          name  = "helloworld"
          image = "debian"
          command = ["/bin/echo", "hello", "world"]
        }

        volume {
          name = "config"

          config_map {
            name = "app-config"
          }
        }

      }
    }
  }
}

Developing the Provider

Development Environment

If you wish to work on the provider, you'll first need Go installed on your machine (version 1.9+ is required). You'll also need to correctly setup a GOPATH, as well as adding $GOPATH/bin to your $PATH.

To compile the provider, run make build. This will build the provider and put the provider binary in the $GOPATH/bin directory.

$ make build
...
$ $GOPATH/bin/terraform-provider-kubernetes
...

In order to test the provider, you can simply run make test.

$ make test

In order to run the full suite of Acceptance tests, run make testacc.

Note: Acceptance tests create real resources, and often cost money to run.

$ make testacc