pkg/multus: downcast empty CNI result to requested cniVersion#1505
pkg/multus: downcast empty CNI result to requested cniVersion#1505SAY-5 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request addresses a panic (issue #1497) in CmdAdd when a pod is not found by ensuring the returned empty CNI result is downcast to the correct CNIVersion before being returned. A regression test has been added to verify the fix for CNI version 0.4.0. Feedback suggests optimizing the conversion logic by only performing it for versions prior to 1.0.0 to avoid redundant calls when the result is already in the correct format.
| versioned, convErr := emptyResult.GetAsVersion(n.CNIVersion) | ||
| if convErr != nil { | ||
| return nil, cmdErr(k8sArgs, "failed to downcast empty CNI result to cniVersion %q: %v", n.CNIVersion, convErr) | ||
| } | ||
| return versioned, nil |
There was a problem hiding this comment.
While this fix correctly addresses the panic by ensuring the concrete type of the result matches the requested cniVersion, calling GetAsVersion for 1.x versions is redundant since emptyCNIResult already returns a *cni100.Result. For performance in this error path, it's negligible, but for code clarity, you might consider only performing the conversion if the version is less than 1.0.0.
@gemini-code-assist suggestion on k8snetworkplumbingwg#1505: emptyCNIResult already returns a *cni100.Result, so GetAsVersion is a no-op (or worse, an unnecessary allocation) for 1.x configs. Gate the conversion on non-1.x cniVersions only; 1.x callers now return the *cni100.Result directly as they did before the panic fix. Behaviour for 0.3.x / 0.4.0 is unchanged: still downcast so skel's conversion chain dispatches convertFrom04x correctly. Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
|
Thanks @gemini-code-assist, good call. Gated the |
|
That sounds like a solid approach. By gating the |
|
Thanks @gemini-code-assist, applied your suggestion. GetAsVersion is now gated on |
|
That sounds like a great resolution. By gating the |
trozet
left a comment
There was a problem hiding this comment.
should also squash the 2 commits into 1
| // conversion path calls the right convertFrom*/To* converter; | ||
| // otherwise convertFrom04x's type assertion to *types040.Result | ||
| // panics (#1497). | ||
| if n.CNIVersion != "" && !strings.HasPrefix(n.CNIVersion, "1.") { |
There was a problem hiding this comment.
This check is not ideal because:
- it is not future proof, if cni 2.0 comes along this breaks
- it can produce a false positive if for example the version was 0.1.0
I would suggest using the functions already exposed in the CNI library:
GreaterThanOrEqualTo
0e4207a to
ac01d8b
Compare
|
Squashed into one commit per @trozet's review. Thanks! |
|
@trozet The branch is currently a single commit ( |
|
Fixed: now using cniversion.GreaterThanOrEqualTo(n.CNIVersion, "1.0.0") matching the pattern already used in confStatus/conflistStatus. This handles 0.1.0 correctly and is forward-compatible with CNI 2.x. |
b85e555 to
f965e5d
Compare
|
Squashed the two commits into one. Single commit now: fix(multus): downcast empty CNI result to requested cniVersion. |
|
@SAY-5 There is an issue in your PR. You're removing the checksums in the GHA files. |
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
f965e5d to
94af75f
Compare
|
Thanks for catching that. The branch had drifted behind master and an old rebase baked the pre-checksum workflow files into the commit, so they showed up as removals. I rebased onto current master and restored the workflows, so the diff is back to just the two pkg/multus files. |
Summary
Fixes #1497.
emptyCNIResultalways returns a*cni100.Result. When the pod-not-found branch setCNIVersionto0.3.1/0.4.0and handed that to skel, the containernetworking/cni conversion chain dispatched toconvertFrom04x, which type-asserts the incomingtypes.Resultto*types040.Resultand panics because we gave it a*cni100.Result.The earlier fix ccfd8f5 hardcoded
1.0.0to paper over the class mismatch; 921191d reverted that so the response would round-trip through the user's configured version again. The panic regressed along with the revert.Fix
Call
emptyResult.GetAsVersion(n.CNIVersion)so the returned Result's concrete type matches whateverconvertFrom*/convertTo*the caller will run:1.xconfigs the Result stays*cni100.Result(existing1.1.0test still asserts on that type).0.3.x/0.4.0configs it now returns a*types040.ResultthatconvertFrom04xcan safely assert.If
n.CNIVersionis unset, we keep the existing behaviour (return*cni100.Resultas-is).Tests
Adds
It("returns empty add result downcast to 0.4.0 cniVersion when pod is not found", ...)that drives pod-not-found throughCmdAddwithcniVersion: 0.4.0and asserts the returned Result is a*types040.ResultwithCNIVersion "0.4.0".go vet ./pkg/multus/...clean underGOOS=linux; the existing1.1.0variant stays green.