Bumping k8s dependencies to 1.13
This commit is contained in:
104
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/cmd.go
generated
vendored
104
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/cmd.go
generated
vendored
@@ -21,6 +21,12 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
utilflag "k8s.io/apiserver/pkg/util/flag"
|
||||
@@ -36,8 +42,7 @@ import (
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/wait"
|
||||
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -257,7 +262,100 @@ var (
|
||||
)
|
||||
|
||||
func NewDefaultKubectlCommand() *cobra.Command {
|
||||
return NewKubectlCommand(os.Stdin, os.Stdout, os.Stderr)
|
||||
return NewDefaultKubectlCommandWithArgs(&defaultPluginHandler{}, os.Args, os.Stdin, os.Stdout, os.Stderr)
|
||||
}
|
||||
|
||||
func NewDefaultKubectlCommandWithArgs(pluginHandler PluginHandler, args []string, in io.Reader, out, errout io.Writer) *cobra.Command {
|
||||
cmd := NewKubectlCommand(in, out, errout)
|
||||
|
||||
if pluginHandler == nil {
|
||||
return cmd
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
cmdPathPieces := args[1:]
|
||||
|
||||
// only look for suitable extension executables if
|
||||
// the specified command does not already exist
|
||||
if _, _, err := cmd.Find(cmdPathPieces); err != nil {
|
||||
if err := handleEndpointExtensions(pluginHandler, cmdPathPieces); err != nil {
|
||||
fmt.Fprintf(errout, "%v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// PluginHandler is capable of parsing command line arguments
|
||||
// and performing executable filename lookups to search
|
||||
// for valid plugin files, and execute found plugins.
|
||||
type PluginHandler interface {
|
||||
// Lookup receives a potential filename and returns
|
||||
// a full or relative path to an executable, if one
|
||||
// exists at the given filename, or an error.
|
||||
Lookup(filename string) (string, error)
|
||||
// Execute receives an executable's filepath, a slice
|
||||
// of arguments, and a slice of environment variables
|
||||
// to relay to the executable.
|
||||
Execute(executablePath string, cmdArgs, environment []string) error
|
||||
}
|
||||
|
||||
type defaultPluginHandler struct{}
|
||||
|
||||
// Lookup implements PluginHandler
|
||||
func (h *defaultPluginHandler) Lookup(filename string) (string, error) {
|
||||
// if on Windows, append the "exe" extension
|
||||
// to the filename that we are looking up.
|
||||
if runtime.GOOS == "windows" {
|
||||
filename = filename + ".exe"
|
||||
}
|
||||
|
||||
return exec.LookPath(filename)
|
||||
}
|
||||
|
||||
// Execute implements PluginHandler
|
||||
func (h *defaultPluginHandler) Execute(executablePath string, cmdArgs, environment []string) error {
|
||||
return syscall.Exec(executablePath, cmdArgs, environment)
|
||||
}
|
||||
|
||||
func handleEndpointExtensions(pluginHandler PluginHandler, cmdArgs []string) error {
|
||||
remainingArgs := []string{} // all "non-flag" arguments
|
||||
|
||||
for idx := range cmdArgs {
|
||||
if strings.HasPrefix(cmdArgs[idx], "-") {
|
||||
break
|
||||
}
|
||||
remainingArgs = append(remainingArgs, strings.Replace(cmdArgs[idx], "-", "_", -1))
|
||||
}
|
||||
|
||||
foundBinaryPath := ""
|
||||
|
||||
// attempt to find binary, starting at longest possible name with given cmdArgs
|
||||
for len(remainingArgs) > 0 {
|
||||
path, err := pluginHandler.Lookup(fmt.Sprintf("kubectl-%s", strings.Join(remainingArgs, "-")))
|
||||
if err != nil || len(path) == 0 {
|
||||
remainingArgs = remainingArgs[:len(remainingArgs)-1]
|
||||
continue
|
||||
}
|
||||
|
||||
foundBinaryPath = path
|
||||
break
|
||||
}
|
||||
|
||||
if len(foundBinaryPath) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// invoke cmd binary relaying the current environment and args given
|
||||
// remainingArgs will always have at least one element.
|
||||
// execve will make remainingArgs[0] the "binary name".
|
||||
if err := pluginHandler.Execute(foundBinaryPath, append([]string{foundBinaryPath}, cmdArgs[len(remainingArgs):]...), os.Environ()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewKubectlCommand creates the `kubectl` command and its nested children.
|
||||
|
Reference in New Issue
Block a user