Skip to content

Commit

Permalink
Adding support for existing cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
dunefro committed Nov 21, 2024
1 parent 3dd19cd commit 03a30af
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 35 deletions.
7 changes: 7 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "aws_eks_cluster" "eks_cluster" {
count = var.use_existing_cluster ? 1 : 0
name = var.cluster_name
}

# To do
# apply data block for openid_connect_provider to fetch openid arn directly using eks cluster oidc[0].issuer[0].url
1 change: 1 addition & 0 deletions eks.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
###################################################################################

module "aws-eks-kubernetes-cluster" {
count = var.use_existing_cluster ? 0 : 1
source = "terraform-aws-modules/eks/aws"
version = "v20.17.2"
cluster_name = var.cluster_name
Expand Down
9 changes: 5 additions & 4 deletions eks_addons.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
###################################################################################

module "eks_blueprints_addons" {
count = var.use_existing_cluster ? 0 : 1
source = "aws-ia/eks-blueprints-addons/aws"
version = "1.16.3"

cluster_name = module.aws-eks-kubernetes-cluster.cluster_name
cluster_endpoint = module.aws-eks-kubernetes-cluster.cluster_endpoint
cluster_version = module.aws-eks-kubernetes-cluster.cluster_version
oidc_provider_arn = module.aws-eks-kubernetes-cluster.oidc_provider_arn
cluster_name = module.aws-eks-kubernetes-cluster[0].cluster_name
cluster_endpoint = module.aws-eks-kubernetes-cluster[0].cluster_endpoint
cluster_version = module.aws-eks-kubernetes-cluster[0].cluster_version
oidc_provider_arn = module.aws-eks-kubernetes-cluster[0].oidc_provider_arn

eks_addons = {
coredns = {
Expand Down
26 changes: 26 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,30 @@ locals {
} : {})

karpenter_profile_name = "${var.cluster_name}-karpenter"

// this is used when use_existing_cluster is set to true, so that we don't have to modify eks_managed_node_groups
output_eks_managed_node_groups = tomap({
"initial" = {
autoscaling_group_schedule_arns = {}
iam_role_arn = var.existing_cluster_node_role_arn
iam_role_unique_id = {}
launch_template_arn = ""
launch_template_id = ""
autoscaling_group_schedule_arns = {}

Check warning on line 116 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (ubuntu-latest)

Duplicate key: "autoscaling_group_schedule_arns", first defined at locals.tf:111,7-38

Check warning on line 116 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (macos-latest)

Duplicate key: "autoscaling_group_schedule_arns", first defined at locals.tf:111,7-38
iam_role_name = ""
iam_role_unique_id = ""

Check warning on line 118 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (ubuntu-latest)

Duplicate key: "iam_role_unique_id", first defined at locals.tf:113,7-25

Check warning on line 118 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (macos-latest)

Duplicate key: "iam_role_unique_id", first defined at locals.tf:113,7-25
launch_template_arn = ""

Check warning on line 119 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (ubuntu-latest)

Duplicate key: "launch_template_arn", first defined at locals.tf:114,7-26

Check warning on line 119 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (macos-latest)

Duplicate key: "launch_template_arn", first defined at locals.tf:114,7-26
launch_template_id = ""

Check warning on line 120 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (ubuntu-latest)

Duplicate key: "launch_template_id", first defined at locals.tf:115,7-25

Check warning on line 120 in locals.tf

View workflow job for this annotation

GitHub Actions / Terraform fmt and tflint / terraform-tflint (macos-latest)

Duplicate key: "launch_template_id", first defined at locals.tf:115,7-25
launch_template_latest_version = 0
launch_template_name = ""
node_group_arn = ""
node_group_autoscaling_group_names = []
node_group_id = ""
node_group_labels = {}
node_group_resources = []
node_group_status = ""
node_group_taints = []
platform = ""
}
})
}
57 changes: 31 additions & 26 deletions output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,55 @@
################################################################################
# Cluster
################################################################################
output "use_existing_cluster" {
description = "Flag to check if you are using an alreday existing cluster"
value = var.use_existing_cluster
}

output "cluster_arn" {
description = "The Amazon Resource Name (ARN) of the cluster"
value = module.aws-eks-kubernetes-cluster.cluster_arn
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].arn : module.aws-eks-kubernetes-cluster[0].cluster_arn
}

output "cluster_certificate_authority_data" {
description = "Base64 encoded certificate data required to communicate with the cluster"
value = module.aws-eks-kubernetes-cluster.cluster_certificate_authority_data
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].certificate_authority[0].data : module.aws-eks-kubernetes-cluster[0].cluster_certificate_authority_data
}

output "cluster_endpoint" {
description = "Endpoint for your Kubernetes API server"
value = module.aws-eks-kubernetes-cluster.cluster_endpoint
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].endpoint : module.aws-eks-kubernetes-cluster[0].cluster_endpoint
}

output "cluster_id" {
description = "DEPRECATED - Use cluster_name"
value = module.aws-eks-kubernetes-cluster.cluster_name
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].id : module.aws-eks-kubernetes-cluster[0].cluster_name
}

output "cluster_name" {
description = "The name/id of the EKS cluster. Will block on cluster creation until the cluster is really ready"
value = module.aws-eks-kubernetes-cluster.cluster_name
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].name : module.aws-eks-kubernetes-cluster[0].cluster_name
}

output "cluster_oidc_issuer_url" {
description = "The URL on the EKS cluster for the OpenID Connect identity provider"
value = module.aws-eks-kubernetes-cluster.cluster_oidc_issuer_url
# value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].identity[0].oidc[0].issuer : module.aws-eks-kubernetes-cluster[0].cluster_oidc_issuer_url
value = var.use_existing_cluster ? var.existing_cluster_oidc_issuer_url : module.aws-eks-kubernetes-cluster[0].cluster_oidc_issuer_url
}

output "cluster_platform_version" {
description = "Platform version for the cluster"
value = module.aws-eks-kubernetes-cluster.cluster_platform_version
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].platform_version : module.aws-eks-kubernetes-cluster[0].cluster_platform_version
}

output "cluster_status" {
description = "Status of the EKS cluster. One of `CREATING`, `ACTIVE`, `DELETING`, `FAILED`"
value = module.aws-eks-kubernetes-cluster.cluster_status
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].status : module.aws-eks-kubernetes-cluster[0].cluster_status
}

output "cluster_primary_security_group_id" {
description = "Cluster security group that was created by Amazon EKS for the cluster. Managed node groups use this security group for control-plane-to-data-plane communication. Referred to as 'Cluster security group' in the EKS console"
value = module.aws-eks-kubernetes-cluster.cluster_primary_security_group_id
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].vpc_config[0].cluster_security_group_id : module.aws-eks-kubernetes-cluster[0].cluster_primary_security_group_id
}

################################################################################
Expand All @@ -55,12 +60,12 @@ output "cluster_primary_security_group_id" {

output "cluster_security_group_arn" {
description = "Amazon Resource Name (ARN) of the cluster security group"
value = module.aws-eks-kubernetes-cluster.cluster_security_group_arn
value = var.use_existing_cluster ? "" : module.aws-eks-kubernetes-cluster[0].cluster_security_group_arn
}

output "cluster_security_group_id" {
description = "ID of the cluster security group"
value = module.aws-eks-kubernetes-cluster.cluster_security_group_id
value = var.use_existing_cluster ? data.aws_eks_cluster.eks_cluster[0].vpc_config[0].cluster_security_group_id : module.aws-eks-kubernetes-cluster[0].cluster_security_group_id
}

################################################################################
Expand All @@ -69,21 +74,21 @@ output "cluster_security_group_id" {

output "node_security_group_arn" {
description = "Amazon Resource Name (ARN) of the node shared security group"
value = module.aws-eks-kubernetes-cluster.node_security_group_arn
value = var.use_existing_cluster ? "" : module.aws-eks-kubernetes-cluster[0].node_security_group_arn
}

output "node_security_group_id" {
description = "ID of the node shared security group"
value = module.aws-eks-kubernetes-cluster.node_security_group_id
value = var.use_existing_cluster ? var.existing_cluster_node_security_group_id : module.aws-eks-kubernetes-cluster[0].node_security_group_id
}

################################################################################
# IRSA
################################################################################

output "oidc_provider_arn" {
description = "The ARN of the OIDC Provider if `enable_irsa = true`"
value = module.aws-eks-kubernetes-cluster.oidc_provider_arn
description = "The ARN of the OIDC Provider"
value = var.use_existing_cluster ? var.existing_cluster_oidc_issuer_arn : module.aws-eks-kubernetes-cluster[0].oidc_provider_arn
}

################################################################################
Expand All @@ -92,17 +97,17 @@ output "oidc_provider_arn" {

output "cluster_iam_role_name" {
description = "IAM role name of the EKS cluster"
value = module.aws-eks-kubernetes-cluster.cluster_iam_role_name
value = var.use_existing_cluster ? "" : module.aws-eks-kubernetes-cluster[0].cluster_iam_role_name
}

output "cluster_iam_role_arn" {
description = "IAM role ARN of the EKS cluster"
value = module.aws-eks-kubernetes-cluster.cluster_iam_role_arn
value = var.use_existing_cluster ? "" : module.aws-eks-kubernetes-cluster[0].cluster_iam_role_arn
}

output "cluster_iam_role_unique_id" {
description = "Stable and unique string identifying the IAM role"
value = module.aws-eks-kubernetes-cluster.cluster_iam_role_unique_id
value = var.use_existing_cluster ? "" : module.aws-eks-kubernetes-cluster[0].cluster_iam_role_unique_id
}

################################################################################
Expand All @@ -111,7 +116,7 @@ output "cluster_iam_role_unique_id" {

output "cluster_addons" {
description = "Map of attribute maps for all EKS cluster addons enabled"
value = module.eks_blueprints_addons.eks_addons
value = var.use_existing_cluster ? {} : module.eks_blueprints_addons[0].eks_addons
}

################################################################################
Expand All @@ -120,7 +125,7 @@ output "cluster_addons" {

output "cluster_identity_providers" {
description = "Map of attribute maps for all EKS identity providers enabled"
value = module.aws-eks-kubernetes-cluster.cluster_identity_providers
value = var.use_existing_cluster ? {} : module.aws-eks-kubernetes-cluster[0].cluster_identity_providers
sensitive = true
}

Expand All @@ -130,12 +135,12 @@ output "cluster_identity_providers" {

output "cloudwatch_log_group_name" {
description = "Name of cloudwatch log group created"
value = module.aws-eks-kubernetes-cluster.cloudwatch_log_group_name
value = var.use_existing_cluster ? "" : module.aws-eks-kubernetes-cluster[0].cloudwatch_log_group_name
}

output "cloudwatch_log_group_arn" {
description = "Arn of cloudwatch log group created"
value = module.aws-eks-kubernetes-cluster.cloudwatch_log_group_arn
value = var.use_existing_cluster ? "" : module.aws-eks-kubernetes-cluster[0].cloudwatch_log_group_arn
}

################################################################################
Expand All @@ -144,7 +149,7 @@ output "cloudwatch_log_group_arn" {

output "fargate_profiles" {
description = "Map of attribute maps for all EKS Fargate Profiles created"
value = module.aws-eks-kubernetes-cluster.fargate_profiles
value = var.use_existing_cluster ? {} : module.aws-eks-kubernetes-cluster[0].fargate_profiles
}

################################################################################
Expand All @@ -153,7 +158,7 @@ output "fargate_profiles" {

output "eks_managed_node_groups" {
description = "Map of attribute maps for all EKS managed node groups created"
value = module.aws-eks-kubernetes-cluster.eks_managed_node_groups
value = var.use_existing_cluster ? local.output_eks_managed_node_groups : module.aws-eks-kubernetes-cluster[0].eks_managed_node_groups
}

################################################################################
Expand All @@ -162,7 +167,7 @@ output "eks_managed_node_groups" {

output "self_managed_node_groups" {
description = "Map of attribute maps for all self managed node groups created"
value = module.aws-eks-kubernetes-cluster.self_managed_node_groups
value = var.use_existing_cluster ? {} : module.aws-eks-kubernetes-cluster[0].self_managed_node_groups
}

################################################################################
Expand All @@ -171,5 +176,5 @@ output "self_managed_node_groups" {

output "aws_access_entries" {
description = "Access entries for the EKS cluster security group"
value = module.aws-eks-kubernetes-cluster.access_entries
value = var.use_existing_cluster ? {} : module.aws-eks-kubernetes-cluster[0].access_entries
}
Loading

0 comments on commit 03a30af

Please sign in to comment.