Last active
April 9, 2024 20:44
-
-
Save soerenmartius/32dc944ba19b896ed25b00d4c300b55d to your computer and use it in GitHub Desktop.
Kubernetes Provider Generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Example on how to generate a more specific provider such as Kubernetes | |
# The deployment trigger is used to deferr the data source at plan time if it's not available quick is a commonly known workaround in Terraform | |
# In TF 1.9 this will most likely be fixes by finally allowing plan time with values that are not yet available natively https://github.com/hashicorp/terraform/releases/tag/v1.9.0-alpha20240404 | |
globals "terraform" "providers" "kubernetes" { | |
source = "hashicorp/kubernetes" | |
version = "~> 2.16" | |
postpone_init_to_apply = true | |
enabled = true | |
} | |
generate_hcl "kubernetes_provider.tf" { | |
condition = tm_try(global.terraform.providers.kubernetes.enabled, false) | |
# Let's allow you define local variable available to the currrent generate_hcl block only | |
lets { | |
location = "some-location" | |
name = "some-name" # could be defined based on the hierachy or stack name | |
} | |
content { | |
tm_dynamic "resource" { | |
condition = tm_try(global.terraform.providers.kubernetes.postpone_init_to_apply, false) | |
labels = ["null_resource", "initial_deployment_trigger"] | |
attributes = {} | |
} | |
tm_dynamic "data" { | |
condition = tm_try(global.terraform.providers.kubernetes.postpone_init_to_apply, false) | |
labels = ["google_container_cluster", "cluster"] | |
content { | |
location = let.location | |
name = let.name | |
depends_on = [null_resource.initial_deployment_trigger] | |
} | |
} | |
tm_dynamic "data" { | |
condition = tm_try(!global.terraform.providers.kubernetes.postpone_init_to_apply, true) | |
labels = ["google_container_cluster", "cluster"] | |
content { | |
location = let.location | |
name = let.name | |
} | |
} | |
data "google_client_config" "current" { | |
} | |
provider "kubernetes" { | |
host = "https://${data.google_container_cluster.cluster.endpoint}" | |
token = data.google_client_config.current.access_token | |
cluster_ca_certificate = base64decode(data.google_container_cluster.cluster.master_auth.0.cluster_ca_certificate) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment