| 1 | // Copyright 2015 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package allocator |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "net" |
| 21 | |
| 22 | "github.com/containernetworking/cni/pkg/types" |
| 23 | "github.com/containernetworking/cni/pkg/version" |
| 24 | "github.com/containernetworking/plugins/pkg/ip" |
| 25 | ) |
| 26 | |
| 27 | // The top-level network config - IPAM plugins are passed the full configuration |
| 28 | // of the calling plugin, not just the IPAM section. |
| 29 | type Net struct { |
| 30 | Name string `json:"name"` |
| 31 | CNIVersion string `json:"cniVersion"` |
| 32 | IPAM *IPAMConfig `json:"ipam"` |
| 33 | RuntimeConfig struct { |
| 34 | // The capability arg |
| 35 | IPRanges []RangeSet `json:"ipRanges,omitempty"` |
| 36 | IPs []*ip.IP `json:"ips,omitempty"` |
| 37 | } `json:"runtimeConfig,omitempty"` |
| 38 | Args *struct { |
| 39 | A *IPAMArgs `json:"cni"` |
| 40 | } `json:"args"` |
| 41 | } |
| 42 | |
| 43 | // IPAMConfig represents the IP related network configuration. |
| 44 | // This nests Range because we initially only supported a single |
| 45 | // range directly, and wish to preserve backwards compatibility |
| 46 | type IPAMConfig struct { |
| 47 | *Range |
| 48 | Name string |
| 49 | Type string `json:"type"` |
| 50 | Routes []*types.Route `json:"routes"` |
| 51 | DataDir string `json:"dataDir"` |
| 52 | ResolvConf string `json:"resolvConf"` |
| 53 | Ranges []RangeSet `json:"ranges"` |
| 54 | IPArgs []net.IP `json:"-"` // Requested IPs from CNI_ARGS, args and capabilities |
| 55 | } |
| 56 | |
| 57 | type IPAMEnvArgs struct { |
| 58 | types.CommonArgs |
| 59 | IP ip.IP `json:"ip,omitempty"` |
| 60 | } |
| 61 | |
| 62 | type IPAMArgs struct { |
| 63 | IPs []*ip.IP `json:"ips"` |
| 64 | } |
| 65 | |
| 66 | type RangeSet []Range |
| 67 | |
| 68 | type Range struct { |
| 69 | RangeStart net.IP `json:"rangeStart,omitempty"` // The first ip, inclusive |
| 70 | RangeEnd net.IP `json:"rangeEnd,omitempty"` // The last ip, inclusive |
| 71 | Subnet types.IPNet `json:"subnet"` |
| 72 | Gateway net.IP `json:"gateway,omitempty"` |
| 73 | } |
| 74 | |
| 75 | // NewIPAMConfig creates a NetworkConfig from the given network name. |
| 76 | func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) { |
| 77 | n := Net{} |
| 78 | if err := json.Unmarshal(bytes, &n); err != nil { |
| 79 | return nil, "", err |
| 80 | } |
| 81 | |
| 82 | if n.IPAM == nil { |
| 83 | return nil, "", fmt.Errorf("IPAM config missing 'ipam' key") |
| 84 | } |
| 85 | |
| 86 | // parse custom IP from env args |
| 87 | if envArgs != "" { |
| 88 | e := IPAMEnvArgs{} |
| 89 | err := types.LoadArgs(envArgs, &e) |
| 90 | if err != nil { |
| 91 | return nil, "", err |
| 92 | } |
| 93 | |
| 94 | if e.IP.ToIP() != nil { |
| 95 | n.IPAM.IPArgs = []net.IP{e.IP.ToIP()} |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // parse custom IPs from CNI args in network config |
| 100 | if n.Args != nil && n.Args.A != nil && len(n.Args.A.IPs) != 0 { |
| 101 | for _, i := range n.Args.A.IPs { |
| 102 | n.IPAM.IPArgs = append(n.IPAM.IPArgs, i.ToIP()) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // parse custom IPs from runtime configuration |
| 107 | if len(n.RuntimeConfig.IPs) > 0 { |
| 108 | for _, i := range n.RuntimeConfig.IPs { |
| 109 | n.IPAM.IPArgs = append(n.IPAM.IPArgs, i.ToIP()) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | for idx := range n.IPAM.IPArgs { |
| 114 | if err := canonicalizeIP(&n.IPAM.IPArgs[idx]); err != nil { |
| 115 | return nil, "", fmt.Errorf("cannot understand ip: %v", err) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // If a single range (old-style config) is specified, prepend it to |
| 120 | // the Ranges array |
| 121 | if n.IPAM.Range != nil && n.IPAM.Range.Subnet.IP != nil { |
| 122 | n.IPAM.Ranges = append([]RangeSet{{*n.IPAM.Range}}, n.IPAM.Ranges...) |
| 123 | } |
| 124 | n.IPAM.Range = nil |
| 125 | |
| 126 | // If a range is supplied as a runtime config, prepend it to the Ranges |
| 127 | if len(n.RuntimeConfig.IPRanges) > 0 { |
| 128 | n.IPAM.Ranges = append(n.RuntimeConfig.IPRanges, n.IPAM.Ranges...) |
| 129 | } |
| 130 | |
| 131 | if len(n.IPAM.Ranges) == 0 { |
| 132 | return nil, "", fmt.Errorf("no IP ranges specified") |
| 133 | } |
| 134 | |
| 135 | // Validate all ranges |
| 136 | numV4 := 0 |
| 137 | numV6 := 0 |
| 138 | for i := range n.IPAM.Ranges { |
| 139 | if err := n.IPAM.Ranges[i].Canonicalize(); err != nil { |
| 140 | return nil, "", fmt.Errorf("invalid range set %d: %s", i, err) |
| 141 | } |
| 142 | |
| 143 | if n.IPAM.Ranges[i][0].RangeStart.To4() != nil { |
| 144 | numV4++ |
| 145 | } else { |
| 146 | numV6++ |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // CNI spec 0.2.0 and below supported only one v4 and v6 address |
| 151 | if numV4 > 1 || numV6 > 1 { |
| 152 | if ok, _ := version.GreaterThanOrEqualTo(n.CNIVersion, "0.3.0"); !ok { |
| 153 | return nil, "", fmt.Errorf("CNI version %v does not support more than 1 address per family", n.CNIVersion) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Check for overlaps |
| 158 | l := len(n.IPAM.Ranges) |
| 159 | for i, p1 := range n.IPAM.Ranges[:l-1] { |
| 160 | for j, p2 := range n.IPAM.Ranges[i+1:] { |
| 161 | if p1.Overlaps(&p2) { |
| 162 | return nil, "", fmt.Errorf("range set %d overlaps with %d", i, (i + j + 1)) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Copy net name into IPAM so not to drag Net struct around |
| 168 | n.IPAM.Name = n.Name |
| 169 | |
| 170 | return n.IPAM, n.CNIVersion, nil |
| 171 | } |