0

Context: I'm reusing terraform modules and I deploy microservices using helm provider within terraform.

Problem:

I'm trying to translate this line into terraform code, to get the current image tag live from prod (in the interest of reusing it). I'm already using kubernetes provider's auth and doesn't make sense to pull kubectl in my CI just for this.

k get deploy my-deployment -n staging -o jsonpath='{$.spec.template.spec.containers[:1].image}'

Kubernetes terraform provider doesn't seem to support data blocks nor helm provider outputs blocks.

Does anyone know how could we get (read) the image tag of a deployment using terraform?

EDIT:

My deployment looks like this:

resource "helm_release" "example" {
  name       = "my-redis-release"
  repository = "https://charts.bitnami.com/bitnami"
  chart      = "redis"
  version    = "6.0.1"

  values = [
    "${file("values.yaml")}"
  ]

  set {
    name  = "image.tag"
    value = "latest"
  }

}

The tag will be a hash that will change often and passed on from another repo.

latest in this case should be replaced by the current running tag in the cluster. I can get it using kubectl, using the line above, but not sure how using terraform.

mattmin
  • 33
  • 7
  • 1
    Are you creating the Deployment with terraform? If so, you can define the output based on the resource. But I would need to see that part of the code. :) – Marko E Aug 22 '22 at 13:16
  • yeah, I'm deploying using terraform via helm provider, but it doesn't look like helm provider supports outputs https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release my deployment code's similar to the one from the example link ^ – mattmin Aug 22 '22 at 13:21
  • Outputs can be created with the arguments or attributes from the provider. Which example from that URL? It's pretty hard to reproduce what you want to achieve without any code. – Marko E Aug 22 '22 at 13:40
  • You're right, I've updated post with an example. – mattmin Aug 22 '22 at 13:56
  • In this case, I think there is no deployment specification in the chart. – Marko E Aug 22 '22 at 14:06

1 Answers1

2

It turns out there are multiple ways of doing it, where the easiest one for me is to reference the set argument of the helm_release resource:

output "helm_image_tag" {
  value = [ for setting in helm_release.example.set : setting.value if setting.name == "image.tag" ]
}

The output will then be a list where you can reference it in a shell script (or another scripting language):

  + helm_image_tag = [
      + "latest",
    ]

If the list format does not suit you, you can create a map output:

output "helm_image_tag" {
value = { for setting in helm_release.example.set : setting.name => setting.value if setting.name == "image.tag" }
}

This produces the following output:

  + helm_image_tag = {
      + "image.tag" = "latest"
    }

By using terraform output helm_image_tag you can access this output value and decide what to do with it in the CI.

Marko E
  • 13,362
  • 2
  • 19
  • 28