feature: support additional ingress host for service exposition

This commit is contained in:
RouxAntoine 2024-09-25 22:06:35 +02:00
parent 7a9d1d9eb1
commit 5dcccbaf01
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
4 changed files with 107 additions and 36 deletions

View File

@ -19,6 +19,10 @@ module "image-uploader-mock" {
}
]
}
```
## running test
```shell
$ tf test
```

View File

@ -9,9 +9,10 @@ locals {
format("port-%s", index) => port if port.expose == true
}
certificate_secret_name = format("%s-certificate", var.application_name)
at_least_one_port_exposed = length(local.exposed_ports_map) > 0 ? 1 : 0
at_least_one_port_exposed = length(local.exposed_ports_map) > 0
}
resource "kubernetes_service_v1" "service" {
count = local.at_least_one_port
metadata {
@ -40,7 +41,12 @@ resource "kubernetes_service_v1" "service" {
}
resource "kubernetes_manifest" "certificate" {
count = local.at_least_one_port_exposed
# at_least_one_port_exposed is_test result
# 0 0 0
# 0 1 0
# 1 0 1
# 1 1 0
count = local.at_least_one_port_exposed && !var.is_test ? 1 : 0
manifest = {
apiVersion = "cert-manager.io/v1"
@ -84,8 +90,12 @@ resource "kubernetes_ingress_v1" "ingress" {
}
}
spec {
rule {
host = local.service_hostname
dynamic rule {
for_each = concat(
var.additional_ingress_host, [local.service_hostname]
)
content {
host = rule.value
http {
path {
path = "/"
@ -100,8 +110,11 @@ resource "kubernetes_ingress_v1" "ingress" {
}
}
}
}
tls {
hosts = [local.service_hostname]
hosts = concat(
var.additional_ingress_host, [local.service_hostname]
)
secret_name = local.certificate_secret_name
}
}
@ -109,7 +122,12 @@ resource "kubernetes_ingress_v1" "ingress" {
# {{ application_name }}.localdomain IN CNAME internal-lb
resource "kubernetes_manifest" "record" {
count = local.at_least_one_port_exposed
# at_least_one_port_exposed is_test result
# 0 0 0
# 0 1 0
# 1 0 1
# 1 1 0
count = local.at_least_one_port_exposed && !var.is_test ? 1 : 0
manifest = {
apiVersion = "externaldns.k8s.io/v1alpha1"

View File

@ -59,3 +59,15 @@ variable "replicas" {
default = 1
description = "number of replicas for the application's pod"
}
variable "additional_ingress_host" {
type = list(string)
default = []
description = "list of additional ingress host allowed for this service"
}
variable "is_test" {
type = bool
default = false
description = "mode to declare if the module is run in terraform test mode or in classical mode"
}

View File

@ -1,6 +1,3 @@
run "terraform-plan" {
command = plan
variables {
application_name = "test-application"
namespace = "test-namespace"
@ -10,6 +7,9 @@ run "terraform-plan" {
replicas = 2
}
run "test_deployment_classic" {
command = plan
assert {
condition = var.application_name == "test-application"
error_message = "incorrect application name"
@ -20,3 +20,40 @@ run "terraform-plan" {
error_message = "invalid number of replicas"
}
}
run "test_deployment_custom_additional_ingress_host" {
command = plan
variables {
is_test = true
ports = [
{
container_port = 8083
expose = true
}
]
additional_ingress_host = ["additional-hostname.localdomain"]
}
assert {
condition = alltrue(flatten([
for ingress in values(kubernetes_ingress_v1.ingress) : [
for ingressSpec in ingress.spec :
contains(ingressSpec.rule.*.host, "additional-hostname.localdomain")
]
]))
error_message = "additional dns not add in ingress host rule"
}
assert {
condition = anytrue(flatten([
for ingress in values(kubernetes_ingress_v1.ingress) : [
for ingressSpec in ingress.spec : [
for ingressSpecTls in ingressSpec.tls :
contains(ingressSpecTls.hosts, "additional-hostname.localdomain")
]
]
]))
error_message = "additional dns not add in ingress tls hosts"
}
}