Skip to content

Commit

Permalink
feat: Use MAC address provided by nerdctl
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mendsley committed Nov 22, 2024
1 parent a0463d8 commit 1d53425
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions common/core/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1d53425

Please sign in to comment.