|
| 1 | +locals { |
| 2 | + name = "example-${basename(path.cwd)}" |
| 3 | + name_with_random_suffix = "${local.name}-${random_integer.operator_name_suffix.result}" |
| 4 | + |
| 5 | + aws_tags = { |
| 6 | + Name = local.name |
| 7 | + } |
| 8 | + |
| 9 | + # Modify these to use your own VPC |
| 10 | + vpc_id = module.vpc.vpc_id |
| 11 | + subnet_ids = module.vpc.private_subnets |
| 12 | + |
| 13 | + # EKS cluster configuration |
| 14 | + cluster_name = local.name_with_random_suffix |
| 15 | + cluster_version = data.aws_eks_cluster_versions.latest.cluster_versions[0].cluster_version |
| 16 | + node_instance_type = "t3.medium" |
| 17 | + desired_size = 2 |
| 18 | + max_size = 2 |
| 19 | + min_size = 1 |
| 20 | + |
| 21 | + # Tailscale Operator configuration |
| 22 | + namespace_name = "tailscale" |
| 23 | + operator_name = local.name_with_random_suffix |
| 24 | + operator_version = "1.92.4" |
| 25 | + tailscale_oauth_client_id = var.tailscale_oauth_client_id |
| 26 | + tailscale_oauth_client_secret = var.tailscale_oauth_client_secret |
| 27 | + |
| 28 | + enable_ha_proxy_service = true |
| 29 | + ha_proxy_service_name = "${helm_release.tailscale_operator.name}-ha" |
| 30 | +} |
| 31 | + |
| 32 | +# This isn't required but helps avoid conflicts and Let's Encrypt throttling to make testing and iterating easier. |
| 33 | +resource "random_integer" "operator_name_suffix" { |
| 34 | + min = 100 |
| 35 | + max = 999 |
| 36 | +} |
| 37 | + |
| 38 | +# Remove this to use your own VPC. |
| 39 | +module "vpc" { |
| 40 | + source = "../internal-modules/aws-vpc" |
| 41 | + |
| 42 | + name = local.name |
| 43 | + tags = local.aws_tags |
| 44 | +} |
| 45 | + |
| 46 | +module "eks" { |
| 47 | + source = "terraform-aws-modules/eks/aws" |
| 48 | + version = ">= 21.0, < 22.0" |
| 49 | + |
| 50 | + name = local.cluster_name |
| 51 | + kubernetes_version = local.cluster_version |
| 52 | + |
| 53 | + tags = local.aws_tags |
| 54 | + |
| 55 | + addons = { |
| 56 | + coredns = {} |
| 57 | + eks-pod-identity-agent = { |
| 58 | + before_compute = true |
| 59 | + } |
| 60 | + kube-proxy = {} |
| 61 | + vpc-cni = { |
| 62 | + before_compute = true |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + # Once the Tailscale operator is installed, `endpoint_public_access` can be disabled. |
| 67 | + # This is left enabled for the sake of easy adoption. |
| 68 | + endpoint_public_access = true |
| 69 | + |
| 70 | + # Optional: Adds the current caller identity as an administrator via cluster access entry |
| 71 | + enable_cluster_creator_admin_permissions = true |
| 72 | + |
| 73 | + vpc_id = local.vpc_id |
| 74 | + subnet_ids = local.subnet_ids |
| 75 | + |
| 76 | + eks_managed_node_groups = { |
| 77 | + main = { |
| 78 | + # Truncate the node group name to 20 characters to comply with AWS/EKS |
| 79 | + # node group naming length constraints. |
| 80 | + name = substr(local.name, 0, 20) |
| 81 | + instance_types = [local.node_instance_type] |
| 82 | + |
| 83 | + labels = {} |
| 84 | + |
| 85 | + launch_template_name = local.name |
| 86 | + launch_template_tags = local.aws_tags |
| 87 | + |
| 88 | + desired_size = local.desired_size |
| 89 | + max_size = local.max_size |
| 90 | + min_size = local.min_size |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +resource "kubernetes_namespace_v1" "tailscale_operator" { |
| 96 | + provider = kubernetes.this |
| 97 | + |
| 98 | + metadata { |
| 99 | + name = local.namespace_name |
| 100 | + labels = { |
| 101 | + "pod-security.kubernetes.io/enforce" = "privileged" |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + depends_on = [ |
| 106 | + module.eks, |
| 107 | + ] |
| 108 | +} |
| 109 | + |
| 110 | +# |
| 111 | +# https://tailscale.com/kb/1236/kubernetes-operator#helm |
| 112 | +# |
| 113 | +resource "helm_release" "tailscale_operator" { |
| 114 | + provider = helm.this |
| 115 | + |
| 116 | + name = local.operator_name |
| 117 | + namespace = kubernetes_namespace_v1.tailscale_operator.metadata[0].name |
| 118 | + |
| 119 | + repository = "https://pkgs.tailscale.com/helmcharts" |
| 120 | + chart = "tailscale-operator" |
| 121 | + version = local.operator_version |
| 122 | + |
| 123 | + values = [ |
| 124 | + yamlencode({ |
| 125 | + operatorConfig = { |
| 126 | + image = { |
| 127 | + tag = "v${local.operator_version}" |
| 128 | + } |
| 129 | + hostname = local.operator_name |
| 130 | + } |
| 131 | + apiServerProxyConfig = { |
| 132 | + mode = "true" |
| 133 | + allowImpersonation = "true" |
| 134 | + } |
| 135 | + }) |
| 136 | + ] |
| 137 | + |
| 138 | + set_sensitive = [ |
| 139 | + { |
| 140 | + name = "oauth.clientId" |
| 141 | + value = local.tailscale_oauth_client_id |
| 142 | + }, |
| 143 | + { |
| 144 | + name = "oauth.clientSecret" |
| 145 | + value = local.tailscale_oauth_client_secret |
| 146 | + }, |
| 147 | + ] |
| 148 | + |
| 149 | + depends_on = [ |
| 150 | + module.eks, |
| 151 | + ] |
| 152 | +} |
| 153 | + |
| 154 | +# |
| 155 | +# https://tailscale.com/kb/1437/kubernetes-operator-api-server-proxy#configuring-a-high-availability-api-server-proxy |
| 156 | +# |
| 157 | +# Remove or comment out the `null_resource` provisioners that deploy `tailscale-api-server-ha-proxy.yaml` for the |
| 158 | +# high availability API server proxy to run from other platforms. |
| 159 | +# |
| 160 | +resource "null_resource" "kubectl_ha_proxy" { |
| 161 | + count = local.enable_ha_proxy_service ? 1 : 0 |
| 162 | + |
| 163 | + triggers = { |
| 164 | + region = data.aws_region.current.region |
| 165 | + cluster_arn = module.eks.cluster_arn |
| 166 | + cluster_name = module.eks.cluster_name |
| 167 | + ha_proxy_service_name = local.ha_proxy_service_name |
| 168 | + } |
| 169 | + |
| 170 | + # |
| 171 | + # Create provisioners |
| 172 | + # |
| 173 | + provisioner "local-exec" { |
| 174 | + command = "aws eks update-kubeconfig --region ${self.triggers.region} --name ${self.triggers.cluster_name}" |
| 175 | + } |
| 176 | + provisioner "local-exec" { |
| 177 | + command = "HA_PROXY_SERVICE_NAME=${self.triggers.ha_proxy_service_name} envsubst < ${path.module}/tailscale-api-server-ha-proxy.yaml | kubectl apply --context=${self.triggers.cluster_arn} -f -" |
| 178 | + } |
| 179 | + |
| 180 | + # |
| 181 | + # Destroy provisioners |
| 182 | + # |
| 183 | + provisioner "local-exec" { |
| 184 | + when = destroy |
| 185 | + command = "aws eks update-kubeconfig --region ${self.triggers.region} --name ${self.triggers.cluster_name}" |
| 186 | + } |
| 187 | + provisioner "local-exec" { |
| 188 | + when = destroy |
| 189 | + command = "HA_PROXY_SERVICE_NAME=${self.triggers.ha_proxy_service_name} envsubst < ${path.module}/tailscale-api-server-ha-proxy.yaml | kubectl delete --context=${self.triggers.cluster_arn} -f -" |
| 190 | + } |
| 191 | + |
| 192 | + depends_on = [ |
| 193 | + module.vpc, # prevent network changes before this finishes during a destroy |
| 194 | + helm_release.tailscale_operator, |
| 195 | + ] |
| 196 | +} |
0 commit comments