/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

# [START gke_quickstart_autopilot_app]

data "google_client_config" "default" {}

provider "kubernetes" {
  host                   = "https://${google_container_cluster.default.endpoint}"
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(google_container_cluster.default.master_auth[0].cluster_ca_certificate)

  ignore_annotations = [
    "^autopilot\\.gke\\.io\\/.*",
    "^cloud\\.google\\.com\\/.*"
  ]
}


{% set default_dep = 1 %}
{% for dep in public_cluster.deployments%}
resource "kubernetes_deployment_v1" "default-{{default_net}}" {
  {% set default_dep = default_dep + 1 %}
  metadata {
    name = "{{dep.name}}" 
    labels = {
      app = "{%for cont in dep.containers%}{{cont.application.name}}{%if not loop.last%},{%endif%}{%endfor%}"   {###Only One APP#}
    }
  }

  spec {
    replicas = {{dep.replicas}}

    selector {
      match_labels = {
        app = "{%for cont in dep.containers%}{{cont.application.name}}{%if not loop.last%},{%endif%}{%endfor%}"   {###Only One APP#}
      }
    }

    template {
      metadata {
        labels = {
          app = "{%for cont in dep.containers%}{{cont.application.name}}{%if not loop.last%},{%endif%}{%endfor%}"   {###Only One APP#}
        }
      }

      spec {
        {%for container in dep.containers%}
        container {
          image = "{{container.application.image_repo}}"
          name  = "{{container.name}}"

          resources {
            limits = {
              cpu    = "{{container.resources_limit.cpu}}"
              memory = "{{container.resources_limit.memory}}Mi"
            }
            requests = {
              cpu    = "{{container.application.required_resources.cpu}}m"
              memory = "{{container.application.required_resources.memory}}Mi"
            }
          }
        }
        {%endfor%}
      }
    }
  }
depends_on = [google_project_service.compute]
}
{%endfor%}

{% set default_service = 1 %}
{% for service in public_cluster.services%}
resource "kubernetes_service_v1" "default-{{default_service}}" {
  {% set default_service = default_service + 1 %}
  metadata {
    name = "{{service.name}}"
  }

  spec {
    selector = {
      app = "{{service.application.name}}"
    }
    port {
      port        = {{service.port}}
      target_port = {{service.target_port}}
    }

    type = "{{service.type | to_str}}"
  }

  depends_on = [time_sleep.wait_service_cleanup]
}
{%endfor%}


# Provide time for Service cleanup
resource "time_sleep" "wait_service_cleanup" {
  depends_on = [google_container_cluster.default]

  destroy_duration = "180s"
}
output "load_balancer_ip" {
  value = kubernetes_service_v1.default-1.status.0.load_balancer.0.ingress.0.ip
}

# [END gke_quickstart_autopilot_app]
