diff --git a/README.md b/README.md index 58acc43..bdba34e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ This module is inspired by [terraform-aws-eks](https://github.com/terraform-aws- ```hcl module "k8s" { source = "terraform-digitalocean-kubernetes" - version = "0.0.3" + version = "0.0.6" cluster_name_prefix = "test-cluster" cluster_region = "nyc1" @@ -33,7 +33,10 @@ module "k8s" { default_node_pool_node_count = 1 default_node_pool_node_size = "s-2vcpu-2gb" - cluster_ipv4_cidr = "10.1.0.0/20" + + # writes the kubeconfig to the local filesystem + path_to_kubeconfig = "/full/path/to/.kube" + use_cluster_name_in_config = true } ``` diff --git a/main.tf b/main.tf index 6c5255d..684628d 100644 --- a/main.tf +++ b/main.tf @@ -6,6 +6,11 @@ locals { create_vpc = var.cluster_ipv4_cidr != null && var.cluster_ipv4_cidr != "" allow_default_vpc = !local.create_vpc && var.allow_default_vpc ? true : false default_vpc_value_or_error = local.create_vpc || local.allow_default_vpc ? null : file("[Error] you must explicitly set the variable `allow_default_vpc` if you want the cluster to access the default vpc") + + create_kubeconfig = var.path_to_kubeconfig != null && var.path_to_kubeconfig != "" + use_cluster_name_in_config = var.use_cluster_name_in_config ? true : false + kubeconfig_filename = local.use_cluster_name_in_config ? join("-", [local.cluster_name, "config"]) : "config" + full_path_to_kubeconfig = join("/", [var.path_to_kubeconfig, local.kubeconfig_filename]) } data "digitalocean_kubernetes_versions" "version" { @@ -36,3 +41,10 @@ resource "digitalocean_vpc" "vpc" { region = var.cluster_region ip_range = var.cluster_ipv4_cidr } + +resource "local_sensitive_file" "kubeconfig" { + count = local.create_kubeconfig ? 1 : 0 + + content = digitalocean_kubernetes_cluster.cluster.kube_config[0].raw_config + filename = local.full_path_to_kubeconfig +} diff --git a/outputs.tf b/outputs.tf index ef745a2..50b6757 100644 --- a/outputs.tf +++ b/outputs.tf @@ -17,3 +17,11 @@ output "cluster_endpoint" { output "cluster_kube_config" { value = digitalocean_kubernetes_cluster.cluster.kube_config } + +output "full_path_to_kubeconfig" { + value = local.full_path_to_kubeconfig +} + +output "environment_variable_kubeconfig" { + value = "export KUBECONFIG=${local.full_path_to_kubeconfig}" +} diff --git a/provider.tf b/provider.tf index 0190b1e..652ff77 100644 --- a/provider.tf +++ b/provider.tf @@ -4,7 +4,11 @@ terraform { required_providers { digitalocean = { source = "digitalocean/digitalocean" - version = "2.32.0" + version = "~> 2.32.0" + } + local = { + source = "hashicorp/local" + version = "~> 2.4.0" } } } diff --git a/variables.tf b/variables.tf index fdd0f5e..1dda6a5 100644 --- a/variables.tf +++ b/variables.tf @@ -41,3 +41,16 @@ variable "default_node_pool_node_count" { description = "default node pool node count" nullable = false } + +variable "path_to_kubeconfig" { + type = string + description = "path to kubeconfig" + nullable = false +} + +variable "use_cluster_name_in_config" { + type = bool + description = "use cluster name in config" + nullable = true + default = false +}