mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
kubelet: respect dockershim exec timeout
Signed-off-by: Andrew Sy Kim <kim.andrewsy@gmail.com>
This commit is contained in:
parent
23839ba649
commit
1dc5fb0190
@ -21,6 +21,7 @@ package dockershim
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
dockertypes "github.com/docker/docker/api/types"
|
dockertypes "github.com/docker/docker/api/types"
|
||||||
@ -30,6 +31,7 @@ import (
|
|||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
|
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
|
||||||
|
utilexec "k8s.io/utils/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExecHandler knows how to execute a command in a running Docker container.
|
// ExecHandler knows how to execute a command in a running Docker container.
|
||||||
@ -110,29 +112,49 @@ func (*NativeExecHandler) ExecInContainer(client libdocker.Interface, container
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var execTimeout <-chan time.Time
|
||||||
|
if timeout > 0 {
|
||||||
|
execTimeout = time.After(timeout)
|
||||||
|
} else {
|
||||||
|
// skip exec timeout if provided timeout is 0
|
||||||
|
execTimeout = make(chan time.Time, 1)
|
||||||
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(2 * time.Second)
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
count := 0
|
count := 0
|
||||||
for {
|
for {
|
||||||
inspect, err2 := client.InspectExec(execObj.ID)
|
select {
|
||||||
if err2 != nil {
|
case <-execTimeout:
|
||||||
return err2
|
// If exec timed out, return utilexec.CodeExitError with an exit status as expected
|
||||||
}
|
// from prober for failed probes.
|
||||||
if !inspect.Running {
|
// TODO: utilexec should have a TimedoutError type and we should return it here once available.
|
||||||
if inspect.ExitCode != 0 {
|
return utilexec.CodeExitError{
|
||||||
err = &dockerExitError{inspect}
|
Err: fmt.Errorf("command %q timed out", strings.Join(cmd, " ")),
|
||||||
|
Code: 1, // exit code here doesn't really matter, as long as it's not 0
|
||||||
|
}
|
||||||
|
// need to use "default" here instead of <-ticker.C, otherwise we delay the initial InspectExec by 2 seconds.
|
||||||
|
default:
|
||||||
|
inspect, inspectErr := client.InspectExec(execObj.ID)
|
||||||
|
if inspectErr != nil {
|
||||||
|
return inspectErr
|
||||||
}
|
}
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
count++
|
if !inspect.Running {
|
||||||
if count == 5 {
|
if inspect.ExitCode != 0 {
|
||||||
klog.Errorf("Exec session %s in container %s terminated but process still running!", execObj.ID, container.ID)
|
return &dockerExitError{inspect}
|
||||||
break
|
}
|
||||||
}
|
|
||||||
|
|
||||||
<-ticker.C
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
count++
|
||||||
|
if count == 5 {
|
||||||
|
klog.Errorf("Exec session %s in container %s terminated but process still running!", execObj.ID, container.ID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
<-ticker.C
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user