Skip to content

Commit

Permalink
fix: Ensure conditional creation applies to data sources (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryantbiggs authored Sep 23, 2023
1 parent c218e0d commit 327207a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.77.2
rev: v1.83.4
hooks:
- id: terraform_fmt
- id: terraform_validate
Expand Down
19 changes: 12 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ resource "helm_release" "this" {
# IAM Role for Service Account(s) (IRSA)
################################################################################

data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {
count = local.create_role ? 1 : 0
}
data "aws_caller_identity" "current" {
count = local.create_role ? 1 : 0
}

locals {
create_role = var.create && var.create_role

account_id = data.aws_caller_identity.current.account_id
partition = data.aws_partition.current.partition
account_id = try(data.aws_caller_identity.current[0].account_id, "*")
partition = try(data.aws_partition.current[0].partition, "*")

role_name = try(coalesce(var.role_name, var.name), "")
role_name_condition = var.role_name_use_prefix ? "${local.role_name}-*" : local.role_name
Expand Down Expand Up @@ -184,10 +188,11 @@ locals {
create_policy = local.create_role && var.create_policy

policy_name = try(coalesce(var.policy_name, local.role_name), "")
perms = concat(var.source_policy_documents, var.override_policy_documents, var.policy_statements)
}

data "aws_iam_policy_document" "this" {
count = local.create_policy ? 1 : 0
count = local.create_policy && length(local.perms) > 0 ? 1 : 0

source_policy_documents = var.source_policy_documents
override_policy_documents = var.override_policy_documents
Expand Down Expand Up @@ -235,7 +240,7 @@ data "aws_iam_policy_document" "this" {
}

resource "aws_iam_policy" "this" {
count = local.create_policy ? 1 : 0
count = local.create_policy && length(local.perms) > 0 ? 1 : 0

name = var.policy_name_use_prefix ? null : local.policy_name
name_prefix = var.policy_name_use_prefix ? "${local.policy_name}-" : null
Expand All @@ -247,7 +252,7 @@ resource "aws_iam_policy" "this" {
}

resource "aws_iam_role_policy_attachment" "this" {
count = local.create_policy ? 1 : 0
count = local.create_policy && length(local.perms) > 0 ? 1 : 0

role = aws_iam_role.this[0].name
policy_arn = aws_iam_policy.this[0].arn
Expand Down
10 changes: 2 additions & 8 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,32 @@ Note that this example may create resources which will incur monetary charges on
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.47 |
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >= 2.9 |
| <a name="requirement_kubectl"></a> [kubectl](#requirement\_kubectl) | >= 1.14 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.47 |
| <a name="provider_kubectl"></a> [kubectl](#provider\_kubectl) | >= 1.14 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_disabled"></a> [disabled](#module\_disabled) | ../ | n/a |
| <a name="module_eks"></a> [eks](#module\_eks) | terraform-aws-modules/eks/aws | ~> 19.10 |
| <a name="module_eks"></a> [eks](#module\_eks) | terraform-aws-modules/eks/aws | ~> 19.16 |
| <a name="module_helm_release_irsa"></a> [helm\_release\_irsa](#module\_helm\_release\_irsa) | ../ | n/a |
| <a name="module_helm_release_only"></a> [helm\_release\_only](#module\_helm\_release\_only) | ../ | n/a |
| <a name="module_irsa_only"></a> [irsa\_only](#module\_irsa\_only) | ../ | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 4.0 |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |

## Resources

| Name | Type |
|------|------|
| [aws_iam_instance_profile.karpenter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
| [aws_iam_policy.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [kubectl_manifest.karpenter_example_deployment](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
| [kubectl_manifest.karpenter_node_template](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
| [kubectl_manifest.karpenter_provisioner](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_eks_cluster_auth.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source |
| [aws_iam_policy_document.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs
Expand Down
118 changes: 15 additions & 103 deletions tests/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@ provider "helm" {
kubernetes {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
token = data.aws_eks_cluster_auth.this.token
}
}

provider "kubectl" {
apply_retry_count = 30
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
load_config_file = false
token = data.aws_eks_cluster_auth.this.token
}

data "aws_eks_cluster_auth" "this" {
name = module.eks.cluster_name
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
# This requires the awscli to be installed locally where Terraform is executed
args = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
}
}
}

data "aws_caller_identity" "current" {}
Expand Down Expand Up @@ -157,21 +151,22 @@ module "disabled" {

module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 19.10"
version = "~> 19.16"

cluster_name = local.name
cluster_version = "1.24"
cluster_name = local.name
cluster_version = "1.27"
cluster_endpoint_public_access = true

vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets

eks_managed_node_groups = {
initial = {
instance_types = ["m5.xlarge"]
instance_types = ["m5.large"]

min_size = 1
max_size = 2
desired_size = 1
max_size = 3
desired_size = 2
}
}

Expand All @@ -185,7 +180,7 @@ module "eks" {

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 4.0"
version = "~> 5.0"

name = local.name
cidr = local.vpc_cidr
Expand Down Expand Up @@ -218,7 +213,6 @@ resource "aws_iam_instance_profile" "karpenter" {
}

data "aws_iam_policy_document" "karpenter_controller" {
# # checkov:skip=CKV_AWS_111
statement {
actions = [
"ec2:CreateLaunchTemplate",
Expand Down Expand Up @@ -298,85 +292,3 @@ resource "aws_iam_policy" "karpenter_controller" {

tags = local.tags
}

################################################################################
# Karpenter Provisioner
################################################################################

# Workaround - https://github.com/hashicorp/terraform-provider-kubernetes/issues/1380#issuecomment-967022975
resource "kubectl_manifest" "karpenter_provisioner" {
yaml_body = <<-YAML
---
apiVersion: karpenter.sh/v1alpha5
kind: Provisioner
metadata:
name: default
spec:
requirements:
- key: karpenter.sh/capacity-type
operator: In
values: ["spot"]
limits:
resources:
cpu: 1000
providerRef:
name: default
ttlSecondsAfterEmpty: 30
YAML

depends_on = [
module.helm_release_irsa.helm_release
]
}

resource "kubectl_manifest" "karpenter_node_template" {
yaml_body = <<-YAML
apiVersion: karpenter.k8s.aws/v1alpha1
kind: AWSNodeTemplate
metadata:
name: default
spec:
subnetSelector:
${local.karpenter_tag_key}: ${module.eks.cluster_name}
securityGroupSelector:
${local.karpenter_tag_key}: ${module.eks.cluster_name}
tags:
${local.karpenter_tag_key}: ${module.eks.cluster_name}
YAML

depends_on = [
kubectl_manifest.karpenter_provisioner
]
}

# Example deployment using the [pause image](https://www.ianlewis.org/en/almighty-pause-container)
# and starts with zero replicas
resource "kubectl_manifest" "karpenter_example_deployment" {
yaml_body = <<-YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: inflate
spec:
replicas: 0
selector:
matchLabels:
app: inflate
template:
metadata:
labels:
app: inflate
spec:
terminationGracePeriodSeconds: 0
containers:
- name: inflate
image: public.ecr.aws/eks-distro/kubernetes/pause:3.2
resources:
requests:
cpu: 1
YAML

depends_on = [
kubectl_manifest.karpenter_node_template
]
}
4 changes: 0 additions & 4 deletions tests/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ terraform {
source = "hashicorp/helm"
version = ">= 2.9"
}
kubectl = {
source = "gavinbunney/kubectl"
version = ">= 1.14"
}
}
}

0 comments on commit 327207a

Please sign in to comment.