From 36141d27fc1c7afcdeaa38de34dda1e1ec994a5a Mon Sep 17 00:00:00 2001 From: Ruidong Cao Date: Thu, 21 Feb 2019 02:26:06 +0800 Subject: [PATCH] Network: add ipvlan unmarshal We have 7 types of endpoints, but forget ipvlan in unmarshal funciton. So add it and refactor for cyclomatic complexity reason. Fixes #1254 Signed-off-by: Ruidong Cao --- virtcontainers/network.go | 80 ++++++++++----------------------------- 1 file changed, 21 insertions(+), 59 deletions(-) diff --git a/virtcontainers/network.go b/virtcontainers/network.go index 01a666c8a0..93b2f11957 100644 --- a/virtcontainers/network.go +++ b/virtcontainers/network.go @@ -234,88 +234,50 @@ func generateEndpoints(typedEndpoints []TypedJSONEndpoint) ([]Endpoint, error) { var endpoints []Endpoint for _, e := range typedEndpoints { + var endpointInf Endpoint switch e.Type { case PhysicalEndpointType: var endpoint PhysicalEndpoint - err := json.Unmarshal(e.Data, &endpoint) - if err != nil { - return nil, err - } - - endpoints = append(endpoints, &endpoint) - networkLogger().WithFields(logrus.Fields{ - "endpoint": endpoint, - "endpoint-type": "physical", - }).Info("endpoint unmarshalled") + endpointInf = &endpoint case VethEndpointType: var endpoint VethEndpoint - err := json.Unmarshal(e.Data, &endpoint) - if err != nil { - return nil, err - } - - endpoints = append(endpoints, &endpoint) - networkLogger().WithFields(logrus.Fields{ - "endpoint": endpoint, - "endpoint-type": "virtual", - }).Info("endpoint unmarshalled") + endpointInf = &endpoint case VhostUserEndpointType: var endpoint VhostUserEndpoint - err := json.Unmarshal(e.Data, &endpoint) - if err != nil { - return nil, err - } - - endpoints = append(endpoints, &endpoint) - networkLogger().WithFields(logrus.Fields{ - "endpoint": endpoint, - "endpoint-type": "vhostuser", - }).Info("endpoint unmarshalled") + endpointInf = &endpoint case BridgedMacvlanEndpointType: var endpoint BridgedMacvlanEndpoint - err := json.Unmarshal(e.Data, &endpoint) - if err != nil { - return nil, err - } - - endpoints = append(endpoints, &endpoint) - networkLogger().WithFields(logrus.Fields{ - "endpoint": endpoint, - "endpoint-type": "macvlan", - }).Info("endpoint unmarshalled") + endpointInf = &endpoint case MacvtapEndpointType: var endpoint MacvtapEndpoint - err := json.Unmarshal(e.Data, &endpoint) - if err != nil { - return nil, err - } - - endpoints = append(endpoints, &endpoint) - networkLogger().WithFields(logrus.Fields{ - "endpoint": endpoint, - "endpoint-type": "macvtap", - }).Info("endpoint unmarshalled") + endpointInf = &endpoint case TapEndpointType: var endpoint TapEndpoint - err := json.Unmarshal(e.Data, &endpoint) - if err != nil { - return nil, err - } + endpointInf = &endpoint - endpoints = append(endpoints, &endpoint) - networkLogger().WithFields(logrus.Fields{ - "endpoint": endpoint, - "endpoint-type": "tap", - }).Info("endpoint unmarshalled") + case IPVlanEndpointType: + var endpoint IPVlanEndpoint + endpointInf = &endpoint default: networkLogger().WithField("endpoint-type", e.Type).Error("Ignoring unknown endpoint type") } + + err := json.Unmarshal(e.Data, endpointInf) + if err != nil { + return nil, err + } + + endpoints = append(endpoints, endpointInf) + networkLogger().WithFields(logrus.Fields{ + "endpoint": endpointInf, + "endpoint-type": e.Type, + }).Info("endpoint unmarshalled") } return endpoints, nil }