From fac9bc8db1a0b64022075ae965bf852881406355 Mon Sep 17 00:00:00 2001 From: Max Williams Date: Fri, 6 Jul 2018 12:11:48 +0200 Subject: [PATCH 1/3] Making kubeconfig file path and generated config unique --- kubectl.tf | 2 +- templates/kubeconfig.tpl | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/kubectl.tf b/kubectl.tf index e2508f0..c360250 100644 --- a/kubectl.tf +++ b/kubectl.tf @@ -1,6 +1,6 @@ resource "local_file" "kubeconfig" { content = "${data.template_file.kubeconfig.rendered}" - filename = "${var.config_output_path}/kubeconfig" + filename = "${var.config_output_path}/kubeconfig_eks_${data.aws_region.current.name}_${var.cluster_name}" count = "${var.configure_kubectl_session ? 1 : 0}" } diff --git a/templates/kubeconfig.tpl b/templates/kubeconfig.tpl index 7babdb5..332d21e 100644 --- a/templates/kubeconfig.tpl +++ b/templates/kubeconfig.tpl @@ -6,17 +6,18 @@ clusters: - cluster: server: ${endpoint} certificate-authority-data: ${cluster_auth_base64} - name: ${cluster_name} + name: eks_${region}_${cluster_name} contexts: - context: - cluster: ${cluster_name} - user: ${user_name} - name: ${context_name} -current-context: ${context_name} + cluster: eks_${region}_${cluster_name} + user: eks_${region}_${cluster_name} + name: eks_${region}_${cluster_name} + +current-context: eks_${region}_${cluster_name} users: -- name: ${user_name} +- name: eks_${region}_${cluster_name} user: exec: apiVersion: client.authentication.k8s.io/v1alpha1 @@ -26,4 +27,4 @@ users: - "-i" - "${cluster_name}" ${aws_authenticator_additional_args} -${aws_authenticator_env_variables} \ No newline at end of file +${aws_authenticator_env_variables} From 0ffa4932f616bf40484c0398084c51a79d7c228e Mon Sep 17 00:00:00 2001 From: Max Williams Date: Fri, 6 Jul 2018 16:39:00 +0200 Subject: [PATCH 2/3] clean up unused variables --- README.md | 2 -- data.tf | 2 -- kubectl.tf | 6 +++--- variables.tf | 10 ---------- 4 files changed, 3 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 95327f8..02c5eaf 100644 --- a/README.md +++ b/README.md @@ -102,8 +102,6 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | kubeconfig_aws_authenticator_additional_args | Any additional arguments to pass to the authenticator such as the role to assume ["-r", "MyEksRole"] | string | `` | no | | kubeconfig_aws_authenticator_command | Command to use to to fetch AWS EKS credentials | string | `heptio-authenticator-aws` | no | | kubeconfig_aws_authenticator_env_variables | Environment variables that should be used when executing the authenticator i.e. { AWS_PROFILE = "eks"} | string | `` | no | -| kubeconfig_context_name | Name of the kubeconfig context. | string | `aws` | no | -| kubeconfig_user_name | Name of the kubeconfig user. | string | `aws` | no | | subnets | A list of subnets to place the EKS cluster and workers within. | list | - | yes | | tags | A map of tags to add to all resources. | string | `` | no | | vpc_id | VPC where the cluster and workers will be deployed. | string | - | yes | diff --git a/data.tf b/data.tf index c47075f..0c835b8 100644 --- a/data.tf +++ b/data.tf @@ -52,8 +52,6 @@ data "template_file" "kubeconfig" { endpoint = "${aws_eks_cluster.this.endpoint}" region = "${data.aws_region.current.name}" cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}" - context_name = "${var.kubeconfig_context_name}" - user_name = "${var.kubeconfig_user_name}" aws_authenticator_command = "${var.kubeconfig_aws_authenticator_command}" aws_authenticator_additional_args = "${length(var.kubeconfig_aws_authenticator_additional_args) > 0 ? " - ${join("\n - ", var.kubeconfig_aws_authenticator_additional_args)}" : "" }" aws_authenticator_env_variables = "${length(var.kubeconfig_aws_authenticator_env_variables) > 0 ? " env:\n${join("\n", data.template_file.aws_authenticator_env_variables.*.rendered)}" : ""}" diff --git a/kubectl.tf b/kubectl.tf index c360250..4c56a62 100644 --- a/kubectl.tf +++ b/kubectl.tf @@ -1,18 +1,18 @@ resource "local_file" "kubeconfig" { content = "${data.template_file.kubeconfig.rendered}" - filename = "${var.config_output_path}/kubeconfig_eks_${data.aws_region.current.name}_${var.cluster_name}" + filename = "${var.config_output_path}/kubeconfig_${var.cluster_name}" count = "${var.configure_kubectl_session ? 1 : 0}" } resource "local_file" "config_map_aws_auth" { content = "${data.template_file.config_map_aws_auth.rendered}" - filename = "${var.config_output_path}/config-map-aws-auth.yaml" + filename = "${var.config_output_path}/config-map-aws-auth_${var.cluster_name}.yaml" count = "${var.configure_kubectl_session ? 1 : 0}" } resource "null_resource" "configure_kubectl" { provisioner "local-exec" { - command = "kubectl apply -f ${var.config_output_path}/config-map-aws-auth.yaml --kubeconfig ${var.config_output_path}/kubeconfig" + command = "kubectl apply -f ${var.config_output_path}/config-map-aws-auth_${var.cluster_name}.yaml --kubeconfig ${var.config_output_path}/kubeconfig_${var.cluster_name}" } triggers { diff --git a/variables.tf b/variables.tf index 4bfb415..9aaae59 100644 --- a/variables.tf +++ b/variables.tf @@ -79,16 +79,6 @@ variable "worker_sg_ingress_from_port" { default = "1025" } -variable "kubeconfig_context_name" { - description = "Name of the kubeconfig context." - default = "aws" -} - -variable "kubeconfig_user_name" { - description = "Name of the kubeconfig user." - default = "aws" -} - variable "kubeconfig_aws_authenticator_command" { description = "Command to use to to fetch AWS EKS credentials" default = "heptio-authenticator-aws" From 7e4e93eeec4a2d0e84440258297688d36b569501 Mon Sep 17 00:00:00 2001 From: Max Williams Date: Mon, 9 Jul 2018 10:31:01 +0200 Subject: [PATCH 3/3] Removing region from kubectl config and adding override variable --- README.md | 1 + data.tf | 1 + local.tf | 2 ++ templates/kubeconfig.tpl | 12 ++++++------ variables.tf | 5 +++++ 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 02c5eaf..38fac85 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | kubeconfig_aws_authenticator_additional_args | Any additional arguments to pass to the authenticator such as the role to assume ["-r", "MyEksRole"] | string | `` | no | | kubeconfig_aws_authenticator_command | Command to use to to fetch AWS EKS credentials | string | `heptio-authenticator-aws` | no | | kubeconfig_aws_authenticator_env_variables | Environment variables that should be used when executing the authenticator i.e. { AWS_PROFILE = "eks"} | string | `` | no | +| kubeconfig_name | Override the default name used for items kubeconfig. | string | `` | no | | subnets | A list of subnets to place the EKS cluster and workers within. | list | - | yes | | tags | A map of tags to add to all resources. | string | `` | no | | vpc_id | VPC where the cluster and workers will be deployed. | string | - | yes | diff --git a/data.tf b/data.tf index 0c835b8..0f914e8 100644 --- a/data.tf +++ b/data.tf @@ -49,6 +49,7 @@ data "template_file" "kubeconfig" { vars { cluster_name = "${var.cluster_name}" + kubeconfig_name = "${local.kubeconfig_name}" endpoint = "${aws_eks_cluster.this.endpoint}" region = "${data.aws_region.current.name}" cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}" diff --git a/local.tf b/local.tf index a634fc4..9c87d73 100644 --- a/local.tf +++ b/local.tf @@ -9,6 +9,8 @@ locals { workstation_external_cidr = "${chomp(data.http.workstation_external_ip.body)}/32" workstation_cidr = "${coalesce(var.workstation_cidr, local.workstation_external_cidr)}" + kubeconfig_name = "${var.kubeconfig_name == "" ? "eks_${var.cluster_name}" : var.kubeconfig_name}" + # Mapping from the node type that we selected and the max number of pods that it can run # Taken from https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-06-05/amazon-eks-nodegroup.yaml max_pod_per_node = { diff --git a/templates/kubeconfig.tpl b/templates/kubeconfig.tpl index 332d21e..18e8a8f 100644 --- a/templates/kubeconfig.tpl +++ b/templates/kubeconfig.tpl @@ -6,18 +6,18 @@ clusters: - cluster: server: ${endpoint} certificate-authority-data: ${cluster_auth_base64} - name: eks_${region}_${cluster_name} + name: ${kubeconfig_name} contexts: - context: - cluster: eks_${region}_${cluster_name} - user: eks_${region}_${cluster_name} - name: eks_${region}_${cluster_name} + cluster: ${kubeconfig_name} + user: ${kubeconfig_name} + name: ${kubeconfig_name} -current-context: eks_${region}_${cluster_name} +current-context: ${kubeconfig_name} users: -- name: eks_${region}_${cluster_name} +- name: ${kubeconfig_name} user: exec: apiVersion: client.authentication.k8s.io/v1alpha1 diff --git a/variables.tf b/variables.tf index 9aaae59..5d54659 100644 --- a/variables.tf +++ b/variables.tf @@ -93,3 +93,8 @@ variable "kubeconfig_aws_authenticator_env_variables" { description = "Environment variables that should be used when executing the authenticator i.e. { AWS_PROFILE = \"eks\"}" default = {} } + +variable "kubeconfig_name" { + description = "Override the default name used for items kubeconfig" + default = "" +}