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

[Feature] Add Bash shell support #66

Merged
merged 9 commits into from
May 29, 2019
Merged

Conversation

andyz-dev
Copy link
Contributor

After creating a cluster, k3d will display a hint on how to set the KUBECONFIG. Often the message scrolled away, and one has to either remember the syntax or hunt for the message.

This PR implemented the 'bash' subcommand to make switch between cluster context easier.

To start a new shell with a cluster context do:
$ k3d bash -n

This will gave a new shell with KUBECONFIG set for the cluster. kubectl will just work with the intended cluster. Use 'exit' to leave the shell

Also support a one liner use cases:

$ k3d bash -n -e 'kubectl cluster-info'

This is useful to quick issue commands against multiple clusters.

Limitations:

  • The PR only support bash, If it is useful, I hope others will contribute support for more shells.

andyz-dev added 7 commits May 24, 2019 17:16
Minor refactor. Add getClusterKubeConfigOath() to make logic reusable
for later changes.
Refactoring. Make createKubeconfigFile() a stand along function, so we
don't have to recreate the file every time we look up the file name.
Move the logic of retrive per cluster kube config file into
cli/cluster.go. Simplify the CLI handling code.
Add the basic frame work for supporting spawning a bash shell by cli
command.

With this change, we can spawn a bash shell in the context of a cluster

$ k3d create -n my-cluster
$ k3d bash -n my-cluster
[my-cluster] $>

// execute commands with KUBECONFIG already set up
[my-cluster] $> kubectl get pods
OS distribution and user may choose to install bash in different path.
This patch uses bash found by "$PATH" environment, rather than hard code
the bash path.

This patch also handle the case 'bash' are not found. Mostly likely due
to bash not being supported by the platform, or it is not installed.
In addition to provide an interactive shell, this patch adds the
'--command' and '-c' options to allow user to issue a command in the
context of a cluster.

For example:

$ k3d bash -c 'kubectl cluster-info'
In theory, we can execute 'k3d bash' again within an cluster shell. I
can't think of any practical value for allowing this capability.
On the contrary, this can lead to confusion to the user.

This patch adds a simple mechanism to detect and block recursive bash
invocation.
@andyz-dev andyz-dev requested review from zeerorg and iwilltry42 May 25, 2019 00:31
Copy link
Member

@iwilltry42 iwilltry42 left a comment

Choose a reason for hiding this comment

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

Works fine for me, although I'd love to have a more generic solution based on $SHELL instead of only bash.
Can we rename it to k3d shell and add different implementations as a flag?
E.g. k3d shell --shell zsh, where bash is the default value?
Because I don't think we should add additional shells as commands to k3d

andyz-dev added 2 commits May 27, 2019 17:14
@iwilltry42 suggested to group all shell support under and single
subcommand -- shell.
Add subshell argument --shell. Currently, support only bash, which is
also the default value.
@andyz-dev
Copy link
Contributor Author

Make sense @iwilltry42. I have implemented the suggestion and updated the PR. Thanks for the review.

@andyz-dev andyz-dev requested a review from iwilltry42 May 28, 2019 21:55
@zeerorg
Copy link
Collaborator

zeerorg commented May 29, 2019

Can we use the Shell from $SHELL as the default, instead of going for bash ?

@andyz-dev
Copy link
Contributor Author

Can we use the Shell from $SHELL as the default, instead of going for bash ?

I don't think $SHELL is that reliable:
azhou@Andys-MacBook-Pro:/projs$ echo $SHELL
/bin/bash
azhou@Andys-MacBook-Pro:
/projs$ ksh
[e]0;u@h: wa][033[01;32m]u@h[033[00m]:[033[01;34m]w[033[00m]$ echo $SHELL
/bin/bash
[e]0;u@h: wa][033[01;32m]u@h[033[00m]:[033[01;34m]w[033[00m]$

On the other hand, if your suggestion is that we always start "$SHELL", I am just not confident the changes in the PR, especially w.r.t. PS1 applies to other shells. I'd rather be conservative since I mostly use bash. I suppose if other shell users find this command useful, we should see more contributions on supporting of other shells -- we can probably generalize it more then.

@iwilltry42
Copy link
Member

Can we use the Shell from $SHELL as the default, instead of going for bash ?

I also was hoping for this to be possible. I tried it though and it's not compatible with the changes of this PR.
It also doesn't play well with other things specific to the different shells. E.g. --noprofile/--norc are only present in bash, but not in zsh or sh, but without them (or similar functionality in other shells), problems with user-specific settings (.bashrc/.zshrc) may arise (e.g. it conflicted with a setting in my .zshrc for auto-merging kubeconfigs).

cli/shell.go Show resolved Hide resolved
@iwilltry42 iwilltry42 merged commit df5cc1d into k3d-io:master May 29, 2019
@andyz-dev andyz-dev deleted the bash branch May 29, 2019 06:30
@sayjeyhi sayjeyhi mentioned this pull request Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants