Skip to content

Commit

Permalink
Merge pull request #75 from gzm55/work/rb-release-url
Browse files Browse the repository at this point in the history
add config key KbReleaseUrl for custom download mirror
  • Loading branch information
flavio authored Mar 26, 2024
2 parents ff7dc77 + ef18133 commit 42136df
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
| Go Report | Unit tests | License |
|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|---------|
| Go Report | Unit tests | License |
| ---------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| [![Go Report Card](https://goreportcard.com/badge/github.com/flavio/kuberlr)](https://goreportcard.com/report/github.com/flavio/kuberlr) | [![tests](https://github.com/flavio/kuberlr/workflows/tests/badge.svg?branch=master)](https://github.com/flavio/kuberlr/actions?query=workflow%3Atests+branch%3Amaster) | [![License: Apache 2.0](https://img.shields.io/badge/License-Apache2.0-brightgreen.svg)](https://opensource.org/licenses/Apache-2.0) |

> One kubectl to rule them all,
> one kubectl to find them,
> One kubectl to bring them all
> and in the darkness bind them.
> and in the darkness bind them.
Managing different kubernetes clusters often requires to keep multiple versions
of the kubectl available on the system, plus it poses the challenge to ensure
Expand All @@ -23,7 +23,7 @@ for all its components. This is [what is stated about kubectl](https://kubernete
> kubectl is supported at 1.19, 1.18, and 1.17
> ```
kuberlr (*kube-ruler*) is a simple wrapper for kubectl. Its main purpose is to
kuberlr (_kube-ruler_) is a simple wrapper for kubectl. Its main purpose is to
make it easy to manage clusters running different versions of kubernetes.
This is how kuberlr looks like in action:
Expand All @@ -48,7 +48,7 @@ $ ln -s ~/bin/kuberlr ~/bin/kubectl
## Usage
Use the `kubectl` *"fake binary"* as you usually do. Behind the scene
Use the `kubectl` _"fake binary"_ as you usually do. Behind the scene
kuberlr will ensure a compatible version of `kubectl` is used.
You can invoke the `kuberlr` binary in a direct fashion to access its
Expand Down Expand Up @@ -93,20 +93,20 @@ level (`~/.kuberlr/<GOOS>-<GOARCH>/`) and at system level (`/usr/bin`).
The kubectl binaries installed at system level must respect one of these naming
schemes in order to be used:
* `kubectl<major version>.<minor version>.<patch level>` (e.g.: `kubectl1.18.3`)
* `kubectl<major version>.<minor version>`: this would be handled as kubectl
version `<major version>.<minor version>.0`
- `kubectl<major version>.<minor version>.<patch level>` (e.g.: `kubectl1.18.3`)
- `kubectl<major version>.<minor version>`: this would be handled as kubectl
version `<major version>.<minor version>.0`
## Configuration
The behaviour of kuberlr can be adjusted by creating a configuration file in
one of these locations:
1. `/usr/etc/kuberlr.conf`: this is the location used by distributions like
openSUSE to handle the split between `/etc` and `/usr/etc`. You can find
more details [here](https://en.opensuse.org/openSUSE:Packaging_UsrEtc).
1. `/etc/kuberlr.conf`
1. `$HOME/.kuberlr/kuberlr.conf`
1. `/usr/etc/kuberlr.conf`: this is the location used by distributions like
openSUSE to handle the split between `/etc` and `/usr/etc`. You can find
more details [here](https://en.opensuse.org/openSUSE:Packaging_UsrEtc).
1. `/etc/kuberlr.conf`
1. `$HOME/.kuberlr/kuberlr.conf`
The configuration files are read in the order written above and merged together.
Configuration files can override the values defined by the previous ones, or
Expand All @@ -123,5 +123,8 @@ SystemPath = "/opt/bin"
# Timeout (sec) for requests made against the kubernetes API
Timeout = 1
```
# URL of the upstream mirror where kubectl binaries can be downloaded from
# Default "https://dl.k8s.io"
KubeMirrorUrl = "https://dl.k8s.io"
```
11 changes: 11 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (c *Cfg) Load() (*viper.Viper, error) {
v.SetDefault("AllowDownload", true)
v.SetDefault("SystemPath", common.SystemPath)
v.SetDefault("Timeout", 5)
v.SetDefault("KubeMirrorUrl", "https://dl.k8s.io")

v.SetConfigType("toml")

Expand All @@ -46,6 +47,16 @@ func (c *Cfg) Load() (*viper.Viper, error) {
return v, nil
}

// GetKubeMirrorURL returns the URL of the kubernetes mirror
func (c *Cfg) GetKubeMirrorURL() (string, error) {
v, err := c.Load()
if err != nil {
return "", err
}

return v.GetString("KubeMirrorUrl"), nil
}

func mergeConfig(v *viper.Viper, extraConfigPath string) error { //nolint: varnamelen
cfgFile := filepath.Join(extraConfigPath, "kuberlr.conf")

Expand Down
26 changes: 21 additions & 5 deletions internal/downloader/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ import (
"time"

"github.com/flavio/kuberlr/internal/common"
"github.com/flavio/kuberlr/internal/config"
"github.com/flavio/kuberlr/internal/osexec"

"github.com/blang/semver/v4"
"github.com/schollz/progressbar/v3"
)

// KubectlStableURL URL of the text file used by kubernetes community
// to hold the latest stable version of kubernetes released
const KubectlStableURL = "https://storage.googleapis.com/kubernetes-release/release/stable.txt"
func getKubeMirrorURL() (string, error) {
cfg := config.NewCfg()
return cfg.GetKubeMirrorURL()
}

// Downloder is a helper class that is used to interact with the
// kubernetes infrastructure holding released binaries and release information
Expand Down Expand Up @@ -57,7 +59,16 @@ func (d *Downloder) getContentsOfURL(url string) (string, error) {
// UpstreamStableVersion returns the latest version of kubernetes that upstream
// considers stable
func (d *Downloder) UpstreamStableVersion() (semver.Version, error) {
v, err := d.getContentsOfURL(KubectlStableURL)
baseURL, err := getKubeMirrorURL()
if err != nil {
return semver.Version{}, err
}
url, err := url.Parse(fmt.Sprintf("%s/release/stable.txt", baseURL))
if err != nil {
return semver.Version{}, err
}

v, err := d.getContentsOfURL(url.String())
if err != nil {
return semver.Version{}, err
}
Expand Down Expand Up @@ -106,8 +117,13 @@ func (d *Downloder) GetKubectlBinary(version semver.Version, destination string)

func (d *Downloder) kubectlDownloadURL(version semver.Version) (string, error) {
// Example: https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectlI
baseURL, err := getKubeMirrorURL()
if err != nil {
return "", err
}
url, err := url.Parse(fmt.Sprintf(
"https://storage.googleapis.com/kubernetes-release/release/v%d.%d.%d/bin/%s/%s/kubectl%s",
"%s/release/v%d.%d.%d/bin/%s/%s/kubectl%s",
baseURL,
version.Major,
version.Minor,
version.Patch,
Expand Down
5 changes: 5 additions & 0 deletions kuberlr.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ SystemPath = "/usr/bin"
# Timeout (sec) for requests made against the kubernetes API
# Default 5 seconds
Timeout = 5

# URL of the upstream mirror where kubectl binaries can be downloaded from
# Default "https://dl.k8s.io"
KubeMirrorUrl = "https://dl.k8s.io"

0 comments on commit 42136df

Please sign in to comment.