Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes stopped command terminate normally #7011

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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