Skip to content

Commit

Permalink
Merge pull request #106 from sondavidb/report-correct-version
Browse files Browse the repository at this point in the history
Return correct CNI version string
  • Loading branch information
sondavidb authored Jan 11, 2025
2 parents f2644ce + 5f807a8 commit fb2c2b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
27 changes: 18 additions & 9 deletions cmd/tc-redirect-tap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func add(args *skel.CmdArgs) error {
return err
}

return types.PrintResult(p.currentResult, p.currentResult.CNIVersion)
return types.PrintResult(p.currentResult, p.confCNIVersion)
}

func del(args *skel.CmdArgs) error {
Expand Down Expand Up @@ -116,7 +116,7 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
}
}

currentResult, err := getCurrentResult(args)
currentResult, confCNIVersion, err := getCurrentResult(args)
if err != nil {
switch err.(type) {
case *NoPreviousResultError:
Expand All @@ -138,7 +138,8 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {

netNS: netNS,

currentResult: currentResult,
currentResult: currentResult,
confCNIVersion: confCNIVersion,
}
parsedArgs, err := extractArgs(args.Args)
if err != nil {
Expand Down Expand Up @@ -168,29 +169,34 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
return plugin, nil
}

func getCurrentResult(args *skel.CmdArgs) (*current.Result, error) {
func getCurrentResult(args *skel.CmdArgs) (*current.Result, string, error) {
// parse the previous CNI result (or throw an error if there wasn't one)
cniConf := types.NetConf{}
err := json.Unmarshal(args.StdinData, &cniConf)
if err != nil {
return nil, fmt.Errorf("failure checking for previous result output: %w", err)
return nil, "", fmt.Errorf("failure checking for previous result output: %w", err)
}

// Store the CNI version here, since current.NewResultFromResult will
// change the CNI version to ImplementedSpecVersion, which is usually
// the latest CNI version, not the version specified in the conf.
confCNIVersion := cniConf.CNIVersion

err = version.ParsePrevResult(&cniConf)
if err != nil {
return nil, fmt.Errorf("failed to parse previous CNI result: %w", err)
return nil, "", fmt.Errorf("failed to parse previous CNI result: %w", err)
}

if cniConf.PrevResult == nil {
return nil, &NoPreviousResultError{}
return nil, "", &NoPreviousResultError{}
}

currentResult, err := current.NewResultFromResult(cniConf.PrevResult)
if err != nil {
return nil, fmt.Errorf("failed to generate current result from previous CNI result: %w", err)
return nil, "", fmt.Errorf("failed to generate current result from previous CNI result: %w", err)
}

return currentResult, nil
return currentResult, confCNIVersion, nil
}

type plugin struct {
Expand Down Expand Up @@ -223,6 +229,9 @@ type plugin struct {
// currentResult is the CNI result object, initialized to the previous CNI
// result if there was any or to nil if there was no previous result provided
currentResult *current.Result

// confCniVersion is the CNI version specified by the conflist passed to tc-redirect-tap
confCNIVersion string
}

func (p plugin) add() error {
Expand Down
8 changes: 5 additions & 3 deletions cmd/tc-redirect-tap/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,14 @@ func TestAddFailsCreateTapErr(t *testing.T) {

func TestGetCurrentResult(t *testing.T) {
expectedResult := defaultTestPlugin().currentResult
expectedCNIVersion := "0.3.1"

netConf := &types.NetConf{
CNIVersion: "0.3.1",
CNIVersion: expectedCNIVersion,
Name: "my-lil-network",
Type: "my-lil-plugin",
RawPrevResult: map[string]interface{}{
"cniVersion": "0.3.1",
"cniVersion": expectedCNIVersion,
"interfaces": expectedResult.Interfaces,
"ips": expectedResult.IPs,
"routes": expectedResult.Routes,
Expand All @@ -216,10 +217,11 @@ func TestGetCurrentResult(t *testing.T) {
StdinData: rawPrevResultBytes,
}

actualResult, err := getCurrentResult(cmdArgs)
actualResult, actualCNIVersion, err := getCurrentResult(cmdArgs)
require.NoError(t, err, "failed to get current result from mock net conf")

assert.Equal(t, expectedResult, actualResult)
assert.Equal(t, expectedCNIVersion, actualCNIVersion)
}

func TestDel(t *testing.T) {
Expand Down

0 comments on commit fb2c2b5

Please sign in to comment.