Skip to content

Commit

Permalink
fix: remove only protected c files and their directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamir David authored and Tamir David committed Aug 13, 2024
1 parent 880f2ce commit 50d70b6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
17 changes: 14 additions & 3 deletions odiglet/pkg/instrumentation/fs/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ func CopyAgentsDirectoryToHost() error {
// as we want a fresh copy of instrumentation agents with no files leftover from previous odigos versions.
// we cannot remove /var/odigos itself: "unlinkat /var/odigos: device or resource busy"
// so we will just remove it's content
// We kept the .so/.node files to avoid removing the instrumentations that are already loaded in the process memory
err := removeFilesInDir(hostDir)

// We kept the following list of files to avoid removing instrumentations that are already loaded in the process memory
filesToKeepMap := map[string]struct{}{
"/var/odigos/nodejs-ebpf/build/Release/dtrace-injector-native.node": {},
"/var/odigos/nodejs-ebpf/build/Release/obj.target/dtrace-injector-native.node": {},
"/var/odigos/nodejs-ebpf/build/Release/.deps/Release/dtrace-injector-native.node.d": {},
"/var/odigos/nodejs-ebpf/build/Release/.deps/Release/obj.target/dtrace-injector-native.node.d": {},
"/var/odigos/java-ebpf/tracing_probes.so": {},
"/var/odigos/java-ext-ebpf/end_span_usdt.so": {},
"/var/odigos/python-ebpf/pythonUSDT.abi3.so": {},
}

err := removeFilesInDir(hostDir, filesToKeepMap)
if err != nil {
log.Logger.Error(err, "Error removing instrumentation directory from host")
return err
}

err = copyDirectories(containerDir, hostDir)
err = copyDirectories(containerDir, hostDir, filesToKeepMap)
if err != nil {
log.Logger.Error(err, "Error copying instrumentation directory to host")
return err
Expand Down
17 changes: 7 additions & 10 deletions odiglet/pkg/instrumentation/fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getNumberOfWorkers() int {
return min(maxWorkers, max(1, runtime.NumCPU()/4))
}

func copyDirectories(srcDir string, destDir string) error {
func copyDirectories(srcDir string, destDir string, filesToKeepMap map[string]struct{}) error {
start := time.Now()

hostContainEbpfDir := HostContainsEbpfDir(destDir)
Expand All @@ -37,7 +37,7 @@ func copyDirectories(srcDir string, destDir string) error {
CopyCFiles := !hostContainEbpfDir || shouldRecreateCFiles
log.Logger.V(0).Info("Copying instrumentation files to host", "srcDir", srcDir, "destDir", destDir, "CopyCFiles", CopyCFiles)

files, err := getFiles(srcDir, CopyCFiles)
files, err := getFiles(srcDir, CopyCFiles, filesToKeepMap)
if err != nil {
return err
}
Expand Down Expand Up @@ -85,28 +85,25 @@ func worker(fileChan <-chan string, sourceDir, destDir string, wg *sync.WaitGrou
}
}

func getFiles(dir string, CopyCFiles bool) ([]string, error) {
func getFiles(dir string, CopyCFiles bool, filesToKeepMap map[string]struct{}) ([]string, error) {
var files []string
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {

if !CopyCFiles {
// filter out C files in ebpf directories
if strings.Contains(filepath.Dir(path), "ebpf") {
switch ext := filepath.Ext(path); ext {
case ".so", ".node", ".node.d", ".a":
return nil
}
if _, found := filesToKeepMap[strings.Replace(path, "/instrumentations/", "/var/odigos/", 1)]; found {
log.Logger.V(0).Info("Skipping copying file", "file", path)
return nil
}
}

files = append(files, path)
}
return nil
})

return files, err
}

Expand Down
9 changes: 6 additions & 3 deletions odiglet/pkg/instrumentation/fs/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func TestGetFiles(t *testing.T) {
t.Fatalf("createTestFiles failed: %v", err)
}

gotFiles, err := getFiles(tempDir+"/dir1", false)
filesToKeep := make(map[string]struct{})
gotFiles, err := getFiles(tempDir+"/dir1", false, filesToKeep)
if err != nil {
t.Fatalf("getFiles failed: %v", err)
}
Expand Down Expand Up @@ -66,14 +67,15 @@ func createTestFiles(tempDir string, num int) ([]string, error) {
}

func TestCopyDirectories(t *testing.T) {
filesToKeep := make(map[string]struct{})
src := t.TempDir()
dest := t.TempDir()
files, err := createTestFiles(src, 10)
if err != nil {
t.Fatalf("createTestFiles failed: %v", err)
}

err = copyDirectories(src, dest)
err = copyDirectories(src, dest, filesToKeep)
if err != nil {
t.Fatalf("copyDirectories failed: %v", err)
}
Expand All @@ -98,6 +100,7 @@ func TestCopyDirectories(t *testing.T) {
}

func BenchmarkCopyDirectories(b *testing.B) {
filesToKeep := make(map[string]struct{})
src := b.TempDir()
dest := b.TempDir()
_, err := createTestFiles(src, b.N)
Expand All @@ -107,7 +110,7 @@ func BenchmarkCopyDirectories(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
err = copyDirectories(src, dest)
err = copyDirectories(src, dest, filesToKeep)
if err != nil {
b.Fatalf("copyDirectories failed: %v", err)
}
Expand Down
50 changes: 33 additions & 17 deletions odiglet/pkg/instrumentation/fs/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,59 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/odigos-io/odigos/odiglet/pkg/log"
)

func removeFilesInDir(hostDir string) error {
func removeFilesInDir(hostDir string, filesToKeep map[string]struct{}) error {
shouldRecreateCFiles := ShouldRecreateAllCFiles()
log.Logger.V(0).Info(fmt.Sprintf("Removing files in directory: %s, shouldRecreateCFiles: %s", hostDir, fmt.Sprintf("%t", shouldRecreateCFiles)))
log.Logger.V(0).Info(fmt.Sprintf("Removing files in directory: %s, shouldRecreateCFiles: %t", hostDir, shouldRecreateCFiles))

// Mark directories as protected if they contain a file that needs to be preserved.
// If C files should be recreated, skip marking any directories as protected.
protectedDirs := make(map[string]bool)
if !shouldRecreateCFiles {
for file := range filesToKeep {
dir := filepath.Dir(file)
for dir != hostDir {
protectedDirs[dir] = true
dir = filepath.Dir(dir)
}
protectedDirs[hostDir] = true // Protect the main directory
}
}

return filepath.Walk(hostDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Skip the root directory itself]
if path == hostDir {
return nil
}

if info.IsDir() {
// Skip removing any files listed in filesToKeepMap
if !info.IsDir() {
if _, found := filesToKeep[path]; found {
log.Logger.V(0).Info(fmt.Sprintf("Skipping protected file: %s", path))
return nil
}
}

// Skip removing protected directories
if info.IsDir() && protectedDirs[path] {
log.Logger.V(0).Info(fmt.Sprintf("Skipping protected directory: %s", path))
return nil
}

if !shouldRecreateCFiles {
// filter out C files in ebpf directories
if strings.Contains(filepath.Dir(path), "ebpf") {
switch ext := filepath.Ext(info.Name()); ext {
case ".so", ".node", "node.d", ".a":
return nil
}
}
// Remove removing unprotected files and directories
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("error removing %s: %w", path, err)
}

// Remove the file
err = os.Remove(path)
if err != nil {
return err
// Skip further processing in this directory since it has been removed
if info.IsDir() {
return filepath.SkipDir
}

return nil
Expand Down

0 comments on commit 50d70b6

Please sign in to comment.