From f85e19a4feeb8e2fa03e85a450ffdddb5b660c69 Mon Sep 17 00:00:00 2001 From: canavar Date: Tue, 3 Sep 2019 17:09:17 +0300 Subject: [PATCH] multiple namespace deployments through count --- README.md | 8 ++++++-- main.tf | 42 +++++++++++++++++++++--------------------- outputs.tf | 4 ++++ variables.tf | 8 ++++++-- 4 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 outputs.tf diff --git a/README.md b/README.md index 0b5e270..bad7f23 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,12 @@ This module creates following resources; ## Inputs -- **namespace** : kubernetes namespace to be deployed -- **replicacount** : replica instance count for Ingress Controller +- **namespaces::[string]** : kubernetes namespaces to be deployed, **list** of namespaces +- **replicacount** : replica instance count for Ingress Controller + +## Outputs + +- **load_balancer_ip** : list of LoadBalancer IPs from GKE ## Dependencies diff --git a/main.tf b/main.tf index 95efe62..f4cf829 100644 --- a/main.tf +++ b/main.tf @@ -1,35 +1,35 @@ resource "kubernetes_config_map" "nginx_configuration" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "nginx-configuration" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } } resource "kubernetes_config_map" "tcp_services" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "tcp-services" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } } resource "kubernetes_config_map" "udp_services" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "udp-services" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } } resource "kubernetes_service_account" "nginx_ingress_serviceaccount" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "nginx-ingress-serviceaccount" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } } @@ -72,10 +72,10 @@ resource "kubernetes_cluster_role" "nginx_ingress_clusterrole" { } resource "kubernetes_role" "nginx_ingress_role" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "nginx-ingress-role" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } rule { @@ -102,16 +102,16 @@ resource "kubernetes_role" "nginx_ingress_role" { } resource "kubernetes_role_binding" "nginx_ingress_role_nisa_binding" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "nginx-ingress-role-nisa-binding" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } subject { kind = "ServiceAccount" name = "nginx-ingress-serviceaccount" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] } role_ref { api_group = "rbac.authorization.k8s.io" @@ -121,15 +121,15 @@ resource "kubernetes_role_binding" "nginx_ingress_role_nisa_binding" { } resource "kubernetes_cluster_role_binding" "nginx_ingress_clusterrole_nisa_binding" { - count = length(var.environments) + count = length(var.namespaces) metadata { - name = "${var.environments[count.index]}-nginx-ingress-clusterrole-nisa-binding" + name = "${var.namespaces[count.index]}-nginx-ingress-clusterrole-nisa-binding" labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } subject { kind = "ServiceAccount" name = "nginx-ingress-serviceaccount" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] } role_ref { api_group = "rbac.authorization.k8s.io" @@ -139,14 +139,14 @@ resource "kubernetes_cluster_role_binding" "nginx_ingress_clusterrole_nisa_bindi } resource "kubernetes_deployment" "nginx_ingress_controller" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "nginx-ingress-controller" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } spec { - replicas = "${var.replicacount}" + replicas = var.replicacount selector { match_labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } @@ -224,10 +224,10 @@ resource "kubernetes_deployment" "nginx_ingress_controller" { } resource "kubernetes_service" "ingress_nginx" { - count = length(var.environments) + count = length(var.namespaces) metadata { name = "ingress-nginx" - namespace = var.environments[count.index] + namespace = var.namespaces[count.index] labels = { "app.kubernetes.io/name" = "ingress-nginx", "app.kubernetes.io/part-of" = "ingress-nginx" } } spec { diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..2f60ffa --- /dev/null +++ b/outputs.tf @@ -0,0 +1,4 @@ +output "load_balancer_ip" { + description = "IP address of the load balancer for nginx ingress controllers" + value = "${kubernetes_service.ingress_nginx.*.load_balancer_ingress.0.ip}" +} diff --git a/variables.tf b/variables.tf index 8983ad8..e1b51d7 100644 --- a/variables.tf +++ b/variables.tf @@ -1,2 +1,6 @@ -variable "namespace" {} -variable "replicacount" {} +variable "namespaces" { + type = list(string) +} +variable "replicacount" { + type = number +}