Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dak1n1 committed Feb 9, 2021
1 parent 946519f commit 242f7b1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 85 deletions.
19 changes: 0 additions & 19 deletions _examples/aks/aks-cluster/output.tf

This file was deleted.

8 changes: 3 additions & 5 deletions _examples/aks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.0.1"
version = ">= 2.0.2"
}
azurerm = {
source = "hashicorp/azurerm"
version = "2.42"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.0.1"
version = ">= 2.0.2"
}
}
}

data "azurerm_kubernetes_cluster" "default" {
depends_on = [module.aks-cluster.cluster_id] # refresh cluster state before reading
depends_on = [module.aks-cluster] # refresh cluster state before reading
name = local.cluster_name
resource_group_name = local.cluster_name
}
Expand All @@ -42,14 +42,12 @@ provider "azurerm" {
}

module "aks-cluster" {
providers = { azurerm = azurerm }
source = "./aks-cluster"
cluster_name = local.cluster_name
location = var.location
}

module "kubernetes-config" {
providers = { kubernetes = kubernetes, helm = helm }
depends_on = [module.aks-cluster]
source = "./kubernetes-config"
cluster_name = local.cluster_name
Expand Down
2 changes: 1 addition & 1 deletion _examples/gke/gke-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data "google_container_engine_versions" "supported" {
version_prefix = var.kubernetes_version
}

resource "google_container_cluster" "primary" {
resource "google_container_cluster" "default" {
name = var.cluster_name
location = local.google_zone
initial_node_count = var.workers_count
Expand Down
18 changes: 1 addition & 17 deletions _examples/gke/gke-cluster/output.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
output "node_version" {
value = google_container_cluster.primary.node_version
}

output "cluster_id" {
value = google_container_cluster.primary.id
}

output "cluster_endpoint" {
value = google_container_cluster.primary.endpoint
}

output "cluster_ca_cert" {
value = google_container_cluster.primary.master_auth[0].cluster_ca_certificate
}

output "cluster_name" {
value = google_container_cluster.primary.name
value = google_container_cluster.default.node_version
}

output "google_zone" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v1
clusters:
- cluster:
certificate-authority-data: ${cluster_ca}
server: https://${endpoint}
server: ${endpoint}
name: ${cluster_name}
contexts:
- context:
Expand Down
17 changes: 0 additions & 17 deletions _examples/gke/kubernetes-config/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,3 @@ resource helm_release nginx_ingress {
value = "ClusterIP"
}
}

data "template_file" "kubeconfig" {
template = file("${path.module}/kubeconfig-template.yaml")

vars = {
cluster_name = var.cluster_name
endpoint = var.cluster_endpoint
cluster_ca = var.cluster_ca_cert
cluster_token = var.cluster_token
}
}

resource "local_file" "kubeconfig" {
content = data.template_file.kubeconfig.rendered
filename = "${path.root}/kubeconfig"
}

16 changes: 0 additions & 16 deletions _examples/gke/kubernetes-config/variables.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
variable "cluster_name" {
type = string
}

variable "cluster_id" {
type = string
}

variable "cluster_endpoint" {
type = string
}

variable "cluster_ca_cert" {
type = string
}

variable "cluster_token" {
type = string
}
31 changes: 22 additions & 9 deletions _examples/gke/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.0.1"
version = ">= 9.9.9"
}
google = {
source = "hashicorp/google"
Expand All @@ -24,12 +24,13 @@ provider "google" {}
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/client_config
# This fetches a new token, which will expire in 1 hour.
data "google_client_config" "default" {
depends_on = [module.gke-cluster.cluster_id]
depends_on = [module.gke-cluster]
}

# Defer reading the cluster data until the latest ca_cert data exists.
data "google_container_cluster" "default" {
name = local.cluster_name
depends_on = [module.gke-cluster.cluster_id]
depends_on = [module.gke-cluster]
}

provider "kubernetes" {
Expand All @@ -51,19 +52,31 @@ provider "helm" {
}

module "gke-cluster" {
providers = { google = google }
source = "./gke-cluster"
cluster_name = local.cluster_name
}

module "kubernetes-config" {
depends_on = [module.gke-cluster]
providers = { kubernetes = kubernetes, helm = helm }
source = "./kubernetes-config"
cluster_name = local.cluster_name
cluster_token = data.google_client_config.default.access_token
cluster_id = module.gke-cluster.cluster_id # creates dependency on cluster creation
cluster_endpoint = module.gke-cluster.cluster_endpoint
cluster_ca_cert = module.gke-cluster.cluster_ca_cert
}

# optional: used for manual CLI access to the cluster when gcloud tool is unavailable.
# The gcloud tool can make a longer-lived kubeconfig. This one expires in one hour and can be updated using `terraform apply`.
data "template_file" "kubeconfig" {
template = file("kubeconfig-template.yaml")

vars = {
cluster_name = local.cluster_name
endpoint = "https://${data.google_container_cluster.default.endpoint}"
cluster_ca = data.google_container_cluster.default.master_auth[0].cluster_ca_certificate
cluster_token = data.google_client_config.default.access_token
}
}

resource "local_file" "kubeconfig" {
content = data.template_file.kubeconfig.rendered
filename = "${path.root}/kubeconfig"
}

0 comments on commit 242f7b1

Please sign in to comment.