Skip to content

Commit 078e9db

Browse files
authored
Replace EKS test-infra with code from example (#1192)
Replace EKS test-infra with code from example.
1 parent 10d5028 commit 078e9db

File tree

15 files changed

+189
-53631
lines changed

15 files changed

+189
-53631
lines changed

.go-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.16
1+
1.16.0
Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,57 @@
1-
# Amazon EKS Clusters
1+
# EKS test infrastructure
22

3-
You will need the standard AWS environment variables to be set, e.g.
3+
This directory contains files used for testing the Kubernetes provider in our internal CI system. See the [examples](https://github.com/hashicorp/terraform-provider-kubernetes/tree/master/_examples/eks) directory instead, if you're looking for example code.
4+
5+
To run this test infrastructure, you will need the following environment variables to be set:
46

57
- `AWS_ACCESS_KEY_ID`
68
- `AWS_SECRET_ACCESS_KEY`
79

8-
See [AWS Provider docs](https://www.terraform.io/docs/providers/aws/index.html#configuration-reference) for more details about these variables
9-
and alternatives, like `AWS_PROFILE`.
10+
See [AWS Provider docs](https://www.terraform.io/docs/providers/aws/index.html#configuration-reference) for more details about these variables and alternatives, like `AWS_PROFILE`.
1011

11-
## Versions
12+
Ensure that `KUBE_CONFIG_PATH` and `KUBE_CONFIG_PATHS` environment variables are NOT set, as they will interfere with the cluster build.
1213

13-
You can set the desired version of Kubernetes via the `kubernetes_version` TF variable.
14+
```
15+
unset KUBE_CONFIG_PATH
16+
unset KUBE_CONFIG_PATHS
17+
```
1418

15-
See https://docs.aws.amazon.com/eks/latest/userguide/platform-versions.html for currently available versions.
19+
To install the EKS cluster using default values, run terraform init and apply from the directory containing this README.
1620

17-
You can set the desired version of Kubernetes via the `kubernetes_version` TF variable, like this:
1821
```
19-
export TF_VAR_kubernetes_version="1.11"
22+
terraform init
23+
terraform apply
2024
```
21-
Alternatively you can pass it to the `apply` command line, like below.
2225

23-
## Worker node count and instance type
26+
## Kubeconfig for manual CLI access
2427

25-
You can control the amount of worker nodes in the cluster as well as their machine type, using the following variables:
28+
The token contained in the kubeconfig expires in 15 minutes. The token can be refreshed by running `terraform apply` again. Export the KUBECONFIG to manually access the cluster:
2629

27-
- `TF_VAR_workers_count`
28-
- `TF_VAR_workers_type`
30+
```
31+
terraform apply
32+
export KUBECONFIG=$(terraform output -raw kubeconfig_path)
33+
kubectl get pods -n test
34+
```
2935

30-
Export values for them or pass them to the apply command line.
36+
## Optional variables
3137

32-
## Build the cluster
38+
The Kubernetes version can be specified at apply time:
3339

3440
```
35-
terraform init
36-
terraform apply -var=kubernetes_version=1.11
41+
terraform apply -var=kubernetes_version=1.18
3742
```
3843

39-
## Exporting K8S variables
40-
To access the cluster you need to export the `KUBECONFIG` variable pointing to the `kubeconfig` file for the current cluster.
41-
```
42-
export KUBECONFIG="$(terraform output kubeconfig_path)"
43-
```
44+
See https://docs.aws.amazon.com/eks/latest/userguide/platform-versions.html for currently available versions.
4445

45-
Now you can access the cluster via `kubectl` and you can run acceptance tests against it.
4646

47-
To run acceptance tests, your the following command in the root of the repository.
48-
```
49-
TESTARGS="-run '^TestAcc'" make testacc
50-
```
47+
### Worker node count and instance type
48+
49+
The number of worker nodes, and the instance type, can be specified at apply time:
5150

52-
To run only a specific set of tests, you can replace `^TestAcc` with any regular expression to filter tests by name.
53-
For example, to run tests for Pod resources, you can do:
5451
```
55-
TESTARGS="-run '^TestAccKubernetesPod_'" make testacc
52+
terraform apply -var=workers_count=4 -var=workers_type=m4.xlarge
5653
```
54+
55+
## Additional configuration of EKS
56+
57+
To view all available configuration options for the EKS module used in this example, see [terraform-aws-modules/eks docs](https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/latest).
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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"
19+
}
20+
}
21+
22+
resource "kubernetes_namespace" "test" {
23+
metadata {
24+
name = "test"
25+
}
26+
}
27+
28+
resource "kubernetes_deployment" "test" {
29+
metadata {
30+
name = "test"
31+
namespace= kubernetes_namespace.test.metadata.0.name
32+
}
33+
spec {
34+
replicas = 2
35+
selector {
36+
match_labels = {
37+
app = "test"
38+
}
39+
}
40+
template {
41+
metadata {
42+
labels = {
43+
app = "test"
44+
}
45+
}
46+
spec {
47+
container {
48+
image = "nginx:1.19.4"
49+
name = "nginx"
50+
51+
resources {
52+
limits = {
53+
memory = "512M"
54+
cpu = "1"
55+
}
56+
requests = {
57+
memory = "256M"
58+
cpu = "50m"
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
resource helm_release nginx_ingress {
68+
name = "nginx-ingress-controller"
69+
70+
repository = "https://charts.bitnami.com/bitnami"
71+
chart = "nginx-ingress-controller"
72+
73+
set {
74+
name = "service.type"
75+
value = "ClusterIP"
76+
}
77+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
variable "k8s_node_role_arn" {
2+
type = string
3+
}
4+
5+
variable "cluster_name" {
6+
type = string
7+
}
8+
9+
locals {
10+
mapped_role_format = <<MAPPEDROLE
11+
- rolearn: %s
12+
username: system:node:{{EC2PrivateDNSName}}
13+
groups:
14+
- system:bootstrappers
15+
- system:nodes
16+
MAPPEDROLE
17+
18+
}

kubernetes/test-infra/eks/main.tf

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
terraform {
22
required_providers {
33
kubernetes = {
4-
source = "hashicorp/kubernetes"
5-
version = "1.13"
4+
source = "hashicorp/kubernetes"
5+
version = ">= 2.0.2"
6+
}
7+
helm = {
8+
source = "hashicorp/helm"
9+
version = ">= 2.0.2"
610
}
711
aws = {
812
source = "hashicorp/aws"
@@ -11,6 +15,48 @@ terraform {
1115
}
1216
}
1317

18+
data "aws_eks_cluster" "default" {
19+
name = module.cluster.cluster_id
20+
}
21+
22+
# This configuration relies on a plugin binary to fetch the token to the EKS cluster.
23+
# The main advantage is that the token will always be up-to-date, even when the `terraform apply` runs for
24+
# a longer time than the token TTL. The downside of this approach is that the binary must be present
25+
# on the system running terraform, either in $PATH as shown below, or in another location, which can be
26+
# specified in the `command`.
27+
# See the commented provider blocks below for alternative configuration options.
28+
provider "kubernetes" {
29+
host = data.aws_eks_cluster.default.endpoint
30+
cluster_ca_certificate = base64decode(data.aws_eks_cluster.default.certificate_authority[0].data)
31+
exec {
32+
api_version = "client.authentication.k8s.io/v1alpha1"
33+
args = ["eks", "get-token", "--cluster-name", module.vpc.cluster_name]
34+
command = "aws"
35+
}
36+
}
37+
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+
#}
47+
48+
provider "helm" {
49+
kubernetes {
50+
host = data.aws_eks_cluster.default.endpoint
51+
cluster_ca_certificate = base64decode(data.aws_eks_cluster.default.certificate_authority[0].data)
52+
exec {
53+
api_version = "client.authentication.k8s.io/v1alpha1"
54+
args = ["eks", "get-token", "--cluster-name", module.vpc.cluster_name]
55+
command = "aws"
56+
}
57+
}
58+
}
59+
1460
provider "aws" {
1561
region = var.region
1662
}
@@ -21,22 +67,26 @@ module "vpc" {
2167

2268
module "cluster" {
2369
source = "terraform-aws-modules/eks/aws"
24-
version = "v13.2.1"
70+
version = "14.0.0"
2571

2672
vpc_id = module.vpc.vpc_id
2773
subnets = module.vpc.subnets
2874

2975
cluster_name = module.vpc.cluster_name
3076
cluster_version = var.kubernetes_version
31-
manage_aws_auth = false
32-
# This kubeconfig expires in 15 minutes, so we'll use another method.
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.
3380
write_kubeconfig = false
3481

82+
workers_group_defaults = {
83+
root_volume_type = "gp2"
84+
}
3585
worker_groups = [
3686
{
3787
instance_type = var.workers_type
3888
asg_desired_capacity = var.workers_count
39-
asg_max_size = "10"
89+
asg_max_size = 4
4090
},
4191
]
4292

@@ -45,11 +95,8 @@ module "cluster" {
4595
}
4696
}
4797

48-
module "node-config" {
49-
source = "./node-config"
50-
k8s_node_role_arn = list(module.cluster.worker_iam_role_arn)
51-
cluster_ca = module.cluster.cluster_certificate_authority_data
52-
cluster_name = module.cluster.cluster_id # creates dependency on cluster creation
53-
cluster_endpoint = module.cluster.cluster_endpoint
54-
cluster_oidc_issuer_url = module.cluster.cluster_oidc_issuer_url
98+
module "kubernetes-config" {
99+
cluster_name = module.cluster.cluster_id # creates dependency on cluster creation
100+
source = "./kubernetes-config"
101+
k8s_node_role_arn = module.cluster.worker_iam_role_arn
55102
}

0 commit comments

Comments
 (0)