Skip to content

Commit 4c30a05

Browse files
add warning on add cluster in case failed to deleter secret (#76)
* add warning on add cluster in case failed to deleter secret * version as branch name * wip * make sure in case kubectl exec fail the whole proccess will fail, for delete secret just warning * wip * test * wip * wip * bump * wip * exit if one of kubectl cmds fail * bump * test * wip * bump
1 parent eb3ca7e commit 4c30a05

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

add-cluster/add-cluster.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
SECRET_NAME=""
1212

1313
function get_service_account_secret_name() {
14-
SECRET_NAME=$(kubectl get ServiceAccount ${SERVICE_ACCOUNT_NAME} -n ${NAMESPACE} -o jsonpath='{.secrets[0].name}')
14+
SECRET_NAME=$(kubectl get ServiceAccount ${SERVICE_ACCOUNT_NAME} -n ${NAMESPACE} -o jsonpath='{.secrets[0].name}') || exit 1
1515
if [[ -z ${SECRET_NAME} ]]; then
1616
echo "Creating new ServiceAccount token"
1717
# create secret for service account
@@ -26,7 +26,7 @@ type: kubernetes.io/service-account-token
2626
EOF
2727
)
2828
SECRET_NAME=$(echo ${SECRET_NAME} | sed s@secret/@@g | sed s/\ created//g)
29-
kubectl patch ServiceAccount ${SERVICE_ACCOUNT_NAME} -n ${NAMESPACE} --patch "{\"secrets\": [{\"name\": \"${SECRET_NAME}\"}]}"
29+
kubectl patch ServiceAccount ${SERVICE_ACCOUNT_NAME} -n ${NAMESPACE} --patch "{\"secrets\": [{\"name\": \"${SECRET_NAME}\"}]}" || exit 1
3030
echo "Created ServiceAccount sercret ${SECRET_NAME}"
3131
else
3232
echo "Found ServiceAccount secret ${SECRET_NAME}"
@@ -48,20 +48,21 @@ NAMESPACE=$(cat ${SERVICEACCOUNT}/namespace)
4848
CACERT=${SERVICEACCOUNT}/ca.crt
4949

5050
# get ServiceAccount token
51-
get_service_account_secret_name
51+
get_service_account_secret_name || exit 1
5252
BEARER_TOKEN=$(kubectl get secret ${SECRET_NAME} -n ${NAMESPACE} -o jsonpath='{.data.token}' | base64 -d)
5353

5454
# write KUBE_COPNFIG_DATA to local file
5555
CLUSTER_NAME=$(echo ${SERVER} | sed s/'http[s]\?:\/\/'//)
56-
kubectl config set-cluster "${CLUSTER_NAME}" --server="${SERVER}" --certificate-authority="${CACERT}"
57-
kubectl config set-credentials "${SERVICE_ACCOUNT_NAME}" --token "${BEARER_TOKEN}"
58-
kubectl config set-context "${CONTEXT_NAME}" --cluster="${CLUSTER_NAME}" --user="${SERVICE_ACCOUNT_NAME}"
59-
KUBE_CONFIG_B64=$(kubectl config view --minify --flatten --output json --context="${CONTEXT_NAME}" | base64 -w 0)
56+
kubectl config set-cluster "${CLUSTER_NAME}" --server="${SERVER}" --certificate-authority="${CACERT}" || exit 1
57+
kubectl config set-credentials "${SERVICE_ACCOUNT_NAME}" --token "${BEARER_TOKEN}" || exit 1
58+
kubectl config set-context "${CONTEXT_NAME}" --cluster="${CLUSTER_NAME}" --user="${SERVICE_ACCOUNT_NAME}" || exit 1
59+
60+
KUBE_CONFIG=$(kubectl config view --minify --flatten --output json --context="${CONTEXT_NAME}") || exit 1
61+
KUBE_CONFIG_B64=`echo -n $KUBE_CONFIG | base64 -w 0`
6062

6163
ANNOTATIONS_B64=$(cat /etc/config/annotations.yaml | base64 -w 0)
6264
LABELS_B64=$(cat /etc/config/labels.yaml | base64 -w 0)
6365

64-
echo "{ \"name\": \"'${CONTEXT_NAME}'\", \"kubeConfig\": \"'${KUBE_CONFIG_B64}'\", \"annotations\": \"'${ANNOTATIONS_B64}'\", \"labels\": \"'${LABELS_B64}'\" }"
6566
STATUS_CODE=$(curl -X POST ${INGRESS_URL%/}/app-proxy/api/clusters \
6667
-H 'Content-Type: application/json' \
6768
-H 'Authorization: '${CSDP_TOKEN}'' \
@@ -82,4 +83,4 @@ if [[ $STATUS_CODE -ge 300 ]]; then
8283
fi
8384

8485
echo "deleting token secret ${CSDP_TOKEN_SECRET}"
85-
kubectl delete secret ${CSDP_TOKEN_SECRET} -n ${NAMESPACE}
86+
kubectl delete secret ${CSDP_TOKEN_SECRET} -n ${NAMESPACE} || echo "warning: failed deleting secret ${CSDP_TOKEN_SECRET}. you can safely delete this secret manually later with: kubectl delete secret ${CSDP_TOKEN_SECRET} -n ${NAMESPACE}"

csdp/base_components/bootstrap/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ configMapGenerator:
1212
- name: codefresh-cm
1313
behavior: create
1414
literals:
15-
- version=0.0.524 # Runtime version
16-
- bootstrapRevision=0.0.524 # Tag to use for bootstrap (change this to the name of your branch if you want to test changes)
15+
- version=0.0.525 # Runtime version
16+
- bootstrapRevision=0.0.525 # Tag to use for bootstrap (change this to the name of your branch if you want to test changes)
1717
- appsetRequeueTime=15
1818

1919
replacements:

0 commit comments

Comments
 (0)