Skip to content

Commit

Permalink
multiple namespace deployments through count
Browse files Browse the repository at this point in the history
  • Loading branch information
yaman committed Sep 3, 2019
1 parent c10c96b commit f85e19a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 21 additions & 21 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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" }
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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" }
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
8 changes: 6 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
variable "namespace" {}
variable "replicacount" {}
variable "namespaces" {
type = list(string)
}
variable "replicacount" {
type = number
}

0 comments on commit f85e19a

Please sign in to comment.