Skip to content

Commit

Permalink
Merge pull request #10 from johncosta/johncosta/gh-6
Browse files Browse the repository at this point in the history
[GH-6] adds kubeconfig file feature
  • Loading branch information
johncosta committed Dec 9, 2023
2 parents 1ee6d00 + ae35eed commit eb49808
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
```
12 changes: 12 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
6 changes: 5 additions & 1 deletion provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
13 changes: 13 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit eb49808

Please sign in to comment.