Skip to content

Commit 795bb18

Browse files
authored
Update EKS test-infra (#1203)
This commit simplifies the EKS infrastructure by moving the config map and kubeconfig file generation into the EKS cluster module. It also adds a test for a progressive apply scenario involving file creation.
1 parent c2ee7a4 commit 795bb18

File tree

4 files changed

+50
-56
lines changed

4 files changed

+50
-56
lines changed

kubernetes/test-infra/eks/kubernetes-config/main.tf

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
resource "kubernetes_config_map" "name" {
2-
metadata {
3-
name = "aws-auth"
4-
namespace = "kube-system"
5-
}
6-
7-
data = {
8-
mapRoles = join(
9-
"\n",
10-
formatlist(local.mapped_role_format, var.k8s_node_role_arn),
11-
)
12-
}
13-
}
14-
15-
# Optional: this kubeconfig file is only used for manual CLI access to the cluster.
16-
resource "null_resource" "generate-kubeconfig" {
17-
provisioner "local-exec" {
18-
command = "aws eks update-kubeconfig --name ${var.cluster_name} --kubeconfig ${path.root}/kubeconfig"
1+
terraform {
2+
required_providers {
3+
kubernetes = {
4+
source = "localhost/test/kubernetes"
5+
version = "9.9.9"
6+
}
7+
helm = {
8+
source = "localhost/test/helm"
9+
version = "9.9.9"
10+
}
1911
}
2012
}
2113

kubernetes/test-infra/eks/main.tf

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
terraform {
22
required_providers {
3-
kubernetes = {
3+
# This is the locally compiled version of the provider, based on the current branch.
4+
kubernetes-local = {
45
source = "localhost/test/kubernetes"
56
version = "9.9.9"
67
}
8+
# The following block configures the latest released version of the provider, which is needed for the EKS cluster module.
9+
# This configuration is a work-around, because required_providers blocks are not inherited by sub-modules.
10+
# A "required_providers" block needs to be added to all sub-modules in order to use a custom "source" and "version".
11+
# Otherwise, the sub-module will use defaults, which in our case means an empty provider config.
12+
# https://github.com/hashicorp/terraform/issues/27361
13+
kubernetes-released = {
14+
source = "hashicorp/kubernetes"
15+
version = ">= 2.0.2"
16+
}
717
helm = {
818
source = "localhost/test/helm"
919
version = "9.9.9"
@@ -25,7 +35,7 @@ data "aws_eks_cluster" "default" {
2535
# on the system running terraform, either in $PATH as shown below, or in another location, which can be
2636
# specified in the `command`.
2737
# See the commented provider blocks below for alternative configuration options.
28-
provider "kubernetes" {
38+
provider "kubernetes-released" {
2939
host = data.aws_eks_cluster.default.endpoint
3040
cluster_ca_certificate = base64decode(data.aws_eks_cluster.default.certificate_authority[0].data)
3141
exec {
@@ -35,15 +45,12 @@ provider "kubernetes" {
3545
}
3646
}
3747

38-
# This configuration is also valid, but the token may expire during long-running applies.
39-
# data "aws_eks_cluster_auth" "default" {
40-
# name = module.cluster.cluster_id
41-
#}
42-
#provider "kubernetes" {
43-
# host = data.aws_eks_cluster.default.endpoint
44-
# cluster_ca_certificate = base64decode(data.aws_eks_cluster.default.certificate_authority[0].data)
45-
# token = data.aws_eks_cluster_auth.default.token
46-
#}
48+
# This tests a progressive apply scenario where the kubeconfig is created in the same apply as Kubernetes resources.
49+
# It should alert us to issues like this one before they're released.
50+
# https://github.com/hashicorp/terraform-provider-kubernetes/issues/1142
51+
provider "kubernetes-local" {
52+
config_path = module.cluster.kubeconfig_filename
53+
}
4754

4855
provider "helm" {
4956
kubernetes {
@@ -58,35 +65,38 @@ provider "helm" {
5865
}
5966

6067
provider "aws" {
61-
region = var.region
6268
}
6369

6470
module "vpc" {
6571
source = "./vpc"
6672
}
6773

6874
module "cluster" {
75+
providers = {kubernetes = kubernetes-released}
6976
source = "terraform-aws-modules/eks/aws"
7077
version = "14.0.0"
7178

7279
vpc_id = module.vpc.vpc_id
7380
subnets = module.vpc.subnets
7481

75-
cluster_name = module.vpc.cluster_name
76-
cluster_version = var.kubernetes_version
77-
manage_aws_auth = false # Managed in ./kubernetes-config/main.tf instead.
78-
# This kubeconfig expires in 15 minutes, so we'll use an exec block instead.
79-
# See ./kubernetes-config/main.tf provider block for details.
80-
write_kubeconfig = false
82+
cluster_name = module.vpc.cluster_name
83+
cluster_version = var.kubernetes_version
84+
manage_aws_auth = true
85+
write_kubeconfig = true
8186

87+
# See this file for more options
88+
# https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/local.tf#L28
8289
workers_group_defaults = {
8390
root_volume_type = "gp2"
8491
}
92+
8593
worker_groups = [
8694
{
87-
instance_type = var.workers_type
88-
asg_desired_capacity = var.workers_count
95+
name = module.vpc.cluster_name
96+
instance_type = "m4.large"
97+
asg_min_size = 1
8998
asg_max_size = 4
99+
asg_desired_capacity = 2
90100
},
91101
]
92102

@@ -96,6 +106,7 @@ module "cluster" {
96106
}
97107

98108
module "kubernetes-config" {
109+
providers = {kubernetes = kubernetes-local}
99110
cluster_name = module.cluster.cluster_id # creates dependency on cluster creation
100111
source = "./kubernetes-config"
101112
k8s_node_role_arn = module.cluster.worker_iam_role_arn
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
1-
#
2-
# Variables Configuration
3-
#
4-
variable "region" {
5-
default = "us-west-1"
6-
type = string
7-
}
8-
91
variable "kubernetes_version" {
102
type = string
113
default = "1.18"
124
}
13-
14-
variable "workers_count" {
15-
default = 2
16-
}
17-
18-
variable "workers_type" {
19-
type = string
20-
default = "m4.large"
21-
}

kubernetes/test-infra/eks/vpc/main.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
#
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "3.22.0"
6+
}
7+
}
8+
}
9+
210
# VPC Resources
311
# * VPC
412
# * Subnets

0 commit comments

Comments
 (0)