@@ -15,7 +15,10 @@ package main
15
15
16
16
import (
17
17
"encoding/json"
18
+ pluginargs "github.com/firecracker-microvm/firecracker-go-sdk/cni/cmd/tc-redirect-tap/args"
18
19
"os"
20
+ "strconv"
21
+ "strings"
19
22
20
23
"github.com/containernetworking/cni/pkg/skel"
21
24
"github.com/containernetworking/cni/pkg/types"
@@ -101,7 +104,7 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
101
104
// It's valid for the netns to no longer exist during DEL commands (in which case DEL is
102
105
// a noop). Thus, we leave validating that netNS is not nil to command implementations.
103
106
switch err .(type ) {
104
- case * ns.NSPathNotExistErr :
107
+ case ns.NSPathNotExistErr :
105
108
netNS = nil
106
109
default :
107
110
return nil , errors .Wrapf (err , "failed to open netns at path %q" , args .Netns )
@@ -118,14 +121,10 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
118
121
}
119
122
}
120
123
121
- return & plugin {
124
+ plugin := & plugin {
122
125
NetlinkOps : internal .DefaultNetlinkOps (),
123
-
124
- // TODO(sipsma) support customizing tap name through args
125
-
126
- // TODO(sipsma) support customizing tap uid/gid through args
127
- tapUID : os .Geteuid (),
128
- tapGID : os .Getegid (),
126
+ tapUID : os .Geteuid (),
127
+ tapGID : os .Getegid (),
129
128
130
129
// given the use case of supporting VMs, we call the "containerID" a "vmID"
131
130
vmID : args .ContainerID ,
@@ -135,7 +134,33 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
135
134
netNS : netNS ,
136
135
137
136
currentResult : currentResult ,
138
- }, nil
137
+ }
138
+ parsedArgs , err := extractArgs (args .Args )
139
+ if err != nil {
140
+ return nil , err
141
+ }
142
+
143
+ if tapName , wasDefined := parsedArgs [pluginargs .TCRedirectTapName ]; wasDefined {
144
+ plugin .tapName = tapName
145
+ }
146
+
147
+ if tapUIDVal , wasDefined := parsedArgs [pluginargs .TCRedirectTapUID ]; wasDefined {
148
+ tapUID , err := strconv .Atoi (tapUIDVal )
149
+ if err != nil {
150
+ return nil , errors .Wrapf (err , "tapUID should be numeric convertible, got %q" , tapUIDVal )
151
+ }
152
+ plugin .tapUID = tapUID
153
+ }
154
+
155
+ if tapGIDVal , wasDefined := parsedArgs [pluginargs .TCRedirectTapGUID ]; wasDefined {
156
+ tapGID , err := strconv .Atoi (tapGIDVal )
157
+ if err != nil {
158
+ return nil , errors .Wrapf (err , "tapGID should be numeric convertible, got %q" , tapGIDVal )
159
+ }
160
+ plugin .tapGID = tapGID
161
+ }
162
+
163
+ return plugin , nil
139
164
}
140
165
141
166
func getCurrentResult (args * skel.CmdArgs ) (* current.Result , error ) {
@@ -376,3 +401,21 @@ type NoPreviousResultError struct{}
376
401
func (e NoPreviousResultError ) Error () string {
377
402
return "no previous result was found, was this plugin chained with a previous one?"
378
403
}
404
+
405
+ // extractArgs returns cli args in form of map of strings
406
+ // args string - cli args string("key1=val1;key2=val2)
407
+ func extractArgs (args string ) (map [string ]string , error ) {
408
+ result := make (map [string ]string )
409
+ if args != "" {
410
+ argumentsPairs := strings .Split (args , ";" )
411
+ for _ , pairStr := range argumentsPairs {
412
+ pair := strings .SplitN (pairStr , "=" , 2 )
413
+ if len (pair ) < 2 {
414
+ return result , errors .Errorf ("Invalid cni arguments format, %q" , pairStr )
415
+ }
416
+ result [pair [0 ]] = pair [1 ]
417
+ }
418
+ }
419
+
420
+ return result , nil
421
+ }
0 commit comments