Skip to content

Commit

Permalink
capture kube task interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Oct 12, 2023
1 parent fb87708 commit 8ae59fd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ Please, feel free to contribute to the companion [repository](https://github.com

<!--

* task kube interrupt
* task/box kube shareDir vs copy dir
* replace task/htb example with thm
* verify/support kube config relative path
Expand Down
14 changes: 13 additions & 1 deletion pkg/client/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"context"
"fmt"
"github.com/hckops/hckctl/pkg/util"
"io"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"

"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -35,6 +36,7 @@ import (
"k8s.io/kubectl/pkg/scheme"

"github.com/hckops/hckctl/pkg/client/common"
"github.com/hckops/hckctl/pkg/util"
)

func NewKubeClient(inCluster bool, configPath string) (*KubeClient, error) {
Expand Down Expand Up @@ -534,6 +536,16 @@ func (client *KubeClient) JobCreate(opts *JobCreateOpts) error {
return errors.Wrapf(err, "error job create: namespace=%s name=%s", opts.Namespace, opts.Spec.Name)
}

if opts.CaptureInterrupt {
// captures CTRL+C
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChan
opts.OnContainerInterruptCallback(job.Name)
}()
}

// blocks until the job is ready, then stop watching
listOptions, err := buildJobLabelSelector(job)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/client/kubernetes/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ type JobOpts struct {
}

type JobCreateOpts struct {
Namespace string
Spec *batchv1.Job
OnStatusEventCallback func(event string)
Namespace string
Spec *batchv1.Job
CaptureInterrupt bool
OnContainerInterruptCallback func(name string)
OnStatusEventCallback func(event string)
}
1 change: 1 addition & 0 deletions pkg/task/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (task *DockerTaskClient) runTask(opts *taskModel.RunOptions) error {
OnContainerInterruptCallback: func(containerId string) {
// returns control to runTask, it will correctly invoke defer to remove the sidecar
// unless it's interrupted while the sidecar is being created
task.eventBus.Publish(newContainerRemoveDockerEvent(containerId))
task.client.ContainerRemove(containerId)
},
OnContainerCreateCallback: func(string) error { return nil },
Expand Down
13 changes: 10 additions & 3 deletions pkg/task/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ func (task *KubeTaskClient) runTask(opts *taskModel.RunOptions) error {
}

jobOpts := &kubernetes.JobCreateOpts{
Namespace: namespace,
Spec: jobSpec,
Namespace: namespace,
Spec: jobSpec,
CaptureInterrupt: true,
OnContainerInterruptCallback: func(name string) {
// ignore error when interrupted: it will attempt to delete the job twice
task.eventBus.Publish(newJobDeleteKubeEvent(namespace, jobName))
task.client.JobDelete(namespace, name)
},
OnStatusEventCallback: func(event string) {
task.eventBus.Publish(newJobCreateStatusKubeEvent(event))
},
Expand All @@ -96,13 +102,14 @@ func (task *KubeTaskClient) runTask(opts *taskModel.RunOptions) error {
// stop loader
task.eventBus.Publish(newContainerWaitKubeLoaderEvent())

logFileName := opts.GenerateLogFileName(taskModel.Kubernetes, podInfo.ContainerName)
logFileName := opts.GenerateLogFileName(taskModel.Kubernetes, jobName)
logOpts := &kubernetes.PodLogsOpts{
Namespace: namespace,
PodName: podInfo.PodName,
PodId: podInfo.ContainerName,
}
task.eventBus.Publish(newPodLogKubeEvent(logFileName))
// blocks and tail logs
if err := task.client.PodLogsTee(logOpts, logFileName); err != nil {
return err
}
Expand Down

0 comments on commit 8ae59fd

Please sign in to comment.