Skip to content

Commit

Permalink
examples: Embed a gadget within the application
Browse files Browse the repository at this point in the history
Signed-off-by: Mauricio Vásquez <mauriciov@microsoft.com>
  • Loading branch information
mauriciovasquezbernal committed May 27, 2024
1 parent 9955ff2 commit bff4dcc
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/gadgets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Examples showing how to use some gadgets in their simplest configuration.
- [trace_dns](./simple/trace_dns/): Run the `trace_dns` gadget with some of the
operators it requires.
- [from_file](./simple/from_file/): Run a gadget from a tarball.
- [from_memory](./simple/from_memory/): Embed and run a gadget from the application binary.

### Operators

Expand Down
1 change: 1 addition & 0 deletions examples/gadgets/simple/from_memory/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from_memory
39 changes: 39 additions & 0 deletions examples/gadgets/simple/from_memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Embedding a gadget image within the application

This example shows how a gadget image can be embedded in the application binary
and how it can be run from there.

The `trace_open.tar` file was created with:

```bash
$ sudo -E ig image export trace_open:latest trace_open.tar
```

### How to compile

```bash
$ go build .
```

### How to run

The compiled binary doesn't need any parameters, just run it with root permissions:

```bash
$ sudo ./from_memory
```

The gadget runs successfully.

```bash
$ sudo ./from_memory
TIMESTAMP PID UID GID MNTNS… E… FD FL… MODE COMM FNAME
33760132075419 308204 0 0 402653 0 23 524 0 from_me /sys/kernel/tra
33760214948931 1101 108 117 402653 0 7 524 0 systemd /proc/meminfo
33760215059910 1101 108 117 402653 0 7 524 0 systemd /sys/fs/cgroup/
33760215123230 1101 108 117 402653 0 7 524 0 systemd /sys/fs/cgroup/
33760215153988 1101 108 117 402653 0 7 524 0 systemd /sys/fs/cgroup/
33760215186640 1101 108 117 402653 0 7 524 0 systemd /sys/fs/cgroup/
33760215219722 1101 108 117 402653 0 7 524 0 systemd /sys/fs/cgroup/
33760215251071 1101 108 117 402653 0 7 524 0 systemd /sys/fs/cgroup/
```
85 changes: 85 additions & 0 deletions examples/gadgets/simple/from_memory/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2024 The Inspektor Gadget authors
//
// 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.

package main

import (
"bytes"
"context"
_ "embed"
"fmt"
"os"

orasoci "oras.land/oras-go/v2/content/oci"

"github.com/quay/claircore/pkg/tarfs"

gadgetcontext "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-context"
clioperator "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/cli"
_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/ebpf"
ocihandler "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/oci-handler"
"github.com/inspektor-gadget/inspektor-gadget/pkg/runtime/local"
)

// embed the tarball containing the gadget image. It was created with
// $ sudo -E ig image export trace_open:latest trace_open.tar

//go:embed trace_open.tar
var traceOpenBytes []byte

func do() error {
ctx := context.Background()

// Create an FS from the tarball bytes
fs, err := tarfs.New(bytes.NewReader(traceOpenBytes))
if err != nil {
return err
}

// Create the oras target from the FS
target, err := orasoci.NewFromFS(ctx, fs)
if err != nil {
return fmt.Errorf("getting oci store from bytes: %w", err)
}

gadgetCtx := gadgetcontext.New(
context.Background(),
// The name of the gadget to run is needed as a tarball can contain multiple images.
"ghcr.io/inspektor-gadget/gadget/trace_open:latest",
gadgetcontext.WithDataOperators(ocihandler.OciHandler, clioperator.CLIOperator),
gadgetcontext.WithOrasReadonlyTarget(target),
)

runtime := local.New()
if err := runtime.Init(nil); err != nil {
return fmt.Errorf("runtime init: %w", err)
}
defer runtime.Close()

params := map[string]string{
"operator.cli.output": "columns",
}
if err := runtime.RunGadget(gadgetCtx, nil, params); err != nil {
return fmt.Errorf("running gadget: %w", err)
}

return nil
}

func main() {
if err := do(); err != nil {
fmt.Printf("Error running application: %s\n", err)
os.Exit(1)
}
}
Binary file not shown.
4 changes: 3 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ require (
github.com/cilium/ebpf v0.15.0
github.com/inspektor-gadget/inspektor-gadget v0.28.1
github.com/opencontainers/runtime-spec v1.2.0
github.com/quay/claircore v1.5.28
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
oras.land/oras-go/v2 v2.4.0
)

require (
Expand Down Expand Up @@ -117,6 +119,7 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/vishvananda/netlink v1.2.1-beta.2 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
Expand Down Expand Up @@ -152,7 +155,6 @@ require (
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 // indirect
oras.land/oras-go/v2 v2.4.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.14.0 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3 // indirect
Expand Down
21 changes: 21 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inspektor-gadget/inspektor-gadget v0.28.1 h1:SyCy9Mrj6Lcl1gkQCtiO4J76wNFd09/wWphNj4OVq7Q=
github.com/inspektor-gadget/inspektor-gadget v0.28.1/go.mod h1:wW6XZUGuZTdVrx7VMpBw3L5Dd9kAwUrDR2swNniTCTg=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/pgconn v1.14.3 h1:bVoTr12EGANZz66nZPkMInAV/KHD2TxH9npjXXgiB3w=
github.com/jackc/pgconn v1.14.3/go.mod h1:RZbme4uasqzybK2RK5c65VsHxoyaml09lx3tXOcO/VM=
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag=
github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgtype v1.14.2 h1:QBdZQTKpPdBlw2AdKwHEyqUcm/lrl2cwWAHjCMyln/o=
github.com/jackc/pgtype v1.14.2/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgx/v4 v4.18.3 h1:dE2/TrEsGX3RBprb3qryqSV9Y60iZN1C6i8IrmW9/BA=
github.com/jackc/pgx/v4 v4.18.3/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0=
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jmhodges/clock v1.2.0 h1:eq4kys+NI0PLngzaHEe7AmPT90XMGIEySD1JfV1PDIs=
github.com/jmhodges/clock v1.2.0/go.mod h1:qKjhA7x7u/lQpPB1XAqX1b1lCI/w3/fNuYpI/ZjLynI=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down Expand Up @@ -289,6 +307,8 @@ github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQy
github.com/prometheus/common v0.51.1 h1:eIjN50Bwglz6a/c3hAgSMcofL3nD+nFQkV6Dd4DsQCw=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/quay/claircore v1.5.28 h1:NQ6zJGm/G406D8DF7XlxNS0IdxhrVic+eehly9VjnOU=
github.com/quay/claircore v1.5.28/go.mod h1:S5sFYMwJiF8bChlCkhu4MpwB6iqozlF+koSMMZS2uMk=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
Expand Down Expand Up @@ -336,6 +356,7 @@ github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtse
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 h1:e/5i7d4oYZ+C1wj2THlRK+oAhjeS/TRQwMfkIuet3w0=
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399/go.mod h1:LdwHTNJT99C5fTAzDz0ud328OgXz+gierycbcIx2fRs=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/vishvananda/netlink v1.2.1-beta.2 h1:Llsql0lnQEbHj0I1OuKyp8otXp0r3q0mPkuhwHfStVs=
github.com/vishvananda/netlink v1.2.1-beta.2/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
Expand Down

0 comments on commit bff4dcc

Please sign in to comment.