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 7, 2025
1 parent d344652 commit f019116
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 125 deletions.
47 changes: 17 additions & 30 deletions lab/_envcommon/helm-provider.hcl
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
dependency "eks" {
config_path = "${get_original_terragrunt_dir()}/../eks"
# mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs = {
cluster_name = "a-cluster-name"
}
%{if cluster_name != "a-cluster-name"~}
data "aws_eks_cluster" "helm" {
name = "${cluster_name}"
}

inputs = {
cluster_name = dependency.eks.outputs.cluster_name
data "aws_eks_cluster_auth" "helm" {
name = "${cluster_name}"
}

# Generate a k8s provider block
generate "helm_provider" {
path = "helm-provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<-EOF
%{if "${dependency.eks.outputs.cluster_name}" != "a-cluster-name" ~}
data "aws_eks_cluster" "helm" {
name = "${dependency.eks.outputs.cluster_name}"
}
data "aws_eks_cluster_auth" "helm" {
name = "${dependency.eks.outputs.cluster_name}"
}
provider "helm" {
kubernetes {
host = data.aws_eks_cluster.helm.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.helm.certificate_authority[0].data)
token = data.aws_eks_cluster_auth.helm.token
}
%{endif~}
provider "helm" {
kubernetes {
%{if cluster_name != "a-cluster-name"~}
host = data.aws_eks_cluster.helm.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.helm.certificate_authority[0].data)
token = data.aws_eks_cluster_auth.helm.token
%{else~}
host = ""
cluster_ca_certificate = ""
token = ""
%{endif~}
}
%{ endif ~}
EOF
}
45 changes: 16 additions & 29 deletions lab/_envcommon/kube-provider.hcl
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
dependency "eks" {
config_path = "${get_original_terragrunt_dir()}/../eks"
# mock_outputs_allowed_terraform_commands = ["plan", "validate"]
mock_outputs = {
cluster_name = "a-cluster-name"
}
%{if cluster_name != "a-cluster-name"~}
data "aws_eks_cluster" "kube" {
name = "${cluster_name}"
}

inputs = {
cluster_name = dependency.eks.outputs.cluster_name
data "aws_eks_cluster_auth" "kube" {
name = "${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~}
}
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
20 changes: 18 additions & 2 deletions lab/root.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ locals {
state_table_name = local.common_vars.locals.state_table_name
terraform = local.cluster_vars.locals.terraform
terragrunt = local.cluster_vars.locals.terragrunt
module_name = get_terragrunt_dir()
module_name = trimprefix(replace(run_cmd("realpath", get_original_terragrunt_dir()), dirname(get_repo_root()), ""), "/")
}

# Configure Terragrunt to automatically store tfstate files in an S3 bucket
Expand Down Expand Up @@ -76,7 +76,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 All @@ -97,6 +97,22 @@ provider "aws" {
EOF
}

generate "helm_provider" {
path = "helm-provider.tf"
if_exists = "overwrite_terragrunt"
contents = templatefile("${get_repo_root()}/lab/_envcommon/helm-provider.hcl", {
cluster_name = local.cluster_name
})
}

generate "kube_provider" {
path = "kube-provider.tf"
if_exists = "overwrite_terragrunt"
contents = templatefile("${get_repo_root()}/lab/_envcommon/kube-provider.hcl", {
cluster_name = local.cluster_name
})
}

# ---------------------------------------------------------------------------------------------------------------------
# GLOBAL PARAMETERS
# These variables apply to all configurations in this subfolder. These are automatically merged into the child
Expand Down

0 comments on commit f019116

Please sign in to comment.