Skip to content

Commit b505329

Browse files
Copilotj143
andcommitted
Address code review feedback
- Use io.MultiWriter to send output to both console and log file - Add warning logs for cgroup degradation instead of silent failures - Remove duplicate command/args extraction in run function - Improve verify-new.sh with proper binary validation and sudo usage Co-authored-by: j143 <[email protected]>
1 parent 4ce98e7 commit b505329

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
lines changed

cgroup.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ func SetupCgroupsV2(containerID string, memoryLimit int64) error {
9595

9696
// Try to enable memory controller
9797
if err := os.WriteFile(parentControl, []byte("+memory"), 0644); err != nil {
98-
// It's OK if this fails - may not have permission
99-
// Continue without memory limits
98+
// Log warning but continue - cgroup limits may not be available
99+
fmt.Printf("Warning: Cannot enable memory controller in cgroup v2 (degraded mode): %v\n", err)
100100
return nil
101101
}
102102

103103
// Set memory limit if supported
104104
memoryMaxFile := filepath.Join(cgroupPath, "memory.max")
105105
if err := os.WriteFile(memoryMaxFile, []byte(strconv.FormatInt(memoryLimit, 10)), 0644); err != nil {
106-
// Not fatal - just means we can't set limits
106+
// Log warning - limits won't be enforced but container can still run
107+
fmt.Printf("Warning: Cannot set memory limit in cgroup v2 (degraded mode): %v\n", err)
107108
return nil
108109
}
109110

@@ -129,7 +130,8 @@ func SetupCgroupsV1(containerID string, memoryLimit int64) error {
129130
// Set memory limit
130131
memoryLimitFile := filepath.Join(cgroupPath, "memory.limit_in_bytes")
131132
if err := os.WriteFile(memoryLimitFile, []byte(strconv.FormatInt(memoryLimit, 10)), 0644); err != nil {
132-
// Not fatal - just means we can't set limits
133+
// Log warning - limits won't be enforced but container can still run
134+
fmt.Printf("Warning: Cannot set memory limit in cgroup v1 (degraded mode): %v\n", err)
133135
return nil
134136
}
135137

main.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"os"
78
"os/exec"
89
"path/filepath"
@@ -581,15 +582,18 @@ func run() {
581582
os.Exit(1)
582583
}
583584

584-
// Create container metadata
585-
createdAt := time.Now()
586-
command := ""
587-
args := []string{}
588-
if len(os.Args) >= 4 {
589-
command = os.Args[3]
590-
args = os.Args[4:]
585+
// Execute the command in the container
586+
if len(os.Args) < 4 {
587+
fmt.Println("Error: Command required for run")
588+
os.Exit(1)
591589
}
592590

591+
// Extract command and args once
592+
command := os.Args[3]
593+
args := os.Args[4:]
594+
595+
// Create container metadata
596+
createdAt := time.Now()
593597
metadata := ContainerMetadata{
594598
ID: containerID,
595599
State: StateCreated,
@@ -608,12 +612,6 @@ func run() {
608612

609613
fmt.Printf("Starting container %s\n", containerID)
610614

611-
// Execute the command in the container
612-
if len(os.Args) < 4 {
613-
fmt.Println("Error: Command required for run")
614-
os.Exit(1)
615-
}
616-
617615
runWithoutNamespaces(containerID, rootfs, command, args)
618616
}
619617

@@ -783,13 +781,14 @@ func runWithoutNamespaces(containerID, rootfs, command string, args []string) {
783781

784782
cmd := exec.Command(command, args...)
785783
cmd.Stdin = os.Stdin
786-
cmd.Stdout = os.Stdout
787-
cmd.Stderr = os.Stderr
788784

789-
// Also write to log file if available
785+
// Use MultiWriter to send output to both console and log file
790786
if logFd != nil {
791-
cmd.Stdout = logFd
792-
cmd.Stderr = logFd
787+
cmd.Stdout = io.MultiWriter(os.Stdout, logFd)
788+
cmd.Stderr = io.MultiWriter(os.Stderr, logFd)
789+
} else {
790+
cmd.Stdout = os.Stdout
791+
cmd.Stderr = os.Stderr
793792
}
794793

795794
err = cmd.Run()

verify-new.sh

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,42 @@ fi
8383
section "Test 2: Creating Test Image"
8484
info "Setting up test image with basic binaries"
8585
TEST_IMAGE_DIR="/tmp/basic-docker/images/test-image/rootfs"
86-
mkdir -p "$TEST_IMAGE_DIR/bin"
87-
cp /bin/echo "$TEST_IMAGE_DIR/bin/" 2>/dev/null || cp /usr/bin/echo "$TEST_IMAGE_DIR/bin/"
88-
cp /bin/sh "$TEST_IMAGE_DIR/bin/" 2>/dev/null || cp /usr/bin/sh "$TEST_IMAGE_DIR/bin/" || cp /bin/bash "$TEST_IMAGE_DIR/bin/sh"
89-
success "Test image created"
86+
sudo mkdir -p "$TEST_IMAGE_DIR/bin"
87+
88+
# Try to copy echo binary
89+
if [ -f /bin/echo ]; then
90+
sudo cp /bin/echo "$TEST_IMAGE_DIR/bin/"
91+
elif [ -f /usr/bin/echo ]; then
92+
sudo cp /usr/bin/echo "$TEST_IMAGE_DIR/bin/"
93+
else
94+
error "Could not find echo binary"
95+
exit 1
96+
fi
97+
98+
# Try to copy shell binary
99+
if [ -f /bin/sh ]; then
100+
sudo cp /bin/sh "$TEST_IMAGE_DIR/bin/"
101+
elif [ -f /usr/bin/sh ]; then
102+
sudo cp /usr/bin/sh "$TEST_IMAGE_DIR/bin/"
103+
elif [ -f /bin/bash ]; then
104+
sudo cp /bin/bash "$TEST_IMAGE_DIR/bin/sh"
105+
else
106+
error "Could not find shell binary"
107+
exit 1
108+
fi
109+
110+
# Verify binaries are actually present
111+
if [ ! -f "$TEST_IMAGE_DIR/bin/echo" ]; then
112+
error "Failed to copy echo binary to test image"
113+
exit 1
114+
fi
115+
116+
if [ ! -f "$TEST_IMAGE_DIR/bin/sh" ]; then
117+
error "Failed to copy shell binary to test image"
118+
exit 1
119+
fi
120+
121+
success "Test image created with required binaries"
90122

91123
# Test 3: Container Lifecycle - Run and State Tracking
92124
section "Test 3: Container Lifecycle - Run Command"

0 commit comments

Comments
 (0)