#!/usr/bin/env bash

# Copyright 2020 Authors of Arktos.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

LOG_DIR=${LOG_DIR:-"/tmp"}
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
CERT_DIR=${CERT_DIR:-"/var/run/kubernetes"}

# Kube-apiserver service group id
APISERVER_SERVICEGROUPID=${APISERVER_SERVICEGROUPID:-"1"}
CLUSTER_UP_NAME="sg_${APISERVER_SERVICEGROUPID}"

source "${KUBE_ROOT}/hack/lib/util.sh"

display_usage() {
  echo "Usage: The script can be run with the following formats:"
  echo ""
  echo "hack/create-kubeconfig.sh command [target_kubeconfigfilepath]"
  echo "-- Run the above commond in a master host. Then it generates a kubeconfig create command based on the admin.kubeconfig."
  echo "-- The new command will be ran on a worker host and the new kubeconfig is saved in the target_kubeconfigfilepath in the worker host."
  echo ""
  echo "hack/create-kubeconfig.sh command [source_kubeconfigfilepath] [target_kubeconfigfilepath]"
  echo "-- Run the above commond in a master host. Then it generates a kubeconfig create command based on the given source_kubeconfigfilepath."
  echo "-- The new command will be ran on a worker host and the new kubeconfig is saved in the target_kubeconfigfilepath in the worker host."
  echo ""
  echo "hack/create-kubeconfig.sh create [server] [certificate-authority-data] [client-certificate-data] [client-key-data] [target_kubeconfigfilepath]"
  echo "-- The command is generated by the two commands above. Run the command and it will generate a new kubeconfig file."
}

# Ensure CERT_DIR is created for crt/key and kubeconfig
mkdir -p "${CERT_DIR}" &>/dev/null || sudo mkdir -p "${CERT_DIR}"
CONTROLPLANE_SUDO=$(test -w "${CERT_DIR}" || echo "sudo -E")
# There are following commands to run the script
# hack/create-kubeconfig.sh is to create a kubeconfig giving the following params
# hack/create-kubeconfig.sh extract key is to extract content from an existing kubeconfig.
if [ $# -lt 1 ] ; then
  display_usage
else
  case $1 in
    extract)
      if [ -n "$2" ]; then
        ${CONTROLPLANE_SUDO} grep $2 ${CERT_DIR}/admin.kubeconfig | awk '{print $2}'
      fi
      ;;
    command)
      filepath=${CERT_DIR}/admin.kubeconfig
      if [[ $# -eq 2 ]] ; then
         newfilepath=$2
      elif [[ $# -eq 3 ]] ; then
         filepath=$2
         newfilepath=$3
      else 
         display_usage
         exit 0
      fi
      host="$(${CONTROLPLANE_SUDO} grep server $filepath | awk '{print $2}')"
      ca="$(${CONTROLPLANE_SUDO} grep certificate-authority-data $filepath | awk '{print $2}')"
      cert="$(${CONTROLPLANE_SUDO} grep client-certificate-data $filepath | awk '{print $2}')"
      key="$(${CONTROLPLANE_SUDO} grep client-key-data $filepath | awk '{print $2}')"
      echo "hack/create-kubeconfig.sh create $host $ca $cert $key $newfilepath"
      ;;
    create)
      if [[ $# -ne 6 ]] ; then
        display_usage
      else
        clustername=$(date +%s | sha256sum | base64 | head -c 10 ; echo)
        cat <<EOF |  ${CONTROLPLANE_SUDO} tee -a $6
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: $3  
    server: $2
  name: cluster_$CLUSTER_UP_NAME
contexts:
- context:
    cluster: cluster_$CLUSTER_UP_NAME
    user: user_$CLUSTER_UP_NAME
  name: context_$CLUSTER_UP_NAME
current-context: context_$CLUSTER_UP_NAME
kind: Config
preferences: {}
users:
- name: user_$CLUSTER_UP_NAME
  user:
    client-certificate-data: $4
    client-key-data: $5
EOF
     fi
       ;;
    *)
      display_usage
      ;;
  esac
fi

