Skip to content

Commit

Permalink
Override or add configuration options in values input (#22)
Browse files Browse the repository at this point in the history
Changes:
* Add ability to specify custom configuration options as values yaml
* Add ability to opt-out from IRSA role creation
* Add new variable k8s_assume_role_arn for assume role arn definition
* Delegate Kubernetes namespace creation to helm provider from Kubernetes provider
* Bump default chart version to the 5.4.4
* Update terraform docs version in pre-commit

Breaking changes:
* Removes fake dependency variable mod_dependency in favor of terraform built-in module dependency injection
* Rename variable k8s_create_namespace to helm_create_namespace
* Assume role arn should be specified by variable k8s_assume_role_arn instead of parameter passed in settings
  • Loading branch information
Thumbiceq authored Aug 30, 2021
1 parent 04fb73f commit 089c191
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ master ]

env:
TERRAFORM_DOCS_VERSION: "v0.11.2"
TERRAFORM_DOCS_VERSION: "v0.15.0"
TFLINT_VERSION: "v0.25.0"
TFSEC_VERSION: "v0.39.6"

Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
# - id: terraform_tfsec
- id: terraform_docs
args:
- '--args=--hide providers --sort-by-required'
- '--args=--hide providers --sort-by required'

- repo: git://github.com/pecigonzalo/pre-commit-terraform-vars
rev: v1.0.0
Expand Down
66 changes: 35 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,55 @@ See [Basic example](examples/basic/README.md) for further information.

| Name | Version |
|------|---------|
| terraform | >= 0.13 |
| aws | >= 2.0 |
| helm | >= 1.0 |
| kubernetes | >= 1.10 |
| local | >= 1.3 |
| null | >= 2.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.0 |
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >= 1.0 |
| <a name="requirement_utils"></a> [utils](#requirement\_utils) | >= 0.12.0 |

## Modules

No Modules.
No modules.

## Resources

| Name |
|------|
| [aws_iam_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) |
| [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) |
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) |
| [aws_iam_role_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) |
| [aws_region](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) |
| [helm_release](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) |
| [kubernetes_namespace](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) |
| Name | Type |
|------|------|
| [aws_iam_policy.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_role.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [helm_release.external_dns](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [aws_iam_policy_document.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.external_dns_assume](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.external_dns_irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [utils_deep_merge_yaml.values](https://registry.terraform.io/providers/cloudposse/utils/latest/docs/data-sources/deep_merge_yaml) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| cluster\_identity\_oidc\_issuer | The OIDC Identity issuer for the cluster | `string` | n/a | yes |
| cluster\_identity\_oidc\_issuer\_arn | The OIDC Identity issuer ARN for the cluster that can be used to associate IAM roles with a service account | `string` | n/a | yes |
| cluster\_name | The name of the cluster | `string` | n/a | yes |
| enabled | Variable indicating whether deployment is enabled | `bool` | `true` | no |
| helm\_chart\_name | Helm chart name to be installed | `string` | `"external-dns"` | no |
| helm\_chart\_version | Version of the Helm chart | `string` | `"4.9.0"` | no |
| helm\_release\_name | Helm release name | `string` | `"external-dns"` | no |
| helm\_repo\_url | Helm repository | `string` | `"https://charts.bitnami.com/bitnami"` | no |
| k8s\_create\_namespace | Whether to create k8s namespace with name defined by `k8s_namespace` | `bool` | `true` | no |
| k8s\_namespace | The k8s namespace in which the external-dns service account has been created | `string` | `"kube-system"` | no |
| k8s\_service\_account\_name | The k8s external-dns service account name | `string` | `"external-dns"` | no |
| mod\_dependency | Dependence variable binds all AWS resources allocated by this module, dependent modules reference this variable | `any` | `null` | no |
| policy\_allowed\_zone\_ids | List of the Route53 zone ids for service account IAM role access | `list(string)` | <pre>[<br> "*"<br>]</pre> | no |
| settings | Additional settings which will be passed to the Helm chart values, see https://hub.helm.sh/charts/bitnami/external-dns | `map(any)` | `{}` | no |
| <a name="input_cluster_identity_oidc_issuer"></a> [cluster\_identity\_oidc\_issuer](#input\_cluster\_identity\_oidc\_issuer) | The OIDC Identity issuer for the cluster | `string` | n/a | yes |
| <a name="input_cluster_identity_oidc_issuer_arn"></a> [cluster\_identity\_oidc\_issuer\_arn](#input\_cluster\_identity\_oidc\_issuer\_arn) | The OIDC Identity issuer ARN for the cluster that can be used to associate IAM roles with a service account | `string` | n/a | yes |
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | The name of the cluster | `string` | n/a | yes |
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Variable indicating whether deployment is enabled | `bool` | `true` | no |
| <a name="input_helm_chart_name"></a> [helm\_chart\_name](#input\_helm\_chart\_name) | Helm chart name to be installed | `string` | `"external-dns"` | no |
| <a name="input_helm_chart_version"></a> [helm\_chart\_version](#input\_helm\_chart\_version) | Version of the Helm chart | `string` | `"5.4.4"` | no |
| <a name="input_helm_create_namespace"></a> [helm\_create\_namespace](#input\_helm\_create\_namespace) | Whether to create k8s namespace with name defined by `k8s_namespace` | `bool` | `true` | no |
| <a name="input_helm_release_name"></a> [helm\_release\_name](#input\_helm\_release\_name) | Helm release name | `string` | `"external-dns"` | no |
| <a name="input_helm_repo_url"></a> [helm\_repo\_url](#input\_helm\_repo\_url) | Helm repository | `string` | `"https://charts.bitnami.com/bitnami"` | no |
| <a name="input_k8s_assume_role_arn"></a> [k8s\_assume\_role\_arn](#input\_k8s\_assume\_role\_arn) | Whether to create and use default role or assume existing role. Useful for hosted zones in another AWS account. Default (empty string) use default role. | `string` | `""` | no |
| <a name="input_k8s_irsa_role_create"></a> [k8s\_irsa\_role\_create](#input\_k8s\_irsa\_role\_create) | Whether to create IRSA role and annotate service account | `bool` | `true` | no |
| <a name="input_k8s_namespace"></a> [k8s\_namespace](#input\_k8s\_namespace) | The K8s namespace in which the external-dns will be installed | `string` | `"kube-system"` | no |
| <a name="input_k8s_rbac_create"></a> [k8s\_rbac\_create](#input\_k8s\_rbac\_create) | Whether to create and use RBAC resources | `bool` | `true` | no |
| <a name="input_k8s_service_account_create"></a> [k8s\_service\_account\_create](#input\_k8s\_service\_account\_create) | Whether to create Service Account | `bool` | `true` | no |
| <a name="input_k8s_service_account_name"></a> [k8s\_service\_account\_name](#input\_k8s\_service\_account\_name) | The k8s external-dns service account name | `string` | `"external-dns"` | no |
| <a name="input_policy_allowed_zone_ids"></a> [policy\_allowed\_zone\_ids](#input\_policy\_allowed\_zone\_ids) | List of the Route53 zone ids for service account IAM role access | `list(string)` | <pre>[<br> "*"<br>]</pre> | no |
| <a name="input_settings"></a> [settings](#input\_settings) | Additional settings which will be passed to the Helm chart values, see https://hub.helm.sh/charts/bitnami/external-dns | `map(any)` | `{}` | no |
| <a name="input_values"></a> [values](#input\_values) | Additional yaml encoded values which will be passed to the Helm chart, see https://hub.helm.sh/charts/bitnami/external-dns | `string` | `""` | no |

## Outputs

No output.
No outputs.
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Contributing and reporting issues
Expand Down
20 changes: 10 additions & 10 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ No requirements.

| Name | Source | Version |
|------|--------|---------|
| eks_cluster | cloudposse/eks-cluster/aws | |
| eks_workers | cloudposse/eks-workers/aws | |
| extenral_dns | ../../ | |
| vpc | terraform-aws-modules/vpc/aws | |
| <a name="module_eks_cluster"></a> [eks\_cluster](#module\_eks\_cluster) | cloudposse/eks-cluster/aws | 0.43.2 |
| <a name="module_eks_node_group"></a> [eks\_node\_group](#module\_eks\_node\_group) | cloudposse/eks-node-group/aws | 0.25.0 |
| <a name="module_extenral_dns"></a> [extenral\_dns](#module\_extenral\_dns) | ../../ | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | 3.6.0 |

## Resources

| Name |
|------|
| [aws_eks_cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster) |
| [aws_eks_cluster_auth](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) |
| Name | Type |
|------|------|
| [aws_eks_cluster.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster) | data source |
| [aws_eks_cluster_auth.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source |

## Inputs

No input.
No inputs.

## Outputs

No output.
No outputs.
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
40 changes: 17 additions & 23 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
source = "terraform-aws-modules/vpc/aws"
version = "3.6.0"

name = "cluster-autoscaler-vpc"
name = "external-dns-vpc"
cidr = "10.0.0.0/16"
azs = ["eu-central-1a", "eu-central-1b"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
}

module "eks_cluster" {
source = "cloudposse/eks-cluster/aws"
source = "cloudposse/eks-cluster/aws"
version = "0.43.2"

region = "eu-central-1"
subnet_ids = module.vpc.public_subnets
vpc_id = module.vpc.vpc_id
name = "cluster-autoscaler"

workers_security_group_ids = [module.eks_workers.security_group_id]
workers_role_arns = [module.eks_workers.workers_role_arn]
name = "external-dns"
}

module "eks_workers" {
source = "cloudposse/eks-workers/aws"

cluster_certificate_authority_data = module.eks_cluster.eks_cluster_certificate_authority_data
cluster_endpoint = module.eks_cluster.eks_cluster_endpoint
cluster_name = module.eks_cluster.eks_cluster_id
cluster_security_group_id = module.eks_cluster.security_group_id
instance_type = "t3.medium"
max_size = 1
min_size = 1
subnet_ids = module.vpc.public_subnets
vpc_id = module.vpc.vpc_id

associate_public_ip_address = true
module "eks_node_group" {
source = "cloudposse/eks-node-group/aws"
version = "0.25.0"

cluster_name = "external-dns"
instance_types = ["t3.medium"]
subnet_ids = module.vpc.public_subnets
min_size = 1
desired_size = 1
max_size = 2
depends_on = [module.eks_cluster.kubernetes_config_map_id]
}

# Use the module:

module "extenral_dns" {
source = "../../"

Expand Down
36 changes: 8 additions & 28 deletions iam.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
# aws.assumeRoleArn

locals {
assume_role = length(try(var.settings["aws.assumeRoleArn"], "")) > 0 ? true : false
}

resource "kubernetes_namespace" "external_dns" {
depends_on = [var.mod_dependency]
count = (var.enabled && var.k8s_create_namespace && var.k8s_namespace != "kube-system") ? 1 : 0

metadata {
name = var.k8s_namespace
}
assume_role = length(var.k8s_assume_role_arn) > 0 ? true : false
}

### iam ###
# Policy
data "aws_iam_policy_document" "external_dns" {
count = var.enabled && !local.assume_role ? 1 : 0
count = local.k8s_irsa_role_create && !local.assume_role ? 1 : 0

statement {
sid = "ChangeResourceRecordSets"
Expand Down Expand Up @@ -48,7 +35,7 @@ data "aws_iam_policy_document" "external_dns" {
}

data "aws_iam_policy_document" "external_dns_assume" {
count = var.enabled && local.assume_role ? 1 : 0
count = local.k8s_irsa_role_create && local.assume_role ? 1 : 0

statement {
sid = "AllowAssumeExternalDNSRole"
Expand All @@ -60,27 +47,23 @@ data "aws_iam_policy_document" "external_dns_assume" {
]

resources = [
var.settings["aws.assumeRoleArn"]
var.k8s_assume_role_arn
]
}
}


resource "aws_iam_policy" "external_dns" {
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0

name = "${var.cluster_name}-external-dns"
path = "/"
description = "Policy for external-dns service"

policy = local.assume_role ? data.aws_iam_policy_document.external_dns_assume[0].json : data.aws_iam_policy_document.external_dns[0].json

depends_on = [var.mod_dependency]
}

# Role
data "aws_iam_policy_document" "external_dns_irsa" {
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0

statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
Expand All @@ -104,19 +87,16 @@ data "aws_iam_policy_document" "external_dns_irsa" {
}

resource "aws_iam_role" "external_dns" {
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0

name = "${var.cluster_name}-external-dns"
assume_role_policy = data.aws_iam_policy_document.external_dns_irsa[0].json

depends_on = [var.mod_dependency]
}

resource "aws_iam_role_policy_attachment" "external_dns" {
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0

role = aws_iam_role.external_dns[0].name
policy_arn = aws_iam_policy.external_dns[0].arn

depends_on = [var.mod_dependency]
}
68 changes: 38 additions & 30 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
data "aws_region" "current" {}

resource "helm_release" "external_dns" {
depends_on = [var.mod_dependency]
count = var.enabled ? 1 : 0
chart = var.helm_chart_name
namespace = var.k8s_namespace
name = var.helm_release_name
version = var.helm_chart_version
repository = var.helm_repo_url
locals {
k8s_irsa_role_create = var.enabled && var.k8s_rbac_create && var.k8s_service_account_create && var.k8s_irsa_role_create

set {
name = "aws.region"
value = data.aws_region.current.name
}
values = yamlencode({
"aws" : {
"region" : data.aws_region.current.name
"assumeRoleArn" : var.k8s_assume_role_arn
}
"rbac" : {
"create" : var.k8s_rbac_create
}
"serviceAccount" : {
"create" : var.k8s_service_account_create
"name" : var.k8s_service_account_name
"annotations" : {
"eks.amazonaws.com/role-arn" : local.k8s_irsa_role_create ? aws_iam_role.external_dns[0].arn : ""
}
}
})
}

set {
name = "rbac.create"
value = "true"
}
data "aws_region" "current" {}

set {
name = "serviceAccount.create"
value = "true"
}
data "utils_deep_merge_yaml" "values" {
count = var.enabled ? 1 : 0
input = compact([
local.values,
var.values
])
}

set {
name = "serviceAccount.name"
value = var.k8s_service_account_name
}
resource "helm_release" "external_dns" {
count = var.enabled ? 1 : 0
chart = var.helm_chart_name
create_namespace = var.helm_create_namespace
namespace = var.k8s_namespace
name = var.helm_release_name
version = var.helm_chart_version
repository = var.helm_repo_url

set {
name = "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
value = aws_iam_role.external_dns[0].arn
}
values = [
data.utils_deep_merge_yaml.values[0].output
]

dynamic "set" {
for_each = var.settings
Expand Down
Loading

0 comments on commit 089c191

Please sign in to comment.