Skip to content

Commit bb56e8b

Browse files
committed
feat(tracejob): support running trace runner instead of bpftrace directly
Signed-off-by: Lorenzo Fontana <[email protected]>
1 parent ca4c379 commit bb56e8b

File tree

10 files changed

+345
-27
lines changed

10 files changed

+345
-27
lines changed

Dockerfile.bpftrace

+14-3
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@ RUN git clone https://github.com/iovisor/bpftrace.git /bpftrace
2626

2727
WORKDIR /bpftrace
2828

29-
RUN git checkout 8f7f8214d7dd7bc25b7740a3c0e9a580a89e0244
29+
RUN git checkout 2ae2a53f62622631a304def6c193680e603994e3
3030

3131
WORKDIR /bpftrace/docker
3232

3333
RUN chmod +x build.sh
3434
RUN ./build.sh /bpftrace/build-release Release
3535

36-
RUN ls -la
36+
FROM golang:1.11.4-alpine3.8 as gobuilder
37+
38+
RUN apk update
39+
RUN apk add make
40+
41+
ADD . /go/src/github.com/fntlnz/kubectl-trace
42+
WORKDIR /go/src/github.com/fntlnz/kubectl-trace
43+
44+
RUN make _output/bin/trace-runner
45+
3746
FROM alpine:3.8
3847

48+
RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
3949
COPY --from=builder /bpftrace/build-release/src/bpftrace /bin/bpftrace
50+
COPY --from=gobuilder /go/src/github.com/fntlnz/kubectl-trace/_output/bin/trace-runner /bin/trace-runner
4051

41-
ENTRYPOINT ["/bin/bpftrace"]
52+
ENTRYPOINT ["/bin/trace-runner"]

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ IMAGE_BPFTRACE_COMMIT := quay.io/fntlnz/kubectl-trace-bpftrace:$(GIT_COMMIT)
1414
IMAGE_BUILD_FLAGS ?= "--no-cache"
1515

1616
kubectl_trace ?= _output/bin/kubectl-trace
17+
trace_runner ?= _output/bin/trace-runner
1718

1819
.PHONY: build
19-
build: clean ${kubectl_trace}
20+
build: clean ${kubectl_trace} ${trace_runner}
2021

2122
${kubectl_trace}:
22-
$(GO) build -o $@ ./cmd/kubectl-trace
23+
CGO_ENABLED=0 $(GO) build -o $@ ./cmd/kubectl-trace
24+
25+
${trace_runner}:
26+
CGO_ENABLED=1 $(GO) build -o $@ ./cmd/trace-runner
2327

2428
.PHONY: clean
2529
clean:

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/fntlnz/kubectl-trace
33
require (
44
cloud.google.com/go v0.34.0 // indirect
55
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
6-
github.com/davecgh/go-spew v1.1.1
76
github.com/docker/distribution v2.6.2+incompatible // indirect
87
github.com/docker/docker v0.7.3-0.20181124105010-0b7cb16dde4a // indirect
98
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 // indirect
@@ -40,7 +39,7 @@ require (
4039
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a // indirect
4140
golang.org/x/oauth2 v0.0.0-20181120190819-8f65e3013eba // indirect
4241
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
43-
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
42+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b
4443
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
4544
google.golang.org/appengine v1.3.0 // indirect
4645
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect

pkg/cmd/run.go

+30-13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type RunOptions struct {
5959
program string
6060
resourceArg string
6161
attach bool
62+
isPod bool
63+
podUID string
6264

6365
nodeName string
6466

@@ -172,16 +174,28 @@ func (o *RunOptions) Complete(factory factory.Factory, cmd *cobra.Command, args
172174
}
173175

174176
// Check we got a pod or a node
175-
// isPod := false
177+
o.isPod = false
176178
switch v := obj.(type) {
177179
case *v1.Pod:
178-
// isPod = true
179-
// if len(o.container) == 0 {
180-
// todo > get the default container or the first one, see https://github.com/fntlnz/kubectl-trace/pull/1#issuecomment-441331255
181-
// } else {
182-
// todo > check the pod has the provided container (o.container)
183-
// }
184-
return fmt.Errorf("running bpftrace programs against pods is not supported yet, see: https://github.com/fntlnz/kubectl-trace/issues/3")
180+
o.isPod = true
181+
found := false
182+
for _, c := range v.Spec.Containers {
183+
// default if no container provided
184+
if len(o.container) == 0 {
185+
o.container = c.Name
186+
found = true
187+
break
188+
}
189+
// check if the provided one exists
190+
if c.Name == o.container {
191+
found = true
192+
break
193+
}
194+
}
195+
196+
if !found {
197+
return fmt.Errorf("no containers found for the provided pod/container combination")
198+
}
185199
break
186200
case *v1.Node:
187201
labels := v.GetLabels()
@@ -223,11 +237,14 @@ func (o *RunOptions) Run() error {
223237
}
224238

225239
tj := tracejob.TraceJob{
226-
Name: fmt.Sprintf("%s%s", meta.ObjectNamePrefix, string(juid)),
227-
Namespace: o.namespace,
228-
ID: juid,
229-
Hostname: o.nodeName,
230-
Program: o.program,
240+
Name: fmt.Sprintf("%s%s", meta.ObjectNamePrefix, string(juid)),
241+
Namespace: o.namespace,
242+
ID: juid,
243+
Hostname: o.nodeName,
244+
Program: o.program,
245+
PodUID: o.podUID,
246+
ContainerName: o.container,
247+
IsPod: o.isPod,
231248
}
232249

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

pkg/tracejob/job.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ type TraceJobClient struct {
2222
}
2323

2424
type TraceJob struct {
25-
Name string
26-
ID types.UID
27-
Namespace string
28-
Hostname string
29-
Program string
25+
Name string
26+
ID types.UID
27+
Namespace string
28+
Hostname string
29+
Program string
30+
PodUID string
31+
ContainerName string
32+
IsPod bool
3033
}
3134

3235
// WithOutStream setup a file stream to output trace job operation information
@@ -171,11 +174,18 @@ func (t *TraceJobClient) DeleteJobs(nf TraceJobFilter) error {
171174
}
172175

173176
func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {
177+
174178
bpfTraceCmd := []string{
175179
"/bin/trace-runner",
176180
"--program=/programs/program.bt",
177181
}
178182

183+
if nj.IsPod {
184+
bpfTraceCmd = append(bpfTraceCmd, "--inpod")
185+
bpfTraceCmd = append(bpfTraceCmd, "--container="+nj.ContainerName)
186+
bpfTraceCmd = append(bpfTraceCmd, "--poduid="+nj.PodUID)
187+
}
188+
179189
commonMeta := metav1.ObjectMeta{
180190
Name: nj.Name,
181191
Namespace: nj.Namespace,
@@ -238,7 +248,7 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {
238248
Containers: []apiv1.Container{
239249
apiv1.Container{
240250
Name: nj.Name,
241-
Image: "quay.io/fntlnz/kubectl-trace-bpftrace:master", //TODO(fntlnz): yes this should be configurable!
251+
Image: "quay.io/fntlnz/kubectl-trace-bpftrace:feature-pod-support-tracerunner", //TODO(fntlnz): yes this should be configurable!
242252
Command: bpfTraceCmd,
243253
TTY: true,
244254
Stdin: true,

program.bt

-1
This file was deleted.

vendor/github.com/fntlnz/mountinfo/LICENSE

+191
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fntlnz/mountinfo/README.md

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)