From c55f4a848c88c137f0c5fd6540dd53a7b4d99e38 Mon Sep 17 00:00:00 2001 From: Kornilios Kourtis Date: Fri, 29 Nov 2024 10:01:01 +0100 Subject: [PATCH] tetragon-rthooks: move code to apply changes Move applyChanges used to patch the containerd configuration file to its own file. Useful for next commit. Signed-off-by: Kornilios Kourtis --- contrib/tetragon-rthooks/cmd/setup/addline.go | 80 +++++++++++++++++++ .../cmd/setup/patch-containerd-conf.go | 68 ---------------- 2 files changed, 80 insertions(+), 68 deletions(-) create mode 100644 contrib/tetragon-rthooks/cmd/setup/addline.go diff --git a/contrib/tetragon-rthooks/cmd/setup/addline.go b/contrib/tetragon-rthooks/cmd/setup/addline.go new file mode 100644 index 00000000000..333701362fe --- /dev/null +++ b/contrib/tetragon-rthooks/cmd/setup/addline.go @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Authors of Tetragon + +package main + +import ( + "bufio" + "os" + "strings" + + "github.com/pelletier/go-toml" +) + +// special line number to append at the end of the file +const appendAtEndLine = -10 + +type addLine struct { + pos toml.Position + line string + replaceLine bool +} + +func applyChanges(fnameIn, fnameOut string, changes []addLine) error { + fIn, err := os.Open(fnameIn) + if err != nil { + return err + } + defer fIn.Close() + cr := "\n" + if usesCR(fIn) { + cr = "\r\n" + } + + fOut, err := os.Create(fnameOut) + if err != nil { + return err + } + defer fOut.Close() + + inLine := 0 + inSc := bufio.NewScanner(fIn) + out := bufio.NewWriter(fOut) + defer out.Flush() + for inSc.Scan() { + inLine++ + lines := []string{} + replaceLine := false + for i := range changes { + ch := &changes[i] + if ch.pos.Line == inLine { + line := strings.Repeat(" ", ch.pos.Col-1) + ch.line + cr + lines = append(lines, line) + if ch.replaceLine { + replaceLine = true + } + } + } + if !replaceLine { + out.WriteString(inSc.Text()) + out.WriteString(cr) + } + for _, line := range lines { + out.WriteString(line) + } + } + + for i := range changes { + ch := &changes[i] + if ch.pos.Line == appendAtEndLine { + indent := "" + if ch.pos.Col > 0 { + indent = strings.Repeat(" ", ch.pos.Col-1) + } + line := indent + ch.line + cr + out.WriteString(line) + } + } + + return nil +} diff --git a/contrib/tetragon-rthooks/cmd/setup/patch-containerd-conf.go b/contrib/tetragon-rthooks/cmd/setup/patch-containerd-conf.go index 1bb9cff224e..57afbcfe09d 100644 --- a/contrib/tetragon-rthooks/cmd/setup/patch-containerd-conf.go +++ b/contrib/tetragon-rthooks/cmd/setup/patch-containerd-conf.go @@ -8,7 +8,6 @@ import ( "fmt" "log/slog" "os" - "strings" srvconf "github.com/containerd/containerd/services/server/config" "github.com/pelletier/go-toml" @@ -18,14 +17,6 @@ import ( // solution would be to modify the config object and just marshall it instead of just doing text // replacements. TBD. -const appendAtEndLine = -10 - -type addLine struct { - pos toml.Position - line string - replaceLine bool -} - type addOCIHookState struct { cnf *addOCIHookCmd // poor man's patch @@ -141,65 +132,6 @@ func usesCR(f *os.File) bool { return false } -func applyChanges(fnameIn, fnameOut string, changes []addLine) error { - fIn, err := os.Open(fnameIn) - if err != nil { - return err - } - defer fIn.Close() - cr := "\n" - if usesCR(fIn) { - cr = "\r\n" - } - - fOut, err := os.Create(fnameOut) - if err != nil { - return err - } - defer fOut.Close() - - inLine := 0 - inSc := bufio.NewScanner(fIn) - out := bufio.NewWriter(fOut) - defer out.Flush() - for inSc.Scan() { - inLine++ - lines := []string{} - replaceLine := false - for i := range changes { - ch := &changes[i] - if ch.pos.Line == inLine { - line := strings.Repeat(" ", ch.pos.Col-1) + ch.line + cr - lines = append(lines, line) - if ch.replaceLine { - replaceLine = true - } - } - } - if !replaceLine { - out.WriteString(inSc.Text()) - out.WriteString(cr) - } - for _, line := range lines { - out.WriteString(line) - } - } - - for i := range changes { - ch := &changes[i] - if ch.pos.Line == appendAtEndLine { - indent := "" - if ch.pos.Col > 0 { - indent = strings.Repeat(" ", ch.pos.Col-1) - } - line := indent + ch.line + cr - out.WriteString(line) - } - } - - return nil -} - func (c *addOCIHookCmd) Run(log *slog.Logger) error { changes, err := addOciHook(log, c) if err != nil {