rancher / terraform-provider-rancher2

Terraform Rancher2 provider
https://www.terraform.io/docs/providers/rancher2/
Mozilla Public License 2.0
262 stars 226 forks source link

Get a list of all clusters? #1022

Open strowi opened 2 years ago

strowi commented 2 years ago

Hi,

i can see the "rancher_cluster(_v2)" data provider, but both require the specific name. Is there a way to get a list of all cluster (e.g. by label, filter..) that are registerd with a rancher-instance?

regards, strowi

Josh-Diamond commented 1 year ago

One option would be to send a GET request - [as admin] - to <host_url>/v3/cluster/. The payload will contain a data key, which will point to a list of all the clusters on the rancher instance, including local. Further filtering could always be applied.

flostru commented 1 year ago

I guess he means to get information about all clusters as data resource for outputs etc., fiddling with http requests from our terraform environment pipelines is kinda ugly.

Actually we need that aswell to integrate all downstream cluster names into an app on one of our clusters, all within terraform.

Until now one could use the kubernetes terraform provider from hashicorp like so:

terraform {
  required_providers {
    rancher2 = {
      source  = "rancher/rancher2"
      version = "1.25.0"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "2.18.1"
    }
  }
}

data "kubernetes_resources" "clusters" {
  api_version = "fleet.cattle.io/v1alpha1"
  kind        = "Cluster"
  namespace   = "fleet-default"
}
output "clusters" {
  value = data.kubernetes_resources.clusters.objects[*].metadata.name
}

But a native "inventory" data resource would be nice.

strowi commented 1 year ago

Didn't know that it was possible this way, thx! But yes some native way would improve handling. E.g. when i want to create the same namespace, secret etc on all clusters.

flostru commented 1 year ago

Actually better use

data "kubernetes_resources" "clusters" {
  api_version = "management.cattle.io/v3"
  kind        = "Cluster"
}
output "clusters" {
  value = data.kubernetes_resources.clusters.objects[*].spec.displayName
}

If you want the friendly names, It has more consistent output with mixed rke/rke2/eks/aks clusters

Shocktrooper commented 1 year ago

I too am looking for a native rancher way to get all clusters