Skip to content

Commit

Permalink
tetragon: Detect move and remove previous bpf instance
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Jun 24, 2024
1 parent 2b70b05 commit 4cf6836
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4cf6836

Please sign in to comment.