|
| 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 | +} |
0 commit comments