forked from Azure/azure-container-networking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipam.go
297 lines (243 loc) · 7.66 KB
/
ipam.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package ipam
import (
"encoding/json"
"net"
"strconv"
"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/ipam"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/platform"
cniSkel "github.com/containernetworking/cni/pkg/skel"
cniTypes "github.com/containernetworking/cni/pkg/types"
cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
)
const (
// Plugin name.
name = "azure-vnet-ipam"
)
var (
ipv4DefaultRouteDstPrefix = net.IPNet{net.IPv4zero, net.IPv4Mask(0, 0, 0, 0)}
)
// IpamPlugin represents the CNI IPAM plugin.
type ipamPlugin struct {
*cni.Plugin
am ipam.AddressManager
}
// NewPlugin creates a new ipamPlugin object.
func NewPlugin(config *common.PluginConfig) (*ipamPlugin, error) {
// Setup base plugin.
plugin, err := cni.NewPlugin(name, config.Version)
if err != nil {
return nil, err
}
// Setup address manager.
am, err := ipam.NewAddressManager()
if err != nil {
return nil, err
}
// Create IPAM plugin.
ipamPlg := &ipamPlugin{
Plugin: plugin,
am: am,
}
config.IpamApi = ipamPlg
return ipamPlg, nil
}
// Starts the plugin.
func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
// Initialize base plugin.
err := plugin.Initialize(config)
if err != nil {
log.Printf("[cni-ipam] Failed to initialize base plugin, err:%v.", err)
return err
}
// Log platform information.
log.Printf("[cni-ipam] Plugin %v version %v.", plugin.Name, plugin.Version)
log.Printf("[cni-ipam] Running on %v", platform.GetOSInfo())
// Initialize address manager.
err = plugin.am.Initialize(config, plugin.Options)
if err != nil {
log.Printf("[cni-ipam] Failed to initialize address manager, err:%v.", err)
return err
}
log.Printf("[cni-ipam] Plugin started.")
return nil
}
// Stops the plugin.
func (plugin *ipamPlugin) Stop() {
plugin.am.Uninitialize()
plugin.Uninitialize()
log.Printf("[cni-ipam] Plugin stopped.")
}
// Configure parses and applies the given network configuration.
func (plugin *ipamPlugin) Configure(stdinData []byte) (*cni.NetworkConfig, error) {
// Parse network configuration from stdin.
nwCfg, err := cni.ParseNetworkConfig(stdinData)
if err != nil {
return nil, err
}
log.Printf("[cni-ipam] Read network configuration %+v.", nwCfg)
// Apply IPAM configuration.
// Set deployment environment.
if nwCfg.Ipam.Environment == "" {
nwCfg.Ipam.Environment = common.OptEnvironmentAzure
}
plugin.SetOption(common.OptEnvironment, nwCfg.Ipam.Environment)
// Set query interval.
if nwCfg.Ipam.QueryInterval != "" {
i, _ := strconv.Atoi(nwCfg.Ipam.QueryInterval)
plugin.SetOption(common.OptIpamQueryInterval, i)
}
err = plugin.am.StartSource(plugin.Options)
if err != nil {
return nil, err
}
// Set default address space if not specified.
if nwCfg.Ipam.AddrSpace == "" {
nwCfg.Ipam.AddrSpace = ipam.LocalDefaultAddressSpaceId
}
return nwCfg, nil
}
//
// CNI implementation
// https://github.com/containernetworking/cni/blob/master/SPEC.md
//
// Add handles CNI add commands.
func (plugin *ipamPlugin) Add(args *cniSkel.CmdArgs) error {
var result *cniTypesCurr.Result
var err error
log.Printf("[cni-ipam] Processing ADD command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}.",
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path)
defer func() { log.Printf("[cni-ipam] ADD command completed with result:%+v err:%v.", result, err) }()
// Parse network configuration from stdin.
nwCfg, err := plugin.Configure(args.StdinData)
if err != nil {
err = plugin.Errorf("Failed to parse network configuration: %v", err)
return err
}
// Check if an address pool is specified.
if nwCfg.Ipam.Subnet == "" {
var poolID string
var subnet string
// Select the requested interface.
options := make(map[string]string)
options[ipam.OptInterfaceName] = nwCfg.Master
// Allocate an address pool.
poolID, subnet, err = plugin.am.RequestPool(nwCfg.Ipam.AddrSpace, "", "", options, false)
if err != nil {
err = plugin.Errorf("Failed to allocate pool: %v", err)
return err
}
// On failure, release the address pool.
defer func() {
if err != nil && poolID != "" {
log.Printf("[cni-ipam] Releasing pool %v.", poolID)
plugin.am.ReleasePool(nwCfg.Ipam.AddrSpace, poolID)
}
}()
nwCfg.Ipam.Subnet = subnet
log.Printf("[cni-ipam] Allocated address poolID %v with subnet %v.", poolID, subnet)
}
// Allocate an address for the endpoint.
address, err := plugin.am.RequestAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address, nil)
if err != nil {
err = plugin.Errorf("Failed to allocate address: %v", err)
return err
}
// On failure, release the address.
defer func() {
if err != nil && address != "" {
log.Printf("[cni-ipam] Releasing address %v.", address)
plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, address, nil)
}
}()
log.Printf("[cni-ipam] Allocated address %v.", address)
// Parse IP address.
ipAddress, err := platform.ConvertStringToIPNet(address)
if err != nil {
err = plugin.Errorf("Failed to parse address: %v", err)
return err
}
// Query pool information for gateways and DNS servers.
apInfo, err := plugin.am.GetPoolInfo(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet)
if err != nil {
err = plugin.Errorf("Failed to get pool information: %v", err)
return err
}
// Populate result.
result = &cniTypesCurr.Result{
IPs: []*cniTypesCurr.IPConfig{
{
Version: "4",
Address: *ipAddress,
Gateway: apInfo.Gateway,
},
},
Routes: []*cniTypes.Route{
{
Dst: ipv4DefaultRouteDstPrefix,
GW: apInfo.Gateway,
},
},
}
// Populate DNS servers.
for _, dnsServer := range apInfo.DnsServers {
result.DNS.Nameservers = append(result.DNS.Nameservers, dnsServer.String())
}
// Convert result to the requested CNI version.
res, err := result.GetAsVersion(nwCfg.CNIVersion)
if err != nil {
err = plugin.Errorf("Failed to convert result: %v", err)
return err
}
// Output the result.
if nwCfg.Ipam.Type == cni.Internal {
// Called via the internal interface. Pass output back in args.
args.StdinData, _ = json.Marshal(res)
} else {
// Called via the executable interface. Print output to stdout.
res.Print()
}
return nil
}
// Get handles CNI Get commands.
func (plugin *ipamPlugin) Get(args *cniSkel.CmdArgs) error {
return nil
}
// Delete handles CNI delete commands.
func (plugin *ipamPlugin) Delete(args *cniSkel.CmdArgs) error {
var err error
log.Printf("[cni-ipam] Processing DEL command with args {ContainerID:%v Netns:%v IfName:%v Args:%v Path:%v}.",
args.ContainerID, args.Netns, args.IfName, args.Args, args.Path)
defer func() { log.Printf("[cni-ipam] DEL command completed with err:%v.", err) }()
// Parse network configuration from stdin.
nwCfg, err := plugin.Configure(args.StdinData)
if err != nil {
err = plugin.Errorf("Failed to parse network configuration: %v", err)
return err
}
// If an address is specified, release that address. Otherwise, release the pool.
if nwCfg.Ipam.Address != "" {
// Release the address.
err := plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet, nwCfg.Ipam.Address, nil)
if err != nil {
err = plugin.Errorf("Failed to release address: %v", err)
return err
}
} else {
// Release the pool.
err := plugin.am.ReleasePool(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet)
if err != nil {
err = plugin.Errorf("Failed to release pool: %v", err)
return err
}
}
return nil
}
// Update handles CNI update command.
func (plugin *ipamPlugin) Update(args *cniSkel.CmdArgs) error {
return nil
}