Skip to content

Commit 7925a5d

Browse files
feat: have an option to overwrite existing pinned programs
1 parent 7921e81 commit 7925a5d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

bpf-prog/azure-block-iptables/cmd/azure-block-iptables/main.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111

1212
"github.com/Azure/azure-container-networking/bpf-prog/azure-block-iptables/pkg/bpfprogram"
13+
"github.com/pkg/errors"
1314
)
1415

1516
// ProgramVersion is set during build
@@ -18,13 +19,15 @@ var version = "unknown"
1819
// Config holds configuration for the application
1920
type Config struct {
2021
Mode string // "attach" or "detach"
22+
Overwrite bool // force detach before attach
2123
AttacherFactory bpfprogram.AttacherFactory
2224
}
2325

2426
// parseArgs parses command line arguments and returns the configuration
2527
func parseArgs() (*Config, error) {
2628
var (
2729
mode = flag.String("mode", "", "Operation mode: 'attach' or 'detach' (required)")
30+
overwrite = flag.Bool("overwrite", false, "Force detach before attach (only applies to attach mode)")
2831
showVersion = flag.Bool("version", false, "Show version information")
2932
showHelp = flag.Bool("help", false, "Show help information")
3033
)
@@ -51,6 +54,7 @@ func parseArgs() (*Config, error) {
5154

5255
return &Config{
5356
Mode: *mode,
57+
Overwrite: *overwrite,
5458
AttacherFactory: bpfprogram.NewProgram,
5559
}, nil
5660
}
@@ -62,9 +66,17 @@ func attachMode(config *Config) error {
6266
// Initialize BPF program attacher using the factory
6367
bp := config.AttacherFactory()
6468

69+
// If overwrite is enabled, first detach any existing programs
70+
if config.Overwrite {
71+
log.Println("Overwrite mode enabled, detaching any existing programs first...")
72+
if err := bp.Detach(); err != nil {
73+
log.Printf("Warning: failed to detach existing programs: %v", err)
74+
}
75+
}
76+
6577
// Attach the BPF program
6678
if err := bp.Attach(); err != nil {
67-
return fmt.Errorf("failed to attach BPF program: %w", err)
79+
return errors.Wrap(err, "failed to attach BPF program")
6880
}
6981

7082
log.Println("BPF program attached successfully")
@@ -77,7 +89,12 @@ func detachMode(config *Config) error {
7789

7890
// Initialize BPF program attacher using the factory
7991
bp := config.AttacherFactory()
80-
bp.Detach()
92+
93+
// Detach the BPF program
94+
if err := bp.Detach(); err != nil {
95+
return errors.Wrap(err, "failed to detach BPF program")
96+
}
97+
8198
log.Println("BPF program detached successfully")
8299
return nil
83100
}

bpf-prog/azure-block-iptables/cmd/azure-block-iptables/main_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@ func TestHandleFileEventWithMock(t *testing.T) {
1717
testCases := []struct {
1818
name string
1919
mode string
20+
overwrite bool
2021
expectedAttach int
2122
expectedDetach int
2223
}{
2324
{
2425
name: "test attach mode",
2526
mode: "attach",
27+
overwrite: false,
2628
expectedAttach: 1,
2729
expectedDetach: 0,
2830
},
31+
{
32+
name: "test attach mode with overwrite",
33+
mode: "attach",
34+
overwrite: true,
35+
expectedAttach: 1,
36+
expectedDetach: 1,
37+
},
2938
{
3039
name: "test detach mode",
3140
mode: "detach",
41+
overwrite: false,
3242
expectedAttach: 0,
3343
expectedDetach: 1,
3444
},
@@ -39,7 +49,7 @@ func TestHandleFileEventWithMock(t *testing.T) {
3949
// Reset mock state
4050
mockAttacher.Reset()
4151

42-
run(&Config{Mode: tc.mode, AttacherFactory: func() bpfprogram.Attacher { return mockAttacher }})
52+
run(&Config{Mode: tc.mode, Overwrite: tc.overwrite, AttacherFactory: func() bpfprogram.Attacher { return mockAttacher }})
4353

4454
// Verify expectations
4555
if mockAttacher.AttachCallCount() != tc.expectedAttach {

0 commit comments

Comments
 (0)