Skip to content

Commit

Permalink
Initial implementation of kubectl command
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Mar 26, 2019
1 parent adc8cf0 commit 9d85895
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 7 deletions.
123 changes: 123 additions & 0 deletions cmd/minikube/cmd/kubectl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"crypto"
"fmt"
"os"
"os/exec"
"path"
"runtime"
"syscall"

"github.com/golang/glog"
download "github.com/jimmidyson/go-download"
"github.com/pkg/errors"
"github.com/spf13/cobra"
pkg_config "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine"
)

// kubectlCmd represents the kubectl command
var kubectlCmd = &cobra.Command{
Use: "kubectl",
Short: "Run kubectl",
Long: `Run the kubernetes client, download it if necessary.`,
Run: func(cmd *cobra.Command, args []string) {
api, err := machine.NewAPIClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting client: %v\n", err)
os.Exit(1)
}
defer api.Close()

cc, err := pkg_config.Load()
if err != nil && !os.IsNotExist(err) {
console.ErrLn("Error loading profile config: %v", err)
}

binary := "kubectl"
if runtime.GOOS == "windows" {
binary = "kubectl.exe"
}

version := constants.DefaultKubernetesVersion
if cc != nil {
version = cc.KubernetesConfig.KubernetesVersion
}

path, err := maybeDownloadAndCache(binary, version)

glog.Infof("Running %s %v", path, args)
c := exec.Command(path, args...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
var rc int
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
rc = waitStatus.ExitStatus()
} else {
fmt.Fprintf(os.Stderr, "Error running %s: %v\n", path, err)
rc = 1
}
os.Exit(rc)
}
},
}

func maybeDownloadAndCache(binary, version string) (string, error) {
targetDir := constants.MakeMiniPath("cache", version)
targetFilepath := path.Join(targetDir, binary)

_, err := os.Stat(targetFilepath)
// If it exists, do no verification and continue
if err == nil {
return targetFilepath, nil
}
if !os.IsNotExist(err) {
return "", errors.Wrapf(err, "stat %s version %s at %s", binary, version, targetDir)
}

if err = os.MkdirAll(targetDir, 0777); err != nil {
return "", errors.Wrapf(err, "mkdir %s", targetDir)
}

url := constants.GetKubernetesReleaseURL(binary, version, runtime.GOOS, runtime.GOARCH)
options := download.FileOptions{
Mkdirs: download.MkdirAll,
}

options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version, runtime.GOOS, runtime.GOARCH)
options.ChecksumHash = crypto.SHA1

console.OutStyle("file-download", "Downloading %s %s", binary, version)
if err := download.ToFile(url, targetFilepath, options); err != nil {
return "", errors.Wrapf(err, "Error downloading %s %s", binary, version)
}
if err = os.Chmod(targetFilepath, 0755); err != nil {
return "", errors.Wrapf(err, "chmod +x %s", targetFilepath)
}
return targetFilepath, nil
}

func init() {
RootCmd.AddCommand(kubectlCmd)
}
5 changes: 3 additions & 2 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"os"
"path"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -601,12 +602,12 @@ func maybeDownloadAndCache(binary, version string) (string, error) {
return "", errors.Wrapf(err, "mkdir %s", targetDir)
}

url := constants.GetKubernetesReleaseURL(binary, version)
url := constants.GetKubernetesReleaseURL(binary, version, "linux", runtime.GOARCH)
options := download.FileOptions{
Mkdirs: download.MkdirAll,
}

options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version)
options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version, "linux", runtime.GOARCH)
options.ChecksumHash = crypto.SHA1

console.OutStyle("file-download", "Downloading %s %s", binary, version)
Expand Down
9 changes: 4 additions & 5 deletions pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -206,13 +205,13 @@ const (
)

// GetKubernetesReleaseURL gets the location of a kubernetes client
func GetKubernetesReleaseURL(binaryName, version string) string {
return fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/%s/%s", version, runtime.GOARCH, binaryName)
func GetKubernetesReleaseURL(binaryName, version, osName, archName string) string {
return fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/%s/bin/%s/%s/%s", version, osName, archName, binaryName)
}

// GetKubernetesReleaseURLSHA1 gets the location of a kubernetes client checksum
func GetKubernetesReleaseURLSHA1(binaryName, version string) string {
return fmt.Sprintf("%s.sha1", GetKubernetesReleaseURL(binaryName, version))
func GetKubernetesReleaseURLSHA1(binaryName, version, osName, archName string) string {
return fmt.Sprintf("%s.sha1", GetKubernetesReleaseURL(binaryName, version, osName, archName))
}

// IsMinikubeChildProcess is the name of "is minikube child process" variable
Expand Down

0 comments on commit 9d85895

Please sign in to comment.