Skip to content

Commit d05043c

Browse files
authored
Merge pull request #142 from Zyqsempai/131-132-added-tapname-tapuid-gid-args
Added possibility to set tapName, tapUID and tapGID from CNI cmd args
2 parents 5d89999 + f931b11 commit d05043c

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

cni/cmd/tc-redirect-tap/args/args.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package args
2+
3+
const TCRedirectTapName = "TC_REDIRECT_TAP_NAME"
4+
const TCRedirectTapUID = "TC_REDIRECT_TAP_UID"
5+
const TCRedirectTapGUID = "TC_REDIRECT_TAP_GID"

cni/cmd/tc-redirect-tap/main.go

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ package main
1515

1616
import (
1717
"encoding/json"
18+
pluginargs "github.com/firecracker-microvm/firecracker-go-sdk/cni/cmd/tc-redirect-tap/args"
1819
"os"
20+
"strconv"
21+
"strings"
1922

2023
"github.com/containernetworking/cni/pkg/skel"
2124
"github.com/containernetworking/cni/pkg/types"
@@ -101,7 +104,7 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
101104
// It's valid for the netns to no longer exist during DEL commands (in which case DEL is
102105
// a noop). Thus, we leave validating that netNS is not nil to command implementations.
103106
switch err.(type) {
104-
case *ns.NSPathNotExistErr:
107+
case ns.NSPathNotExistErr:
105108
netNS = nil
106109
default:
107110
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) {
118121
}
119122
}
120123

121-
return &plugin{
124+
plugin := &plugin{
122125
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(),
129128

130129
// given the use case of supporting VMs, we call the "containerID" a "vmID"
131130
vmID: args.ContainerID,
@@ -135,7 +134,33 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
135134
netNS: netNS,
136135

137136
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
139164
}
140165

141166
func getCurrentResult(args *skel.CmdArgs) (*current.Result, error) {
@@ -376,3 +401,21 @@ type NoPreviousResultError struct{}
376401
func (e NoPreviousResultError) Error() string {
377402
return "no previous result was found, was this plugin chained with a previous one?"
378403
}
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+
}

cni/cmd/tc-redirect-tap/main_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,47 @@ func TestCheckFails(t *testing.T) {
348348
err := testPlugin.check()
349349
require.Error(t, err, "check should fail when configuration not as expected")
350350
}
351+
352+
func TestNewPlugin(t *testing.T) {
353+
expectedResult := defaultTestPlugin().currentResult
354+
nspath := "/tmp/IDoNotExist"
355+
netConf := &types.NetConf{
356+
CNIVersion: "0.3.1",
357+
Name: "my-lil-network",
358+
Type: "my-lil-plugin",
359+
RawPrevResult: map[string]interface{}{
360+
"cniVersion": "0.3.1",
361+
"interfaces": expectedResult.Interfaces,
362+
"ips": expectedResult.IPs,
363+
"routes": expectedResult.Routes,
364+
"dns": expectedResult.DNS,
365+
},
366+
}
367+
rawPrevResultBytes, err := json.Marshal(netConf)
368+
testArgs := skel.CmdArgs{
369+
ContainerID: "continer-id",
370+
Netns: nspath,
371+
IfName: "test-name",
372+
Args: "TC_REDIRECT_TAP_NAME=tap_name;TC_REDIRECT_TAP_UID=123;TC_REDIRECT_TAP_GID=321",
373+
Path: "",
374+
StdinData: rawPrevResultBytes,
375+
}
376+
377+
plugin, err := newPlugin(&testArgs)
378+
require.NoError(t, err, "failed to create new plugin")
379+
assert.Equal(t, plugin.tapName, "tap_name",
380+
"TC_REDIRECT_TAP_NAME should be equal to `tap_name`")
381+
assert.Equal(t, plugin.tapGID, 321,
382+
"TC_REDIRECT_TAP_NAME should be equal to `321`")
383+
assert.Equal(t, plugin.tapUID, 123,
384+
"TC_REDIRECT_TAP_NAME should be equal to `123`")
385+
}
386+
387+
func TestExtractArgs(t *testing.T) {
388+
cliArgs := "key1=val1;key2=val2"
389+
parsedArgs, err := extractArgs(cliArgs)
390+
require.NoError(t, err,
391+
"failed to extract cli args")
392+
assert.Equal(t, "val2", parsedArgs["key2"],
393+
"Parameter value for key2 should be `val2`")
394+
}

0 commit comments

Comments
 (0)