Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion kubens
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

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...

Copy link
Author

@mnussbaum mnussbaum Jun 27, 2019

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 the kubectl auth call interactive, so client-go auth plugins are triggered

exec {STDOUT}>&1
exec {STDERR}>&2

usage() {
cat <<"EOF"
USAGE:
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the point of repeating this? (making the stderr visible to the user?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the kubectl auth can-i get namespaces successfully triggered an auth plugin above then this kubectl get namespaces call should now succeed and return the namespace output that get_namespaces is supposed to provide

}

escape_context_name() {
Expand Down