Skip to content

Commit

Permalink
add k8s version strategy
Browse files Browse the repository at this point in the history
Although there is a version strategy for github, whereby the latest version is resolved via the gh release pages something similar did not exist for k8s releases.

https://cdn.dl.k8s.io/release/stable.txt is made available to enable resolution of the latest version.

This change integrates functionality that interrogates this file and uses its contents to assert the version of `kubectl` to retrieve.  This will negate the situation described in #1048 where the version is out of date until the `arkade` code is updated.

Signed-off-by: Richard Gee <richard@technologee.co.uk>
  • Loading branch information
rgee0 authored and alexellis committed May 11, 2024
1 parent 9be14a2 commit 9da1958
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
68 changes: 59 additions & 9 deletions pkg/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"github.com/alexellis/arkade/pkg/env"
)

const GitHubVersionStrategy = "github"
const k8sVersionStrategy = "k8s"

var supportedOS = [...]string{"linux", "darwin", "ming"}
var supportedArchitectures = [...]string{"x86_64", "arm", "amd64", "armv6l", "armv7l", "arm64", "aarch64"}

Expand Down Expand Up @@ -139,22 +142,34 @@ func (tool Tool) Head(uri string) (int, string, http.Header, error) {

func (tool Tool) GetURL(os, arch, version string, quiet bool) (string, error) {

if len(version) == 0 &&
(len(tool.URLTemplate) == 0 ||
strings.Contains(tool.URLTemplate, "https://github.com/") ||
tool.VersionStrategy == "github") {
if len(version) == 0 {

if !quiet {
log.Printf("Looking up version for %s", tool.Name)
}

v, err := FindGitHubRelease(tool.Owner, tool.Repo)
if err != nil {
return "", err
if len(tool.URLTemplate) == 0 ||
strings.Contains(tool.URLTemplate, "https://github.com/") ||
tool.VersionStrategy == GitHubVersionStrategy {

v, err := FindGitHubRelease(tool.Owner, tool.Repo)
if err != nil {
return "", err
}
version = v
}

if tool.VersionStrategy == k8sVersionStrategy {
v, err := FindK8sRelease()
if err != nil {
return "", err
}
version = v
}

if !quiet {
log.Printf("Found: %s", v)
log.Printf("Found: %s", version)
}
version = v
}

if len(tool.URLTemplate) > 0 {
Expand Down Expand Up @@ -230,6 +245,41 @@ func FindGitHubRelease(owner, repo string) (string, error) {
return version, nil
}

func FindK8sRelease() (string, error) {
url := "https://cdn.dl.k8s.io/release/stable.txt"

timeout := time.Second * 5
client := makeHTTPClient(&timeout, false)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", err
}

req.Header.Set("User-Agent", pkg.UserAgent())

res, err := client.Do(req)
if err != nil {
return "", err
}

if res.Body == nil {
return "", fmt.Errorf("unable to determine release of tool")
}

defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}

version := string(bodyBytes)
return version, nil
}

func getBinaryURL(owner, repo, version, downloadName string) string {
if in := strings.Index(downloadName, "/"); in > -1 {
return fmt.Sprintf(
Expand Down
34 changes: 34 additions & 0 deletions pkg/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ func getTool(name string, tools []Tool) *Tool {
return tool
}

func TestGetToolVersion(t *testing.T) {

testCases := []struct {
description string
tool Tool
version string
expected string
}{
{
description: "Version is empty, expect the tool's version to be returned",
tool: Tool{Version: "1.0.0"},
version: "",
expected: "1.0.0",
},
{
description: "Version argument is provided, expect the provided version to be returned",
tool: Tool{Version: "2.0.0"},
version: "1.2.0",
expected: "1.2.0",
},
}

// Iterate over test cases
for _, tc := range testCases {
// Call the function with test inputs
result := GetToolVersion(&tc.tool, tc.version)

// Check if the result matches the expected output
if result != tc.expected {
t.Errorf("%s: For tool version %s and input version %s, expected %s but got %s", tc.description, tc.tool.Version, tc.version, tc.expected, result)
}
}
}

func Test_MakeSureNoDuplicates(t *testing.T) {
count := map[string]int{}
tools := MakeTools()
Expand Down
12 changes: 6 additions & 6 deletions pkg/get/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func MakeTools() Tools {
Owner: "helm",
Repo: "helm",
Name: "helm",
VersionStrategy: "github",
VersionStrategy: GitHubVersionStrategy,
Description: "The Kubernetes Package Manager: Think of it like apt/yum/homebrew for Kubernetes.",
URLTemplate: `
{{$os := .OS}}
Expand Down Expand Up @@ -150,11 +150,11 @@ func MakeTools() Tools {
// https://dl.k8s.io/release/v1.22.2/bin/darwin/amd64/kubectl
tools = append(tools,
Tool{
Owner: "kubernetes",
Repo: "kubernetes",
Name: "kubectl",
Version: "v1.29.2",
Description: "Run commands against Kubernetes clusters",
Owner: "kubernetes",
Repo: "kubernetes",
Name: "kubectl",
VersionStrategy: k8sVersionStrategy,
Description: "Run commands against Kubernetes clusters",
URLTemplate: `{{$arch := "arm"}}
{{- if eq .Arch "x86_64" -}}
Expand Down

0 comments on commit 9da1958

Please sign in to comment.