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

[WIP] Dynamically download kubectl client #24

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.5.1
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/mod v0.2.0
gopkg.in/yaml.v2 v2.2.4
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
2 changes: 2 additions & 0 deletions pkg/kubernetes/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type TestMixin struct {
TestContext *context.TestContext
}

const MockkubectlClientVersion string = "v1.15.5"

func NewTestMixin(t *testing.T) *TestMixin {
c := context.NewTestContext(t)
m := New()
Expand Down
127 changes: 127 additions & 0 deletions pkg/kubernetes/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package kubernetes

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/pkg/errors"
"golang.org/x/mod/semver"
)

type KubectlVersion struct {
ClientVersion struct {
Major string `json:"major"`
Minor string `json:"minor"`
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate time.Time `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
} `json:"clientVersion"`
ServerVersion struct {
Major string `json:"major"`
Minor string `json:"minor"`
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate time.Time `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
} `json:"serverVersion"`
}

func (m *Mixin) init() error {

serverVersion, err := getServerVersion(m)

if err != nil {
return err
}

if semver.Compare(m.KubernetesClientVersion, serverVersion) == -1 && serverVersion != "" {

// Here we are triggering the download
fmt.Fprintf(m.Out, "Kubectl server version (%s) does not match client version (%s); downloading a compatible client.\n",
serverVersion, m.KubernetesClientVersion)
// try to install the new client
err := installClient(m, serverVersion)
if err != nil {
return errors.Wrap(err, "unable to install a compatible kubectl client")
}
}

return err
}

func installClient(m *Mixin, version string) error {

url := fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/amd64/kubectl", version)

// Fetch kubectl from url
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return errors.Wrap(err, "failed to construct GET request for fetching kubectl client binary")
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrapf(err, "failed to download kubectl client binary via url: %s", url)
}
defer res.Body.Close()

// Create a temp dir
tmpDir, err := m.FileSystem.TempDir("", "tmp")
if err != nil {
return errors.Wrap(err, "unable to create a temporary directory for downloading the kubectl client binary")
}
defer os.RemoveAll(tmpDir)

// Create the local binary
kubectlBinPath, err := m.FileSystem.Create(filepath.Join(tmpDir, "kubectlBin"))
if err != nil {
return errors.Wrap(err, "unable to create a local file for the kubectl client binary")
}

// Copy response body to local binary
_, err = io.Copy(kubectlBinPath, res.Body)
if err != nil {
return errors.Wrap(err, "unable to copy the kubectl client binary to the local binary file")
}

// Move the kubectl binary into the appropriate location
binPath := "/usr/local/bin/kubectl"
err = m.FileSystem.Rename(fmt.Sprintf("%s", kubectlBinPath.Name()), binPath)
if err != nil {
return errors.Wrapf(err, "unable to install the kubectl client binary to %q", binPath)
}
return nil
}

func getServerVersion(m *Mixin) (string, error) {

var stderr bytes.Buffer
currentKubectl := KubectlVersion{}

cmd := exec.Command("kubectl", "version", "-o", "json")
cmd.Stderr = &stderr
// Execute the command and the version output
outputBytes, err := cmd.Output()
if err != nil {
return "", errors.Wrapf(err, "unable to determine kubernetes server version: %s", stderr.String())
}
// Rebuild version json object
json.Unmarshal(outputBytes, &currentKubectl)

version := currentKubectl.ServerVersion.GitVersion

return version, nil
}
1 change: 0 additions & 1 deletion pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:generate packr2

package kubernetes

import (
Expand Down