feature: support additional ingress host for service exposition
This commit is contained in:
parent
7a9d1d9eb1
commit
5dcccbaf01
@ -19,6 +19,10 @@ module "image-uploader-mock" {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## running test
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ tf test
|
||||||
|
```
|
||||||
|
@ -9,9 +9,10 @@ locals {
|
|||||||
format("port-%s", index) => port if port.expose == true
|
format("port-%s", index) => port if port.expose == true
|
||||||
}
|
}
|
||||||
certificate_secret_name = format("%s-certificate", var.application_name)
|
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" {
|
resource "kubernetes_service_v1" "service" {
|
||||||
count = local.at_least_one_port
|
count = local.at_least_one_port
|
||||||
metadata {
|
metadata {
|
||||||
@ -40,7 +41,12 @@ resource "kubernetes_service_v1" "service" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resource "kubernetes_manifest" "certificate" {
|
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 = {
|
manifest = {
|
||||||
apiVersion = "cert-manager.io/v1"
|
apiVersion = "cert-manager.io/v1"
|
||||||
@ -84,8 +90,12 @@ resource "kubernetes_ingress_v1" "ingress" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
spec {
|
spec {
|
||||||
rule {
|
dynamic rule {
|
||||||
host = local.service_hostname
|
for_each = concat(
|
||||||
|
var.additional_ingress_host, [local.service_hostname]
|
||||||
|
)
|
||||||
|
content {
|
||||||
|
host = rule.value
|
||||||
http {
|
http {
|
||||||
path {
|
path {
|
||||||
path = "/"
|
path = "/"
|
||||||
@ -100,8 +110,11 @@ resource "kubernetes_ingress_v1" "ingress" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
tls {
|
tls {
|
||||||
hosts = [local.service_hostname]
|
hosts = concat(
|
||||||
|
var.additional_ingress_host, [local.service_hostname]
|
||||||
|
)
|
||||||
secret_name = local.certificate_secret_name
|
secret_name = local.certificate_secret_name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +122,12 @@ resource "kubernetes_ingress_v1" "ingress" {
|
|||||||
|
|
||||||
# {{ application_name }}.localdomain IN CNAME internal-lb
|
# {{ application_name }}.localdomain IN CNAME internal-lb
|
||||||
resource "kubernetes_manifest" "record" {
|
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 = {
|
manifest = {
|
||||||
apiVersion = "externaldns.k8s.io/v1alpha1"
|
apiVersion = "externaldns.k8s.io/v1alpha1"
|
||||||
|
12
input.tf
12
input.tf
@ -59,3 +59,15 @@ variable "replicas" {
|
|||||||
default = 1
|
default = 1
|
||||||
description = "number of replicas for the application's pod"
|
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"
|
||||||
|
}
|
@ -1,6 +1,3 @@
|
|||||||
run "terraform-plan" {
|
|
||||||
command = plan
|
|
||||||
|
|
||||||
variables {
|
variables {
|
||||||
application_name = "test-application"
|
application_name = "test-application"
|
||||||
namespace = "test-namespace"
|
namespace = "test-namespace"
|
||||||
@ -10,6 +7,9 @@ run "terraform-plan" {
|
|||||||
replicas = 2
|
replicas = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run "test_deployment_classic" {
|
||||||
|
command = plan
|
||||||
|
|
||||||
assert {
|
assert {
|
||||||
condition = var.application_name == "test-application"
|
condition = var.application_name == "test-application"
|
||||||
error_message = "incorrect application name"
|
error_message = "incorrect application name"
|
||||||
@ -20,3 +20,40 @@ run "terraform-plan" {
|
|||||||
error_message = "invalid number of replicas"
|
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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user