Skip to content

Commit

Permalink
conditional the provider DATA
Browse files Browse the repository at this point in the history
  • Loading branch information
morga471 committed Feb 12, 2025
1 parent 40a54f8 commit cd11a92
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 142 deletions.
25 changes: 0 additions & 25 deletions lab/_envcommon/helm-provider.hcl
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
generate "helm-provider" {
path = "helm-provider.tf"
if_exists = "overwrite"
contents = <<-EOF
%{ if startswith(local.module_name, "tfmod-eks-") ~}
provider "helm" {
kubernetes {
host = try(data.aws_eks_cluster.this[0].endpoint, "")
cluster_ca_certificate = try(base64decode(data.aws_eks_cluster.this[0].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.aws_region]
}
}
}
data "aws_eks_cluster" "this" {
count = var.create_eks ? 1 : 0
name = local.cluster_name
}
%{ endif }
EOF
}
dependency "eks" {
config_path = "${get_original_terragrunt_dir()}/../eks"
# mock_outputs_allowed_terraform_commands = ["plan", "validate"]
Expand Down Expand Up @@ -56,4 +32,3 @@ generate "helm_provider" {
%{ endif ~}
EOF
}
>>>>>>> 354afd7 (make providers great again)
68 changes: 16 additions & 52 deletions lab/_envcommon/kube-provider.hcl
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
generate "kube-provider" {
path = "kube-provider.tf"
if_exists = "overwrite"
contents = <<-EOF
%{ if startswith(local.module_name, "tfmod-eks-") ~}
provider "kubernetes" {
host = try(data.aws_eks_cluster.this[0].endpoint, "")
cluster_ca_certificate = try(base64decode(data.aws_eks_cluster.this[0].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.aws_region]
}
}
data "aws_eks_cluster" "this" {
count = var.create_eks ? 1 : 0
name = local.cluster_name
}
%{ endif }
EOF
%{if cluster_name != "a-cluster-name"~}
data "aws_eks_cluster" "kube" {
name = "${cluster_name}"
}
dependency "eks" {
config_path = "${get_original_terragrunt_dir()}/../eks"
# mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs = {
cluster_name = "a-cluster-name"
}
data "aws_eks_cluster_auth" "kube" {
name = "${cluster_name}"
}

inputs = {
cluster_name = dependency.eks.outputs.cluster_name
}

# Generate a k8s provider block
generate "kube_provider" {
path = "kube-provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<-EOF
%{if "${dependency.eks.outputs.cluster_name}" != "a-cluster-name" ~}
data "aws_eks_cluster" "kube" {
name = "${dependency.eks.outputs.cluster_name}"
}
data "aws_eks_cluster_auth" "kube" {
name = "${dependency.eks.outputs.cluster_name}"
}
provider "kubernetes" {
host = data.aws_eks_cluster.kube.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.kube.certificate_authority[0].data)
token = data.aws_eks_cluster_auth.kube.token
}
%{ endif ~}
EOF
%{endif~}
provider "kubernetes" {
%{if cluster_name != "a-cluster-name"~}
host = data.aws_eks_cluster.kube.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.kube.certificate_authority[0].data)
token = data.aws_eks_cluster_auth.kube.token
%{else~}
host = ""
cluster_ca_certificate = ""
token = ""
%{endif~}
}
>>>>>>> 354afd7 (make providers great again)
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,7 @@ dependency "eks" {
}

dependencies {
paths = ["../eks-config"]
}

locals {
helm = read_terragrunt_config(find_in_parent_folders("./_envcommon/helm-provider.hcl"))
kube = read_terragrunt_config(find_in_parent_folders("./_envcommon/kube-provider.hcl"))
}

generate "helm" {
path = local.helm.generate.helm_provider.path
if_exists = local.helm.generate.helm_provider.if_exists
contents = local.helm.generate.helm_provider.contents
}

generate "kube" {
path = local.kube.generate.kube_provider.path
if_exists = local.kube.generate.kube_provider.if_exists
contents = local.kube.generate.kube_provider.contents
paths = ["../eks", "../eks-config"]
}

inputs = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ terraform {

dependency "eks" {
config_path = "../eks"
# mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs = {
cluster_certificate_authority_data = [{ data = "THISISAVERYLONGCERTSTRINGTHATGOESHEREFORSURENODYEP" }]
cluster_endpoint = "https://12345ABCDEE42BF9C24D4C362D1DC.sk1.us-gov-east-1.eks.amazonaws.com"
Expand All @@ -24,6 +24,10 @@ dependency "eks" {
}
}

dependencies {
paths = ["../eks", "../eks-karpenter"]
}

locals {
helm = read_terragrunt_config(find_in_parent_folders("./_envcommon/helm-provider.hcl"))
kube = read_terragrunt_config(find_in_parent_folders("./_envcommon/kube-provider.hcl"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependency "eks" {

dependency "istio" {
config_path = "../eks-istio"
mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs = {
istio_ingress_lb = {
dns_name = "a1111111111111111111111111111111-2bbbbbbbbbbbbbbb.elb.us-gov-east-1.amazonaws.com"
Expand All @@ -25,6 +26,11 @@ dependency "istio" {
}
}

dependencies {
paths = ["../eks", "../eks-config", "../eks-istio"]
}


locals {
helm = read_terragrunt_config(find_in_parent_folders("./_envcommon/helm-provider.hcl"))
kube = read_terragrunt_config(find_in_parent_folders("./_envcommon/kube-provider.hcl"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependency "eks" {
}

dependencies {
paths = ["../eks-karpenter"]
paths = ["../eks", "../eks-karpenter", "../eks-config"]
}

locals {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ terraform {
}

dependency "eks" {
config_path = "../eks"
config_path = "../eks"
mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs = {
cluster_endpoint = "https://0000000000000000AAAAAAAAAAAAAAAA.sk1.us-gov-east-1.eks.amazonaws.com"
Expand All @@ -19,27 +19,6 @@ dependency "eks" {
}
}

dependencies {
paths = ["../eks-config"]
}

locals {
helm = read_terragrunt_config(find_in_parent_folders("./_envcommon/helm-provider.hcl"))
kube = read_terragrunt_config(find_in_parent_folders("./_envcommon/kube-provider.hcl"))
}

generate "helm" {
path = local.helm.generate.helm_provider.path
if_exists = local.helm.generate.helm_provider.if_exists
contents = local.helm.generate.helm_provider.contents
}

generate "kube" {
path = local.kube.generate.kube_provider.path
if_exists = local.kube.generate.kube_provider.if_exists
contents = local.kube.generate.kube_provider.contents
}

inputs = {
cluster_endpoint = dependency.eks.outputs.cluster_endpoint
cluster_name = dependency.eks.outputs.cluster_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ dependency "eks" {
}
}

dependency "eks_config" {
config_path = "../eks-config"
skip_outputs = true
}

locals {
helm = read_terragrunt_config(find_in_parent_folders("./_envcommon/helm-provider.hcl"))
kube = read_terragrunt_config(find_in_parent_folders("./_envcommon/kube-provider.hcl"))
}

generate "helm" {
path = local.helm.generate.helm_provider.path
if_exists = local.helm.generate.helm_provider.if_exists
contents = local.helm.generate.helm_provider.contents
}

generate "kube" {
path = local.kube.generate.kube_provider.path
if_exists = local.kube.generate.kube_provider.if_exists
contents = local.kube.generate.kube_provider.contents
}

inputs = {
profile = include.root.inputs.aws_profile
cluster_name = dependency.eks.outputs.cluster_name
Expand Down
6 changes: 5 additions & 1 deletion lab/root.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ locals {
state_table_name = local.common_vars.locals.state_table_name
terraform = local.cluster_vars.locals.terraform
terragrunt = local.cluster_vars.locals.terragrunt
<<<<<<< HEAD
module_name = get_terragrunt_dir()
create_eks = get_env("TERRAGRUNT_CREATE_EKS", "true")
=======
module_name = trimprefix(replace(run_cmd("realpath", get_original_terragrunt_dir()), dirname(get_repo_root()), ""), "/")
>>>>>>> f019116 (conditional the provider DATA)
}
# Configure Terragrunt to automatically store tfstate files in an S3 bucket
Expand Down Expand Up @@ -77,7 +81,7 @@ provider "aws" {
profile = "${local.aws_profile}"
default_tags {
tags = {
clsuter_name = "${local.cluster_name}"
cluster_name = "${local.cluster_name}"
module_name = "${local.module_name}"
created_by = "${local.creator}"
created_for = "${local.creator}"
Expand Down

0 comments on commit cd11a92

Please sign in to comment.