Skip to content

Commit

Permalink
Override or add configuration options in values input (#7)
Browse files Browse the repository at this point in the history
Changes:
* Add ability to specify custom configuration options as values yaml
* Delegate Kubernetes namespace creation to helm provider from Kubernetes provider
* Bump default chart version to the 9.10.3
* Update terraform docs version in pre-commit

Breaking changes:
* Removes fake dependency variable mod_dependency in favor of terraform built-in module dependency injection
  • Loading branch information
dojci authored Aug 30, 2021
1 parent 1d8a470 commit 86dd6dd
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 134 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
60 changes: 32 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,52 @@ See [Basic example](examples/basic/README.md) for further information.

| Name | Version |
|------|---------|
| terraform | >= 0.13 |
| aws | >= 2.0 |
| helm | >= 1.0 |
| kubernetes | >= 1.10 |
| <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.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_role.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [helm_release.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [aws_iam_policy_document.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.cluster_autoscaler_assume](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` | `"cluster-autoscaler"` | no |
| helm\_chart\_version | Version of the Helm chart | `string` | `"9.7.0"` | no |
| helm\_release\_name | Helm release name | `string` | `"cluster-autoscaler"` | no |
| helm\_repo\_url | Helm repository | `string` | `"https://kubernetes.github.io/autoscaler"` | no |
| k8s\_namespace | The K8s namespace in which the node-problem-detector service account has been created | `string` | `"cluster-autoscaler"` | no |
| k8s\_service\_account\_name | The k8s cluster-autoscaler service account name | `string` | `"cluster-autoscaler"` | no |
| mod\_dependency | Dependence variable binds all AWS resources allocated by this module, dependent modules reference this variable | `bool` | `null` | no |
| settings | Additional settings which will be passed to the Helm chart values, see https://hub.helm.sh/charts/stable/cluster-autoscaler | `map(any)` | `{}` | no |
| values | Additional yaml encoded values which will be passed to the Helm chart, see https://hub.helm.sh/charts/stable/cluster-autoscaler | `string` | `""` | 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` | `"cluster-autoscaler"` | no |
| <a name="input_helm_chart_version"></a> [helm\_chart\_version](#input\_helm\_chart\_version) | Version of the Helm chart | `string` | `"9.10.3"` | no |
| <a name="input_helm_create_namespace"></a> [helm\_create\_namespace](#input\_helm\_create\_namespace) | Create the namespace if it does not yet exist | `bool` | `true` | no |
| <a name="input_helm_release_name"></a> [helm\_release\_name](#input\_helm\_release\_name) | Helm release name | `string` | `"cluster-autoscaler"` | no |
| <a name="input_helm_repo_url"></a> [helm\_repo\_url](#input\_helm\_repo\_url) | Helm repository | `string` | `"https://kubernetes.github.io/autoscaler"` | 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 node-problem-detector service account has been created | `string` | `"cluster-autoscaler"` | 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 cluster-autoscaler service account name | `string` | `"cluster-autoscaler"` | 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/stable/cluster-autoscaler | `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/stable/cluster-autoscaler | `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 |
|------|--------|---------|
| cluster_autoscaler | ../../ | |
| eks_cluster | cloudposse/eks-cluster/aws | |
| eks_workers | cloudposse/eks-workers/aws | |
| vpc | terraform-aws-modules/vpc/aws | |
| <a name="module_cluster_autoscaler"></a> [cluster\_autoscaler](#module\_cluster\_autoscaler) | ../../ | n/a |
| <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_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 -->
36 changes: 15 additions & 21 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
source = "terraform-aws-modules/vpc/aws"
version = "3.6.0"

name = "cluster-autoscaler-vpc"
cidr = "10.0.0.0/16"
Expand All @@ -9,35 +10,28 @@ module "vpc" {
}

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]
}

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 = "cluster-autoscaler"
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 "cluster_autoscaler" {
source = "../../"

Expand Down
6 changes: 0 additions & 6 deletions examples/basic/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ data "aws_eks_cluster_auth" "this" {
name = module.eks_cluster.eks_cluster_id
}

provider "kubernetes" {
host = data.aws_eks_cluster.this.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.this.certificate_authority.0.data)
token = data.aws_eks_cluster_auth.this.token
}

provider "helm" {
kubernetes {
host = data.aws_eks_cluster.this.endpoint
Expand Down
25 changes: 5 additions & 20 deletions iam.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
resource "kubernetes_namespace" "cluster_autoscaler" {
depends_on = [var.mod_dependency]
count = (var.enabled && var.k8s_namespace != "kube-system") ? 1 : 0

metadata {
name = var.k8s_namespace
}
}

### iam ###
# Policy
data "aws_iam_policy_document" "cluster_autoscaler" {
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0

statement {
sid = "Autoscaling"
Expand All @@ -35,18 +24,16 @@ data "aws_iam_policy_document" "cluster_autoscaler" {
}

resource "aws_iam_policy" "cluster_autoscaler" {
depends_on = [var.mod_dependency]
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0
name = "${var.cluster_name}-cluster-autoscaler"
path = "/"
description = "Policy for cluster-autoscaler service"

policy = data.aws_iam_policy_document.cluster_autoscaler[0].json
}

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

statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
Expand All @@ -70,15 +57,13 @@ data "aws_iam_policy_document" "cluster_autoscaler_assume" {
}

resource "aws_iam_role" "cluster_autoscaler" {
depends_on = [var.mod_dependency]
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0
name = "${var.cluster_name}-cluster-autoscaler"
assume_role_policy = data.aws_iam_policy_document.cluster_autoscaler_assume[0].json
}

resource "aws_iam_role_policy_attachment" "cluster_autoscaler" {
depends_on = [var.mod_dependency]
count = var.enabled ? 1 : 0
count = local.k8s_irsa_role_create ? 1 : 0
role = aws_iam_role.cluster_autoscaler[0].name
policy_arn = aws_iam_policy.cluster_autoscaler[0].arn
}
62 changes: 38 additions & 24 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
locals {
k8s_irsa_role_create = var.enabled && var.k8s_rbac_create && var.k8s_service_account_create && var.k8s_irsa_role_create

values = yamlencode({
"awsRegion" : data.aws_region.current.name,
"autoDiscovery" : {
"clusterName" : var.cluster_name
},
"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.cluster_autoscaler[0].arn : ""
}
}
}
})
}

data "aws_region" "current" {}

data "utils_deep_merge_yaml" "values" {
count = var.enabled ? 1 : 0
input = compact([
local.values,
var.values
])
}

resource "helm_release" "cluster_autoscaler" {
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
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

values = [
yamlencode({
"awsRegion" : data.aws_region.current.name,
"autoDiscovery" : {
"clusterName" : var.cluster_name
},
"rbac" : {
"create" : true,
"serviceAccount" : {
"create" : true,
"name" : var.k8s_service_account_name
"annotations" : {
"eks.amazonaws.com/role-arn" : aws_iam_role.cluster_autoscaler[0].arn
}
}
}
}),
var.values]
data.utils_deep_merge_yaml.values[0].output
]

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

0 comments on commit 86dd6dd

Please sign in to comment.