|
6 | 6 | package main |
7 | 7 |
|
8 | 8 | import ( |
9 | | - "encoding/json" |
10 | | - "fmt" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
11 | 11 |
|
12 | | - "github.com/containernetworking/cni/pkg/types" |
13 | | - current "github.com/containernetworking/cni/pkg/types/100" |
| 12 | + "github.com/containernetworking/cni/pkg/types" |
| 13 | + current "github.com/containernetworking/cni/pkg/types/100" |
| 14 | + "github.com/vishvananda/netlink" |
14 | 15 | ) |
15 | 16 |
|
16 | 17 | // MACVLANNetConfig defines macvlan net config |
17 | 18 | type MACVLANNetConfig struct { |
18 | | - types.NetConf |
19 | | - MainPlugin MACVLANTypeNetConf `json:"plugin"` |
| 19 | + types.NetConf |
| 20 | + MainPlugin MACVLANTypeNetConf `json:"plugin"` |
20 | 21 | } |
21 | 22 |
|
22 | 23 | type MACVLANTypeNetConf struct { |
23 | | - types.NetConf |
24 | | - Master string `json:"master"` |
25 | | - Mode string `json:"mode"` |
26 | | - MTU int `json:"mtu"` |
| 24 | + types.NetConf |
| 25 | + Master string `json:"master"` |
| 26 | + Mode string `json:"mode"` |
| 27 | + MTU int `json:"mtu"` |
27 | 28 | } |
28 | 29 |
|
29 | 30 | // loadMACVLANConf unmarshals to MACVLANNetConfig and returns a list of MACVLAN configs |
30 | | -func loadMACVLANConf(bytes []byte, ifName string, n *NetConf, ipConfigs []*current.IPConfig) ([][]byte, error) { |
31 | | - confBytesArray := [][]byte{} |
| 31 | +func loadMACVLANConf(bytes []byte, ifName string, n *NetConf, ipConfigs []*current.IPConfig) (confBytesArray [][]byte, multiPathRoutes map[string][]*netlink.NexthopInfo, loadError error) { |
| 32 | + confBytesArray = [][]byte{} |
32 | 33 |
|
33 | | - configInMACVLAN := &MACVLANNetConfig{} |
34 | | - if err := json.Unmarshal(bytes, configInMACVLAN); err != nil { |
35 | | - return confBytesArray, err |
36 | | - } |
| 34 | + configInMACVLAN := &MACVLANNetConfig{} |
| 35 | + if err := json.Unmarshal(bytes, configInMACVLAN); err != nil { |
| 36 | + loadError = err |
| 37 | + return |
| 38 | + } |
37 | 39 |
|
38 | | - // Interfaces are orderly assigned from the interface set |
39 | | - for index, masterName := range n.Masters { |
40 | | - if masterName == "" { |
41 | | - continue |
42 | | - } |
| 40 | + // Interfaces are orderly assigned from the interface set |
| 41 | + for index, masterName := range n.Masters { |
| 42 | + if masterName == "" { |
| 43 | + continue |
| 44 | + } |
43 | 45 |
|
44 | | - // Add config |
45 | | - singleConfig, err := copyMACVLANConfig(configInMACVLAN.MainPlugin) |
46 | | - if err != nil { |
47 | | - return confBytesArray, err |
48 | | - } |
49 | | - if singleConfig.CNIVersion == "" { |
50 | | - singleConfig.CNIVersion = n.CNIVersion |
51 | | - } |
52 | | - singleConfig.Name = fmt.Sprintf("%s-%d", ifName, index) |
53 | | - singleConfig.Master = masterName |
54 | | - confBytes, err := json.Marshal(singleConfig) |
55 | | - if err != nil { |
56 | | - return confBytesArray, err |
57 | | - } |
| 46 | + // Add config |
| 47 | + singleConfig, err := copyMACVLANConfig(configInMACVLAN.MainPlugin) |
| 48 | + if err != nil { |
| 49 | + loadError = err |
| 50 | + return |
| 51 | + } |
| 52 | + if singleConfig.CNIVersion == "" { |
| 53 | + singleConfig.CNIVersion = n.CNIVersion |
| 54 | + } |
| 55 | + singleConfig.Name = fmt.Sprintf("%s-%d", ifName, index) |
| 56 | + singleConfig.Master = masterName |
| 57 | + confBytes, err := json.Marshal(singleConfig) |
| 58 | + if err != nil { |
| 59 | + loadError = err |
| 60 | + return |
| 61 | + } |
58 | 62 |
|
59 | | - if n.IsMultiNICIPAM { |
60 | | - // Multi-NIC IPAM config |
61 | | - confBytes = injectMultiNicIPAM(confBytes, ipConfigs, index) |
62 | | - } else { |
63 | | - // Single-NIC IPAM config |
64 | | - confBytes = injectSingleNicIPAM(confBytes, bytes) |
65 | | - } |
66 | | - confBytesArray = append(confBytesArray, confBytes) |
67 | | - } |
68 | | - return confBytesArray, nil |
| 63 | + if n.IsMultiNICIPAM { |
| 64 | + // Multi-NIC IPAM config |
| 65 | + confBytes, multiPathRoutes = injectMultiNicIPAM(confBytes, bytes, ipConfigs, index) |
| 66 | + } else { |
| 67 | + // Single-NIC IPAM config |
| 68 | + confBytes, multiPathRoutes = injectSingleNicIPAM(confBytes, bytes) |
| 69 | + } |
| 70 | + confBytesArray = append(confBytesArray, confBytes) |
| 71 | + } |
| 72 | + return |
69 | 73 | } |
70 | 74 |
|
71 | 75 | // copyMACVLANConfig makes a copy of the base MACVLAN config |
72 | 76 | func copyMACVLANConfig(original MACVLANTypeNetConf) (*MACVLANTypeNetConf, error) { |
73 | | - copiedObject := &MACVLANTypeNetConf{} |
74 | | - byteObject, err := json.Marshal(original) |
75 | | - if err != nil { |
76 | | - return copiedObject, err |
77 | | - } |
78 | | - err = json.Unmarshal(byteObject, copiedObject) |
79 | | - if err != nil { |
80 | | - return copiedObject, err |
81 | | - } |
82 | | - return copiedObject, nil |
| 77 | + copiedObject := &MACVLANTypeNetConf{} |
| 78 | + byteObject, err := json.Marshal(original) |
| 79 | + if err != nil { |
| 80 | + return copiedObject, err |
| 81 | + } |
| 82 | + err = json.Unmarshal(byteObject, copiedObject) |
| 83 | + if err != nil { |
| 84 | + return copiedObject, err |
| 85 | + } |
| 86 | + return copiedObject, nil |
83 | 87 | } |
0 commit comments