@@ -30,6 +30,12 @@ type PluginConf struct {
3030 DefaultAction string `json:"defaultAction"`
3131 OutboundRules []iptables.OutboundRule `json:"outboundRules"`
3232 Logging LogConfig `json:"logging"`
33+ Metadata map [string ]string `json:"metadata"`
34+ }
35+
36+ type argResults struct {
37+ additionalRules []iptables.OutboundRule
38+ metadata map [string ]string
3339}
3440
3541var logger * slog.Logger
@@ -71,57 +77,72 @@ func generateChainName(netName, containerID string) string {
7177 return utils .MustFormatChainNameWithPrefix (netName , containerID , "OUT-" )
7278}
7379
74- func parseAdditionalRules (args , containerID string ) ([]iptables.OutboundRule , error ) {
80+ func parseArgs (args , containerID string ) ([]iptables.OutboundRule , map [ string ] string , error ) {
7581 logger .Log (context .Background (), slog .LevelInfo ,
76- "Parsing additional rules from args " ,
82+ "Parsing CNI arguments " ,
7783 slog .String ("component" , "CNI-Outbound" ),
7884 slog .String ("containerID" , containerID ),
7985 slog .String ("details" , args ),
8086 )
8187
88+ metadata := make (map [string ]string )
89+ var additionalRules []iptables.OutboundRule
90+
8291 if args == "" {
8392 logger .Log (context .Background (), slog .LevelInfo ,
8493 "No additional args provided" ,
8594 slog .String ("component" , "CNI-Outbound" ),
8695 slog .String ("containerID" , containerID ),
8796 )
88- return nil , nil // Return nil
97+ return nil , metadata , nil
8998 }
9099
91- var additionalRules []iptables.OutboundRule // Initialize as nil
92100 kvs := strings .Split (args , ";" )
93101 for _ , kv := range kvs {
94102 parts := strings .SplitN (kv , "=" , 2 )
95- if len (parts ) != 2 || parts [ 0 ] != "outbound.additional_rules" {
103+ if len (parts ) != 2 {
96104 continue
97105 }
98106
99- logger .Log (context .Background (), slog .LevelInfo ,
100- "Found outbound.additional_rules" ,
101- slog .String ("component" , "CNI-Outbound" ),
102- slog .String ("containerID" , containerID ),
103- slog .String ("rules" , parts [1 ]),
104- )
107+ key , value := parts [0 ], parts [1 ]
105108
106- if err := json . Unmarshal ([] byte ( parts [ 1 ]), & additionalRules ); err != nil {
107- logger .Log (context .Background (), slog .LevelError ,
108- "Failed to parse additional rules " ,
109+ if key == "outbound.additional_rules" {
110+ logger .Log (context .Background (), slog .LevelInfo ,
111+ "Found outbound.additional_rules " ,
109112 slog .String ("component" , "CNI-Outbound" ),
110113 slog .String ("containerID" , containerID ),
111- slog .Any ("error" , err ),
114+ slog .String ("rules" , value ),
115+ )
116+
117+ if err := json .Unmarshal ([]byte (value ), & additionalRules ); err != nil {
118+ logger .Log (context .Background (), slog .LevelError ,
119+ "Failed to parse additional rules" ,
120+ slog .String ("component" , "CNI-Outbound" ),
121+ slog .String ("containerID" , containerID ),
122+ slog .Any ("error" , err ),
123+ )
124+ return nil , nil , fmt .Errorf ("failed to parse additional rules from CNI args: %v" , err )
125+ }
126+ } else {
127+ metadata [key ] = value
128+ logger .Log (context .Background (), slog .LevelInfo ,
129+ "Found metadata" ,
130+ slog .String ("component" , "CNI-Outbound" ),
131+ slog .String ("containerID" , containerID ),
132+ slog .String ("key" , key ),
112133 )
113- return nil , fmt .Errorf ("failed to parse additional rules from CNI args: %v" , err )
114134 }
115- break
116135 }
117136
118137 logger .Log (context .Background (), slog .LevelInfo ,
119- "Parsed additional rules " ,
138+ "Parsed args " ,
120139 slog .String ("component" , "CNI-Outbound" ),
121140 slog .String ("containerID" , containerID ),
122141 slog .Int ("ruleCount" , len (additionalRules )),
142+ slog .Int ("metadataCount" , len (metadata )),
123143 )
124- return additionalRules , nil
144+
145+ return additionalRules , metadata , nil
125146}
126147
127148func parseConfig (stdin []byte , args , containerID string ) (* PluginConf , error ) {
@@ -165,7 +186,6 @@ func parseConfig(stdin []byte, args, containerID string) (*PluginConf, error) {
165186 return nil , fmt .Errorf ("could not parse prevResult: %v" , err )
166187 }
167188
168- // Convert prevResult to current.Result
169189 result , err := current .NewResultFromResult (conf .PrevResult )
170190 if err != nil {
171191 logger .Log (context .Background (), slog .LevelError ,
@@ -177,7 +197,6 @@ func parseConfig(stdin []byte, args, containerID string) (*PluginConf, error) {
177197 return nil , fmt .Errorf ("failed to convert prevResult to current.Result: %v" , err )
178198 }
179199
180- // Check for required fields
181200 if len (result .Interfaces ) == 0 {
182201 return nil , fmt .Errorf ("invalid prevResult structure: missing interfaces" )
183202 }
@@ -189,54 +208,54 @@ func parseConfig(stdin []byte, args, containerID string) (*PluginConf, error) {
189208 conf .PrevResult = result
190209 }
191210
192- if conf .MainChainName == "" {
211+ // Parse additional rules and metadata from args
212+ additionalRules , argsMetadata , err := parseArgs (args , containerID )
213+ if err != nil {
214+ return nil , err
215+ }
216+
217+ // Initialize metadata map only if we have metadata to add
218+ if len (argsMetadata ) > 0 && conf .Metadata == nil {
219+ conf .Metadata = make (map [string ]string )
220+ }
221+
222+ // Add rules from args if any exist
223+ if len (additionalRules ) > 0 {
193224 logger .Log (context .Background (), slog .LevelInfo ,
194- "Using default MainChainName: CNI-OUTBOUND " ,
225+ "Appending additional rules " ,
195226 slog .String ("component" , "CNI-Outbound" ),
196227 slog .String ("containerID" , containerID ),
228+ slog .Int ("ruleCount" , len (additionalRules )),
197229 )
198- conf .MainChainName = "CNI-OUTBOUND"
230+ conf .OutboundRules = append ( conf . OutboundRules , additionalRules ... )
199231 }
200232
201- if conf .DefaultAction == "" {
233+ // Merge metadata from args only if we have metadata
234+ if len (argsMetadata ) > 0 {
235+ for k , v := range argsMetadata {
236+ conf .Metadata [k ] = v
237+ }
238+ }
239+
240+ // Set defaults if needed
241+ if conf .MainChainName == "" {
202242 logger .Log (context .Background (), slog .LevelInfo ,
203- "Using default DefaultAction: DROP " ,
243+ "Using default MainChainName: CNI-OUTBOUND " ,
204244 slog .String ("component" , "CNI-Outbound" ),
205245 slog .String ("containerID" , containerID ),
206246 )
207- conf .DefaultAction = "DROP "
247+ conf .MainChainName = "CNI-OUTBOUND "
208248 }
209249
210- logger .Log (context .Background (), slog .LevelInfo ,
211- "Base configuration" ,
212- slog .String ("component" , "CNI-Outbound" ),
213- slog .String ("containerID" , containerID ),
214- slog .String ("MainChainName" , conf .MainChainName ),
215- slog .String ("DefaultAction" , conf .DefaultAction ),
216- )
217-
218- // Parse and append additional rules from CNI args, if any
219- additionalRules , err := parseAdditionalRules (args , containerID )
220- if err != nil {
221- return nil , err
222- }
223- if len (additionalRules ) > 0 {
250+ if conf .DefaultAction == "" {
224251 logger .Log (context .Background (), slog .LevelInfo ,
225- "Appending additional rules " ,
252+ "Using default DefaultAction: DROP " ,
226253 slog .String ("component" , "CNI-Outbound" ),
227254 slog .String ("containerID" , containerID ),
228- slog .Int ("ruleCount" , len (additionalRules )),
229255 )
230- conf .OutboundRules = append ( conf . OutboundRules , additionalRules ... )
256+ conf .DefaultAction = "DROP"
231257 }
232258
233- logger .Log (context .Background (), slog .LevelInfo ,
234- "Total outbound rules" ,
235- slog .String ("component" , "CNI-Outbound" ),
236- slog .String ("containerID" , containerID ),
237- slog .Int ("totalRules" , len (conf .OutboundRules )),
238- )
239-
240259 return & conf , nil
241260}
242261
0 commit comments