Skip to content

Commit cbd512b

Browse files
authored
Revert changes to EKS test-infra (#1225)
Revert changes to EKS test-infra back to commit 078e9db. Add experimental test-infra for testing provider authentication issues.
1 parent 0ff498a commit cbd512b

File tree

12 files changed

+424
-75
lines changed

12 files changed

+424
-75
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# EKS test infrastructure
2+
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:
6+
7+
- `AWS_ACCESS_KEY_ID`
8+
- `AWS_SECRET_ACCESS_KEY`
9+
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`.
11+
12+
Ensure that `KUBE_CONFIG_PATH` and `KUBE_CONFIG_PATHS` environment variables are NOT set, as they will interfere with the cluster build.
13+
14+
```
15+
unset KUBE_CONFIG_PATH
16+
unset KUBE_CONFIG_PATHS
17+
```
18+
19+
To install the EKS cluster using default values, run terraform init and apply from the directory containing this README.
20+
21+
```
22+
terraform init
23+
terraform apply
24+
```
25+
26+
## Kubeconfig for manual CLI access
27+
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:
29+
30+
```
31+
terraform apply
32+
export KUBECONFIG=$(terraform output -raw kubeconfig_path)
33+
kubectl get pods -n test
34+
```
35+
36+
## Optional variables
37+
38+
The Kubernetes version can be specified at apply time:
39+
40+
```
41+
terraform apply -var=kubernetes_version=1.18
42+
```
43+
44+
See https://docs.aws.amazon.com/eks/latest/userguide/platform-versions.html for currently available versions.
45+
46+
47+
### Worker node count and instance type
48+
49+
The number of worker nodes, and the instance type, can be specified at apply time:
50+
51+
```
52+
terraform apply -var=workers_count=4 -var=workers_type=m4.xlarge
53+
```
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
terraform {
2+
required_providers {
3+
kubernetes-local = {
4+
source = "localhost/test/kubernetes"
5+
version = "9.9.9"
6+
}
7+
helm = {
8+
source = "localhost/test/helm"
9+
version = "9.9.9"
10+
}
11+
}
12+
}
13+
14+
# For this resource, we need to explicitly establish the dependency on the cluster API, because the dependency is not yet present in this file.
15+
# https://github.com/terraform-aws-modules/terraform-aws-eks/blob/31ad394dbc61390dc46643b571249a2b670e9caa/kubectl.tf
16+
resource "kubernetes_namespace" "test" {
17+
depends_on = [var.cluster_name]
18+
provider = kubernetes-local
19+
metadata {
20+
name = "test"
21+
}
22+
}
23+
24+
resource helm_release nginx_ingress {
25+
wait = true
26+
timeout = 600
27+
28+
name = "ingress-nginx"
29+
30+
repository = "https://kubernetes.github.io/ingress-nginx"
31+
chart = "ingress-nginx"
32+
version = "v3.24.0"
33+
34+
set {
35+
name = "controller.updateStrategy.rollingUpdate.maxUnavailable"
36+
value = "1"
37+
}
38+
set {
39+
name = "controller.replicaCount"
40+
value = "2"
41+
}
42+
set_sensitive {
43+
name = "controller.maxmindLicenseKey"
44+
value = "testSensitiveValue"
45+
}
46+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
terraform {
2+
required_providers {
3+
# This is the locally compiled version of the provider, based on the current branch.
4+
kubernetes-local = {
5+
source = "localhost/test/kubernetes"
6+
version = "9.9.9"
7+
}
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 = {
14+
source = "hashicorp/kubernetes"
15+
version = ">= 2.0.2"
16+
}
17+
helm = {
18+
source = "localhost/test/helm"
19+
version = "9.9.9"
20+
}
21+
aws = {
22+
source = "hashicorp/aws"
23+
version = "3.22.0"
24+
}
25+
}
26+
}
27+
28+
data "aws_eks_cluster" "default" {
29+
name = module.cluster.cluster_id
30+
}
31+
32+
data "aws_eks_cluster_auth" "default" {
33+
name = module.cluster.cluster_id
34+
}
35+
36+
# Test exec plugin based auth.
37+
provider "kubernetes" {
38+
host = data.aws_eks_cluster.default.endpoint
39+
cluster_ca_certificate = base64decode(data.aws_eks_cluster.default.certificate_authority[0].data)
40+
exec {
41+
api_version = "client.authentication.k8s.io/v1alpha1"
42+
args = ["eks", "get-token", "--cluster-name", module.vpc.cluster_name]
43+
command = "aws"
44+
}
45+
}
46+
47+
# This tests a progressive apply scenario where the kubeconfig is created in the same apply as Kubernetes resources.
48+
# It should alert us to issues like this one before they're released.
49+
# https://github.com/hashicorp/terraform-provider-kubernetes/issues/1142
50+
provider "kubernetes-local" {
51+
config_path = module.cluster.kubeconfig_filename
52+
}
53+
54+
# Test token data source based auth.
55+
provider "helm" {
56+
experiments {
57+
manifest = true
58+
}
59+
kubernetes {
60+
host = data.aws_eks_cluster.default.endpoint
61+
cluster_ca_certificate = base64decode(data.aws_eks_cluster.default.certificate_authority[0].data)
62+
token = data.aws_eks_cluster_auth.default.token
63+
}
64+
}
65+
66+
provider "aws" {
67+
}
68+
69+
module "vpc" {
70+
source = "./vpc"
71+
}
72+
73+
module "cluster" {
74+
source = "terraform-aws-modules/eks/aws"
75+
version = "14.0.0"
76+
77+
vpc_id = module.vpc.vpc_id
78+
subnets = module.vpc.subnets
79+
80+
cluster_name = module.vpc.cluster_name
81+
cluster_version = var.kubernetes_version
82+
manage_aws_auth = true
83+
write_kubeconfig = true
84+
85+
# See this file for more options
86+
# https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/local.tf#L28
87+
workers_group_defaults = {
88+
root_volume_type = "gp2"
89+
}
90+
91+
worker_groups = [
92+
{
93+
name = module.vpc.cluster_name
94+
instance_type = "m4.large"
95+
asg_min_size = 1
96+
asg_max_size = 4
97+
asg_desired_capacity = 2
98+
},
99+
]
100+
101+
tags = {
102+
environment = "test"
103+
}
104+
}
105+
106+
module "kubernetes-config" {
107+
cluster_name = module.cluster.cluster_id
108+
source = "./kubernetes-config"
109+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
output "kubeconfig_path" {
2+
value = abspath(module.cluster.kubeconfig_filename)
3+
}
4+
5+
output "cluster_name" {
6+
value = module.vpc.cluster_name
7+
}
8+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "kubernetes_version" {
2+
type = string
3+
default = "1.18"
4+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "3.22.0"
6+
}
7+
}
8+
}
9+
10+
# VPC Resources
11+
# * VPC
12+
# * Subnets
13+
# * Internet Gateway
14+
# * Route Table
15+
#
16+
# Using these data sources allows the configuration to be
17+
# generic for any region.
18+
data "aws_region" "current" {
19+
}
20+
21+
data "aws_availability_zones" "available" {
22+
}
23+
24+
resource "random_id" "cluster_name" {
25+
byte_length = 2
26+
prefix = "k8s-acc-"
27+
}
28+
29+
resource "aws_vpc" "k8s-acc" {
30+
cidr_block = "10.0.0.0/16"
31+
enable_dns_support = true
32+
enable_dns_hostnames = true
33+
tags = {
34+
"Name" = "terraform-eks-k8s-acc-node"
35+
"kubernetes.io/cluster/${random_id.cluster_name.hex}" = "shared"
36+
}
37+
}
38+
39+
resource "aws_subnet" "k8s-acc" {
40+
count = 2
41+
42+
availability_zone = data.aws_availability_zones.available.names[count.index]
43+
cidr_block = "10.0.${count.index}.0/24"
44+
vpc_id = aws_vpc.k8s-acc.id
45+
map_public_ip_on_launch = true
46+
47+
tags = {
48+
"Name" = "terraform-eks-k8s-acc-node"
49+
"kubernetes.io/cluster/${random_id.cluster_name.hex}" = "shared"
50+
"kubernetes.io/role/elb" = 1
51+
}
52+
}
53+
54+
resource "aws_internet_gateway" "k8s-acc" {
55+
vpc_id = aws_vpc.k8s-acc.id
56+
57+
tags = {
58+
Name = "terraform-eks-k8s-acc"
59+
}
60+
}
61+
62+
resource "aws_route_table" "k8s-acc" {
63+
vpc_id = aws_vpc.k8s-acc.id
64+
65+
route {
66+
cidr_block = "0.0.0.0/0"
67+
gateway_id = aws_internet_gateway.k8s-acc.id
68+
}
69+
}
70+
71+
resource "aws_route_table_association" "k8s-acc" {
72+
count = 2
73+
74+
subnet_id = aws_subnet.k8s-acc[count.index].id
75+
route_table_id = aws_route_table.k8s-acc.id
76+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
output "vpc_id" {
2+
value = aws_vpc.k8s-acc.id
3+
}
4+
5+
output "subnets" {
6+
value = aws_subnet.k8s-acc.*.id
7+
}
8+
9+
output "cluster_name" {
10+
value = random_id.cluster_name.hex
11+
}
12+

0 commit comments

Comments
 (0)