diff --git a/cluster/aws/util.sh b/cluster/aws/util.sh index 5952f7a7dd..38ce605d21 100644 --- a/cluster/aws/util.sh +++ b/cluster/aws/util.sh @@ -274,7 +274,7 @@ function upload-server-tars() { function get-password { # 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 template='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{$user := index $dot "contexts" $ctx "user"}}{{index $dot "users" $user "auth-path"}}{{end}}' + local template='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "auth-path" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{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"]') diff --git a/cluster/common.sh b/cluster/common.sh index f00558d101..9943619bda 100644 --- a/cluster/common.sh +++ b/cluster/common.sh @@ -91,8 +91,8 @@ function get-kubeconfig-basicauth() { # is missing. # Note: we save dot ('.') to $root because the 'with' action overrides it. # See http://golang.org/pkg/text/template/. - local username='{{$root := .}}{{with index $root "current-context"}}{{with index $root "contexts" .}}{{with index . "user"}}{{with index $root "users" .}}{{index . "username"}}{{end}}{{end}}{{end}}{{end}}' - local password='{{$root := .}}{{with index $root "current-context"}}{{with index $root "contexts" .}}{{with index . "user"}}{{with index $root "users" .}}{{index . "password"}}{{end}}{{end}}{{end}}{{end}}' + local username='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "username" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}' + local password='{{$dot := .}}{{with $ctx := index $dot "current-context"}}{{range $element := (index $dot "contexts")}}{{ if eq .name $ctx }}{{ with $user := .context.user }}{{range $element := (index $dot "users")}}{{ if eq .name $user }}{{ index . "user" "password" }}{{end}}{{end}}{{end}}{{end}}{{end}}{{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}") # Handle empty/missing username|password diff --git a/docs/kubectl-config-view.md b/docs/kubectl-config-view.md index afe81c4005..784220bd81 100644 --- a/docs/kubectl-config-view.md +++ b/docs/kubectl-config-view.md @@ -23,7 +23,7 @@ $ kubectl config view $ kubectl config view --local // Get the password for the e2e user -$ kubectl config view -o template --template='{{ index . "users" "e2e" "password" }}' +$ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2e" }}{{ index .user.password }}{{end}}{{end}}' ``` ### Options diff --git a/examples/openshift-origin/resource-generator.sh b/examples/openshift-origin/resource-generator.sh index 87b80323f3..56b953bbe7 100755 --- a/examples/openshift-origin/resource-generator.sh +++ b/examples/openshift-origin/resource-generator.sh @@ -41,19 +41,19 @@ fi TEMPLATE="--template=\"{{ index . \"current-context\" }}\"" CURRENT_CONTEXT=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template=\"{{ index . \"contexts\" ${CURRENT_CONTEXT} \"cluster\" }}\"" +TEMPLATE="--template=\"{{range .contexts}}{{ if eq .name ${CURRENT_CONTEXT} }}{{ .context.cluster }}{{end}}{{end}}\"" CURRENT_CLUSTER=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template=\"{{ index . \"contexts\" ${CURRENT_CONTEXT} \"user\" }}\"" +TEMPLATE="--template=\"{{range .contexts}}{{ if eq .name ${CURRENT_CONTEXT} }}{{ .context.user }}{{end}}{{end}}\"" CURRENT_USER=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template={{ index . \"clusters\" ${CURRENT_CLUSTER} \"certificate-authority\" }}" +TEMPLATE="--template=\"{{range .clusters}}{{ if eq .name ${CURRENT_CLUSTER} }}{{ index . \"cluster\" \"certificate-authority\" }}{{end}}{{end}}\"" CERTIFICATE_AUTHORITY=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template={{ index . \"clusters\" ${CURRENT_CLUSTER} \"server\" }}" +TEMPLATE="--template=\"{{range .clusters}}{{ if eq .name ${CURRENT_CLUSTER} }}{{ .cluster.server }}{{end}}{{end}}\"" KUBE_MASTER=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) -TEMPLATE="--template={{ index . \"users\" ${CURRENT_USER} \"auth-path\" }}" +TEMPLATE="--template=\"{{range .users}}{{ if eq .name ${CURRENT_USER} }}{{ index . \"user\" \"auth-path\" }}{{end}}{{end}}\"" AUTH_PATH=$( "${kubectl}" "${config[@]:+${config[@]}}" config view -o template "${TEMPLATE}" ) # Build an auth_path file to embed as a secret