Skip to content

Commit

Permalink
feat(tracejob): support running trace runner instead of bpftrace dire…
Browse files Browse the repository at this point in the history
…ctly

Signed-off-by: Lorenzo Fontana <lo@linux.com>
  • Loading branch information
fntlnz committed Dec 24, 2018
1 parent ca4c379 commit bb56e8b
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 27 deletions.
17 changes: 14 additions & 3 deletions Dockerfile.bpftrace
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@ RUN git clone https://github.com/iovisor/bpftrace.git /bpftrace

WORKDIR /bpftrace

RUN git checkout 8f7f8214d7dd7bc25b7740a3c0e9a580a89e0244
RUN git checkout 2ae2a53f62622631a304def6c193680e603994e3

WORKDIR /bpftrace/docker

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

RUN ls -la
FROM golang:1.11.4-alpine3.8 as gobuilder

RUN apk update
RUN apk add make

ADD . /go/src/github.com/fntlnz/kubectl-trace
WORKDIR /go/src/github.com/fntlnz/kubectl-trace

RUN make _output/bin/trace-runner

FROM alpine:3.8

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

ENTRYPOINT ["/bin/bpftrace"]
ENTRYPOINT ["/bin/trace-runner"]
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ IMAGE_BPFTRACE_COMMIT := quay.io/fntlnz/kubectl-trace-bpftrace:$(GIT_COMMIT)
IMAGE_BUILD_FLAGS ?= "--no-cache"

kubectl_trace ?= _output/bin/kubectl-trace
trace_runner ?= _output/bin/trace-runner

.PHONY: build
build: clean ${kubectl_trace}
build: clean ${kubectl_trace} ${trace_runner}

${kubectl_trace}:
$(GO) build -o $@ ./cmd/kubectl-trace
CGO_ENABLED=0 $(GO) build -o $@ ./cmd/kubectl-trace

${trace_runner}:
CGO_ENABLED=1 $(GO) build -o $@ ./cmd/trace-runner

.PHONY: clean
clean:
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/fntlnz/kubectl-trace
require (
cloud.google.com/go v0.34.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/docker/distribution v2.6.2+incompatible // indirect
github.com/docker/docker v0.7.3-0.20181124105010-0b7cb16dde4a // indirect
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 // indirect
Expand Down Expand Up @@ -40,7 +39,7 @@ require (
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a // indirect
golang.org/x/oauth2 v0.0.0-20181120190819-8f65e3013eba // indirect
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
google.golang.org/appengine v1.3.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
Expand Down
43 changes: 30 additions & 13 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type RunOptions struct {
program string
resourceArg string
attach bool
isPod bool
podUID string

nodeName string

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

// Check we got a pod or a node
// isPod := false
o.isPod = false
switch v := obj.(type) {
case *v1.Pod:
// isPod = true
// if len(o.container) == 0 {
// todo > get the default container or the first one, see https://github.com/fntlnz/kubectl-trace/pull/1#issuecomment-441331255
// } else {
// todo > check the pod has the provided container (o.container)
// }
return fmt.Errorf("running bpftrace programs against pods is not supported yet, see: https://github.com/fntlnz/kubectl-trace/issues/3")
o.isPod = true
found := false
for _, c := range v.Spec.Containers {
// default if no container provided
if len(o.container) == 0 {
o.container = c.Name
found = true
break
}
// check if the provided one exists
if c.Name == o.container {
found = true
break
}
}

if !found {
return fmt.Errorf("no containers found for the provided pod/container combination")
}
break
case *v1.Node:
labels := v.GetLabels()
Expand Down Expand Up @@ -223,11 +237,14 @@ func (o *RunOptions) Run() error {
}

tj := tracejob.TraceJob{
Name: fmt.Sprintf("%s%s", meta.ObjectNamePrefix, string(juid)),
Namespace: o.namespace,
ID: juid,
Hostname: o.nodeName,
Program: o.program,
Name: fmt.Sprintf("%s%s", meta.ObjectNamePrefix, string(juid)),
Namespace: o.namespace,
ID: juid,
Hostname: o.nodeName,
Program: o.program,
PodUID: o.podUID,
ContainerName: o.container,
IsPod: o.isPod,
}

job, err := tc.CreateJob(tj)
Expand Down
22 changes: 16 additions & 6 deletions pkg/tracejob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ type TraceJobClient struct {
}

type TraceJob struct {
Name string
ID types.UID
Namespace string
Hostname string
Program string
Name string
ID types.UID
Namespace string
Hostname string
Program string
PodUID string
ContainerName string
IsPod bool
}

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

func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {

bpfTraceCmd := []string{
"/bin/trace-runner",
"--program=/programs/program.bt",
}

if nj.IsPod {
bpfTraceCmd = append(bpfTraceCmd, "--inpod")
bpfTraceCmd = append(bpfTraceCmd, "--container="+nj.ContainerName)
bpfTraceCmd = append(bpfTraceCmd, "--poduid="+nj.PodUID)
}

commonMeta := metav1.ObjectMeta{
Name: nj.Name,
Namespace: nj.Namespace,
Expand Down Expand Up @@ -238,7 +248,7 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {
Containers: []apiv1.Container{
apiv1.Container{
Name: nj.Name,
Image: "quay.io/fntlnz/kubectl-trace-bpftrace:master", //TODO(fntlnz): yes this should be configurable!
Image: "quay.io/fntlnz/kubectl-trace-bpftrace:feature-pod-support-tracerunner", //TODO(fntlnz): yes this should be configurable!
Command: bpfTraceCmd,
TTY: true,
Stdin: true,
Expand Down
1 change: 0 additions & 1 deletion program.bt

This file was deleted.

191 changes: 191 additions & 0 deletions vendor/github.com/fntlnz/mountinfo/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/fntlnz/mountinfo/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bb56e8b

Please sign in to comment.