Skip to content

Commit

Permalink
Fix the functionality of generating kubeconfig (#1204) (#1205)
Browse files Browse the repository at this point in the history
Signed-off-by: 向先 <[email protected]>
Co-authored-by: 向先 <[email protected]>
  • Loading branch information
wqlparallel and wqlparallel authored Nov 16, 2024
1 parent 74fd3f2 commit d61a784
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
25 changes: 23 additions & 2 deletions arena-gen-kubeconfig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function generate_manifest_yaml() {
exit 2
fi
output=$1
arena-helm template --name $USER_NAME --namespace $USER_NAMESPACE -f $USER_VALUES $CHART_DIR/user > $output
arena-helm template --release-name $USER_NAME --namespace $USER_NAMESPACE -f $USER_VALUES $CHART_DIR/user > $output
}

function apply_manifest_yaml() {
Expand Down Expand Up @@ -188,10 +188,31 @@ function get_cluster_url() {
export CLUSTER_URL=$(arena-kubectl get endpoints | grep -E '\<kubernetes\>' | awk '{print $2}')
}

function is_k8s_version_gte_1_24() {
version=$(kubectl version | grep Server | awk '{print $3}')

version_number=${version:1}

major_version=$(echo $version_number | cut -d. -f1)
minor_version=$(echo $version_number | cut -d. -f2)

if [ "$major_version" -gt 1 ] || { [ "$major_version" -eq 1 ] && [ "$minor_version" -ge 24 ]; }; then
return 0
else
return 1
fi
}

function generate_kubeconfig() {
user=$USER_NAME
namespace=$USER_NAMESPACE
SA_SECRET=$( arena-kubectl get sa -n $namespace $user -o jsonpath='{.secrets[0].name}' )

if is_k8s_version_gte_1_24; then
SA_SECRET="${user}-secret"
else
SA_SECRET=$( arena-kubectl get sa -n $namespace $user -o jsonpath='{.secrets[0].name}' )
fi

BEARER_TOKEN=$( arena-kubectl get secrets -n $namespace $SA_SECRET -o jsonpath='{.data.token}' | base64 --decode)
arena-kubectl get secrets -n $namespace $SA_SECRET -o jsonpath='{.data.ca\.crt}' | base64 --decode > $TEMPDIR/ca.crt
arena-kubectl config \
Expand Down
11 changes: 11 additions & 0 deletions charts/user/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ kind: ServiceAccount
metadata:
name: {{ .Release.Name }}
namespace: {{ .Release.Namespace }}
{{- if and (semverCompare ">=1.24.0" .Capabilities.KubeVersion.Version) }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}-secret
namespace: {{ .Release.Namespace }}
annotations:
kubernetes.io/service-account.name: {{ .Release.Name }}
type: kubernetes.io/service-account-token
{{- end }}

0 comments on commit d61a784

Please sign in to comment.