Skip to content

Commit ca26ae8

Browse files
committed
Initial implementation of kubectl command
1 parent b497fad commit ca26ae8

File tree

4 files changed

+104
-10
lines changed

4 files changed

+104
-10
lines changed

cmd/minikube/cmd/kubectl.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"os/exec"
23+
"runtime"
24+
"syscall"
25+
26+
"github.com/golang/glog"
27+
"github.com/spf13/cobra"
28+
pkg_config "k8s.io/minikube/pkg/minikube/config"
29+
"k8s.io/minikube/pkg/minikube/console"
30+
"k8s.io/minikube/pkg/minikube/constants"
31+
"k8s.io/minikube/pkg/minikube/exit"
32+
"k8s.io/minikube/pkg/minikube/machine"
33+
)
34+
35+
// kubectlCmd represents the kubectl command
36+
var kubectlCmd = &cobra.Command{
37+
Use: "kubectl",
38+
Short: "Run kubectl",
39+
Long: `Run the kubernetes client, download it if necessary.`,
40+
Run: func(cmd *cobra.Command, args []string) {
41+
api, err := machine.NewAPIClient()
42+
if err != nil {
43+
fmt.Fprintf(os.Stderr, "Error getting client: %v\n", err)
44+
os.Exit(1)
45+
}
46+
defer api.Close()
47+
48+
cc, err := pkg_config.Load()
49+
if err != nil && !os.IsNotExist(err) {
50+
console.ErrLn("Error loading profile config: %v", err)
51+
}
52+
53+
binary := "kubectl"
54+
if runtime.GOOS == "windows" {
55+
binary = "kubectl.exe"
56+
}
57+
58+
version := constants.DefaultKubernetesVersion
59+
if cc != nil {
60+
version = cc.KubernetesConfig.KubernetesVersion
61+
}
62+
63+
path, err := machine.CacheBinary(binary, version, runtime.GOOS, runtime.GOARCH)
64+
if err != nil {
65+
exit.WithError("Failed to download kubectl", err)
66+
}
67+
68+
glog.Infof("Running %s %v", path, args)
69+
c := exec.Command(path, args...)
70+
c.Stdout = os.Stdout
71+
c.Stderr = os.Stderr
72+
if err := c.Run(); err != nil {
73+
var rc int
74+
if exitError, ok := err.(*exec.ExitError); ok {
75+
waitStatus := exitError.Sys().(syscall.WaitStatus)
76+
rc = waitStatus.ExitStatus()
77+
} else {
78+
fmt.Fprintf(os.Stderr, "Error running %s: %v\n", path, err)
79+
rc = 1
80+
}
81+
os.Exit(rc)
82+
}
83+
},
84+
}
85+
86+
func init() {
87+
RootCmd.AddCommand(kubectlCmd)
88+
}

pkg/minikube/bootstrapper/kubeadm/kubeadm.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net"
2424
"net/http"
25+
"runtime"
2526
"strings"
2627
"time"
2728

@@ -572,7 +573,7 @@ func downloadBinaries(cfg config.KubernetesConfig, c bootstrapper.CommandRunner)
572573
for _, bin := range constants.GetKubeadmCachedBinaries() {
573574
bin := bin
574575
g.Go(func() error {
575-
path, err := machine.CacheBinary(bin, cfg.KubernetesVersion)
576+
path, err := machine.CacheBinary(bin, cfg.KubernetesVersion, "linux", runtime.GOARCH)
576577
if err != nil {
577578
return errors.Wrapf(err, "downloading %s", bin)
578579
}

pkg/minikube/constants/constants.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"os"
2222
"path/filepath"
23-
"runtime"
2423
"strings"
2524
"time"
2625

@@ -212,13 +211,13 @@ const (
212211
)
213212

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

219218
// GetKubernetesReleaseURLSHA1 gets the location of a kubernetes client checksum
220-
func GetKubernetesReleaseURLSHA1(binaryName, version string) string {
221-
return fmt.Sprintf("%s.sha1", GetKubernetesReleaseURL(binaryName, version))
219+
func GetKubernetesReleaseURLSHA1(binaryName, version, osName, archName string) string {
220+
return fmt.Sprintf("%s.sha1", GetKubernetesReleaseURL(binaryName, version, osName, archName))
222221
}
223222

224223
// IsMinikubeChildProcess is the name of "is minikube child process" variable

pkg/minikube/machine/cache_binaries.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto"
2121
"os"
2222
"path"
23+
"runtime"
2324

2425
"github.com/golang/glog"
2526
"github.com/jimmidyson/go-download"
@@ -39,7 +40,7 @@ func CacheBinariesForBootstrapper(version string, clusterBootstrapper string) er
3940
for _, bin := range binaries {
4041
bin := bin
4142
g.Go(func() error {
42-
if _, err := CacheBinary(bin, version); err != nil {
43+
if _, err := CacheBinary(bin, version, "linux", runtime.GOARCH); err != nil {
4344
return errors.Wrapf(err, "caching image %s", bin)
4445
}
4546
return nil
@@ -49,11 +50,11 @@ func CacheBinariesForBootstrapper(version string, clusterBootstrapper string) er
4950
}
5051

5152
// CacheBinary will cache a binary on the host
52-
func CacheBinary(binary, version string) (string, error) {
53+
func CacheBinary(binary, version, osName, archName string) (string, error) {
5354
targetDir := constants.MakeMiniPath("cache", version)
5455
targetFilepath := path.Join(targetDir, binary)
5556

56-
url := constants.GetKubernetesReleaseURL(binary, version)
57+
url := constants.GetKubernetesReleaseURL(binary, version, osName, archName)
5758

5859
_, err := os.Stat(targetFilepath)
5960
// If it exists, do no verification and continue
@@ -73,13 +74,18 @@ func CacheBinary(binary, version string) (string, error) {
7374
Mkdirs: download.MkdirAll,
7475
}
7576

76-
options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version)
77+
options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version, osName, archName)
7778
options.ChecksumHash = crypto.SHA1
7879

7980
console.OutStyle("file-download", "Downloading %s %s", binary, version)
8081
if err := download.ToFile(url, targetFilepath, options); err != nil {
8182
return "", errors.Wrapf(err, "Error downloading %s %s", binary, version)
8283
}
84+
if osName == runtime.GOOS && archName == runtime.GOARCH {
85+
if err = os.Chmod(targetFilepath, 0755); err != nil {
86+
return "", errors.Wrapf(err, "chmod +x %s", targetFilepath)
87+
}
88+
}
8389
return targetFilepath, nil
8490
}
8591

0 commit comments

Comments
 (0)