Skip to content

Commit 1aa8d54

Browse files
authored
fix(kube-run): handle SIGINT/SIGTERM properly (#6888)
Signed-off-by: Alexandr Zaytsev <alexandr.zaytsev@flant.com>
1 parent 600b7a5 commit 1aa8d54

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

cmd/werf/kube_run/kube_run.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,6 @@ func runMain(ctx context.Context) error {
297297
return err
298298
}
299299

300-
cleanupResources(ctx, pod, secret, namespace)
301-
302300
return nil
303301
})
304302
} else {
@@ -680,6 +678,8 @@ func isPodReady(ctx context.Context, namespace, pod string, extraArgs []string)
680678
}
681679

682680
func copyFromPod(ctx context.Context, namespace, pod, container string, copyFrom copyFromTo, extraArgs []string) {
681+
ctx = context.WithoutCancel(ctx)
682+
683683
logboek.Context(ctx).LogF("Copying %q from pod to %q ...\n", copyFrom.Src, copyFrom.Dst)
684684

685685
args := []string{
@@ -730,6 +730,8 @@ func copyToPod(ctx context.Context, namespace, pod, container string, copyFrom c
730730
}
731731

732732
func stopContainer(ctx context.Context, namespace, pod, container string, extraArgs []string) {
733+
ctx = context.WithoutCancel(ctx)
734+
733735
logboek.Context(ctx).LogF("Stopping container %q in pod ...\n", container)
734736

735737
args := []string{
@@ -775,6 +777,7 @@ func execCommandInPod(ctx context.Context, namespace, pod, container string, com
775777
args = append(args, command...)
776778

777779
cmd := werfExec.PrepareGracefulCancellation(util.ExecKubectlCmdContext(ctx, args...))
780+
cmd.Stdin = os.Stdin
778781

779782
if *commonCmdData.DryRun {
780783
fmt.Println(cmd.String())
@@ -792,6 +795,8 @@ func execCommandInPod(ctx context.Context, namespace, pod, container string, com
792795
}
793796

794797
func cleanupResources(ctx context.Context, pod, secret, namespace string) {
798+
ctx = context.WithoutCancel(ctx)
799+
795800
if !cmdData.Rm || *commonCmdData.DryRun {
796801
return
797802
}

cmd/werf/kubectl/kubectl.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
3333
ConfigFlags: configFlags,
3434
IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
3535
})
36+
kubectlCmd.SetContext(ctx)
3637

3738
logs.AddFlags(kubectlCmd.PersistentFlags())
3839

@@ -59,18 +60,16 @@ func NewCmd(ctx context.Context) *cobra.Command {
5960
}
6061
}
6162

62-
wrapPreRun(kubectlCmd)
63+
wrapPreRun(ctx, kubectlCmd)
6364

6465
return kubectlCmd
6566
}
6667

67-
func wrapPreRun(kubectlCmd *cobra.Command) {
68+
func wrapPreRun(ctx context.Context, kubectlCmd *cobra.Command) {
6869
switch {
6970
case kubectlCmd.PersistentPreRunE != nil:
7071
oldFunc := kubectlCmd.PersistentPreRunE
7172
kubectlCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
72-
ctx := cmd.Context()
73-
7473
if err := prePreRun(ctx); err != nil {
7574
return err
7675
}
@@ -79,17 +78,13 @@ func wrapPreRun(kubectlCmd *cobra.Command) {
7978
case kubectlCmd.PersistentPreRun != nil:
8079
oldFunc := kubectlCmd.PersistentPreRun
8180
kubectlCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
82-
ctx := cmd.Context()
83-
8481
if err := prePreRun(ctx); err != nil {
8582
util.CheckErr(err)
8683
}
8784
oldFunc(cmd, args)
8885
}
8986
default:
9087
kubectlCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
91-
ctx := cmd.Context()
92-
9388
if err := prePreRun(ctx); err != nil {
9489
return err
9590
}

cmd/werf/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func main() {
6363
return
6464
}
6565

66-
if err := rootCmd.Execute(); err != nil {
66+
if err := rootCmd.ExecuteContext(ctx); err != nil {
6767
if helm_v3.IsPluginError(err) {
6868
common.ShutdownTelemetry(ctx, helm_v3.PluginErrorCode(err))
6969
graceful.Terminate(ctx, err, helm_v3.PluginErrorCode(err))

0 commit comments

Comments
 (0)