Merge "Create kubeconfig-setup script"

This commit is contained in:
Zuul 2023-08-11 21:05:49 +00:00 committed by Gerrit Code Review
commit 4d1bec7adf
5 changed files with 54 additions and 2 deletions

@ -69,6 +69,7 @@ install -m 555 %{_buildsubdir}/scripts/show-certs.sh %{buildroot}%{local_bindir}
install -m 555 %{_buildsubdir}/scripts/update_docker_registry_auth.sh %{buildroot}%{local_bindir}
install -m 555 %{_buildsubdir}/scripts/change_system_private_registry.sh %{buildroot}%{local_bindir}
install -m 555 %{_buildsubdir}/scripts/local_starlingxrc %{buildroot}%{local_bindir}
install -m 555 %{_buildsubdir}/scripts/kubeconfig-setup %{buildroot}%{local_bindir}
install -d %{buildroot}%{local_etc_initd}
install %{_buildsubdir}/scripts/log_functions.sh %{buildroot}%{local_etc_initd}
@ -123,6 +124,7 @@ systemctl enable opt-platform.service
%{local_bindir}/update_docker_registry_auth.sh
%{local_bindir}/change_system_private_registry.sh
%{local_bindir}/local_starlingxrc
%{local_bindir}/kubeconfig-setup
%files noncontroller
%defattr(-,root,root,-)

@ -8,3 +8,4 @@ scripts/update-dm.sh usr/local/bin
scripts/update_docker_registry_auth.sh usr/local/bin
scripts/change_system_private_registry.sh usr/local/bin
scripts/local_starlingxrc usr/local/bin
scripts/kubeconfig-setup usr/local/bin

@ -8,3 +8,4 @@
/usr/local/bin/update_docker_registry_auth.sh
/usr/local/bin/change_system_private_registry.sh
/usr/local/bin/local_starlingxrc
/usr/local/bin/kubeconfig-setup

@ -40,6 +40,7 @@ override_dh_auto_install:
install -m 555 scripts/change_system_private_registry.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
install -m 555 scripts/is-rootdisk-device.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
install -m 555 scripts/local_starlingxrc $(DEBIAN_BUILDDIR)/usr/local/bin/
install -m 555 scripts/kubeconfig-setup $(DEBIAN_BUILDDIR)/usr/local/bin/
install -m 755 scripts/connectivity_test $(DEBIAN_BUILDDIR)/usr/local/bin/
install -m 750 scripts/set_keystone_user_option.sh $(DEBIAN_BUILDDIR)/usr/local/bin/
@ -53,5 +54,5 @@ override_dh_auto_install:
override_dh_fixperms:
dh_fixperms -Xupdate-iso.sh -Xpatch-dm.sh -Xgen-bootloader-iso.sh -Xstx-iso-utils.sh \
-Xshow-certs.sh -Xupdate_docker_registry_auth.sh -Xchange_system_private_registry.sh \
-Xis-rootdisk-device.sh -Xlocal_starlingxrc -Xpatch-restart-* -Xconnectivity_test \
-Xset_keystone_user_option.sh
-Xis-rootdisk-device.sh -Xlocal_starlingxrc -Xkubeconfig-setup -Xpatch-restart-* \
-Xconnectivity_test -Xset_keystone_user_option.sh

@ -0,0 +1,47 @@
#!/bin/bash
#
# Copyright (c) 2023 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# Setups Kubernetes configuration for logged in user.
# No password/token/key is set.
#
# Set Kubernetes config file.
mkdir -p -m 750 $HOME/.kube
rm -f $HOME/.kube/config
touch $HOME/.kube/config
chmod 600 $HOME/.kube/config
CERT_AUTH_DATA=$(base64 -w0 /etc/kubernetes/pki/ca.crt)
CLUSTER_HOST_IP=$(grep -w controller-cluster-host /etc/hosts | awk '{print $1}')
if [[ $CLUSTER_HOST_IP =~ .*:.* ]]; then
CLUSTER_HOST_IP="[$CLUSTER_HOST_IP]"
fi
SERVER_URL="https://$CLUSTER_HOST_IP:6443"
printf "%s\n" \
"apiVersion: v1" \
"clusters:" \
"- cluster:" \
" certificate-authority-data: $CERT_AUTH_DATA" \
" server: $SERVER_URL" \
" name: kubernetes" \
"contexts:" \
"- context:" \
" cluster: kubernetes" \
" user: $USER" \
" name: $USER@kubernetes" \
"current-context: $USER@kubernetes" \
"kind: Config" \
"preferences: {}" \
"users:" \
"- name: $USER" \
" user:" > $HOME/.kube/config
# Add or replace KUBECONFIG variable in ".profile" file.
VAR_COUNT=$(grep -cw '^export KUBECONFIG' $HOME/.profile)
if [[ "$VAR_COUNT" == "0" ]]; then
echo 'export KUBECONFIG="$HOME/.kube/config"' >> $HOME/.profile
else
sed -i '/^\bexport KUBECONFIG\b/c\export KUBECONFIG="$HOME/.kube/config"' $HOME/.profile
fi