#!/bin/bash
#
# Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 3 as shown at https://www.gnu.org/licenses/gpl-3.0.txt.

KUBECFG=$1
EXISTINGCM=$2
export KUBECONFIG=${KUBECFG}

# see https://metallb.universe.tf/installation/#preparation
ipvs=$(kubectl get configmap kube-proxy -n kube-system -o yaml | grep "mode: ipvs")
if [[ ! -z "${ipvs}" ]]; then
        kubectl get configmap kube-proxy -n kube-system -o yaml | \
        sed -e "s/strictARP: false/strictARP: true/" | \
        kubectl apply -f - -n kube-system
fi

if [[ ! -z "${EXISTINGCM}" ]]; then
	# check if config map consists of namespace field
	namespace=$(grep namespace ${EXISTINGCM}  | awk '{print $NF}')
	if [[ ! -z "${namespace}" ]]; then
		kubectl create ns "${namespace}"
	fi
	kubectl apply -f "${EXISTINGCM}" -n "${namespace}"
fi
