From 56b8dabbd1df94ba43534fe5c862f54e09c78676 Mon Sep 17 00:00:00 2001 From: Dale Hamel Date: Mon, 16 Sep 2019 11:50:30 -0400 Subject: [PATCH] Require all tracejobs to have a deadline, allow overriding this --- pkg/cmd/run.go | 25 +++++++++++++++---------- pkg/tracejob/job.go | 2 ++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 7d8aa5ae..091cd7fe 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -25,6 +25,8 @@ var ( ImageNameTag = "quay.io/iovisor/kubectl-trace-bpftrace:latest" // InitImageNameTag represents the default init container image InitImageNameTag = "quay.io/iovisor/kubectl-trace-init:latest" + // By default do not allow traces to run for longer than one hour + DefaultDeadline = 3600 ) var ( @@ -73,6 +75,7 @@ type RunOptions struct { imageName string initImageName string fetchHeaders bool + deadline int64 resourceArg string attach bool @@ -91,6 +94,7 @@ func NewRunOptions(streams genericclioptions.IOStreams) *RunOptions { serviceAccount: "default", imageName: ImageNameTag, initImageName: InitImageNameTag, + deadline: int64(DefaultDeadline), } } @@ -127,6 +131,7 @@ func NewRunCommand(factory factory.Factory, streams genericclioptions.IOStreams) cmd.Flags().StringVar(&o.imageName, "imagename", o.imageName, "Custom image for the tracerunner") cmd.Flags().StringVar(&o.initImageName, "init-imagename", o.initImageName, "Custom image for the init container responsible to fetch and prepare linux headers") cmd.Flags().BoolVar(&o.fetchHeaders, "fetch-headers", o.fetchHeaders, "Whether to fetch linux headers or not") + cmd.Flags().Int64Var(&o.deadline, "deadline", o.deadline, "Maximum time to allow trace to run in seconds") return cmd } @@ -289,19 +294,19 @@ func (o *RunOptions) Run() error { } tj := tracejob.TraceJob{ - Name: fmt.Sprintf("%s%s", meta.ObjectNamePrefix, string(juid)), - Namespace: o.namespace, - ServiceAccount: o.serviceAccount, - ID: juid, - Hostname: o.nodeName, - Program: o.program, - PodUID: o.podUID, - ContainerName: o.container, - IsPod: o.isPod, - // todo(dalehamel) > following fields to be used for #48 + Name: fmt.Sprintf("%s%s", meta.ObjectNamePrefix, string(juid)), + Namespace: o.namespace, + ServiceAccount: o.serviceAccount, + ID: juid, + Hostname: o.nodeName, + Program: o.program, + PodUID: o.podUID, + ContainerName: o.container, + IsPod: o.isPod, ImageNameTag: o.imageName, InitImageNameTag: o.initImageName, FetchHeaders: o.fetchHeaders, + Deadline: o.deadline, } job, err := tc.CreateJob(tj) diff --git a/pkg/tracejob/job.go b/pkg/tracejob/job.go index 31ed24aa..a3ded20d 100644 --- a/pkg/tracejob/job.go +++ b/pkg/tracejob/job.go @@ -35,6 +35,7 @@ type TraceJob struct { ImageNameTag string InitImageNameTag string FetchHeaders bool + Deadline int64 StartTime metav1.Time Status TraceJobStatus } @@ -217,6 +218,7 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) { job := &batchv1.Job{ ObjectMeta: commonMeta, Spec: batchv1.JobSpec{ + ActiveDeadlineSeconds: int64Ptr(nj.Deadline), TTLSecondsAfterFinished: int32Ptr(5), Parallelism: int32Ptr(1), Completions: int32Ptr(1),