I'm using terraform in deployment. I have a list of deployment and using for_each on deployment. Based on string condition I need to add a sidecar.
Briefly, I need to write a condition to add a sidecar image.
Something like it:
${substr(each.key, 0, 3) == "tst" ? 1 : 0 }
Is there a way to achieve that?
Here is my deployment tf:
resource "kubernetes_deployment" "x" {
for_each = data
metadata {
name = each.key
labels = {
app = each.key
name = each.key
}
namespace = var.namespace
}
spec {
replicas = 1
selector {
match_labels = {
app = each.key
}
}
template {
metadata {
labels = {
app = each.key
name = each.key
}
}
spec {
service_account_name = "default"
container {
image = each.value
image_pull_policy = "Always"
name = each.key
}
// I need a condtion to create the second container
container {
image = "sidecar_image"
image_pull_policy = "Always"
name = "sidecar-container"
port {
name = "default-port"
container_port = 50050
}
}
restart_policy = "Always"
}
}
}
}