Makes stopped command terminate normally (#7011)

This commit is contained in:
Philippe Martin
2023-08-02 14:44:04 +02:00
committed by GitHub
parent d41364e68e
commit 4361b9f39a
2 changed files with 18 additions and 11 deletions

View File

@@ -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)
@@ -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

View File

@@ -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()).
@@ -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()).
@@ -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()).
@@ -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)
}