mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-19 19:01:42 +00:00
67092d40be
The kubernetes provider in the eks-quickstart, eks-addons-access and eks-https-ingress workbooks was configured from the managed cluster resource (host = aws_eks_cluster.this.endpoint). On destroy the endpoint resolved empty, the provider fell back to localhost:80, and refreshing kubernetes_deployment_v1.hello / kubernetes_service_v1.hello failed with 'dial tcp [::1]:80: connect: connection refused'. Move the kubernetes provider and the hello Deployment + Service into a nested workloads/ root module with its own state. It reads cluster identity from the parent via terraform_remote_state and the live endpoint/CA from a data aws_eks_cluster source, so the provider is only ever configured while the cluster exists. Apply parent then workloads; destroy workloads then parent.
149 lines
3.6 KiB
Terraform
149 lines
3.6 KiB
Terraform
# Demo workload for eks-https-ingress — separate root module / state.
|
|
#
|
|
# Kept apart from the cluster root module on purpose: the kubernetes provider
|
|
# below reads the cluster endpoint from a *live* data source, so it is only ever
|
|
# configured while the cluster exists. Destroy this module before the parent and
|
|
# the provider never falls back to localhost.
|
|
#
|
|
# The ALB target group in the parent forwards to the workers' NodePort; this
|
|
# module is what actually answers on that port.
|
|
#
|
|
# Usage:
|
|
# cd spinifex/docs/terraform-workbooks/eks-https-ingress
|
|
# tofu init && tofu apply # parent: cluster + ALB + infra
|
|
# cd workloads && tofu init && tofu apply # this module: demo app
|
|
# # teardown is the reverse — destroy here first, then the parent.
|
|
|
|
terraform {
|
|
required_version = ">= 1.6.0"
|
|
|
|
required_providers {
|
|
aws = {
|
|
source = "hashicorp/aws"
|
|
version = ">= 5.40, < 6.0"
|
|
}
|
|
kubernetes = {
|
|
source = "hashicorp/kubernetes"
|
|
version = ">= 2.20"
|
|
}
|
|
}
|
|
}
|
|
|
|
variable "spinifex_endpoint" {
|
|
type = string
|
|
default = "https://127.0.0.1:9999"
|
|
}
|
|
|
|
provider "aws" {
|
|
region = data.terraform_remote_state.infra.outputs.region
|
|
|
|
endpoints {
|
|
ec2 = var.spinifex_endpoint
|
|
iam = var.spinifex_endpoint
|
|
sts = var.spinifex_endpoint
|
|
eks = var.spinifex_endpoint
|
|
}
|
|
|
|
skip_credentials_validation = true
|
|
skip_metadata_api_check = true
|
|
skip_requesting_account_id = true
|
|
skip_region_validation = true
|
|
}
|
|
|
|
# Cluster identity comes from the parent module's state; the live endpoint/CA
|
|
# come from a data source so the provider is only configured while the cluster
|
|
# is up.
|
|
data "terraform_remote_state" "infra" {
|
|
backend = "local"
|
|
|
|
config = {
|
|
path = "../terraform.tfstate"
|
|
}
|
|
}
|
|
|
|
locals {
|
|
cluster_name = data.terraform_remote_state.infra.outputs.cluster_name
|
|
region = data.terraform_remote_state.infra.outputs.region
|
|
node_port = data.terraform_remote_state.infra.outputs.node_port
|
|
replicas = data.terraform_remote_state.infra.outputs.node_desired_size
|
|
}
|
|
|
|
data "aws_eks_cluster" "this" {
|
|
name = local.cluster_name
|
|
}
|
|
|
|
provider "kubernetes" {
|
|
host = data.aws_eks_cluster.this.endpoint
|
|
cluster_ca_certificate = base64decode(data.aws_eks_cluster.this.certificate_authority[0].data)
|
|
|
|
exec {
|
|
api_version = "client.authentication.k8s.io/v1beta1"
|
|
command = "aws"
|
|
args = ["eks", "get-token", "--cluster-name", local.cluster_name, "--region", local.region]
|
|
}
|
|
}
|
|
|
|
resource "kubernetes_deployment_v1" "hello" {
|
|
metadata {
|
|
name = "hello"
|
|
namespace = "default"
|
|
labels = { app = "hello" }
|
|
}
|
|
|
|
spec {
|
|
replicas = local.replicas
|
|
|
|
selector {
|
|
match_labels = { app = "hello" }
|
|
}
|
|
|
|
template {
|
|
metadata {
|
|
labels = { app = "hello" }
|
|
}
|
|
|
|
spec {
|
|
container {
|
|
name = "hello"
|
|
image = "nginxdemos/hello"
|
|
|
|
port {
|
|
container_port = 80
|
|
}
|
|
}
|
|
|
|
# Spread one replica per node so a refresh visibly hits different nodes.
|
|
topology_spread_constraint {
|
|
max_skew = 1
|
|
topology_key = "kubernetes.io/hostname"
|
|
when_unsatisfiable = "ScheduleAnyway"
|
|
|
|
label_selector {
|
|
match_labels = { app = "hello" }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "kubernetes_service_v1" "hello" {
|
|
metadata {
|
|
name = "hello"
|
|
namespace = "default"
|
|
}
|
|
|
|
spec {
|
|
selector = { app = "hello" }
|
|
type = "NodePort"
|
|
|
|
port {
|
|
port = 80
|
|
target_port = 80
|
|
node_port = local.node_port
|
|
}
|
|
}
|
|
|
|
depends_on = [kubernetes_deployment_v1.hello]
|
|
}
|