mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-16 23:29:21 +00:00
Add exec module to client-go/util only with required code
client-go/pkt/util was removed in favor of client-go util, which consists only from CodeExitError and ExitError interface
This commit is contained in:
parent
2612e0c78a
commit
8574bc564f
22
staging/src/k8s.io/client-go/Godeps/Godeps.json
generated
22
staging/src/k8s.io/client-go/Godeps/Godeps.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/client-go",
|
"ImportPath": "k8s.io/client-go",
|
||||||
"GoVersion": "go1.8",
|
"GoVersion": "go1.7",
|
||||||
"GodepVersion": "v79",
|
"GodepVersion": "v79",
|
||||||
"Packages": [
|
"Packages": [
|
||||||
"./..."
|
"./..."
|
||||||
@ -66,6 +66,14 @@
|
|||||||
"ImportPath": "github.com/docker/distribution/reference",
|
"ImportPath": "github.com/docker/distribution/reference",
|
||||||
"Rev": "cd27f179f2c10c5d300e6d09025b538c475b0d51"
|
"Rev": "cd27f179f2c10c5d300e6d09025b538c475b0d51"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/docker/spdystream",
|
||||||
|
"Rev": "449fdfce4d962303d702fec724ef0ad181c92528"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/docker/spdystream/spdy",
|
||||||
|
"Rev": "449fdfce4d962303d702fec724ef0ad181c92528"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/emicklei/go-restful",
|
"ImportPath": "github.com/emicklei/go-restful",
|
||||||
"Rev": "ff4f55a206334ef123e4f79bbf348980da81ca46"
|
"Rev": "ff4f55a206334ef123e4f79bbf348980da81ca46"
|
||||||
@ -390,6 +398,10 @@
|
|||||||
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream",
|
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream/spdy",
|
||||||
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/apimachinery/pkg/util/intstr",
|
"ImportPath": "k8s.io/apimachinery/pkg/util/intstr",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
@ -410,6 +422,10 @@
|
|||||||
"ImportPath": "k8s.io/apimachinery/pkg/util/rand",
|
"ImportPath": "k8s.io/apimachinery/pkg/util/rand",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/pkg/util/remotecommand",
|
||||||
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/apimachinery/pkg/util/runtime",
|
"ImportPath": "k8s.io/apimachinery/pkg/util/runtime",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
@ -450,6 +466,10 @@
|
|||||||
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/json",
|
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/json",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/netutil",
|
||||||
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/reflect",
|
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/reflect",
|
||||||
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
@ -1,188 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2014 The Kubernetes Authors.
|
|
||||||
|
|
||||||
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 exec
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
osexec "os/exec"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ErrExecutableNotFound is returned if the executable is not found.
|
|
||||||
var ErrExecutableNotFound = osexec.ErrNotFound
|
|
||||||
|
|
||||||
// Interface is an interface that presents a subset of the os/exec API. Use this
|
|
||||||
// when you want to inject fakeable/mockable exec behavior.
|
|
||||||
type Interface interface {
|
|
||||||
// Command returns a Cmd instance which can be used to run a single command.
|
|
||||||
// This follows the pattern of package os/exec.
|
|
||||||
Command(cmd string, args ...string) Cmd
|
|
||||||
|
|
||||||
// LookPath wraps os/exec.LookPath
|
|
||||||
LookPath(file string) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cmd is an interface that presents an API that is very similar to Cmd from os/exec.
|
|
||||||
// As more functionality is needed, this can grow. Since Cmd is a struct, we will have
|
|
||||||
// to replace fields with get/set method pairs.
|
|
||||||
type Cmd interface {
|
|
||||||
// CombinedOutput runs the command and returns its combined standard output
|
|
||||||
// and standard error. This follows the pattern of package os/exec.
|
|
||||||
CombinedOutput() ([]byte, error)
|
|
||||||
// Output runs the command and returns standard output, but not standard err
|
|
||||||
Output() ([]byte, error)
|
|
||||||
SetDir(dir string)
|
|
||||||
SetStdin(in io.Reader)
|
|
||||||
SetStdout(out io.Writer)
|
|
||||||
// Stops the command by sending SIGTERM. It is not guaranteed the
|
|
||||||
// process will stop before this function returns. If the process is not
|
|
||||||
// responding, an internal timer function will send a SIGKILL to force
|
|
||||||
// terminate after 10 seconds.
|
|
||||||
Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExitError is an interface that presents an API similar to os.ProcessState, which is
|
|
||||||
// what ExitError from os/exec is. This is designed to make testing a bit easier and
|
|
||||||
// probably loses some of the cross-platform properties of the underlying library.
|
|
||||||
type ExitError interface {
|
|
||||||
String() string
|
|
||||||
Error() string
|
|
||||||
Exited() bool
|
|
||||||
ExitStatus() int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implements Interface in terms of really exec()ing.
|
|
||||||
type executor struct{}
|
|
||||||
|
|
||||||
// New returns a new Interface which will os/exec to run commands.
|
|
||||||
func New() Interface {
|
|
||||||
return &executor{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command is part of the Interface interface.
|
|
||||||
func (executor *executor) Command(cmd string, args ...string) Cmd {
|
|
||||||
return (*cmdWrapper)(osexec.Command(cmd, args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// LookPath is part of the Interface interface
|
|
||||||
func (executor *executor) LookPath(file string) (string, error) {
|
|
||||||
return osexec.LookPath(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wraps exec.Cmd so we can capture errors.
|
|
||||||
type cmdWrapper osexec.Cmd
|
|
||||||
|
|
||||||
func (cmd *cmdWrapper) SetDir(dir string) {
|
|
||||||
cmd.Dir = dir
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmd *cmdWrapper) SetStdin(in io.Reader) {
|
|
||||||
cmd.Stdin = in
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmd *cmdWrapper) SetStdout(out io.Writer) {
|
|
||||||
cmd.Stdout = out
|
|
||||||
}
|
|
||||||
|
|
||||||
// CombinedOutput is part of the Cmd interface.
|
|
||||||
func (cmd *cmdWrapper) CombinedOutput() ([]byte, error) {
|
|
||||||
out, err := (*osexec.Cmd)(cmd).CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return out, handleError(err)
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmd *cmdWrapper) Output() ([]byte, error) {
|
|
||||||
out, err := (*osexec.Cmd)(cmd).Output()
|
|
||||||
if err != nil {
|
|
||||||
return out, handleError(err)
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop is part of the Cmd interface.
|
|
||||||
func (cmd *cmdWrapper) Stop() {
|
|
||||||
c := (*osexec.Cmd)(cmd)
|
|
||||||
if c.ProcessState.Exited() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.Process.Signal(syscall.SIGTERM)
|
|
||||||
time.AfterFunc(10*time.Second, func() {
|
|
||||||
if c.ProcessState.Exited() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.Process.Signal(syscall.SIGKILL)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleError(err error) error {
|
|
||||||
if ee, ok := err.(*osexec.ExitError); ok {
|
|
||||||
// Force a compile fail if exitErrorWrapper can't convert to ExitError.
|
|
||||||
var x ExitError = &ExitErrorWrapper{ee}
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
if ee, ok := err.(*osexec.Error); ok {
|
|
||||||
if ee.Err == osexec.ErrNotFound {
|
|
||||||
return ErrExecutableNotFound
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExitErrorWrapper is an implementation of ExitError in terms of os/exec ExitError.
|
|
||||||
// Note: standard exec.ExitError is type *os.ProcessState, which already implements Exited().
|
|
||||||
type ExitErrorWrapper struct {
|
|
||||||
*osexec.ExitError
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ ExitError = ExitErrorWrapper{}
|
|
||||||
|
|
||||||
// ExitStatus is part of the ExitError interface.
|
|
||||||
func (eew ExitErrorWrapper) ExitStatus() int {
|
|
||||||
ws, ok := eew.Sys().(syscall.WaitStatus)
|
|
||||||
if !ok {
|
|
||||||
panic("can't call ExitStatus() on a non-WaitStatus exitErrorWrapper")
|
|
||||||
}
|
|
||||||
return ws.ExitStatus()
|
|
||||||
}
|
|
||||||
|
|
||||||
// CodeExitError is an implementation of ExitError consisting of an error object
|
|
||||||
// and an exit code (the upper bits of os.exec.ExitStatus).
|
|
||||||
type CodeExitError struct {
|
|
||||||
Err error
|
|
||||||
Code int
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ ExitError = CodeExitError{}
|
|
||||||
|
|
||||||
func (e CodeExitError) Error() string {
|
|
||||||
return e.Err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e CodeExitError) String() string {
|
|
||||||
return e.Err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e CodeExitError) Exited() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e CodeExitError) ExitStatus() int {
|
|
||||||
return e.Code
|
|
||||||
}
|
|
@ -44,8 +44,8 @@ go_library(
|
|||||||
"//vendor/k8s.io/apimachinery/pkg/util/remotecommand:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/remotecommand:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/pkg/api:go_default_library",
|
"//vendor/k8s.io/client-go/pkg/api:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/pkg/util/exec:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/rest:go_default_library",
|
"//vendor/k8s.io/client-go/rest:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/transport:go_default_library",
|
"//vendor/k8s.io/client-go/transport:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/util/exec:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/remotecommand"
|
"k8s.io/apimachinery/pkg/util/remotecommand"
|
||||||
"k8s.io/client-go/pkg/util/exec"
|
"k8s.io/client-go/util/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// streamProtocolV4 implements version 4 of the streaming protocol for attach
|
// streamProtocolV4 implements version 4 of the streaming protocol for attach
|
||||||
|
52
staging/src/k8s.io/client-go/util/exec/exec.go
Normal file
52
staging/src/k8s.io/client-go/util/exec/exec.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 The Kubernetes Authors.
|
||||||
|
|
||||||
|
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 exec
|
||||||
|
|
||||||
|
// ExitError is an interface that presents an API similar to os.ProcessState, which is
|
||||||
|
// what ExitError from os/exec is. This is designed to make testing a bit easier and
|
||||||
|
// probably loses some of the cross-platform properties of the underlying library.
|
||||||
|
type ExitError interface {
|
||||||
|
String() string
|
||||||
|
Error() string
|
||||||
|
Exited() bool
|
||||||
|
ExitStatus() int
|
||||||
|
}
|
||||||
|
|
||||||
|
// CodeExitError is an implementation of ExitError consisting of an error object
|
||||||
|
// and an exit code (the upper bits of os.exec.ExitStatus).
|
||||||
|
type CodeExitError struct {
|
||||||
|
Err error
|
||||||
|
Code int
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ ExitError = CodeExitError{}
|
||||||
|
|
||||||
|
func (e CodeExitError) Error() string {
|
||||||
|
return e.Err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e CodeExitError) String() string {
|
||||||
|
return e.Err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e CodeExitError) Exited() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e CodeExitError) ExitStatus() int {
|
||||||
|
return e.Code
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user