# Copyright 2022 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.

timeout: 7200s # 2hr
tags:
  - infra-features-gke-prod-mesh-confirm
substitutions:
  _PROJECT_ID: ${PROJECT_ID}
steps:
# Confirm ASM installation on GKE Prod
# Docs:
# 1. Configure managed Anthos Service Mesh with fleet API -> https://cloud.google.com/service-mesh/docs/managed/auto-control-plane-with-fleet
- name: "gcr.io/cloud-builders/gcloud"
  id: "infra-features-gke-prod-mesh-confirm"
  entrypoint: bash
  dir: "infra"
  args:
    - -c
    - |
      echo -e "Verifying ASM on clusters. If you're running this for the first time, this can take 10-15 minutes..."

      source vars.sh
      export PROJECT_NUMBER=$(gcloud projects describe ${_PROJECT_ID} --format 'value(projectNumber)')
      export PROD1_MEMBERSHIP="projects/$${PROJECT_NUMBER}/locations/global/memberships/$${GKE_PROD1_NAME}"
      export PROD2_MEMBERSHIP="projects/$${PROJECT_NUMBER}/locations/global/memberships/$${GKE_PROD2_NAME}"
      export PROD3_MEMBERSHIP="projects/$${PROJECT_NUMBER}/locations/global/memberships/$${GKE_PROD3_NAME}"
      export PROD4_MEMBERSHIP="projects/$${PROJECT_NUMBER}/locations/global/memberships/$${GKE_PROD4_NAME}"
      export PROD5_MEMBERSHIP="projects/$${PROJECT_NUMBER}/locations/global/memberships/$${GKE_PROD5_NAME}"
      export PROD6_MEMBERSHIP="projects/$${PROJECT_NUMBER}/locations/global/memberships/$${GKE_PROD6_NAME}"
      export PROD1_MEMBERSHIP_ACTIVE=NOT_ACTIVE
      until [[ $$PROD1_MEMBERSHIP_ACTIVE == "ACTIVE" ]]
      do
          export PROD1_MEMBERSHIP_ACTIVE=$(gcloud container fleet mesh describe --project ${_PROJECT_ID} --format "value(membershipStates[$${PROD1_MEMBERSHIP}].servicemesh.controlPlaneManagement.state)")
          sleep 5
          echo -e "PROD1_MEMBERSHIP_ACTIVE is $$PROD1_MEMBERSHIP_ACTIVE"
      done
      export PROD2_MEMBERSHIP_ACTIVE=NOT_ACTIVE
      until [[ $$PROD2_MEMBERSHIP_ACTIVE == "ACTIVE" ]]
      do
          export PROD2_MEMBERSHIP_ACTIVE=$(gcloud container fleet mesh describe --project ${_PROJECT_ID} --format "value(membershipStates[$${PROD2_MEMBERSHIP}].servicemesh.controlPlaneManagement.state)")
          sleep 5
          echo -e "PROD2_MEMBERSHIP_ACTIVE is $$PROD2_MEMBERSHIP_ACTIVE"
      done
      export PROD3_MEMBERSHIP_ACTIVE=NOT_ACTIVE
      until [[ $$PROD3_MEMBERSHIP_ACTIVE == "ACTIVE" ]]
      do
          export PROD3_MEMBERSHIP_ACTIVE=$(gcloud container fleet mesh describe --project ${_PROJECT_ID} --format "value(membershipStates[$${PROD3_MEMBERSHIP}].servicemesh.controlPlaneManagement.state)")
          sleep 5
          echo -e "PROD3_MEMBERSHIP_ACTIVE is $$PROD3_MEMBERSHIP_ACTIVE"
      done
      export PROD4_MEMBERSHIP_ACTIVE=NOT_ACTIVE
      until [[ $$PROD4_MEMBERSHIP_ACTIVE == "ACTIVE" ]]
      do
          export PROD4_MEMBERSHIP_ACTIVE=$(gcloud container fleet mesh describe --project ${_PROJECT_ID} --format "value(membershipStates[$${PROD4_MEMBERSHIP}].servicemesh.controlPlaneManagement.state)")
          sleep 5
          echo -e "PROD4_MEMBERSHIP_ACTIVE is $$PROD4_MEMBERSHIP_ACTIVE"
      done
      export PROD5_MEMBERSHIP_ACTIVE=NOT_ACTIVE
      until [[ $$PROD5_MEMBERSHIP_ACTIVE == "ACTIVE" ]]
      do
          export PROD5_MEMBERSHIP_ACTIVE=$(gcloud container fleet mesh describe --project ${_PROJECT_ID} --format "value(membershipStates[$${PROD5_MEMBERSHIP}].servicemesh.controlPlaneManagement.state)")
          sleep 5
          echo -e "PROD5_MEMBERSHIP_ACTIVE is $$PROD5_MEMBERSHIP_ACTIVE"
      done
      export PROD6_MEMBERSHIP_ACTIVE=NOT_ACTIVE
      until [[ $$PROD6_MEMBERSHIP_ACTIVE == "ACTIVE" ]]
      do
          export PROD6_MEMBERSHIP_ACTIVE=$(gcloud container fleet mesh describe --project ${_PROJECT_ID} --format "value(membershipStates[$${PROD6_MEMBERSHIP}].servicemesh.controlPlaneManagement.state)")
          sleep 5
          echo -e "PROD6_MEMBERSHIP_ACTIVE is $$PROD6_MEMBERSHIP_ACTIVE"
      done
# Confirm MCP installation on GKE Prod
# Docs:
# 1. Configure managed Anthos Service Mesh with fleet API -> https://cloud.google.com/service-mesh/docs/managed/auto-control-plane-with-fleet
- name: "gcr.io/cloud-builders/gcloud"
  id: "infra-features-gke-prod-mesh-cpr-confirm"
  entrypoint: bash
  dir: "infra"
  args:
    - -c
    - |
      source vars.sh
      echo "If you're running this for the first time, this step can take a long time..."
      while [[ $(gcloud container clusters list --project ${_PROJECT_ID} --filter "STATUS=RUNNING" --format="value(name)"| wc -l | awk '{print $1}') < "6" ]]; do
        echo "Waiting for all  the cluster to be RUNNING."
        sleep 5
      done
      gcloud container clusters get-credentials $${GKE_PROD1_NAME} --region $${GKE_PROD_REGION_1} --project ${_PROJECT_ID}
      gcloud container clusters get-credentials $${GKE_PROD2_NAME} --region $${GKE_PROD_REGION_1} --project ${_PROJECT_ID}
      gcloud container clusters get-credentials $${GKE_PROD3_NAME} --region $${GKE_PROD_REGION_1} --project ${_PROJECT_ID}
      gcloud container clusters get-credentials $${GKE_PROD4_NAME} --region $${GKE_PROD_REGION_2} --project ${_PROJECT_ID}
      gcloud container clusters get-credentials $${GKE_PROD5_NAME} --region $${GKE_PROD_REGION_2} --project ${_PROJECT_ID}
      gcloud container clusters get-credentials $${GKE_PROD6_NAME} --region $${GKE_PROD_REGION_2} --project ${_PROJECT_ID}
      kubectl --context=gke_${_PROJECT_ID}_$${GKE_PROD_REGION_1}_$${GKE_PROD1_NAME} wait --for=condition=ProvisioningFinished controlplanerevision asm-managed -n istio-system --timeout 15m
      kubectl --context=gke_${_PROJECT_ID}_$${GKE_PROD_REGION_1}_$${GKE_PROD2_NAME} wait --for=condition=ProvisioningFinished controlplanerevision asm-managed -n istio-system --timeout 15m
      kubectl --context=gke_${_PROJECT_ID}_$${GKE_PROD_REGION_1}_$${GKE_PROD3_NAME} wait --for=condition=ProvisioningFinished controlplanerevision asm-managed -n istio-system --timeout 15m
      kubectl --context=gke_${_PROJECT_ID}_$${GKE_PROD_REGION_2}_$${GKE_PROD4_NAME} wait --for=condition=ProvisioningFinished controlplanerevision asm-managed -n istio-system --timeout 15m
      kubectl --context=gke_${_PROJECT_ID}_$${GKE_PROD_REGION_2}_$${GKE_PROD5_NAME} wait --for=condition=ProvisioningFinished controlplanerevision asm-managed -n istio-system --timeout 15m
      kubectl --context=gke_${_PROJECT_ID}_$${GKE_PROD_REGION_2}_$${GKE_PROD6_NAME} wait --for=condition=ProvisioningFinished controlplanerevision asm-managed -n istio-system --timeout 15m