Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy without c files7 #1437

Merged
merged 6 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions odiglet/pkg/instrumentation/fs/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,16 @@ const (
)

func CopyAgentsDirectoryToHost() error {

// remove the current content of /var/odigos
// 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
entries, err := os.ReadDir(hostDir)
// We kept the .so/.node files to avoid removing the instrumentations that are already loaded in the process memory
err := removeFilesInDir(hostDir)
if err != nil {
log.Logger.Error(err, "Error removing instrumentation directory from host")
return err
}
for _, entry := range entries {
entryPath := filepath.Join(hostDir, entry.Name())
err := os.RemoveAll(entryPath)
if err != nil {
return err
}
}

err = copyDirectories(containerDir, hostDir)
if err != nil {
Expand Down Expand Up @@ -67,3 +61,8 @@ func CopyAgentsDirectoryToHost() error {

return nil
}

func ShouldRecreateAllCFiles() bool {
value, exists := os.LookupEnv("RECREATE_ALL_C_FILES")
return exists && value == "true"
}
39 changes: 37 additions & 2 deletions odiglet/pkg/instrumentation/fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"

Expand All @@ -28,7 +29,15 @@ func getNumberOfWorkers() int {

func copyDirectories(srcDir string, destDir string) error {
start := time.Now()
files, err := getFiles(srcDir)

hostContainEbpfDir := HostContainsEbpfDir(destDir)
shouldRecreateCFiles := ShouldRecreateAllCFiles()

// If the host directory NOT contains ebpf directories OR we should recreate C files, we copy all files
CopyCFiles := !hostContainEbpfDir || shouldRecreateCFiles
log.Logger.V(0).Info("Copying instrumentation files to host", "srcDir", srcDir, "destDir", destDir, "CopyCFiles", CopyCFiles)

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

func getFiles(dir string) ([]string, error) {
func getFiles(dir string, CopyCFiles bool) ([]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
}
}
}

files = append(files, path)
}
return nil
Expand Down Expand Up @@ -127,3 +147,18 @@ func copyFile(src, dst string, buf []byte) error {

return nil
}

func HostContainsEbpfDir(dir string) bool {
found := false
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil || found {
return err
}
if info.IsDir() && strings.Contains(info.Name(), "ebpf") {
found = true
return filepath.SkipDir
}
return nil
})
return found
}
2 changes: 1 addition & 1 deletion odiglet/pkg/instrumentation/fs/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestGetFiles(t *testing.T) {
t.Fatalf("createTestFiles failed: %v", err)
}

gotFiles, err := getFiles(tempDir + "/dir1")
gotFiles, err := getFiles(tempDir+"/dir1", false)
if err != nil {
t.Fatalf("getFiles failed: %v", err)
}
Expand Down
48 changes: 48 additions & 0 deletions odiglet/pkg/instrumentation/fs/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fs

import (
"fmt"
"os"
"path/filepath"
"strings"

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

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

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() {
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 the file
err = os.Remove(path)
if err != nil {
return err
}

return nil
})
}
Loading