-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kubeconfig code/doc and misc updates. (#137)
- Loading branch information
Showing
19 changed files
with
183 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
locals { | ||
service_account_name = "${var.prefix}-cluster-admin-sa" | ||
cluster_role_binding_name = "${var.prefix}-cluster-admin-crb" | ||
service_account_secret_name = "${var.prefix}-sa-secret" | ||
} | ||
|
||
# Provider based kube config data/template/resources | ||
data "template_file" "kubeconfig_provider" { | ||
count = var.create_static_kubeconfig ? 0 : 1 | ||
template = file("${path.module}/templates/kubeconfig-provider.tmpl") | ||
|
||
vars = { | ||
cluster_name = var.cluster_name | ||
endpoint = var.endpoint | ||
ca_crt = var.ca_crt | ||
client_crt = var.client_crt | ||
client_key = var.client_key | ||
token = var.token | ||
} | ||
} | ||
|
||
# Service Account based kube config data/template/resources | ||
data "kubernetes_secret" "sa_secret" { | ||
count = var.create_static_kubeconfig ? 1 : 0 | ||
metadata { | ||
name = kubernetes_service_account.kubernetes_sa.0.default_secret_name | ||
namespace = var.namespace | ||
} | ||
} | ||
|
||
data "template_file" "kubeconfig_sa" { | ||
count = var.create_static_kubeconfig ? 1 : 0 | ||
template = file("${path.module}/templates/kubeconfig-sa.tmpl") | ||
|
||
vars = { | ||
cluster_name = var.cluster_name | ||
endpoint = var.endpoint | ||
name = local.service_account_name | ||
ca_crt = base64encode(lookup(data.kubernetes_secret.sa_secret.0.data,"ca.crt", "")) | ||
token = lookup(data.kubernetes_secret.sa_secret.0.data,"token", "") | ||
namespace = var.namespace | ||
} | ||
} | ||
|
||
resource "kubernetes_service_account" "kubernetes_sa" { | ||
count = var.create_static_kubeconfig ? 1 : 0 | ||
metadata { | ||
name = local.service_account_name | ||
namespace = var.namespace | ||
} | ||
} | ||
|
||
resource "kubernetes_cluster_role_binding" "kubernetes_crb" { | ||
count = var.create_static_kubeconfig ? 1 : 0 | ||
metadata { | ||
name = local.cluster_role_binding_name | ||
} | ||
role_ref { | ||
api_group = "rbac.authorization.k8s.io" | ||
kind = "ClusterRole" | ||
name = "cluster-admin" | ||
} | ||
subject { | ||
kind = "ServiceAccount" | ||
name = local.service_account_name | ||
namespace = var.namespace | ||
} | ||
} | ||
|
||
# kube config file generation | ||
resource "local_file" "kubeconfig" { | ||
content = var.create_static_kubeconfig ? data.template_file.kubeconfig_sa.0.rendered : data.template_file.kubeconfig_provider.0.rendered | ||
filename = var.path | ||
file_permission = "0644" | ||
directory_permission = "0755" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "kube_config" { | ||
value = local_file.kubeconfig.content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
certificate-authority-data: ${ca_crt} | ||
server: '${endpoint}' | ||
name: ${cluster_name} | ||
contexts: | ||
- context: | ||
cluster: ${cluster_name} | ||
user: ${cluster_name} | ||
name: ${cluster_name} | ||
current-context: ${cluster_name} | ||
kind: Config | ||
preferences: {} | ||
users: | ||
- name: ${cluster_name} | ||
user: | ||
client-certificate-data: ${client_crt} | ||
client-key-data: ${client_key} | ||
token: ${token} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
apiVersion: v1 | ||
kind: Config | ||
clusters: | ||
- name: ${cluster_name} | ||
cluster: | ||
server: '${endpoint}' | ||
certificate-authority-data: >- | ||
${ca_crt} | ||
users: | ||
- name: ${name} | ||
user: | ||
token: >- | ||
${token} | ||
contexts: | ||
- name: ${cluster_name} | ||
context: | ||
user: ${name} | ||
cluster: ${cluster_name} | ||
namespace: ${namespace} | ||
current-context: ${cluster_name} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
variable "prefix" { | ||
description = "A prefix used for all Google Cloud resources created by this script" | ||
type = string | ||
} | ||
|
||
variable "namespace" { | ||
description = "Namespace that the service account and cluster role binding will placed." | ||
type = string | ||
default = "kube-system" | ||
} | ||
|
||
variable "create_static_kubeconfig" { | ||
description = "Allows the user to create a provider / service account based kube config file" | ||
type = bool | ||
default = false | ||
} | ||
|
||
variable "path" {} | ||
variable "cluster_name" {} | ||
variable "endpoint" {} | ||
variable "ca_crt" {} | ||
variable "client_crt" {} | ||
variable "client_key" {} | ||
variable "token" {} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters