Skip to content

Instantly share code, notes, and snippets.

@rdump
Last active April 4, 2024 15:20
Show Gist options
  • Star 71 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save rdump/b79a63084b47f99a41514432132bd408 to your computer and use it in GitHub Desktop.
Save rdump/b79a63084b47f99a41514432132bd408 to your computer and use it in GitHub Desktop.
kubectl multi-version brews (kubernetes-cli formula)

kubectl multi-version brews

Applicability

The instructions below apply to older versions of Homebrew which still provide switch capability.

For current Homebrew, you'll likely need to keep Versions around, and build locally. Here's my versions repository https://github.com/rdump/homebrew-versions

MacPorts is now keeping versioned installations available as well, by default.

Introduction to Older Brew Switch Option

We need to run multiple versions of kubectl for compatibility with various kubernetes clusters.

It's a bit easier to get and use multiple versions of kubectl using Homebrew than it is using Macports.

With brew, it's simple to:

  • Install a bunch of versions of kubectl (in the kubernetes-cli Formula)
  • Switch between them as needed
  • Pin a particular release so you don't auto-upgrade into incompatibility with your kubernetes cluster(s)

Preparation

Get the versions and commit IDs from

     https://github.com/Homebrew/homebrew-core/commits/master/Formula/kubernetes-cli.rb

or locally from your homebrew-core clone

     cd ~/src/github.com/Homebrew/homebrew-core

     git log master -- Formula/kubernetes-cli.rb

Then use the commit IDs (typically for the ones referencing updates to the bottle) to fetch a specific version via

    brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/{commitIDhere}/Formula/kubernetes-cli.rb

(See Example Install Commands below)

Initial Installs

If you install a bunch of versions and set environment variable HOMEBREW_NO_INSTALL_CLEANUP, they'll be available for hot brew switch action later.

To keep prior versions around despite brew's new default behavior of automatically running cleanup (nuke) during installs of newer versions, run

     export HOMEBREW_NO_INSTALL_CLEANUP=1

or maybe even put it in your .profile || .bashrc if you have the disk for old versions in general.

Between each initial version install, you'll need to unlink each time to make way for the next version

     brew install ...

     brew unlink kubernetes-cli

     brew install ...

     brew unlink kubernetes-cli

     etc.

Once you have the versions installed, you don't need to unlink to switch between the versions.

Switching

To link up a specific version from your available versions

  • Check your active version

     kubectl version

  • Get available versions, short form, using the bogus version trick

     brew switch kubernetes-cli 0

  • Pick one version

     brew switch kubernetes-cli 1.8.5

  • Or pick another version

     brew switch kubernetes-cli 1.11.3

You could also get versions, long form with other info

     brew info kubernetes-cli

Pinning

Once you're switched to a version you want to stick with for a while, you can pin it to prevent upgrades during a global brew upgrade.

     brew pin kubernetes-cli

You'll need to brew unpin kubernetes-cli when you want to move back to the head release. Otherwise, you can switch between previously installed Formula versions while pinned.

Example Install Commands

Here are some versions' commit IDs all set up for you.

1.18.8

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/6bfaa02698bd847abcc125591600c7d3bf8f0964/Formula/kubernetes-cli.rb

1.17.4

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/7104022700e3c6eff3ea5b3f61f52fb9ebc3800c/Formula/kubernetes-cli.rb

1.16.3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/944a4a0dff60d13904763a2455e3261eac833691/Formula/kubernetes-cli.rb

1.15.3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/7ee32113351bbd913b90f9578bcd52dfe85d675e/Formula/kubernetes-cli.rb

1.14.3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/fcb6cce3bdd37db3c53c2fcbdf9a47671a592c96/Formula/kubernetes-cli.rb

1.13.4

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/1827ebb2fe905c931deb283f7245eaf465c7cbbf/Formula/kubernetes-cli.rb

1.12.3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/7a371a4d27fa6d80b1e37cac16c2dadcb78c7111/Formula/kubernetes-cli.rb

1.11.3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/41764e07727d9a86b0f8a077117dc7876ca294c4/Formula/kubernetes-cli.rb

1.10.5

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/d09d97241b17a5e02a25fc51fc56e2a5de74501c/Formula/kubernetes-cli.rb

1.9.6

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/505fcec7a3cf4f1a073b45bc7ae8294649a33f89/Formula/kubernetes-cli.rb

1.8.5

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/e5538d7e7966795ae50cd94678ee7131fad2d7b7/Formula/kubernetes-cli.rb

1.7.6

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/1e866e9cd915c37184c248fb726775ff8a30c91e/Formula/kubernetes-cli.rb

1.6.6

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/4635c994b205a72c9e488b81306d46622fe72b1b/Formula/kubernetes-cli.rb

1.5.5

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/2d45e95b57c024eb9bda7c6fba9151a28bc0345f/Formula/kubernetes-cli.rb

@onmomo
Copy link

onmomo commented Oct 21, 2019

thanks for this!

@mathiasPop
Copy link

Great! Thanks for this! Will you keep it up to date?

@rdump
Copy link
Author

rdump commented Nov 10, 2019

Yes, updated occasionally to get the latest x.x.Y version in the list. And with instructions to avoid nuking old versions by default, or the like.

@michaeljohnbennett
Copy link

dude, why is this not in the JX doc package! This is so important, so much problems installing the correct kube and helm versions on OSX today trying this out.

@naviat
Copy link

naviat commented May 9, 2020

Thanks @rdump !

@yosit
Copy link

yosit commented May 9, 2020

asdf has an excellent support for kubectl:
asdf plugin add kubectl
asdf install kubectl 1.15.10
asdf install kubectl latest

@abdennour
Copy link

v1.18

alias kubectl='docker run --rm -it -v $(pwd):/hostdir -w /hostdir abdennour/kubectl:v1.18.3';
# you are now working with kubectl v1.18

v1.17

alias kubectl='docker run --rm -it -v $(pwd):/hostdir -w /hostdir abdennour/kubectl:v1.17.4';
# now you downgrade  

So and and so forth.

@tgelpi-gmail
Copy link

Thank you for this info

@rdump
Copy link
Author

rdump commented Sep 4, 2020

Since homebrew has now disabled direct installation from a github commit URL, I'm testing a replacement method.

The error you'll see is:

Error: Calling Installation of kubernetes-cli from a GitHub commit URL is disabled! Use 'brew extract kubernetes-cli' to stable tap on GitHub instead.

The replacement will involve using/cloning https://github.com/rdump/homebrew-versions and picking the versions you care about. I'll update this gist after some more testing.

@kehao95
Copy link

kehao95 commented Sep 11, 2020

you can use this git command for better log output

git --no-pager log --oneline  master -- Formula/kubernetes-cli.rb

But unfortunately I think there are no Formulas for branched kubernetes-cli yet.
Brew may have that for example you can install

brew search postgresql
==> Formulae
postgresql ✔               postgresql@11              postgresql@9.5
postgresql@10              postgresql@9.4             postgresql@9.6
==> Casks
navicat-for-postgresql

Im expecting brew have that for kubernetes-cli in the future might look like

brew search kubernetes-cli
==> Formulae
kubernetes-cli ✔.    kubernetes-cli@1.19       kubernetes-cli@1.18    
kubernetes-cli@1.17       kubernetes-cli@1.16

@rdump
Copy link
Author

rdump commented Sep 17, 2020

The current test state. Backporting and building anew is a far poorer option than the pouring of an existing build which doesn't need all kinds of spec tweaks for architectures, go versions, and the like. But it's the best I've been able to find so far since brew broke using existing builds.

$ brew search kubernetes-cli
==> Formulae
kubernetes-cli ✔                              rdump/versions/kubernetes-cli@1.14.3 ✔        rdump/versions/kubernetes-cli@1.6.6 ✔
rdump/versions/kubernetes-cli@1.10.5 ✔        rdump/versions/kubernetes-cli@1.15.3 ✔        rdump/versions/kubernetes-cli@1.7.6
rdump/versions/kubernetes-cli@1.11.3 ✔        rdump/versions/kubernetes-cli@1.16.3 ✔        rdump/versions/kubernetes-cli@1.8.5 ✔
rdump/versions/kubernetes-cli@1.12.3 ✔        rdump/versions/kubernetes-cli@1.17.4 ✔        rdump/versions/kubernetes-cli@1.9.6 ✔
rdump/versions/kubernetes-cli@1.13.4 ✔        rdump/versions/kubernetes-cli@1.18.8 ✔

https://github.com/rdump/homebrew-versions has work in progress

@IdiosApps
Copy link

IdiosApps commented Sep 25, 2020

Since homebrew has now disabled direct installation from a github commit URL, I'm testing a replacement method.

The error you'll see is:

Error: Calling Installation of kubernetes-cli from a GitHub commit URL is disabled! Use 'brew extract kubernetes-cli' to stable tap on GitHub instead.

@rdump I'm not sure if it helps your use-case, but you can download the Ruby file and use the local file for brew install.

@cm0s
Copy link

cm0s commented Mar 1, 2021

brew switch command has been removed. Any other way to accomplish the same result without this command?

@tvalasek
Copy link

same here.. brew switch won't work. I switched to using asdf (https://migueldavid.eu/using-multiple-versions-of-kubectl-on-macos)

@zation
Copy link

zation commented Mar 31, 2021

Thanks for your info. I've tried to install 1.6.6 on Apple M1, but got Error: go@1.9 has been disabled because it is not supported upstream!. How can I build it with new version of go?

@yosit
Copy link

yosit commented Apr 1, 2021

Putting it here for future reference.
asdf https://github.com/asdf-vm/asdf has amazing support for installing different versions of kubectl

@rajpatel9498
Copy link

v1.18

alias kubectl='docker run --rm -it -v $(pwd):/hostdir -w /hostdir abdennour/kubectl:v1.18.3';
# you are now working with kubectl v1.18

v1.17

alias kubectl='docker run --rm -it -v $(pwd):/hostdir -w /hostdir abdennour/kubectl:v1.17.4';
# now you downgrade  

So and and so forth.

This helped me.

@mleklund
Copy link

Went down the asdf rabbit hole, an issue for us is that asdf kubectl downloads directly from github, not compiling. This means that CGO is off and if you are connecting over a VPN this can cause dns issues. Maybe we will fall back to docker. sigh.

@sastorsl
Copy link

sastorsl commented Apr 4, 2024

I'm really not sure if it was worth it, I think I'll rather end up with a separate version of kubectl in my $HOME/bin, but here goes.

How to create one's own cask with the last available version of the kubernetes-cli version 1.28 that was in homebrew/core.
...to find the version I had do go git log in the homebrew/core repo on github.

# Create a "new" brew tap of the kubernetes-cli on the last
# v1.28 version available _in homebrew/core_
brew tap-new foo/bar
brew tap --force homebrew/core  # need to force now, pr March 2024
brew extract --version=1.28.4 kubernetes-cli foo/bar
brew install foo/bar/kubernetes-cli@1.28.4
brew link --overwrite kubernetes-cli@1.28.4

# Revert back to the latest
brew unlink kubernetes-cli@1.28.4
brew link --overwrite kubernetes-cli

# Lastly, don't want to be a developer anymore
brew developer off

I wanted to put all of this in a separate tap, hence the foo/bar - don't want to mess with brew itself.

Caveat: There are a lot of dependencies in other packages on kubernetes-cli - so afaik it's not trivial to maintain multiple versions in brew itself. ymmv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment