diff --git a/Dockerfile.bpftrace b/Dockerfile.bpftrace index b242cd35..b695f98a 100644 --- a/Dockerfile.bpftrace +++ b/Dockerfile.bpftrace @@ -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"] diff --git a/Makefile b/Makefile index a2c5f81a..74f4bfb9 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/go.mod b/go.mod index bfd708fd..7fd5b964 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 0c83a8e5..676d8f4e 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -59,6 +59,8 @@ type RunOptions struct { program string resourceArg string attach bool + isPod bool + podUID string nodeName string @@ -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() @@ -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) diff --git a/pkg/tracejob/job.go b/pkg/tracejob/job.go index b35825c6..3f50ccec 100644 --- a/pkg/tracejob/job.go +++ b/pkg/tracejob/job.go @@ -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 @@ -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, @@ -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, diff --git a/program.bt b/program.bt deleted file mode 100644 index b6d8bdd1..00000000 --- a/program.bt +++ /dev/null @@ -1 +0,0 @@ -uretprobe:/caturday:"main.counterValue" { printf("%d\n", retval) }' diff --git a/vendor/github.com/fntlnz/mountinfo/LICENSE b/vendor/github.com/fntlnz/mountinfo/LICENSE new file mode 100644 index 00000000..27448585 --- /dev/null +++ b/vendor/github.com/fntlnz/mountinfo/LICENSE @@ -0,0 +1,191 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + Copyright 2014 Docker, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/fntlnz/mountinfo/README.md b/vendor/github.com/fntlnz/mountinfo/README.md new file mode 100644 index 00000000..55e4884b --- /dev/null +++ b/vendor/github.com/fntlnz/mountinfo/README.md @@ -0,0 +1,6 @@ +# mountinfo + +The purpose of this library is to read the `mountinfo` file as a whole +or as single lines in order to get information about the mount points of +a specific process as described in `man 5 proc`. + diff --git a/vendor/github.com/fntlnz/mountinfo/mountinfo.go b/vendor/github.com/fntlnz/mountinfo/mountinfo.go new file mode 100644 index 00000000..18dfcdf1 --- /dev/null +++ b/vendor/github.com/fntlnz/mountinfo/mountinfo.go @@ -0,0 +1,79 @@ +package mountinfo + +import ( + "bufio" + "io" + "os" + "strings" +) + +// Mountinfo struct representing a mountinfo entry +type Mountinfo struct { + MountID string + ParentID string + MajorMinor string + Root string + MountPoint string + MountOptions string + OptionalFields string + FilesystemType string + MountSource string + SuperOptions string +} + +func getMountPart(pieces []string, index int) string { + if len(pieces) > index { + return pieces[index] + } + return "" +} + +// GetMountInfo opens a mountinfo file, returns +func GetMountInfo(fd string) ([]Mountinfo, error) { + file, err := os.Open(fd) + if err != nil { + return nil, err + } + + defer file.Close() + return ParseMountInfo(file) +} + +// ParseMountInfoString transforms a mountinfo string in a struct of type Mountinfo +func ParseMountInfoString(tx string) *Mountinfo { + pieces := strings.Split(tx, " ") + count := len(pieces) + if count < 1 { + return nil + } + i := strings.Index(tx, " - ") + postFields := strings.Fields(tx[i+3:]) + preFields := strings.Fields(tx[:i]) + return &Mountinfo{ + MountID: getMountPart(preFields, 0), + ParentID: getMountPart(preFields, 1), + MajorMinor: getMountPart(preFields, 2), + Root: getMountPart(preFields, 3), + MountPoint: getMountPart(preFields, 4), + MountOptions: getMountPart(preFields, 5), + OptionalFields: getMountPart(preFields, 6), + FilesystemType: getMountPart(postFields, 0), + MountSource: getMountPart(postFields, 1), + SuperOptions: getMountPart(postFields, 2), + } +} + +// ParseMountInfo parses the mountinfo content from an io.Reader, e.g a file +func ParseMountInfo(buffer io.Reader) ([]Mountinfo, error) { + info := []Mountinfo{} + scanner := bufio.NewScanner(buffer) + for scanner.Scan() { + tx := scanner.Text() + info = append(info, *ParseMountInfoString(tx)) + } + + if err := scanner.Err(); err != nil { + return info, err + } + return info, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index a097a768..332f1703 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -17,6 +17,8 @@ github.com/docker/spdystream/spdy github.com/evanphx/json-patch # github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d github.com/exponent-io/jsonpath +# github.com/fntlnz/mountinfo v0.0.0-20171106231217-40cb42681fad +github.com/fntlnz/mountinfo # github.com/ghodss/yaml v1.0.0 github.com/ghodss/yaml # github.com/go-openapi/jsonpointer v0.17.0