Skip to content

Commit

Permalink
Makes stopped command terminate normally (#7011)
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy authored Aug 2, 2023
1 parent d41364e commit 4361b9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
13 changes: 6 additions & 7 deletions pkg/remotecmd/kubeexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,6 @@ func (k *kubeExecProcessHandler) StartProcessForCommand(ctx context.Context, def
// Then killing those children will exit the parent 'sh' process.
func (k *kubeExecProcessHandler) StopProcessForCommand(ctx context.Context, def CommandDefinition, podName string, containerName string) error {
klog.V(4).Infof("StopProcessForCommand for %q", def.Id)
defer func() {
pidFile := getPidFileForCommand(def)
_, _, err := k.execClient.ExecuteCommand(ctx, []string{ShellExecutable, "-c", fmt.Sprintf("rm -f %s", pidFile)}, podName, containerName, false, nil, nil)
if err != nil {
klog.V(2).Infof("Could not remove file %q: %v", pidFile, err)
}
}()

kill := func(p int) error {
_, _, err := k.execClient.ExecuteCommand(ctx, []string{ShellExecutable, "-c", fmt.Sprintf("kill %d || true", p)}, podName, containerName, false, nil, nil)
Expand Down Expand Up @@ -201,6 +194,12 @@ func (k *kubeExecProcessHandler) StopProcessForCommand(ctx context.Context, def

klog.V(3).Infof("Found %d children (either direct and indirect) for parent process %d: %v", len(children), ppid, children)

pidFile := getPidFileForCommand(def)
_, _, err = k.execClient.ExecuteCommand(ctx, []string{ShellExecutable, "-c", fmt.Sprintf("rm -f %s", pidFile)}, podName, containerName, false, nil, nil)
if err != nil {
klog.V(2).Infof("Could not remove file %q: %v", pidFile, err)
}

for _, child := range children {
if err = kill(child); err != nil {
return err
Expand Down
16 changes: 12 additions & 4 deletions pkg/remotecmd/kubeexec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ func Test_kubeExecProcessHandler_StopProcessForCommand(t *testing.T) {
{
name: "no process children killed if no children file found",
kubeClientCustomizer: func(kclient *kclient.MockClientInterface) {
kclient.EXPECT().ExecCMDInContainer(gomock.Any(), gomock.Eq(_containerName), gomock.Eq(_podName),
gomock.Eq([]string{ShellExecutable, "-c", fmt.Sprintf("rm -f %s", getPidFileForCommand(cmdDef))}),
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(errors.New("an error which should be ignored"))
kclient.EXPECT().ExecCMDInContainer(gomock.Any(), gomock.Eq(_containerName), gomock.Eq(_podName),
gomock.Eq([]string{ShellExecutable, "-c", fmt.Sprintf("cat %s || true", getPidFileForCommand(cmdDef))}),
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Expand Down Expand Up @@ -396,6 +400,10 @@ func Test_kubeExecProcessHandler_StopProcessForCommand(t *testing.T) {
{
name: "process children should get killed",
kubeClientCustomizer: func(kclient *kclient.MockClientInterface) {
kclient.EXPECT().ExecCMDInContainer(gomock.Any(), gomock.Eq(_containerName), gomock.Eq(_podName),
gomock.Eq([]string{ShellExecutable, "-c", fmt.Sprintf("rm -f %s", getPidFileForCommand(cmdDef))}),
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(errors.New("an error which should be ignored"))
kclient.EXPECT().ExecCMDInContainer(gomock.Any(), gomock.Eq(_containerName), gomock.Eq(_podName),
gomock.Eq([]string{ShellExecutable, "-c", fmt.Sprintf("cat %s || true", getPidFileForCommand(cmdDef))}),
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Expand Down Expand Up @@ -426,6 +434,10 @@ func Test_kubeExecProcessHandler_StopProcessForCommand(t *testing.T) {
{
name: "error if any child process could not be killed",
kubeClientCustomizer: func(kclient *kclient.MockClientInterface) {
kclient.EXPECT().ExecCMDInContainer(gomock.Any(), gomock.Eq(_containerName), gomock.Eq(_podName),
gomock.Eq([]string{ShellExecutable, "-c", fmt.Sprintf("rm -f %s", getPidFileForCommand(cmdDef))}),
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(errors.New("an error which should be ignored"))
kclient.EXPECT().ExecCMDInContainer(gomock.Any(), gomock.Eq(_containerName), gomock.Eq(_podName),
gomock.Eq([]string{ShellExecutable, "-c", fmt.Sprintf("cat %s || true", getPidFileForCommand(cmdDef))}),
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Expand Down Expand Up @@ -460,10 +472,6 @@ func Test_kubeExecProcessHandler_StopProcessForCommand(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
kubeClient := kclient.NewMockClientInterface(ctrl)
kubeClient.EXPECT().ExecCMDInContainer(gomock.Any(), gomock.Eq(_containerName), gomock.Eq(_podName),
gomock.Eq([]string{ShellExecutable, "-c", fmt.Sprintf("rm -f %s", getPidFileForCommand(cmdDef))}),
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(errors.New("an error which should be ignored"))
if tt.kubeClientCustomizer != nil {
tt.kubeClientCustomizer(kubeClient)
}
Expand Down

0 comments on commit 4361b9f

Please sign in to comment.