Skip to content

Commit e29e247

Browse files
leodidofntlnz
andcommitted
feat(cmd): support custom images for tracerunner and init container
Signed-off-by: Leonardo Di Donato <[email protected]> Co-Authored-By: Lorenzo Fontana <[email protected]>
1 parent 782c432 commit e29e247

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

pkg/cmd/get.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (o *GetOptions) Validate(cmd *cobra.Command, args []string) error {
111111
return nil
112112
}
113113

114+
// Complete completes the setup of the command.
114115
func (o *GetOptions) Complete(factory factory.Factory, cmd *cobra.Command, args []string) error {
115116
// Prepare namespace
116117
var err error
@@ -125,7 +126,7 @@ func (o *GetOptions) Complete(factory factory.Factory, cmd *cobra.Command, args
125126
o.namespace = ""
126127
}
127128

128-
//// Prepare client
129+
// Prepare client
129130
o.clientConfig, err = factory.ToRESTConfig()
130131
if err != nil {
131132
return err

pkg/cmd/run.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ import (
2020
"k8s.io/client-go/rest"
2121
)
2222

23+
const (
24+
imageNameTag = "quay.io/fntlnz/kubectl-trace-bpftrace:latest"
25+
initImageNameTag = "quay.io/dalehamel/kubectl-trace-init"
26+
)
27+
2328
var (
2429
runShort = `Execute a bpftrace program on resources` // Wrap with i18n.T()
2530

@@ -35,7 +40,10 @@ var (
3540
# Run an bpftrace inline program on a pod container
3641
%[1]s trace run pod/nginx -c nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); }"
3742
%[1]s trace run pod/nginx nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); }"
38-
%[1]s trace run pod/nginx nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); }"`
43+
%[1]s trace run pod/nginx nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); }"
44+
45+
# Run a bpftrace inline program on a pod container with a custom image for the init container responsible to fetch linux headers
46+
%[1]s trace run pod/nginx nginx -e "tracepoint:syscalls:sys_enter_* { @[probe] = count(); } --init-imagename=quay.io/custom-init-image-name --fetch-headers"`
3947

4048
runCommand = "run"
4149
usageString = "(POD | TYPE/NAME)"
@@ -58,6 +66,9 @@ type RunOptions struct {
5866
eval string
5967
program string
6068
serviceAccount string
69+
imageName string
70+
initImageName string
71+
fetchHeaders bool
6172

6273
resourceArg string
6374
attach bool
@@ -72,6 +83,10 @@ type RunOptions struct {
7283
func NewRunOptions(streams genericclioptions.IOStreams) *RunOptions {
7384
return &RunOptions{
7485
IOStreams: streams,
86+
87+
serviceAccount: "default",
88+
imageName: imageNameTag,
89+
initImageName: initImageNameTag,
7590
}
7691
}
7792

@@ -102,9 +117,12 @@ func NewRunCommand(factory factory.Factory, streams genericclioptions.IOStreams)
102117

103118
cmd.Flags().StringVarP(&o.container, "container", "c", o.container, "Specify the container")
104119
cmd.Flags().BoolVarP(&o.attach, "attach", "a", o.attach, "Wheter or not to attach to the trace program once it is created")
105-
cmd.Flags().StringVarP(&o.eval, "eval", "e", "", "Literal string to be evaluated as a bpftrace program")
106-
cmd.Flags().StringVarP(&o.program, "filename", "f", "", "File containing a bpftrace program")
107-
cmd.Flags().StringVar(&o.serviceAccount, "serviceaccount", "default", "Service account to use to set in the pod spec of the kubectl-trace job")
120+
cmd.Flags().StringVarP(&o.eval, "eval", "e", o.eval, "Literal string to be evaluated as a bpftrace program")
121+
cmd.Flags().StringVarP(&o.program, "filename", "f", o.program, "File containing a bpftrace program")
122+
cmd.Flags().StringVar(&o.serviceAccount, "serviceaccount", o.serviceAccount, "Service account to use to set in the pod spec of the kubectl-trace job")
123+
cmd.Flags().StringVar(&o.imageName, "imagename", o.imageName, "Custom image for the tracerunner")
124+
cmd.Flags().StringVar(&o.initImageName, "init-imagename", o.initImageName, "Custom image for the init container responsible to fetch and prepare linux headers")
125+
cmd.Flags().BoolVar(&o.fetchHeaders, "fetch-headers", o.fetchHeaders, "Wheter to fetch linux headers or not")
108126

109127
return cmd
110128
}
@@ -276,6 +294,10 @@ func (o *RunOptions) Run() error {
276294
PodUID: o.podUID,
277295
ContainerName: o.container,
278296
IsPod: o.isPod,
297+
// todo(dalehamel) > following fields to be used for #48
298+
ImageNameTag: o.imageName,
299+
InitImageNameTag: o.initImageName,
300+
FetchHeaders: o.fetchHeaders,
279301
}
280302

281303
job, err := tc.CreateJob(tj)

pkg/tracejob/job.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ package tracejob
22

33
import (
44
"fmt"
5-
6-
"github.com/iovisor/kubectl-trace/pkg/version"
7-
85
"io"
96
"io/ioutil"
107

118
"github.com/iovisor/kubectl-trace/pkg/meta"
129
batchv1 "k8s.io/api/batch/v1"
1310
apiv1 "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/api/resource"
1412
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1513
"k8s.io/apimachinery/pkg/types"
16-
"k8s.io/apimachinery/pkg/api/resource"
1714
batchv1typed "k8s.io/client-go/kubernetes/typed/batch/v1"
1815
corev1typed "k8s.io/client-go/kubernetes/typed/core/v1"
1916
)
@@ -24,16 +21,20 @@ type TraceJobClient struct {
2421
outStream io.Writer
2522
}
2623

24+
// TraceJob is a container of info needed to create the job responsible for tracing.
2725
type TraceJob struct {
28-
Name string
29-
ID types.UID
30-
Namespace string
31-
ServiceAccount string
32-
Hostname string
33-
Program string
34-
PodUID string
35-
ContainerName string
36-
IsPod bool
26+
Name string
27+
ID types.UID
28+
Namespace string
29+
ServiceAccount string
30+
Hostname string
31+
Program string
32+
PodUID string
33+
ContainerName string
34+
IsPod bool
35+
ImageNameTag string
36+
InitImageNameTag string
37+
FetchHeaders bool
3738
}
3839

3940
// WithOutStream setup a file stream to output trace job operation information
@@ -252,19 +253,19 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {
252253
Containers: []apiv1.Container{
253254
apiv1.Container{
254255
Name: nj.Name,
255-
Image: version.ImageNameTag(),
256+
Image: nj.ImageNameTag,
256257
Command: bpfTraceCmd,
257258
TTY: true,
258259
Stdin: true,
259260
Resources: apiv1.ResourceRequirements{
260-
Requests: apiv1.ResourceList{
261-
apiv1.ResourceCPU: resource.MustParse("100m"),
262-
apiv1.ResourceMemory: resource.MustParse("100Mi"),
263-
},
264-
Limits: apiv1.ResourceList{
265-
apiv1.ResourceCPU: resource.MustParse("1"),
266-
apiv1.ResourceMemory: resource.MustParse("1G"),
267-
},
261+
Requests: apiv1.ResourceList{
262+
apiv1.ResourceCPU: resource.MustParse("100m"),
263+
apiv1.ResourceMemory: resource.MustParse("100Mi"),
264+
},
265+
Limits: apiv1.ResourceList{
266+
apiv1.ResourceCPU: resource.MustParse("1"),
267+
apiv1.ResourceMemory: resource.MustParse("1G"),
268+
},
268269
},
269270
VolumeMounts: []apiv1.VolumeMount{
270271
apiv1.VolumeMount{

0 commit comments

Comments
 (0)