Skip to content

Commit f2219f5

Browse files
authored
commands/.../up/local.go: handle sigint and sigterm (#375)
* commands/.../up/local.go: handle sigint and sigterm This commit fixes issue #366 and kills the `go run` command spawned by `up local` when a `SIGINT` or `SIGTERM` signal is sent. * commands/.../up/local.go: use cmdError instead of os.Exit(1)
1 parent a72f861 commit f2219f5

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

commands/operator-sdk/cmd/up/local.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"os/signal"
78
"os/user"
89
"path/filepath"
910
"strings"
11+
"syscall"
1012

1113
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
1214
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
@@ -78,6 +80,16 @@ func upLocal(projectName string) {
7880
args = append(args, extraArgs...)
7981
}
8082
dc := exec.Command(gocmd, args...)
83+
c := make(chan os.Signal)
84+
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
85+
go func() {
86+
<-c
87+
err := dc.Process.Kill()
88+
if err != nil {
89+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to terminate the operator: %v", err))
90+
}
91+
os.Exit(0)
92+
}()
8193
dc.Stdout = os.Stdout
8294
dc.Stderr = os.Stderr
8395
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig), fmt.Sprintf("%v=%v", k8sutil.WatchNamespaceEnvVar, namespace))

0 commit comments

Comments
 (0)