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 <caoruidong@huawei.com>
This commit is contained in:
Ruidong Cao 2019-02-21 02:26:06 +08:00
parent 22cee2d0cd
commit 36141d27fc

View File

@ -234,88 +234,50 @@ func generateEndpoints(typedEndpoints []TypedJSONEndpoint) ([]Endpoint, error) {
var endpoints []Endpoint var endpoints []Endpoint
for _, e := range typedEndpoints { for _, e := range typedEndpoints {
var endpointInf Endpoint
switch e.Type { switch e.Type {
case PhysicalEndpointType: case PhysicalEndpointType:
var endpoint PhysicalEndpoint var endpoint PhysicalEndpoint
err := json.Unmarshal(e.Data, &endpoint) endpointInf = &endpoint
if err != nil {
return nil, err
}
endpoints = append(endpoints, &endpoint)
networkLogger().WithFields(logrus.Fields{
"endpoint": endpoint,
"endpoint-type": "physical",
}).Info("endpoint unmarshalled")
case VethEndpointType: case VethEndpointType:
var endpoint VethEndpoint var endpoint VethEndpoint
err := json.Unmarshal(e.Data, &endpoint) endpointInf = &endpoint
if err != nil {
return nil, err
}
endpoints = append(endpoints, &endpoint)
networkLogger().WithFields(logrus.Fields{
"endpoint": endpoint,
"endpoint-type": "virtual",
}).Info("endpoint unmarshalled")
case VhostUserEndpointType: case VhostUserEndpointType:
var endpoint VhostUserEndpoint var endpoint VhostUserEndpoint
err := json.Unmarshal(e.Data, &endpoint) endpointInf = &endpoint
if err != nil {
return nil, err
}
endpoints = append(endpoints, &endpoint)
networkLogger().WithFields(logrus.Fields{
"endpoint": endpoint,
"endpoint-type": "vhostuser",
}).Info("endpoint unmarshalled")
case BridgedMacvlanEndpointType: case BridgedMacvlanEndpointType:
var endpoint BridgedMacvlanEndpoint var endpoint BridgedMacvlanEndpoint
err := json.Unmarshal(e.Data, &endpoint) endpointInf = &endpoint
if err != nil {
return nil, err
}
endpoints = append(endpoints, &endpoint)
networkLogger().WithFields(logrus.Fields{
"endpoint": endpoint,
"endpoint-type": "macvlan",
}).Info("endpoint unmarshalled")
case MacvtapEndpointType: case MacvtapEndpointType:
var endpoint MacvtapEndpoint var endpoint MacvtapEndpoint
err := json.Unmarshal(e.Data, &endpoint) endpointInf = &endpoint
if err != nil {
return nil, err
}
endpoints = append(endpoints, &endpoint)
networkLogger().WithFields(logrus.Fields{
"endpoint": endpoint,
"endpoint-type": "macvtap",
}).Info("endpoint unmarshalled")
case TapEndpointType: case TapEndpointType:
var endpoint TapEndpoint var endpoint TapEndpoint
err := json.Unmarshal(e.Data, &endpoint) endpointInf = &endpoint
if err != nil {
return nil, err
}
endpoints = append(endpoints, &endpoint) case IPVlanEndpointType:
networkLogger().WithFields(logrus.Fields{ var endpoint IPVlanEndpoint
"endpoint": endpoint, endpointInf = &endpoint
"endpoint-type": "tap",
}).Info("endpoint unmarshalled")
default: default:
networkLogger().WithField("endpoint-type", e.Type).Error("Ignoring unknown endpoint type") 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 return endpoints, nil
} }