├── .github ├── CODEOWNERS └── workflows │ └── workflow.yml ├── LICENSE ├── Makefile ├── README.md └── bin ├── install ├── latest-stable └── list-all /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @asdf-community/asdf-kubectl 2 | -------------------------------------------------------------------------------- /.github/workflows/workflow.yml: -------------------------------------------------------------------------------- 1 | name: Test Plugin 2 | 3 | on: 4 | pull_request: 5 | push: 6 | schedule: 7 | - cron: 0 1 * * MON 8 | 9 | jobs: 10 | test-plugin: 11 | name: Test Plugin 12 | 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | os: 17 | - macos-latest 18 | - ubuntu-latest 19 | 20 | runs-on: ${{ matrix.os }} 21 | 22 | steps: 23 | - uses: asdf-vm/actions/plugin-test@v2 24 | with: 25 | command: kubectl version --client=true 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jack Henry & Associates 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SRCFILES = $(shell git ls-files "bin/**") 2 | SHFMT_BASE_FLAGS = -s -i 2 -ci 3 | 4 | format: 5 | shfmt -w $(SHFMT_BASE_FLAGS) $(SRCFILES) 6 | .PHONY: format 7 | 8 | format-check: 9 | shfmt -d $(SHFMT_BASE_FLAGS) $(SRCFILES) 10 | .PHONY: format-check 11 | 12 | lint: 13 | shellcheck $(SRCFILES) 14 | .PHONY: lint 15 | 16 | test: 17 | bats test 18 | .PHONY: test 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # asdf-kubectl 2 | 3 | Kubectl plugin for [asdf](https://github.com/asdf-vm/asdf) version manager 4 | 5 | ## Install 6 | 7 | ``` 8 | asdf plugin-add kubectl https://github.com/asdf-community/asdf-kubectl.git 9 | ``` 10 | 11 | ## Use 12 | 13 | Check out the [asdf documentation](https://asdf-vm.com/#/core-manage-versions?id=install-version) for instructions on how to install and manage versions of Kubectl. 14 | 15 | ## Architecture Override 16 | 17 | The `ASDF_KUBECTL_OVERWRITE_ARCH` variable can be used to override the architecture that is used for determining which `kubectl` build to download. The primary use case is when attempting to install an older version of `kubectl` for use on an Apple M1 computer as `kubectl` was not built for ARM at the time. 18 | 19 | ### Without `ASDF_KUBECTL_OVERWRITE_ARCH`: 20 | 21 | ``` 22 | % asdf install kubectl 1.18.17 23 | Downloading kubectl from https://storage.googleapis.com/kubernetes-release/release/v1.18.17/bin/darwin/arm64/kubectl 24 | ``` 25 | 26 | ### With `ASDF_KUBECTL_OVERWRITE_ARCH`: 27 | 28 | ``` 29 | % ASDF_KUBECTL_OVERWRITE_ARCH=amd64 asdf install kubectl 1.18.17 30 | Downloading kubectl from https://storage.googleapis.com/kubernetes-release/release/v1.18.17/bin/darwin/amd64/kubectl 31 | ``` 32 | -------------------------------------------------------------------------------- /bin/install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | set -o pipefail 5 | 6 | ASDF_INSTALL_TYPE=${ASDF_INSTALL_TYPE:-version } 7 | [ -n "$ASDF_INSTALL_VERSION" ] || (echo >&2 'Missing ASDF_INSTALL_VERSION' && exit 1) 8 | [ -n "$ASDF_INSTALL_PATH" ] || (echo >&2 'Missing ASDF_INSTALL_PATH' && exit 1) 9 | 10 | install_kubectl() { 11 | # local install_type=$1 12 | local version=$2 13 | local install_path=$3 14 | local bin_install_path="$install_path/bin" 15 | local download_url 16 | download_url="$(get_download_url "$version")" 17 | 18 | mkdir -p "${bin_install_path}" 19 | 20 | local bin_path="${bin_install_path}/kubectl" 21 | echo "Downloading kubectl from ${download_url}" 22 | if curl -L -sf "$download_url" -o "$bin_path"; then 23 | chmod +x "$bin_path" 24 | else 25 | echo "Failed to download kubectl from ${download_url}. Does the version exist and is available in the requested $(get_cpu) architecture?" 26 | exit 1 27 | fi 28 | } 29 | 30 | get_arch() { 31 | uname | tr '[:upper:]' '[:lower:]' 32 | } 33 | 34 | get_cpu() { 35 | local machine_hardware_name 36 | machine_hardware_name=${ASDF_KUBECTL_OVERWRITE_ARCH:-"$(uname -m)"} 37 | 38 | case "$machine_hardware_name" in 39 | 'x86_64') local cpu_type="amd64" ;; 40 | 'powerpc64le' | 'ppc64le') local cpu_type="ppc64le" ;; 41 | 'aarch64') local cpu_type="arm64" ;; 42 | 'armv7l') local cpu_type="arm" ;; 43 | *) local cpu_type="$machine_hardware_name" ;; 44 | esac 45 | 46 | echo "$cpu_type" 47 | } 48 | 49 | get_download_url() { 50 | local version="$1" 51 | local platform 52 | platform="$(get_arch)" 53 | local cpu 54 | cpu=$(get_cpu) 55 | echo "https://dl.k8s.io/release/v${version}/bin/${platform}/${cpu}/kubectl" 56 | } 57 | 58 | install_kubectl "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" 59 | -------------------------------------------------------------------------------- /bin/latest-stable: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | query="${1-}" 6 | if [ -z "$query" ]; then 7 | # No filtering needed, select latest stable 8 | curl -L -s https://dl.k8s.io/release/stable.txt | cut -c 2- - 9 | else 10 | asdf list all kubectl "$query" | grep -ivE '(^Available version:|-src|-dev|-latest|-stm|[-\.]rc|-alpha|-beta|[-\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)' | sed 's/^[[:space:]]\+//' | tail -1 11 | fi 12 | -------------------------------------------------------------------------------- /bin/list-all: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | # stolen from https://github.com/rbenv/ruby-build/pull/631/files#diff-fdcfb8a18714b33b07529b7d02b54f1dR942 6 | function sort_versions() { 7 | sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' | 8 | LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' 9 | } 10 | 11 | list_all_versions() { 12 | git ls-remote --tags --refs https://github.com/kubernetes/kubernetes.git | 13 | grep -o 'refs/tags/.*' | 14 | cut -d/ -f3- | 15 | sed 's/^v//' 16 | } 17 | 18 | list_all_versions | sort_versions | xargs echo 19 | --------------------------------------------------------------------------------