Skip to content

cjmateos/terraform-provider-kubernetes

 
 

Repository files navigation

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:

  • Cluster Role kubernetes_cluster_role
  • Cluster Role Binding kubernetes_cluster_role_binding
  • Cron Job kubernetes_cron_job
  • Daemonset kubernetes_daemonset
  • Deployment kubernetes_deployment
  • Ingress kubernetes_ingress
  • Job kubernetes_job
  • Role kubernetes_role
  • Role Binding kubernetes_role_binding
  • Stateful Set kubernetes_stateful_set

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
  • 1.10.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 https://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

Alternatively, use the golang docker to build the provider

  1. clone this repo: https://github.com/sl1pm4t/terraform-provider-kubernetes.git
  2. run:
cd <path to cloned repo>
docker run --rm -v `pwd`:/go/src/github.com/sl1pm4t/terraform-provider-kubernetes/ \
  -w /go/src/github.com/sl1pm4t/terraform-provider-kubernetes/ \
  -e GOOS=windows -e GOARCH=386 golang \
  go build -v

be sure to use the appropriate OS and architecture for your environment (see official docker for golang)

  1. From the root director of your terraform project, run
terraform init --plugin-dir=<location of the plugin executable built in step 2>`

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")}"
}
Initialise provider with plugin directory

After this fork has been downloaded and built into $GOPATH (as in the previous step), specify the location of the built binaries when initialising the project:

$ terraform init -plugin-dir=$GOPATH/bin

This step is important in order to make terraform actually use this fork of the kubernetes provider. If you fail to do this upon the first init the official provider is downloaded instead. If this happens, delete the .terraform/ folder that has been created inside your project folder and perform the above init again.

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

About

Terraform kubernetes provider

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 96.3%
  • HCL 2.4%
  • Other 1.3%