@@ -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
1920type 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
2527func 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}
0 commit comments