From 1d534255a733f840c89a92470b7cebb4ff2ff1e5 Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Thu, 21 Nov 2024 18:33:08 -0800 Subject: [PATCH 1/4] feat: Use MAC address provided by nerdctl The nerdctl `--mac-address` command line passes the address to the CNI plugin with the `MAC` arg. See `getCNINamespaceOpts`: https://github.com/containerd/nerdctl/blob/main/pkg/containerutil/container_network_manager_windows.go#L184-L186 --- cni/cni.go | 16 ++++++++++++++++ common/core/network.go | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cni/cni.go b/cni/cni.go index 4a58dcc1..67586440 100644 --- a/cni/cni.go +++ b/cni/cni.go @@ -105,6 +105,11 @@ type K8SPodEnvArgs struct { K8S_POD_INFRA_CONTAINER_ID cniTypes.UnmarshallableString `json:"K8S_POD_INFRA_CONTAINER_ID,omitempty"` } +type EndpointArgs struct { + cniTypes.CommonArgs + MAC cniTypes.UnmarshallableString +} + type OptionalFlags struct { LocalRoutePortMapping bool `json:"localRoutedPortMapping"` AllowAclPortMapping bool `json:"allowAclPortMapping"` @@ -194,6 +199,17 @@ func ParseCniArgs(args string) (*K8SPodEnvArgs, error) { return &podConfig, nil } +// ParseCniEndpointArgs +func ParseCniEndpointArgs(args string) (*EndpointArgs, error) { + epArgs := EndpointArgs{} + err := cniTypes.LoadArgs(args, &epArgs) + if err != nil { + return nil, err + } + + return &epArgs, nil +} + // Serialize marshals a network configuration to bytes. func (config *NetworkConfig) Serialize() []byte { bytes, _ := json.Marshal(config) diff --git a/common/core/network.go b/common/core/network.go index d15dd2a4..a8fa157c 100644 --- a/common/core/network.go +++ b/common/core/network.go @@ -105,6 +105,12 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) { k8sNamespace = string(podConfig.K8S_POD_NAMESPACE) } + epConfig, err := cni.ParseCniEndpointArgs(args.Args) + if err != nil { + logrus.Errorf("[cni-net] Failed to parse endpoint args, err:%v", err) + return err + } + // Parse network configuration from stdin. cniConfig, err := cni.ParseNetworkConfig(args.StdinData) if err != nil { @@ -133,6 +139,15 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) { return err } + if epConfig.MAC != "" { + hwAddr, err := net.ParseMAC(string(epConfig.MAC)) + if err != nil { + logrus.Errorf("[cni-net] Failed to parse MAC addres '%s', err:%v", epConfig.MAC, err) + return err + } + + epInfo.MacAddress = hwAddr + } epInfo.DualStack = cniConfig.OptionalFlags.EnableDualStack // Check for missing namespace From 1c75a46d75a81377db9a44fc76abc7da132de48b Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Thu, 21 Nov 2024 20:29:39 -0800 Subject: [PATCH 2/4] Only handle valid CNI_ARGS If arguments fail to parse, continue without additional endpoint configuration. --- common/core/network.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/common/core/network.go b/common/core/network.go index a8fa157c..3a6e880f 100644 --- a/common/core/network.go +++ b/common/core/network.go @@ -105,12 +105,6 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) { k8sNamespace = string(podConfig.K8S_POD_NAMESPACE) } - epConfig, err := cni.ParseCniEndpointArgs(args.Args) - if err != nil { - logrus.Errorf("[cni-net] Failed to parse endpoint args, err:%v", err) - return err - } - // Parse network configuration from stdin. cniConfig, err := cni.ParseNetworkConfig(args.StdinData) if err != nil { @@ -139,14 +133,17 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) { return err } - if epConfig.MAC != "" { - hwAddr, err := net.ParseMAC(string(epConfig.MAC)) - if err != nil { - logrus.Errorf("[cni-net] Failed to parse MAC addres '%s', err:%v", epConfig.MAC, err) - return err - } + epConfig, err := cni.ParseCniEndpointArgs(args.Args) + if err == nil { + if epConfig.MAC != "" { + hwAddr, err := net.ParseMAC(string(epConfig.MAC)) + if err != nil { + logrus.Errorf("[cni-net] Failed to parse MAC addres '%s', err:%v", epConfig.MAC, err) + return err + } - epInfo.MacAddress = hwAddr + epInfo.MacAddress = hwAddr + } } epInfo.DualStack = cniConfig.OptionalFlags.EnableDualStack From 4046edbdc04d58d842b03202076fc0098e6151b1 Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Tue, 8 Apr 2025 23:16:05 -0700 Subject: [PATCH 3/4] Fix addres/address typo --- common/core/network.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/core/network.go b/common/core/network.go index 3a6e880f..9bed1732 100644 --- a/common/core/network.go +++ b/common/core/network.go @@ -138,7 +138,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) { if epConfig.MAC != "" { hwAddr, err := net.ParseMAC(string(epConfig.MAC)) if err != nil { - logrus.Errorf("[cni-net] Failed to parse MAC addres '%s', err:%v", epConfig.MAC, err) + logrus.Errorf("[cni-net] Failed to parse MAC address '%s', err:%v", epConfig.MAC, err) return err } From 06becdd0d5202dd36fd5dcc7d5e277fad9e25eb0 Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Tue, 8 Apr 2025 23:16:49 -0700 Subject: [PATCH 4/4] Remove superfluous comment --- cni/cni.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cni/cni.go b/cni/cni.go index 67586440..7473a518 100644 --- a/cni/cni.go +++ b/cni/cni.go @@ -199,7 +199,6 @@ func ParseCniArgs(args string) (*K8SPodEnvArgs, error) { return &podConfig, nil } -// ParseCniEndpointArgs func ParseCniEndpointArgs(args string) (*EndpointArgs, error) { epArgs := EndpointArgs{} err := cniTypes.LoadArgs(args, &epArgs)