-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of kubectl command
Some duplication, always using default k8s
- Loading branch information
1 parent
6e1b9d0
commit cfd7011
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
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" | ||
|
||
"github.com/golang/glog" | ||
download "github.com/jimmidyson/go-download" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"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() | ||
|
||
binary := "kubectl" | ||
if runtime.GOOS == "windows" { | ||
binary = "kubectl.exe" | ||
} | ||
version := constants.DefaultKubernetesVersion | ||
path, err := maybeDownloadAndCache(binary, version) | ||
|
||
glog.Infof("Running %s %v", binary, args) | ||
c := exec.Command(path, args...) | ||
c.Stdout = os.Stdout | ||
c.Stderr = os.Stderr | ||
if err := c.Run(); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error running %s: %v\n", path, err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
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.GetKubernetesClientReleaseURL(binary, version) | ||
options := download.FileOptions{ | ||
Mkdirs: download.MkdirAll, | ||
} | ||
|
||
options.Checksum = constants.GetKubernetesClientReleaseURLSha1(binary, version) | ||
options.ChecksumHash = crypto.SHA1 | ||
|
||
glog.Infof("Downloading %s %s", binary, version) | ||
if err := download.ToFile(url, targetFilepath, options); err != nil { | ||
return "", errors.Wrapf(err, "Error downloading %s %s", binary, version) | ||
} | ||
glog.Infof("Finished 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters