#!/bin/sh
#
# Copyright (C) 2021 Bruce Ashfield
#
# SPDX-License-Identifier: Apache-2.0

# cleanup the iptables
if [ -f /etc/sysconfig/iptables ]; then
    cp /etc/sysconfig/iptables /etc/sysconfig/iptables.ORIG
fi
iptables --flush && iptables --flush && iptables-save > /etc/sysconfig/iptables
systemctl restart iptables.service

# Load/Enable br_netfilter kernel module
modprobe br_netfilter
modprobe overlay

echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
echo '1' > /proc/sys/net/bridge/bridge-nf-call-ip6tables
echo 'net.bridge.bridge-nf-call-iptables=1' >> /etc/sysctl.d/k8s.conf
echo 'net.bridge.bridge-nf-call-ip6tables=1' >> /etc/sysctl.d/k8s.conf
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.d/k8s.conf

systemctl restart systemd-sysctl.service

kubeadm init

# Copy the cluster configuration to the regular users home directory
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
