modify directory kubectl with output for gha integration 87/14787/6
authorBimo Fransiscus Asisi <d11002806@gapps.ntust.edu.tw>
Tue, 5 Aug 2025 05:22:06 +0000 (13:22 +0800)
committerBimo Fransiscus Asisi <d11002806@gapps.ntust.edu.tw>
Thu, 7 Aug 2025 09:04:16 +0000 (17:04 +0800)
issue-id: INT-185
Change-Id: If3d57526165d8b5f8afbae7ff464232b0602308a
Signed-off-by: Bimo Fransiscus Asisi <d11002806@gapps.ntust.edu.tw>
tools/setup_k8s/setup_k8s.sh

index 282a37c..5869d76 100755 (executable)
@@ -1,4 +1,43 @@
 #!/bin/bash
+################################################################################
+#   Copyright (c) 2025 Broadband Multimedia Wireless Lab, NTUST                #                                #
+#                                                                              #
+#   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 to be executed on script exit or interruption
+cleanup() {
+  echo "" # Add a newline for cleaner output after Ctrl+C
+  echo "Caught Ctrl+C or script exiting. Performing cleanup..."
+  # Add any specific cleanup tasks here that you want to happen
+  # For example:
+  # rm -f /tmp/my_temp_file
+  # killall -SIGTERM any_background_processes_started_by_script
+
+  echo "Cleanup complete. Exiting."
+  exit 1 # Exit with a non-zero status to indicate abnormal termination
+}
+
+# Trap SIGINT (Ctrl+C) to call the cleanup function
+trap cleanup SIGINT
+
+# Trap EXIT to also call the cleanup function (useful for general cleanup)
+# If you want cleanup to *always* happen, regardless of how the script ends,
+# use EXIT. If you only want it on Ctrl+C, remove this line.
+# Note: If you trap both SIGINT and EXIT, be careful with logic to avoid
+# double-cleanup if SIGINT also causes an EXIT. A common pattern is to
+# have SIGINT *call* EXIT after its specific actions.
+trap cleanup EXIT
 
 # Capture start time
 start_time=$(date +%s)
@@ -274,15 +313,43 @@ apt-cache policy kubelet | grep 'Installed: (none)' -A 1000 | grep 'Candidate:'
 apt install -y kubeadm=${KUBEVERSION} kubelet=${KUBEVERSION} kubectl=${KUBEVERSION}
 kubeadm init --apiserver-advertise-address=${IP_ADDR} --pod-network-cidr=${POD_CIDR} --v=5
 
-mkdir -p $HOME/.kube
-cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
-chown $(id -u):$(id -g) $HOME/.kube/config
-export KUBECONFIG=/etc/kubernetes/admin.conf
+# For CICD purpose
+TARGET_USER="${SUDO_USER}"
+
+# Get the home directory of the target user
+TARGET_HOME=$(getent passwd "${TARGET_USER}" | cut -d: -f6)
+
+if [[ -z "${TARGET_HOME}" ]]; then
+    echo "Error: Home directory for user ${TARGET_USER} could not be found."
+    exit 1
+fi
+
+echo "Setting up kubectl for user: ${TARGET_USER} in home directory: ${TARGET_HOME}"
+
+# Create the .kube directory in the correct user's home
+mkdir -p "${TARGET_HOME}/.kube"
+
+# Copy the admin.conf file
+cp -i /etc/kubernetes/admin.conf "${TARGET_HOME}/.kube/config"
 
+# Change the ownership to the correct user.
+chown "${TARGET_USER}:${TARGET_USER}" "${TARGET_HOME}/.kube/config"
+
+# Set the correct permissions for the file.
+chmod 600 "${TARGET_HOME}/.kube/config"
+
+export KUBECONFIG="${TARGET_HOME}/.kube/config"
+
+# Release taint
 kubectl taint nodes --all node-role.kubernetes.io/control-plane-
 kubectl taint nodes --all node.kubernetes.io/not-ready-
 
 kubectl get pods -A
+
+echo "***************************************************************************************************************"
+echo "                                         Installing CNI                                          "
+echo "***************************************************************************************************************"
+
 kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.1/manifests/calico.yaml
 
 wait_for_pods_running 7 kube-system
@@ -291,4 +358,4 @@ echo "**************************************************************************
 
 kubectl get pods -A
 
-echo "***************************************************************************************************************"
+echo "***************************************************************************************************************"
\ No newline at end of file