Debian Codegraph @

plugins/ipam/host-local/main.go

raw symbols SCIP
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 main
16
17 import (
18 "errors"
19 "fmt"
20 "net"
21 "strings"
22
23 "github.com/containernetworking/cni/pkg/skel"
24 "github.com/containernetworking/cni/pkg/types"
25 current "github.com/containernetworking/cni/pkg/types/100"
26 "github.com/containernetworking/cni/pkg/version"
27 bv "github.com/containernetworking/plugins/pkg/utils/buildversion"
28 "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator"
29 "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk"
30 )
31
32 func main() {
33 skel.PluginMainFuncs(skel.CNIFuncs{
34 Add: cmdAdd,
35 Check: cmdCheck,
36 Del: cmdDel,
37 /* FIXME GC */
38 /* FIXME Status */
39 }, version.All, bv.BuildString("host-local"))
40 }
41
42 func cmdCheck(args *skel.CmdArgs) error {
43 ipamConf, _, err := allocator.LoadIPAMConfig(args.StdinData, args.Args)
44 if err != nil {
45 return err
46 }
47
48 // Look to see if there is at least one IP address allocated to the container
49 // in the data dir, irrespective of what that address actually is
50 store, err := disk.New(ipamConf.Name, ipamConf.DataDir)
51 if err != nil {
52 return err
53 }
54 defer store.Close()
55
56 containerIPFound := store.FindByID(args.ContainerID, args.IfName)
57 if !containerIPFound {
58 return fmt.Errorf("host-local: Failed to find address added by container %v", args.ContainerID)
59 }
60
61 return nil
62 }
63
64 func cmdAdd(args *skel.CmdArgs) error {
65 ipamConf, confVersion, err := allocator.LoadIPAMConfig(args.StdinData, args.Args)
66 if err != nil {
67 return err
68 }
69
70 result := &current.Result{CNIVersion: current.ImplementedSpecVersion}
71
72 if ipamConf.ResolvConf != "" {
73 dns, err := parseResolvConf(ipamConf.ResolvConf)
74 if err != nil {
75 return err
76 }
77 result.DNS = *dns
78 }
79
80 store, err := disk.New(ipamConf.Name, ipamConf.DataDir)
81 if err != nil {
82 return err
83 }
84 defer store.Close()
85
86 // Keep the allocators we used, so we can release all IPs if an error
87 // occurs after we start allocating
88 allocs := []*allocator.IPAllocator{}
89
90 // Store all requested IPs in a map, so we can easily remove ones we use
91 // and error if some remain
92 requestedIPs := map[string]net.IP{} // net.IP cannot be a key
93
94 for _, ip := range ipamConf.IPArgs {
95 requestedIPs[ip.String()] = ip
96 }
97
98 for idx, rangeset := range ipamConf.Ranges {
99 allocator := allocator.NewIPAllocator(&rangeset, store, idx)
100
101 // Check to see if there are any custom IPs requested in this range.
102 var requestedIP net.IP
103 for k, ip := range requestedIPs {
104 if rangeset.Contains(ip) {
105 requestedIP = ip
106 delete(requestedIPs, k)
107 break
108 }
109 }
110
111 ipConf, err := allocator.Get(args.ContainerID, args.IfName, requestedIP)
112 if err != nil {
113 // Deallocate all already allocated IPs
114 for _, alloc := range allocs {
115 _ = alloc.Release(args.ContainerID, args.IfName)
116 }
117 return fmt.Errorf("failed to allocate for range %d: %v", idx, err)
118 }
119
120 allocs = append(allocs, allocator)
121
122 result.IPs = append(result.IPs, ipConf)
123 }
124
125 // If an IP was requested that wasn't fulfilled, fail
126 if len(requestedIPs) != 0 {
127 for _, alloc := range allocs {
128 _ = alloc.Release(args.ContainerID, args.IfName)
129 }
130 errstr := "failed to allocate all requested IPs:"
131 for _, ip := range requestedIPs {
132 errstr = errstr + " " + ip.String()
133 }
134 return errors.New(errstr)
135 }
136
137 result.Routes = ipamConf.Routes
138
139 return types.PrintResult(result, confVersion)
140 }
141
142 func cmdDel(args *skel.CmdArgs) error {
143 ipamConf, _, err := allocator.LoadIPAMConfig(args.StdinData, args.Args)
144 if err != nil {
145 return err
146 }
147
148 store, err := disk.New(ipamConf.Name, ipamConf.DataDir)
149 if err != nil {
150 return err
151 }
152 defer store.Close()
153
154 // Loop through all ranges, releasing all IPs, even if an error occurs
155 var errs []string
156 for idx, rangeset := range ipamConf.Ranges {
157 ipAllocator := allocator.NewIPAllocator(&rangeset, store, idx)
158
159 err := ipAllocator.Release(args.ContainerID, args.IfName)
160 if err != nil {
161 errs = append(errs, err.Error())
162 }
163 }
164
165 if errs != nil {
166 return errors.New(strings.Join(errs, ";"))
167 }
168 return nil
169 }