Skip to content

Commit

Permalink
tetragon-oci-hook-setup: crio.conf annotations
Browse files Browse the repository at this point in the history
Add a command to patch crio.conf to add allowed annotations.

For example:
$ ssh cat /etc/crio/crio.conf > crio.conf
$ ./tetragon-oci-hook-setup patch-crio-conf enable-annotations --config-file=crio.conf --output-file crio-patched.conf --annotations='io.kubernetes.cri-o.cgroup2-mount-hierarchy-rw'
$ diff -u crio.conf crio-patched.conf
--- crio.conf   2024-11-29 10:47:11.622015385 +0100
+++ crio-patched.conf   2024-11-29 11:03:39.856306109 +0100
@@ -300,6 +300,7 @@
 runtime_path = "/usr/bin/runc"
 runtime_type = "oci"
 runtime_root = "/run/runc"
+allowed_annotations = ["io.kubernetes.cri-o.cgroup2-mount-hierarchy-rw"]

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Dec 6, 2024
1 parent c55f4a8 commit 4b08e1e
Show file tree
Hide file tree
Showing 2,598 changed files with 841,389 additions and 249 deletions.
1 change: 1 addition & 0 deletions contrib/tetragon-rthooks/cmd/setup/addline.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func applyChanges(fnameIn, fnameOut string, changes []addLine) error {
for i := range changes {
ch := &changes[i]
if ch.pos.Line == inLine {
// NB: we assume that everything before is indentation
line := strings.Repeat(" ", ch.pos.Col-1) + ch.line + cr
lines = append(lines, line)
if ch.replaceLine {
Expand Down
1 change: 1 addition & 0 deletions contrib/tetragon-rthooks/cmd/setup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ type CLI struct {
Uninstall Uninstall `cmd:"" help:"Uninstall hook"`
PrintConfig PrintConfig `cmd:"" help:"Print config"`
PatchContainerdConf patchContainerdConf `cmd:"patch containerd configuration"`
PatchCrioConf patchCrioConf `cmd:"patch crio configuration"`

LogLevel string `name:"log-level" default:"info" help:"log level"`
}
Expand Down
136 changes: 136 additions & 0 deletions contrib/tetragon-rthooks/cmd/setup/patch-crio-conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package main

import (
"fmt"
"log/slog"
"os"
"strings"

"github.com/pelletier/go-toml"
tomlparser "github.com/pelletier/go-toml/v2/unstable"
)

type patchCrioConf struct {
EnableAnnotations enableAnnotations `cmd:"" help:"enable annotations"`
}

type enableAnnotations struct {
ConfFile string `name:"config-file" default:"/etc/crio/crio.conf" help:"crio configuration file location (input) (${default}))"`
Outfile string `name:"output-file" default:"" help:"output file location"`
Annotations []string `name:"annotations"`
}

func doEnableAnnotations(log *slog.Logger, c *enableAnnotations) ([]addLine, error) {
data, err := os.ReadFile(c.ConfFile)
if err != nil {
return nil, err
}

p := tomlparser.Parser{
KeepComments: true,
}
p.Reset(data)

lines := []addLine{}
insideRuntime := false
var annotationsLoc tomlparser.Shape
for p.NextExpression() {
e := p.Expression()
switch insideRuntime {
case false:
if e.Kind == tomlparser.Table {
c := e.Child()
if c == nil || c.Kind != tomlparser.Key || string(c.Data) != "crio" {
continue
}
c = c.Next()
if c == nil || c.Kind != tomlparser.Key || string(c.Data) != "runtime" {
continue
}
c = c.Next()
if c == nil || c.Kind != tomlparser.Key || string(c.Data) != "runtimes" {
continue
}
c = c.Next()
if c == nil || c.Kind != tomlparser.Key {
continue
}
insideRuntime = true
}
case true:
foundAnnotations := false
done := false
annotations := []string{}
if e.Kind == tomlparser.KeyValue {
var array *tomlparser.Node
for c := e.Child(); c != nil; c = c.Next() {
annotationsLoc = p.Shape(c.Raw)
if c.Kind == tomlparser.Key && string(c.Data) == "allowed_annotations" {
foundAnnotations = true
} else if c.Kind == tomlparser.Array {
array = c
}

if foundAnnotations && array != nil {
for cc := array.Child(); cc != nil; cc = cc.Next() {
if cc.Kind == tomlparser.String {
annotations = append(annotations, string(cc.Data))
}
}
break
}
}

} else {
done = true
}

if foundAnnotations || done {
annotations = append(annotations, c.Annotations...)
qannotations := make([]string, 0, len(annotations))
for _, a := range annotations {
qannotations = append(qannotations, fmt.Sprintf("%q", a))
}
insideRuntime = false
lines = append(lines, addLine{
pos: toml.Position{Col: annotationsLoc.Start.Column, Line: annotationsLoc.Start.Line},
line: fmt.Sprintf("allowed_annotations = [%s]", strings.Join(qannotations, ", ")),
replaceLine: foundAnnotations,
})
}
}
}

return lines, nil

}

func (c *enableAnnotations) Run(log *slog.Logger) error {

changes, err := doEnableAnnotations(log, c)
if len(changes) == 0 {
log.Info("nothing to do")
return nil
}

outFname := c.Outfile
if outFname == "" {
f, err := os.CreateTemp("", "crio.*.conf")
if err != nil {
return err
}
outFname = f.Name()
f.Close()
}

err = applyChanges(c.ConfFile, outFname, changes)
if err != nil {
return err
}
log.Info("written output", "filename", outFname)
return nil

}
92 changes: 89 additions & 3 deletions contrib/tetragon-rthooks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,133 @@ require (
github.com/containerd/containerd v1.7.24
github.com/containerd/nri v0.8.0
github.com/containers/common v0.61.0
github.com/cri-o/cri-o v1.31.2
github.com/google/cel-go v0.22.1
github.com/opencontainers/runc v1.2.2
github.com/opencontainers/runtime-spec v1.2.0
github.com/pelletier/go-toml v1.9.5
github.com/pelletier/go-toml/v2 v2.2.3
github.com/stretchr/testify v1.10.0
google.golang.org/grpc v1.68.0
)

require (
cel.dev/expr v0.18.0 // indirect
dario.cat/mergo v1.0.1 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Microsoft/hcsshim v0.12.9 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/otelttrpc v0.0.0-20240305015340-ea5083fda723 // indirect
github.com/containerd/ttrpc v1.2.6-0.20240827082320-b5cd6e4b3287 // indirect
github.com/containerd/typeurl/v2 v2.2.2 // indirect
github.com/containernetworking/cni v1.2.3 // indirect
github.com/containernetworking/plugins v1.5.1 // indirect
github.com/containers/conmon v2.0.20+incompatible // indirect
github.com/containers/image/v5 v5.33.0 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.2.0 // indirect
github.com/containers/storage v1.56.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.1-0.20231103132048-7d375ecc2b09 // indirect
github.com/cri-o/ocicni v0.4.2 // indirect
github.com/cyphar/filepath-securejoin v0.3.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v27.3.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.2 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.1-0.20230522191255-76236955d466 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-intervals v0.0.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/renameio v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/intel/goresctrl v0.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/mistifyio/go-zfs/v3 v3.0.1 // indirect
github.com/moby/spdystream v0.4.0 // indirect
github.com/moby/sys/capability v0.3.0 // indirect
github.com/moby/sys/mountinfo v0.7.2 // indirect
github.com/moby/sys/user v0.3.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/runtime-tools v0.9.1-0.20241001195557-6c9570a1678f // indirect
github.com/opencontainers/selinux v1.11.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.57.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/seccomp/libseccomp-golang v0.10.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/vbatts/tar-split v0.11.6 // indirect
github.com/vishvananda/netlink v1.3.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.26.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.0 // indirect
k8s.io/apimachinery v0.31.0 // indirect
k8s.io/client-go v0.31.0 // indirect
k8s.io/cri-api v0.31.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
tags.cncf.io/container-device-interface v0.8.0 // indirect
tags.cncf.io/container-device-interface/specs-go v0.8.0 // indirect
)

replace github.com/cilium/tetragon/api => ../../api
Loading

0 comments on commit 4b08e1e

Please sign in to comment.