Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create kubeconfig format file/string #22

Merged
merged 6 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .mdlrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# a seperate "style" file must be used to pass "parameters" to a rule
# a separate "style" file must be used to pass "parameters" to a rule
#
# https://github.com/markdownlint/markdownlint/blob/master/docs/configuration.md
# https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- repo: git://github.com/antonbabenko/pre-commit-terraform
rev: v1.8.1
rev: v1.11.0
hooks:
- id: terraform_fmt
- id: terraform_docs
8 changes: 1 addition & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
sudo: false
language: c
language: minimal
matrix:
include:
- env: TEST=markdownlint
Expand All @@ -13,11 +12,6 @@ matrix:
- docker
script: ./tests/pre-commit.sh

- env: TEST=pre-commit
services:
- docker
script: ./tests/pre-commit.sh

- env: TEST=terraform
services:
- docker
Expand Down
6 changes: 5 additions & 1 deletion .yamllint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ extends: default

rules:
# 80 chars should be enough, but don't fail if a line is longer
line-length: disable
line-length: false
# do not obsess over comment formatting
comments-indentation: false
comments:
require-starting-space: false
68 changes: 44 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,55 @@ terraform `gke-std` module
Usage
---

module "gke4u" {
source = "git::https://github.com/jhoblitt/terraform-gke-std.git//?ref=master"
name = "mycluster"
google_project = "plasma-geode-127520" # default
google_region = "us-central1" # default
google_zone = "us-central1-b" # default
initial_node_count = 3 # default
gke_version = "latest" # default
machine_type = "n1-standard-1" # default
}

provider "kubernetes" {
version = "~> 1.4"

load_config_file = true

host = "${module.gke4u.host}"
cluster_ca_certificate = "${base64decode("${module.gke4u.cluster_ca_certificate}")}"
}
```hcl
#
# create gke cluster
#

provider "google" {
project = "${var.google_project}"
region = "${var.google_region}"
zone = "${var.google_zone}"
}

# uses google provider
module "gke" {
source = "git::https://github.com/jhoblitt/terraform-gke-std.git//?ref=master"
name = "mycluster"
initial_node_count = 3 # default
gke_version = "latest" # default
machine_type = "n1-standard-1" # default
}

#
# configure kubernetes provider
#

# write out our own copy of kubeconfig
resource "local_file" "kubeconfig" {
content = "${module.gke.kubeconfig}"
filename = "${local.kubeconfig_filename}"

# this forces the gke cluster to be up before proceeding to the efd deployment
depends_on = ["module.gke"]
}

provider "kubernetes" {
version = "~> 1.6.2"

config_path = "${local_file.kubeconfig.filename}"
load_config_file = true
}

```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| gcloud\_cmd | Whether to write a Kubectl config file containing the cluster configuration. Saved to `kubeconfig_output_path`. | string | `"gcloud"` | no |
| gke\_version | gke master/node version | string | `"latest"` | no |
| google\_project | google cloud project ID | string | `"plasma-geode-127520"` | no |
| google\_region | google cloud region | string | `"us-central1"` | no |
| google\_zone | google cloud region/zone | string | `"us-central1-b"` | no |
| initial\_node\_count | number of gke nodes to start | string | `"3"` | no |
| machine\_type | machine type of default gke pool nodes | string | `"n1-standard-1"` | no |
| name | gke cluster name | string | n/a | yes |
Expand All @@ -43,11 +63,11 @@ Usage

| Name | Description |
|------|-------------|
| client\_certificate | |
| client\_key | |
| cluster\_ca\_certificate | |
| host | |
| id | |
| kubeconfig | kubeconfig format string |
| kubeconfig\_filename | path to output kubeconfig format file |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Expand Down
16 changes: 16 additions & 0 deletions kubeconfig.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
data "template_file" "kubeconfig" {
template = "${file("${path.module}/templates/kubeconfig.yaml")}"

vars {
kubeconfig_name = "${var.name}"
endpoint = "https://${google_container_cluster.gke_std.endpoint}"
cluster_auth_base64 = "${google_container_cluster.gke_std.master_auth.0.cluster_ca_certificate}"
gcloud_cmd = "${var.gcloud_cmd}"
}
}

# there does not seem to be a sane way to ignore changes in the file on disk
resource "local_file" "kubeconfig" {
content = "${data.template_file.kubeconfig.rendered}"
filename = "${local.kubeconfig_filename}"
}
42 changes: 27 additions & 15 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,42 @@ locals {
gke_version = "${var.gke_version != "latest" ? var.gke_version : data.google_container_engine_versions.gke_std.latest_node_version}"
}

provider "google" {
alias = "gke_std"
provider "kubernetes" {
version = "1.6.2"
alias = "gke_std"

project = "${var.google_project}"
region = "${var.google_region}"
zone = "${var.google_zone}"
load_config_file = true
config_path = "${local.kubeconfig_filename}"
}

provider "kubernetes" {
alias = "gke_std"
resource "null_resource" "k8s_ready" {
provisioner "local-exec" {
working_dir = "${path.module}"

load_config_file = true
command = <<EOS
for i in `seq 1 10`; do \
kubectl --kubeconfig ${null_resource.k8s_ready.triggers.config_path} get ns && break || \
sleep 10; \
done; \
EOS

host = "${google_container_cluster.gke_std.endpoint}"
cluster_ca_certificate = "${base64decode("${google_container_cluster.gke_std.master_auth.0.cluster_ca_certificate}")}"
}
interpreter = ["/bin/sh", "-c"]
}

triggers {
config_path = "${local_file.kubeconfig.filename}"
kubeconfig = "${local_file.kubeconfig.content}"
}

data "google_container_engine_versions" "gke_std" {
provider = "google.gke_std"
depends_on = [
"local_file.kubeconfig",
"google_container_cluster.gke_std",
]
}

resource "google_container_cluster" "gke_std" {
provider = "google.gke_std"
data "google_container_engine_versions" "gke_std" {}

resource "google_container_cluster" "gke_std" {
name = "${var.name}"
min_master_version = "${local.gke_version}"
node_version = "${local.gke_version}"
Expand Down
19 changes: 11 additions & 8 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ output "host" {
value = "${google_container_cluster.gke_std.endpoint}"
}

output "client_certificate" {
output "cluster_ca_certificate" {
# not actually sensitive... just a lot of output
sensitive = true
value = "${google_container_cluster.gke_std.master_auth.0.client_certificate}"
value = "${google_container_cluster.gke_std.master_auth.0.cluster_ca_certificate}"
}

output "client_key" {
sensitive = true
value = "${google_container_cluster.gke_std.master_auth.0.client_key}"
output "kubeconfig_filename" {
description = "path to output kubeconfig format file"
value = "${pathexpand(local_file.kubeconfig.*.filename[0])}"
}

output "cluster_ca_certificate" {
sensitive = true
value = "${google_container_cluster.gke_std.master_auth.0.cluster_ca_certificate}"
output "kubeconfig" {
# not actually sensitive... just a lot of output
sensitive = true
description = "kubeconfig format string"
value = "${data.template_file.kubeconfig.rendered}"
}

output "id" {
Expand Down
4 changes: 3 additions & 1 deletion pd-ssd-storageclass.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ resource "kubernetes_storage_class" "pd_ssd" {
}

# needed when the gke cluster is recreated
depends_on = ["google_container_cluster.gke_std"]
depends_on = [
"null_resource.k8s_ready",
]
}
29 changes: 29 additions & 0 deletions templates/kubeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
apiVersion: v1
preferences: {}
kind: Config

clusters:
- cluster:
certificate-authority-data: ${cluster_auth_base64}
server: ${endpoint}
name: ${kubeconfig_name}

contexts:
- context:
cluster: ${kubeconfig_name}
user: ${kubeconfig_name}
name: ${kubeconfig_name}

current-context: ${kubeconfig_name}

users:
- name: ${kubeconfig_name}
user:
auth-provider:
config:
cmd-args: config config-helper --format=json
cmd-path: ${gcloud_cmd}
expiry-key: '{.credential.token_expiry}'
token-key: '{.credential.access_token}'
name: gcp
2 changes: 1 addition & 1 deletion tests/shellcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
shopt -s globstar nullglob

CHECK=( **/*.sh )
IGNORE=( environments/** .bundle/** .tmp/** )
IGNORE=( {environments,.bundle,.tmp}/** )

for c in "${!CHECK[@]}"; do
for i in "${IGNORE[@]}"; do
Expand Down
2 changes: 1 addition & 1 deletion tests/terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

TF_VER="0.11.11"
TF_VER="0.11.13"

tf() {
docker run -ti -v "$(pwd):$(pwd)" -w "$(pwd)" \
Expand Down
2 changes: 1 addition & 1 deletion tests/yamllint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ EYAML=( **/*.eyaml )
IGNORE=()
CONF_FILE=".yamllint.yaml"

# filter out plaintext versions of .eyaml files
# filter out plain text versions of .eyaml files
for e in "${!EYAML[@]}"; do
uneyaml=${EYAML[e]/eyaml/yaml}
for c in "${!CHECK[@]}"; do
Expand Down
24 changes: 9 additions & 15 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@ variable "name" {
description = "gke cluster name"
}

variable "google_project" {
description = "google cloud project ID"
default = "plasma-geode-127520"
}

variable "google_region" {
description = "google cloud region"
default = "us-central1"
}

variable "google_zone" {
description = "google cloud region/zone"
default = "us-central1-b"
}

variable "initial_node_count" {
description = "number of gke nodes to start"
default = 3
Expand All @@ -31,3 +16,12 @@ variable "machine_type" {
description = "machine type of default gke pool nodes"
default = "n1-standard-1"
}

variable "gcloud_cmd" {
description = "Whether to write a Kubectl config file containing the cluster configuration. Saved to `kubeconfig_output_path`."
default = "gcloud"
}

locals {
kubeconfig_filename = "${path.module}/kubeconfig_${var.name}"
}