From 0beb76e89a8804b6236bca8ec82e4b45e2f182f6 Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Mon, 24 Jun 2024 12:12:20 +0000 Subject: [PATCH] tetragon: Detect move and remove previous bpf instance Signed-off-by: Jiri Olsa --- cmd/tetragon/main.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cmd/tetragon/main.go b/cmd/tetragon/main.go index 58435463472..720b119a64c 100644 --- a/cmd/tetragon/main.go +++ b/cmd/tetragon/main.go @@ -158,6 +158,34 @@ func stopProfile() { } } +func getOldBpfDir(path string) (string, error) { + if _, err := os.Stat(path); err != nil { + return "", nil + } + old := path + "_old" + // remove the 'xxx_old' leftover if neded + if _, err := os.Stat(old); err == nil { + os.RemoveAll(old) + log.Info("Found bpf leftover instance, removing: %s", old) + } + if err := os.Rename(path, old); err != nil { + return "", err + } + log.Infof("Found bpf instance: %s, moved to: %s", path, old) + return old, nil +} + +func deleteOlBpfDir(path string) { + if path == "" { + return + } + if err := os.RemoveAll(path); err != nil { + log.Errorf("Failed to remove old bpf isntance '%s': %s\n", path, err) + return + } + log.Infof("Removed bpf instance: %s", path) +} + func tetragonExecute() error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -249,6 +277,11 @@ func tetragonExecute() error { bpf.CheckOrMountCgroup2() bpf.SetMapPrefix(option.Config.BpfDir) + oldBpfDir, err := getOldBpfDir(bpf.MapPrefixPath()) + if err != nil { + return fmt.Errorf("Failed to move old tetragon base directory: %w", err) + } + // we need file system mounts setup above before we detect features log.Info("BPF detected features: ", bpf.LogFeatures()) @@ -492,6 +525,8 @@ func tetragonExecute() error { } } + deleteOlBpfDir(oldBpfDir) + // k8s should have metrics, so periodically log only in a non k8s if !option.Config.EnableK8s { go logStatus(ctx, obs)