#!/bin/bash
#
# Copyright 2020 VEXXHOST, Inc.
#
# 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.

function get_kubernetes_service_ip {
	local svc="$1"

	for i in {1..60}; do
		ip=$(kubectl get svc/$svc -ojsonpath='{.spec.clusterIP}') && break || sleep 1;
	done

	echo "$ip"
}

function kubernetes_rollout_status {
	local resource="$1"

	for i in {1..60}; do
		kubectl get $resource && break || sleep 1;
	done

	kubectl rollout status --timeout=300s $resource
}

function kubernetes_wait_pod_ready {
	while
		[[ $(kubectl get pods $1 -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]];
	do
		echo "waiting for pod" && sleep 1;
	done
}

function kubernetes_rollout_restart {
	local resource="$1"

	for i in {1..60}; do
		kubectl get $resource && break || sleep 1;
	done

	kubectl rollout restart $resource
}

function kubernetes_ensure_resource {
	local resource="$1"
	for i in {1..120}; do
		kubectl get $resource && break || sleep 5;
	done
}

function proxy_pass_to_kubernetes {
	local url=$1
	local svc=$2
	local conf=$3

	local ip=$(get_kubernetes_service_ip $svc)
	local apache_conf=$(apache_site_config_for $conf)

    enable_apache_mod proxy
    enable_apache_mod proxy_http

    echo "KeepAlive Off" | sudo tee $apache_conf
    echo "SetEnv proxy-sendchunked 1" | sudo tee -a $apache_conf

	echo "ProxyPass \"${url}\" \"http://${ip}/\"" | sudo tee $apache_conf

	enable_apache_site $conf
	restart_apache_server
}

# Gets or creates service
# Usage: get_or_create_service <name> <type> <description>
function get_or_create_service {
	cat <<EOF | kubectl apply -f-
---
apiVersion: identity.openstack.org/v1alpha1
kind: Service
metadata:
  name: ${1//_/-}
spec:
  type: $2
  description: $3
EOF
}
export -f get_or_create_service

# Create an endpoint with a specific interface
# Usage: _get_or_create_endpoint_with_interface <service> <interface> <url> <region>
function _get_or_create_endpoint_with_interface {
	cat <<EOF | kubectl apply -f-
---
apiVersion: identity.openstack.org/v1alpha1
kind: Endpoint
metadata:
  name: ${1//_/-}-$2
spec:
  service: $1
  interface: $2
  url: $3
EOF
}
export -f _get_or_create_endpoint_with_interface

# Get plain data from the specified secret
# Usage: get_data_from_secret <secret> <namespace> <key>
function get_data_from_secret {
	local secret=$1
	local ns=$2
	local key=$3
	local data=$(kubectl get secret -n $ns $secret -o jsonpath="{.data.$key}" | base64 --decode)
	echo $data
}
export -f get_data_from_secret