#!/bin/bash
<%#
 Copyright 2013-2026 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 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

      https://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.
-%>
# Files are ordered in proper order with needed wait for the dependent custom resource definitions to get initialized.
# Usage: bash kubectl-apply.sh

usage(){
 cat << EOF

 Usage: $0 -f
 Description: To apply k8s manifests using the default \`kubectl apply -f\` command
[OR]
 Usage: $0 -k
 Description: To apply k8s manifests using the kustomize \`kubectl apply -k\` command
[OR]
 Usage: $0 -s
 Description: To apply k8s manifests using the skaffold binary \`skaffold run\` command

EOF
exit 0
}

logSummary() {
    echo ""
<%_ if (monitoringPrometheus || kubernetesServiceTypeIngress || istio) { _%>
        echo "#####################################################"
        echo "Please find the below useful endpoints,"
  <%_ if (monitoringPrometheus) { _%>
        echo "JHipster Grafana - http://jhipster-grafana.<%= kubernetesNamespace %>.<%= ingressDomain %>"
  <%_ } _%>
  <%_ if (kubernetesServiceTypeIngress) { _%>
    <%_ appConfigs.forEach((appConfig) =>  { _%>
      <%_ if (appConfig.applicationTypeGateway) {_%>
        echo "Gateway - http://<%= appConfig.baseName.toLowerCase() %>.<%= kubernetesNamespace %>.<%= ingressDomain %>"
      <%_ } _%>
      <%_ if (appConfig.applicationTypeMonolith) {_%>
        echo "<%= appConfig.baseName %> - http://<%= appConfig.baseName.toLowerCase() %>.<%= kubernetesNamespace %>.<%= ingressDomain %>"
      <%_ } _%>
    <%_ }) _%>
    <%_ if (istio) { _%>
        echo "Zipkin - http://zipkin.istio-system.<%= ingressDomain %>"
        echo "Grafana - http://grafana.istio-system.<%= ingressDomain %>"
        echo "Kiali - http://kiali.istio-system.<%= ingressDomain %>"
    <%_ } _%>
  <%_ } _%>
  <%_ if (useKeycloak) { _%>
      <%_ if (ingressTypeGke) { _%>
        echo "Keycloak - https://keycloak.<%= kubernetesNamespace %>.<%= ingressDomain %>"
      <%_ } _%>
      <%_ if (ingressTypeNginx) { _%>
        echo "Keycloak - http://keycloak.<%= kubernetesNamespace %>.<%= ingressDomain %>"
      <%_ } _%>
  <%_ } _%>
        echo "#####################################################"
<%_ } _%>
}

default() {
    suffix=k8s
<%_ if (!kubernetesNamespaceDefault) { _%>
    kubectl apply -f namespace.yml
<%_ } _%> <%_ if (istio) { _%>
    kubectl label namespace <%-kubernetesNamespace%> istio-injection=enabled --overwrite=true
<%_ } _%>
<%_ if (useKeycloak) { _%>
    <%_ if (ingressTypeGke) { _%>
    kubectl apply -f cert-manager/
    <%_ } _%>
    kubectl apply -f keycloak-${suffix}/
<%_ } _%>
<%_ if (serviceDiscoveryEureka || serviceDiscoveryConsul) { _%>
    kubectl apply -f registry-${suffix}/
    <%_ } _%> <%_ appConfigs.forEach((appConfig) =>  { _%>
    kubectl apply -f <%- appConfig.baseName.toLowerCase() %>-${suffix}/
<%_ }) _%>
<%_ if (useKafka) { _%>
    kubectl apply -f messagebroker-${suffix}/
<%_ } _%> <%_ if (monitoringPrometheus) { _%>
    kubectl apply -f monitoring-${suffix}/jhipster-prometheus-crd.yml
    until [ $(kubectl get crd prometheuses.monitoring.coreos.com 2>>/dev/null | wc -l) -ge 2 ]; do
        echo "Waiting for the custom resource prometheus operator to get initialised";
        sleep 5;
    done
    kubectl apply -f monitoring-${suffix}/jhipster-prometheus-cr.yml
    kubectl apply -f monitoring-${suffix}/jhipster-grafana.yml
    kubectl apply -f monitoring-${suffix}/jhipster-grafana-dashboard.yml
<%_ } _%>

<%_ if (istio && kubernetesServiceTypeIngress) { _%>
    kubectl apply -f istio-${suffix}/
<%_ } _%>
}

kustomize() {
    kubectl apply -k ./
}

scaffold() {
    // this will build the source and apply the manifests the K8s target. To turn the working directory
    // into a CI/CD space, initilaize it with `skaffold dev`
    skaffold run
}

[[ "$@" =~ ^-[fks]{1}$ ]]  || usage;

while getopts ":fks" opt; do
    case ${opt} in
    f ) echo "Applying default \`kubectl apply -f\`"; default ;;
    k ) echo "Applying kustomize \`kubectl apply -k\`"; kustomize ;;
    s ) echo "Applying using skaffold \`skaffold run\`"; scaffold ;;
    \? | * ) usage ;;
    esac
done

logSummary
