Skip to content

Commit 38688ba

Browse files
committed
Support using existing kubeconfig while import data protection
Signed-off-by: Willis Ren <willis.ren@broadcom.com>
1 parent 4d65532 commit 38688ba

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

061-cluster-access-policies-resync.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /bin/bash
22

33
source utils/common.sh
4+
source utils/kubeconfig.sh
45

56
register_last_words "Resync rolebindings on clusters and namespaces"
67

@@ -30,15 +31,15 @@ fi
3031

3132
while IFS="." read -r mgmt prvn cls; do
3233
log info "Connecting to the cluster $mgmt/$prvn/$cls..."
33-
if ! tanzu tmc cluster kubeconfig get $cls -m $mgmt -p $prvn > ${TEMPKUBECONFIG} 2>/dev/null; then
34-
log error "Failed to get kubeconfig for cluster $mgmt/$prvn/$cls"
35-
exit 1
34+
if ! get_kubeconfig ${cls} ${prvn} ${mgmt} ${TEMPKUBECONFIG}; then
35+
log error "Failed to get kubeconfig for cluster $mgmt/$prvn/$cls, skip resyncing access policies for this cluster"
36+
continue
3637
fi
3738

3839
# Verify cluster connection
3940
if ! kubectl cluster-info >/dev/null 2>&1; then
40-
log error "Failed to connect to cluster $mgmt/$prvn/$cls"
41-
exit 1
41+
log error "Failed to connect to cluster $mgmt/$prvn/$cls, skip resyncing access policies for this cluster"
42+
continue
4243
fi
4344

4445
log info "Delete stale custom cluster roles for $mgmt/$prvn/$cls ..."
@@ -48,8 +49,8 @@ while IFS="." read -r mgmt prvn cls; do
4849

4950
org_id=$(kubectl -n $TMC_NAMESPACE get cm stack-config -o jsonpath='{.data.org_id}' 2>/dev/null)
5051
if [[ -z "$org_id" ]]; then
51-
log error "Failed to get org_id for the cluster $mgmt/$prvn/$cls"
52-
exit 1
52+
log error "Failed to get org_id for the cluster $mgmt/$prvn/$cls, skip resyncing access policies for this cluster"
53+
continue
5354
fi
5455

5556
log info "Delete stale cluster rolebindings on $mgmt/$prvn/$cls ..."

064-cluster-data_protection-import.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/bin/bash
22

33
set +e
4+
source utils/kubeconfig.sh
45

56
ONBOARDED_CLUSTER_INDEX_FILE="data/clusters/onboarded-clusters-name-index"
7+
68
TEMPFILE=/tmp/_temp_dp_file_$(date +%s)
79
TEMPKUBECONFIG=${TEMPFILE}_kubeconfig
810
DPDIR=data/data-protection
@@ -151,7 +153,10 @@ function create_old_bsl() {
151153
clname=$(echo "${elem}" | yq '.fullName.clusterName')
152154
mcname=$(echo "${elem}" | yq '.fullName.managementClusterName')
153155
provname=$(echo "${elem}" | yq '.fullName.provisionerName')
154-
${TANZU} tmc cluster kubeconfig get ${clname} -m ${mcname} -p ${provname} > ${TEMPKUBECONFIG}
156+
if ! get_kubeconfig ${clname} ${provname} ${mcname} ${TEMPKUBECONFIG}; then
157+
echo "Failed to get kubeconfig for cluster ${mcname}/${provname}/${clname}, skip importing BSL for this cluster"
158+
continue
159+
fi
155160
# Create new BSL
156161
bslname=$(echo "${elem}" | yq '.fullName.name')
157162
bslprefix=$(echo "${elem}" | yq '.spec.prefix')

utils/kubeconfig.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
WC_KUBECONFIG_INDEX_FILE="data/clusters/attached-wc-kubeconfig-index-file"
4+
5+
function get_kubeconfig() {
6+
local cluster_name=$1
7+
local provisioner_name=$2
8+
local management_cluster_name=$3
9+
local kubeconfig_file=$4
10+
11+
local kubeconfig_dir=$(dirname $kubeconfig_file)
12+
mkdir -p $kubeconfig_dir
13+
14+
if [[ "${management_cluster_name}" == "attached" ]]; then
15+
if [[ -n "$WC_KUBECONFIG_INDEX_FILE" && -f "$WC_KUBECONFIG_INDEX_FILE" ]]; then
16+
wc_kubeconfig=$(grep "^$cluster_name:" "$WC_KUBECONFIG_INDEX_FILE" | awk '{print $2}')
17+
if [[ -n "$wc_kubeconfig" && -f "$wc_kubeconfig" ]]; then
18+
cp -f $wc_kubeconfig $kubeconfig_file
19+
return 0
20+
fi
21+
fi
22+
23+
if ! tanzu tmc cluster kubeconfig get ${cluster_name} -m ${management_cluster_name} -p ${provisioner_name} > ${kubeconfig_file}; then
24+
return 1
25+
fi
26+
elif ! tanzu tmc cluster admin-kubeconfig get ${cluster_name} -m ${management_cluster_name} -p ${provisioner_name} > ${kubeconfig_file}; then
27+
return 1
28+
fi
29+
30+
return 0
31+
}

0 commit comments

Comments
 (0)