1+ # main.tf (FOR K8S APP UNIT)
2+
3+ # -----------------------------------------------------------------------------
4+ # PROVIDER CONFIGURATION
5+ # -----------------------------------------------------------------------------
6+
7+ provider "google" {
8+ project = var. tenant_project_id
9+ }
10+
11+ # -----------------------------------------------------------------------------
12+ # DATA SOURCE: GET GKE CLUSTER CREDENTIALS
13+ # -----------------------------------------------------------------------------
14+ # Use the input variables (from Unit 1) to find the cluster and get its auth data.
15+ # -----------------------------------------------------------------------------
16+
17+ data "google_container_cluster" "cluster" {
18+ project = var. tenant_project_id
19+ name = var. gke_cluster_name
20+ location = var. gke_cluster_location
21+ }
22+
23+ # -----------------------------------------------------------------------------
24+ # PROVIDER CONFIGURATION: KUBERNETES & HELM
25+ # -----------------------------------------------------------------------------
26+ # Configure the Kubernetes and Helm providers using the cluster data we just fetched.
27+ # -----------------------------------------------------------------------------
28+
29+ provider "kubernetes" {
30+ host = " https://${ data . google_container_cluster . cluster . endpoint } "
31+ cluster_ca_certificate = base64decode (data. google_container_cluster . cluster . master_auth [0 ]. cluster_ca_certificate )
32+ token = data. google_client_config . default . access_token
33+ }
34+
35+ provider "helm" {
36+ kubernetes = {
37+ host = " https://${ data . google_container_cluster . cluster . endpoint } "
38+ cluster_ca_certificate = base64decode (data. google_container_cluster . cluster . master_auth [0 ]. cluster_ca_certificate )
39+ token = data.google_client_config.default.access_token
40+ }
41+ }
42+
43+ data "google_client_config" "default" {}
44+
45+ # -----------------------------------------------------------------------------
46+ # RESOURCE: DEPLOY NGINX VIA HELM
47+ # -----------------------------------------------------------------------------
48+ # This creates a Helm release for NGINX from the public Bitnami chart repo.
49+ # We set the service type to LoadBalancer so we can get an external IP.
50+ # -----------------------------------------------------------------------------
51+
52+ resource "helm_release" "nginx_ingress" {
53+ name = " my-nginx"
54+ repository = " https://charts.bitnami.com/bitnami"
55+ chart = " nginx"
56+ version = " 15.5.1" # Pinning version is best practice
57+ namespace = " default"
58+
59+ # Set values for the Helm chart
60+ set = [
61+ {
62+ name = " service.type"
63+ value = " ClusterIP"
64+ }
65+ ]
66+ }
67+
68+ resource "time_sleep" "wait_for_service_ip" {
69+ # This sleep will only start AFTER the helm release is "done"
70+ depends_on = [helm_release . nginx_ingress ]
71+
72+ create_duration = " 60s"
73+ }
0 commit comments