Skip to content

Commit 0e4207a

Browse files
committed
pkg/multus: skip GetAsVersion conversion when cniVersion is already 1.x
@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>
1 parent dd95690 commit 0e4207a

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

pkg/multus/multus.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,13 @@ func CmdAdd(args *skel.CmdArgs, exec invoke.Exec, kubeClient *k8s.ClientInfo) (c
768768
if stderrors.Is(err, errPodNotFound) {
769769
emptyResult := emptyCNIResult(args, n.CNIVersion)
770770
logging.Verbosef("CmdAdd: Warning: pod [%s/%s] not found, exiting with empty CNI result: %v", k8sArgs.K8S_POD_NAMESPACE, k8sArgs.K8S_POD_NAME, emptyResult)
771-
// Downcast to the types package matching n.CNIVersion so skel's
772-
// response conversion path calls the right convertFrom*/To*
773-
// converter. Returning a *cni100.Result with CNIVersion 0.3.1
774-
// trips convertFrom04x's type assertion to *types040.Result
775-
// and panics (#1497).
776-
if n.CNIVersion != "" {
771+
// emptyCNIResult already returns a *cni100.Result, so 1.x
772+
// configs don't need conversion. For older cniVersions
773+
// (0.3.x / 0.4.0) we must downcast so skel's response
774+
// conversion path calls the right convertFrom*/To* converter;
775+
// otherwise convertFrom04x's type assertion to *types040.Result
776+
// panics (#1497).
777+
if n.CNIVersion != "" && !strings.HasPrefix(n.CNIVersion, "1.") {
777778
versioned, convErr := emptyResult.GetAsVersion(n.CNIVersion)
778779
if convErr != nil {
779780
return nil, cmdErr(k8sArgs, "failed to downcast empty CNI result to cniVersion %q: %v", n.CNIVersion, convErr)

0 commit comments

Comments
 (0)