Skip to content

Commit

Permalink
Kubectl binary more tests and fix kots run support
Browse files Browse the repository at this point in the history
  • Loading branch information
emosbaugh committed Oct 20, 2021
1 parent 91e454b commit 04074b1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
15 changes: 6 additions & 9 deletions pkg/binaries/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,11 @@ type kubectlFuzzyVersion struct {
}

func newKubectlFuzzyVersion(major, minor uint64, path string) kubectlFuzzyVersion {
patch := uint64(0)
// allow for zero value catch all
if major != 0 && minor != 0 {
// I am making an assumption here that likely the package that we are bundling will
// be greater than x.x.0 thus setting patch version to 1 to prevent a semver range
// such as in kots.io docs (>1.16.0 <1.17.0) from matching an incorrect version.
patch = 1
}
return kubectlFuzzyVersion{
Version: semver.Version{
Major: major,
Minor: minor,
Patch: patch,
Patch: 0,
},
Path: path,
}
Expand All @@ -98,6 +90,11 @@ func (v kubectlFuzzyVersion) Match(userString string) bool {
return false
}

// match non-semver format 1.17 or v1.17
if userString == v.String() || userString == strings.TrimLeft(v.String(), "v") {
return true
}

// ignore error here as this could be a semver range
if exactVer, err := semver.Parse(userString); err == nil {
// fuzzy match major and minor, not patch
Expand Down
10 changes: 10 additions & 0 deletions pkg/binaries/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ func TestGetKubectlPathForVersion(t *testing.T) {
userString: "<1.17.0",
want: "/usr/local/bin/kubectl-v1.16",
},
{
name: "<=1.17.0",
userString: "<=1.17.0",
want: "/usr/local/bin/kubectl-v1.17",
},
{
name: "1.17",
userString: "1.17",
want: "/usr/local/bin/kubectl-v1.17",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
28 changes: 24 additions & 4 deletions pkg/cluster/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ import (
"path/filepath"
"time"

"github.com/blang/semver"
"github.com/mholt/archiver"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
"go.uber.org/zap"
)

const (
KotsKubeVersion = "1.22.1"
)

var (
kotsKubeSemVersion = semver.MustParse(KotsKubeVersion)
)

// ClientInit will download all binaries for the cluster
func ClientInit(ctx context.Context, dataDir string) error {
binRoot := BinRoot(dataDir)
Expand Down Expand Up @@ -621,7 +630,7 @@ current-context: authz`, "test")
}

func ensureKubeletBinary(rootDir string) error {
packageURI := `https://dl.k8s.io/v1.22.1/kubernetes-server-linux-amd64.tar.gz`
packageURI := fmt.Sprintf("https://dl.k8s.io/v%s/kubernetes-server-linux-amd64.tar.gz", KotsKubeVersion)
resp, err := http.Get(packageURI)
if err != nil {
return errors.Wrap(err, "download kubelet")
Expand All @@ -637,15 +646,22 @@ func ensureKubeletBinary(rootDir string) error {
}

func ensureKubectlBinary(rootDir string) error {
kubectlFilePath := filepath.Join(rootDir, "kubectl")
if err := downloadFileFromURL(kubectlFilePath, "https://dl.k8s.io/release/v1.22.1/bin/linux/amd64/kubectl"); err != nil {
filename := fmt.Sprintf("kubectl-v%d.%d", kotsKubeSemVersion.Major, kotsKubeSemVersion.Minor)
dest := filepath.Join(rootDir, filename)

if err := downloadFileFromURL(dest, fmt.Sprintf("https://dl.k8s.io/release/v%s/bin/linux/amd64/kubectl", KotsKubeVersion)); err != nil {
return err
}

if err := os.Chmod(kubectlFilePath, 0755); err != nil {
if err := os.Chmod(dest, 0755); err != nil {
return err
}

err := linkFile(dest, filepath.Join(rootDir, "kubectl"))
if err != nil {
return errors.Wrap(err, "link file")
}

return nil
}

Expand Down Expand Up @@ -735,3 +751,7 @@ func moveFile(sourcePath, destPath string) error {

return nil
}

func linkFile(sourcePath, destPath string) error {
return os.Symlink(sourcePath, destPath)
}

0 comments on commit 04074b1

Please sign in to comment.