#######################################################################################
# Copyright ETSI Contributors and Others.
#
# 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.
#######################################################################################

# Retrieves Minio connection info data

# Internal services and ports
export MINIO_INTERNAL_CONSOLE_HOST=console.minio-operator
export MINIO_INTERNAL_TENANT_HOST=minio.${MINIO_TENANT_NAME}
export MINIO_CONSOLE_HTTP_PORT=$(kubectl get svc/console -n minio-operator -o jsonpath='{.spec.ports[?(.name=="http")].port}')
export MINIO_CONSOLE_HTTPS_PORT=$(kubectl get svc/console -n minio-operator -o jsonpath='{.spec.ports[?(.name=="https")].port}')
export MINIO_TENANT_HTTPS_PORT=$(kubectl get svc/minio -n ${MINIO_TENANT_NAME} -o jsonpath='{.spec.ports[?(.name=="https-minio")].port}')
# NOTE: There is no HTTP port for the Minio Tenant. For HTTP-like accesses, use the `--insecure` flag in Minio client.

# Final Minio's Console HTTP service
## In case it is behind an Ingress
if [[ -n $(kubectl get ingress/minio-console-ingress -n minio-operator 2> /dev/null) ]]
then
    # Retrieves the external host name
    INGRESS_IP=$(kubectl get svc/ingress-nginx-controller -n ingress-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    export MINIO_INGRESS_CONSOLE_HOST="console.s3.${INGRESS_IP}.nip.io"

    # Uses the external host name as the default console endpoint
    export MINIO_CONSOLE_HOST=${MINIO_INGRESS_CONSOLE_HOST}
## Otherwise just uses the internal service name
else
    export MINIO_CONSOLE_HOST=${MINIO_INTERNAL_CONSOLE_HOST}
fi
export MINIO_CONSOLE_URL="http://${MINIO_CONSOLE_HOST}"

# Final Minio's Tenant HTTPS service
## In case it is behind an Ingress
if [[ -n $(kubectl get ingress/minio-tenant-ingress -n ${MINIO_TENANT_NAME} 2> /dev/null) ]]
then
    # Retrieves the external host name
    INGRESS_IP=$(kubectl get svc/ingress-nginx-controller -n ingress-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    export MINIO_INGRESS_TENANT_HOST="${MINIO_TENANT_NAME}.s3.${INGRESS_IP}.nip.io"

    # Uses the external host name as the default console endpoint
    export MINIO_TENANT_HOST=${MINIO_INGRESS_TENANT_HOST}
## Otherwise just uses the internal service name
else
    export MINIO_TENANT_HOST=${MINIO_INTERNAL_TENANT_HOST}
fi
export MINIO_TENANT_URL="https://${MINIO_TENANT_HOST}"

# Determine locations of TLS certificates for tenant's endpoint, if applicable
export MINIO_TENANT_TLS_KEY="${CREDENTIALS_DIR}/tls.${MINIO_TENANT_NAME}.key"
export MINIO_TENANT_TLS_CERT="${CREDENTIALS_DIR}/tls.${MINIO_TENANT_NAME}.cert"
