#!/bin/bash -e
#
# Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# 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.

source $(dirname "${0}")/../common/helpers

KUBECONFIGPATH=${1:-"$(dirname $0)/kubeconfigs/default-admin.conf"}

gardenlet_admin_templates=""

if is_seedauthorizer_enabled; then
  KUBECONFIG=$KUBECONFIGPATH kubectl delete clusterrole/gardener.cloud:system:seeds        --ignore-not-found
  KUBECONFIG=$KUBECONFIGPATH kubectl delete clusterrolebinding/gardener.cloud:system:seeds --ignore-not-found
else
  gardenlet_admin_templates="-x templates/clusterrole-seeds.yaml -x templates/clusterrolebinding-seeds.yaml"
fi


helm template \
  "$(dirname "$0")/../../../charts/gardener/controlplane/charts/application" \
  -x templates/clusterrole-admission-controller.yaml \
  -x templates/clusterrole-apiserver.yaml \
  -x templates/clusterrole-controller-manager.yaml \
  -x templates/clusterrole-scheduler.yaml $gardenlet_admin_templates \
  --set global.admission.enabled=true \
  --set global.apiserver.enabled=true \
  --set global.controller.enabled=true \
  --set global.scheduler.enabled=true | \
KUBECONFIG=$KUBECONFIGPATH kubectl apply -f -

if ! KUBECONFIG=$KUBECONFIGPATH kubectl get clusterrolebinding "gardener.cloud:system:admission-controller" >/dev/null 2>&1; then
  KUBECONFIG=$KUBECONFIGPATH kubectl create clusterrolebinding "gardener.cloud:system:admission-controller" --clusterrole="gardener.cloud:system:admission-controller" --user="gardener.cloud:system:admission-controller"
fi
if ! KUBECONFIG=$KUBECONFIGPATH kubectl get clusterrolebinding "gardener.cloud:system:apiserver" >/dev/null 2>&1; then
  KUBECONFIG=$KUBECONFIGPATH kubectl create clusterrolebinding "gardener.cloud:system:apiserver" --clusterrole="gardener.cloud:system:apiserver" --user="gardener.cloud:system:apiserver"
fi
if ! KUBECONFIG=$KUBECONFIGPATH kubectl get clusterrolebinding "gardener.cloud:system:controller-manager" >/dev/null 2>&1; then
  KUBECONFIG=$KUBECONFIGPATH kubectl create clusterrolebinding "gardener.cloud:system:controller-manager" --clusterrole="gardener.cloud:system:controller-manager" --user="gardener.cloud:system:controller-manager"
fi
if ! KUBECONFIG=$KUBECONFIGPATH kubectl get clusterrolebinding "gardener.cloud:system:scheduler" >/dev/null 2>&1; then
  KUBECONFIG=$KUBECONFIGPATH kubectl create clusterrolebinding "gardener.cloud:system:scheduler" --clusterrole="gardener.cloud:system:scheduler" --user="gardener.cloud:system:scheduler"
fi
