Skip to content

Commit

Permalink
fix: perpetual tag drift on aws resources
Browse files Browse the repository at this point in the history
  • Loading branch information
fullykubed committed Mar 20, 2024
1 parent 4a8fa5f commit c0516a1
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ provider "aws" {
ignore_tags {
key_prefixes = [
"kubernetes.io",
"karpenter.sh",
"panfactum.com"
"karpenter.sh"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ provider "aws" {
ignore_tags {
key_prefixes = [
"kubernetes.io",
"karpenter.sh",
"panfactum.com"
"karpenter.sh"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ provider "aws" {
ignore_tags {
key_prefixes = [
"kubernetes.io",
"karpenter.sh",
"panfactum.com"
"karpenter.sh"
]
}
}
5 changes: 4 additions & 1 deletion packages/reference/environments/providers/aws.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ provider "aws" {
allowed_account_ids = ["${aws_account_id}"]
profile = "${aws_profile}"
ignore_tags {
key_prefixes = ["kubernetes.io", "karpenter.sh"]
key_prefixes = [
"kubernetes.io",
"karpenter.sh"
]
}
}
5 changes: 4 additions & 1 deletion packages/reference/environments/providers/aws_global.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ provider "aws" {
allowed_account_ids = ["${aws_account_id}"]
profile = "${aws_profile}"
ignore_tags {
key_prefixes = ["kubernetes.io", "karpenter.sh"]
key_prefixes = [
"kubernetes.io",
"karpenter.sh"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ provider "aws" {
allowed_account_ids = ["${aws_account_id}"]
profile = "${aws_profile}"
ignore_tags {
key_prefixes = ["kubernetes.io", "karpenter.sh"]
key_prefixes = [
"kubernetes.io",
"karpenter.sh"
]
}
}
8 changes: 4 additions & 4 deletions packages/reference/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 30 additions & 20 deletions packages/terraform/aws_eks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ terraform {
}

locals {
vpc_id = values(data.aws_subnet.control_plane_subnets)[0].vpc_id // a bit hacky but we can just assume all subnets are in the same aws_vpc
common_tags = merge({
environment = var.environment
pf_root_module = var.pf_root_module
region = var.region
terraform = "true"
},
{
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
})
vpc_id = values(data.aws_subnet.control_plane_subnets)[0].vpc_id // a bit hacky but we can just assume all subnets are in the same aws_vpc
controller_nodes_description = "Nodes for cluster-critical components and bootstrapping processes. Not autoscaled."
}

Expand Down Expand Up @@ -128,15 +119,22 @@ resource "aws_security_group" "control_plane" {
description = "Security group for the ${var.cluster_name} EKS control plane."
vpc_id = local.vpc_id
tags = merge(module.tags.tags, {
Name = var.cluster_name
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
description = "Security group for the ${var.cluster_name} EKS control plane."
Name = var.cluster_name
description = "Security group for the ${var.cluster_name} EKS control plane."
})
lifecycle {
prevent_destroy = true
}
}

// This needs to be managed separately because they are included in the ignore_tags provider configuration
resource "aws_ec2_tag" "control_plane_kubernetes" {
resource_id = aws_security_group.control_plane.id
key = "kubernetes.io/cluster/${var.cluster_name}"
value = "owned"
}


resource "aws_security_group_rule" "control_plane_nodes" {
type = "ingress"
description = "Allow nodes to talk with API server."
Expand Down Expand Up @@ -288,14 +286,14 @@ resource "aws_launch_template" "controller" {

tag_specifications {
resource_type = "instance"
tags = merge(module.tags.tags, local.common_tags, {
tags = merge(module.tags.tags, {
Name = "${var.cluster_name}-controller"
description = local.controller_nodes_description
eks-managed = "true"
})
}

tags = merge(module.tags.tags, local.common_tags, {
tags = merge(module.tags.tags, {
description = local.controller_nodes_description
})

Expand Down Expand Up @@ -327,7 +325,7 @@ resource "aws_eks_node_group" "controllers" {
}

capacity_type = "ON_DEMAND"
tags = merge(module.tags.tags, local.common_tags, {
tags = merge(module.tags.tags, {
description = local.controller_nodes_description
})
labels = {
Expand Down Expand Up @@ -362,17 +360,29 @@ resource "aws_security_group" "all_nodes" {
vpc_id = local.vpc_id

tags = merge(module.tags.tags, {
"Name" = "${var.cluster_name}-nodes"
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
description = "Security group for all nodes in the ${var.cluster_name} EKS cluster"
"karpenter.sh/discovery" = var.cluster_name
Name = "${var.cluster_name}-nodes"
description = "Security group for all nodes in the ${var.cluster_name} EKS cluster"
})

lifecycle {
prevent_destroy = true
}
}

// These need to be managed separately because they are included in the ignore_tags provider configuration
resource "aws_ec2_tag" "all_nodes_kubernetes" {
resource_id = aws_security_group.all_nodes.id
key = "kubernetes.io/cluster/${var.cluster_name}"
value = "owned"
}

resource "aws_ec2_tag" "all_nodes_karpenter" {
resource_id = aws_security_group.all_nodes.id
key = "karpenter.sh/discovery"
value = var.cluster_name
}


resource "aws_security_group_rule" "ingress_self" {
security_group_id = aws_security_group.all_nodes.id
type = "ingress"
Expand Down
4 changes: 4 additions & 0 deletions packages/terraform/aws_vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ resource "aws_subnet" "subnets" {
Name = each.key
"panfactum.com/type" = each.value.public ? "public" : contains(keys(var.nat_associations), each.key) ? "private" : "isolated"
})

lifecycle {
ignore_changes = [tags["panfactum.com/public-ip"]]
}
}

##########################################################################
Expand Down

0 comments on commit c0516a1

Please sign in to comment.