-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow kubens to work with client-go credential plugins #156
Changes from all commits
43c04de
3d6ad27
c96f86f
bdeaeb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,13 @@ IFS=$'\n\t' | |
SELF_CMD="$0" | ||
KUBENS_DIR="${XDG_CACHE_HOME:-$HOME/.kube}/kubens" | ||
|
||
# Assign the main process' standard streams to variables so that they can be | ||
# used to invoke kubectl interactively while in a function. Interactively | ||
# invoking kubectl allows any configured client-go auth plugins to trigger. | ||
exec {STDIN}>&0 | ||
exec {STDOUT}>&1 | ||
exec {STDERR}>&2 | ||
|
||
usage() { | ||
cat <<"EOF" | ||
USAGE: | ||
|
@@ -58,7 +65,27 @@ current_context() { | |
} | ||
|
||
get_namespaces() { | ||
$KUBECTL get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}' | ||
name_jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}' | ||
namespaces="$($KUBECTL get namespaces -o=jsonpath="${name_jsonpath}" 2>/dev/null)" | ||
if [[ $? -eq 0 ]]; then | ||
echo "${namespaces}" | ||
return 0 | ||
fi | ||
|
||
# If kubectl fails to get namespaces it might be due to the need authenticate | ||
# with the cluster via a client-go authentication plugin. Auth plugins are | ||
# only triggered when kubectl is invoked with an interactive STDOUT, so the | ||
# original get namespaces attempt won't trigger them. | ||
# | ||
# This auth attempt with interactive standard streams will trigger any | ||
# existing auth plugins. If the auth is successful then the subsequent get | ||
# namespaces attempt will succeed and return the necessary namespace output. | ||
$KUBECTL auth can-i get namespaces --quiet 1>&$STDOUT 2>&$STDERR <&$STDIN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a comment here explaining the fallback etc? |
||
if [[ $? -ne 0 ]]; then | ||
return $? | ||
fi | ||
|
||
$KUBECTL get namespaces -o=jsonpath="${name_jsonpath}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the point of repeating this? (making the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the |
||
} | ||
|
||
escape_context_name() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add a comment about what do these do? I'm not sure...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just pushed those comments you requested. Happy to clarify them further if possible!
These three lines assign the main process' standard streams to variables so that they can be used to invoke
kubectl auth can-i get namespaces
in a function, but with the main process' standard streams as its STDOUT, STDERR and STDIN. Using the main process' standard streams makes thekubectl auth
call interactive, so client-go auth plugins are triggered