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..3a6e880f 100644 --- a/common/core/network.go +++ b/common/core/network.go @@ -133,6 +133,18 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) { 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.DualStack = cniConfig.OptionalFlags.EnableDualStack // Check for missing namespace