mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-08 12:41:58 +00:00
Revert "Standalone kubeconfig for gce kube-up"
This commit is contained in:
@@ -20,7 +20,6 @@
|
||||
# config-default.sh.
|
||||
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
|
||||
source "${KUBE_ROOT}/cluster/gce/${KUBE_CONFIG_FILE-"config-default.sh"}"
|
||||
source "${KUBE_ROOT}/cluster/common.sh"
|
||||
|
||||
NODE_INSTANCE_PREFIX="${INSTANCE_PREFIX}-minion"
|
||||
|
||||
@@ -234,16 +233,17 @@ function detect-master () {
|
||||
# KUBE_USER
|
||||
# KUBE_PASSWORD
|
||||
function get-password {
|
||||
# templates to extract the username,password for the current-context user
|
||||
# go template to extract the auth-path of the current-context user
|
||||
# Note: we save dot ('.') to $dot because the 'with' action overrides dot
|
||||
local username='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{$user := index $dot "contexts" $ctx "user"}}{{index $dot "users" $user "username"}}{{end}}'
|
||||
local password='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{$user := index $dot "contexts" $ctx "user"}}{{index $dot "users" $user "password"}}{{end}}'
|
||||
KUBE_USER=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${username}")
|
||||
KUBE_PASSWORD=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${password}")
|
||||
if [[ -z "${KUBE_USER}" || -z "${KUBE_PASSWORD}" ]]; then
|
||||
KUBE_USER=admin
|
||||
KUBE_PASSWORD=$(python -c 'import string,random; print "".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16))')
|
||||
local template='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{$user := index $dot "contexts" $ctx "user"}}{{index $dot "users" $user "auth-path"}}{{end}}'
|
||||
local file=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o template --template="${template}")
|
||||
if [[ ! -z "$file" && -r "$file" ]]; then
|
||||
KUBE_USER=$(cat "$file" | python -c 'import json,sys;print json.load(sys.stdin)["User"]')
|
||||
KUBE_PASSWORD=$(cat "$file" | python -c 'import json,sys;print json.load(sys.stdin)["Password"]')
|
||||
return
|
||||
fi
|
||||
KUBE_USER=admin
|
||||
KUBE_PASSWORD=$(python -c 'import string,random; print "".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16))')
|
||||
}
|
||||
|
||||
# Generate authentication token for admin user. Will
|
||||
@@ -580,22 +580,44 @@ function kube-up {
|
||||
|
||||
echo "Kubernetes cluster created."
|
||||
|
||||
# TODO use token instead of basic auth
|
||||
export KUBECONFIG="${HOME}/.kube/.kubeconfig"
|
||||
export KUBE_CERT="/tmp/kubecfg.crt"
|
||||
export KUBE_KEY="/tmp/kubecfg.key"
|
||||
export CA_CERT="/tmp/kubernetes.ca.crt"
|
||||
export CONTEXT="${PROJECT}_${INSTANCE_PREFIX}"
|
||||
local kube_cert="kubecfg.crt"
|
||||
local kube_key="kubecfg.key"
|
||||
local ca_cert="kubernetes.ca.crt"
|
||||
# TODO use token instead of kube_auth
|
||||
local kube_auth="kubernetes_auth"
|
||||
|
||||
local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
|
||||
local context="${PROJECT}_${INSTANCE_PREFIX}"
|
||||
local user="${context}-admin"
|
||||
local config_dir="${HOME}/.kube/${context}"
|
||||
|
||||
# TODO: generate ADMIN (and KUBELET) tokens and put those in the master's
|
||||
# config file. Distribute the same way the htpasswd is done.
|
||||
(
|
||||
mkdir -p "${config_dir}"
|
||||
umask 077
|
||||
gcloud compute ssh --project "${PROJECT}" --zone "$ZONE" "${MASTER_NAME}" --command "sudo cat /srv/kubernetes/kubecfg.crt" >"${KUBE_CERT}" 2>/dev/null
|
||||
gcloud compute ssh --project "${PROJECT}" --zone "$ZONE" "${MASTER_NAME}" --command "sudo cat /srv/kubernetes/kubecfg.key" >"${KUBE_KEY}" 2>/dev/null
|
||||
gcloud compute ssh --project "${PROJECT}" --zone "$ZONE" "${MASTER_NAME}" --command "sudo cat /srv/kubernetes/ca.crt" >"${CA_CERT}" 2>/dev/null
|
||||
gcloud compute ssh --project "${PROJECT}" --zone "$ZONE" "${MASTER_NAME}" --command "sudo cat /srv/kubernetes/kubecfg.crt" >"${config_dir}/${kube_cert}" 2>/dev/null
|
||||
gcloud compute ssh --project "${PROJECT}" --zone "$ZONE" "${MASTER_NAME}" --command "sudo cat /srv/kubernetes/kubecfg.key" >"${config_dir}/${kube_key}" 2>/dev/null
|
||||
gcloud compute ssh --project "${PROJECT}" --zone "$ZONE" "${MASTER_NAME}" --command "sudo cat /srv/kubernetes/ca.crt" >"${config_dir}/${ca_cert}" 2>/dev/null
|
||||
|
||||
create-kubeconfig
|
||||
"${kubectl}" config set-cluster "${context}" --server="https://${KUBE_MASTER_IP}" --certificate-authority="${config_dir}/${ca_cert}" --global
|
||||
"${kubectl}" config set-credentials "${user}" --auth-path="${config_dir}/${kube_auth}" --global
|
||||
"${kubectl}" config set-context "${context}" --cluster="${context}" --user="${user}" --global
|
||||
"${kubectl}" config use-context "${context}" --global
|
||||
|
||||
cat << EOF > "${config_dir}/${kube_auth}"
|
||||
{
|
||||
"User": "$KUBE_USER",
|
||||
"Password": "$KUBE_PASSWORD",
|
||||
"CAFile": "${config_dir}/${ca_cert}",
|
||||
"CertFile": "${config_dir}/${kube_cert}",
|
||||
"KeyFile": "${config_dir}/${kube_key}"
|
||||
}
|
||||
EOF
|
||||
|
||||
chmod 0600 "${config_dir}/${kube_auth}" "${config_dir}/$kube_cert" \
|
||||
"${config_dir}/${kube_key}" "${config_dir}/${ca_cert}"
|
||||
echo "Wrote ${config_dir}/${kube_auth}"
|
||||
)
|
||||
|
||||
echo "Sanity checking cluster..."
|
||||
@@ -643,7 +665,7 @@ function kube-up {
|
||||
echo
|
||||
echo -e "${color_yellow} https://${KUBE_MASTER_IP}"
|
||||
echo
|
||||
echo -e "${color_green}The user name and password to use is located in ${KUBECONFIG}.${color_norm}"
|
||||
echo -e "${color_green}The user name and password to use is located in ${config_dir}/${kube_auth}.${color_norm}"
|
||||
echo
|
||||
|
||||
}
|
||||
@@ -730,9 +752,6 @@ function kube-down {
|
||||
--quiet \
|
||||
"${MASTER_NAME}-ip" || true
|
||||
|
||||
export KUBECONFIG="${HOME}/.kube/.kubeconfig"
|
||||
export CONTEXT="${PROJECT}_${INSTANCE_PREFIX}"
|
||||
clear-kubeconfig
|
||||
}
|
||||
|
||||
# Update a kubernetes cluster with latest source
|
||||
|
Reference in New Issue
Block a user