-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.go
265 lines (232 loc) · 7.73 KB
/
main.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
// Copyright 2017 CNI authors
// Copyright 2017 Lyft, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This is a sample chained plugin that supports multiple CNI versions. It
// parses prevResult according to the cniVersion
package main
import (
"encoding/json"
"fmt"
"net"
"runtime"
"time"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"
"github.com/lyft/cni-ipvlan-vpc-k8s/aws"
"github.com/lyft/cni-ipvlan-vpc-k8s/lib"
"github.com/lyft/cni-ipvlan-vpc-k8s/nl"
)
// PluginConf contains configuration parameters
type PluginConf struct {
Name string `json:"name"`
CNIVersion string `json:"cniVersion"`
SecGroupIds []string `json:"secGroupIds"`
SubnetTags map[string]string `json:"subnetTags"`
IfaceIndex int `json:"interfaceIndex"`
SkipDeallocation bool `json:"skipDeallocation"`
RouteToVPCPeers bool `json:"routeToVpcPeers"`
ReuseIPWait int `json:"reuseIPWait"`
IPBatchSize int64 `json:"ipBatchSize"`
RouteToCidrs []string `json:"routeToCidrs"`
}
func init() {
// this ensures that main runs only on main thread (thread group leader).
// since namespace ops (unshare, setns) are done for a single thread, we
// must ensure that the goroutine does not jump from OS thread to thread
runtime.LockOSThread()
}
// parseConfig parses the supplied configuration from stdin.
func parseConfig(stdin []byte) (*PluginConf, error) {
conf := PluginConf{
ReuseIPWait: 60, // default 60 second wait
IPBatchSize: 1, // default 1 (backward compatibility)
}
if err := json.Unmarshal(stdin, &conf); err != nil {
return nil, fmt.Errorf("failed to parse network configuration: %v", err)
}
if conf.SecGroupIds == nil {
return nil, fmt.Errorf("secGroupIds must be specified")
}
return &conf, nil
}
// cmdAdd is called for ADD requests
func cmdAdd(args *skel.CmdArgs) error {
conf, err := parseConfig(args.StdinData)
if err != nil {
return err
}
var alloc *aws.AllocationResult
registry := &aws.Registry{}
// Try to find a free IP first - possibly from a broken
// container, or torn down namespace. IP must also be at least
// conf.ReuseIPWait seconds old in the registry to be
// considered for use.
free, err := aws.FindFreeIPsAtIndex(conf.IfaceIndex, true)
if err == nil || len(free) > 0 {
registryFreeIPs, err := registry.TrackedBefore(time.Now().Add(time.Duration(-conf.ReuseIPWait) * time.Second))
if err == nil && len(registryFreeIPs) > 0 {
loop:
for _, freeAlloc := range free {
for _, freeRegistry := range registryFreeIPs {
if freeAlloc.IP.Equal(freeRegistry) {
alloc = freeAlloc
// update timestamp
err := registry.TrackIP(freeRegistry)
if err != nil {
return fmt.Errorf("failed to track ip: %s", err)
}
break loop
}
}
}
}
}
// No free IPs available for use, so let's allocate one
if alloc == nil {
// allocate IPs on an available interface
allocs, err := aws.DefaultClient.AllocateIPsFirstAvailableAtIndex(conf.IfaceIndex, conf.IPBatchSize)
if err == nil || len(allocs) > 0 {
alloc = allocs[0]
} else {
// failed, so attempt to add an IP to a new interface
newIf, err := aws.DefaultClient.NewInterface(conf.SecGroupIds, conf.SubnetTags, conf.IPBatchSize)
if err != nil || len(newIf.IPv4s) < 1 {
return fmt.Errorf("unable to create a new elastic network interface due to %v",
err)
}
// Freshly allocated interfaces will always have at least one valid IP - use
// this IP address.
alloc = &aws.AllocationResult{
IP: &newIf.IPv4s[0],
Interface: *newIf,
}
}
}
// Per https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html
// subnet + 1 is our gateway
// primary cidr + 2 is the dns server
subnetAddr := alloc.Interface.SubnetCidr.IP.To4()
gw := append(subnetAddr[:3], subnetAddr[3]+1)
vpcPrimaryAddr := alloc.Interface.VpcPrimaryCidr.IP.To4()
dns := append(vpcPrimaryAddr[:3], vpcPrimaryAddr[3]+2)
addr := net.IPNet{
IP: *alloc.IP,
Mask: alloc.Interface.SubnetCidr.Mask,
}
master := alloc.Interface.LocalName()
iface := ¤t.Interface{
Name: master,
}
// Ensure the master interface is always up
err = nl.UpInterfacePoll(master)
if err != nil {
return fmt.Errorf("unable to bring up interface %v due to %v",
master, err)
}
ipconfig := ¤t.IPConfig{
Version: "4",
Address: addr,
Gateway: gw,
Interface: current.Int(0),
}
result := ¤t.Result{}
rDNS := types.DNS{}
rDNS.Nameservers = append(rDNS.Nameservers, dns.String())
result.DNS = rDNS
result.IPs = append(result.IPs, ipconfig)
result.Interfaces = append(result.Interfaces, iface)
cidrs := alloc.Interface.VpcCidrs
if aws.HasBugBrokenVPCCidrs(aws.DefaultClient) {
cidrs, err = aws.DefaultClient.DescribeVPCCIDRs(alloc.Interface.VpcID)
if err != nil {
return fmt.Errorf("Unable to enumerate CIDRs from the AWS API due to a specific meta-data bug %v", err)
}
}
if conf.RouteToVPCPeers {
peerCidr, err := aws.DefaultClient.DescribeVPCPeerCIDRs(alloc.Interface.VpcID)
if err != nil {
return fmt.Errorf("unable to enumerate peer CIDrs %v", err)
}
cidrs = append(cidrs, peerCidr...)
}
if conf.RouteToCidrs != nil {
for _, cidr := range conf.RouteToCidrs {
_, parsed, err := net.ParseCIDR(cidr)
if err != nil {
return fmt.Errorf("unable to parse routeToCidrs element %v", err)
}
cidrs = append(cidrs, parsed)
}
}
// add routes for all VPC cidrs via the subnet gateway
for _, dst := range cidrs {
result.Routes = append(result.Routes, &types.Route{Dst: *dst, GW: gw})
}
// remove the IP from the registry just before handing off to ipvlan
err = registry.ForgetIP(*alloc.IP)
if err != nil {
return fmt.Errorf("failed to forget ip: %s", err)
}
return types.PrintResult(result, conf.CNIVersion)
}
// cmdDel is called for DELETE requests
func cmdDel(args *skel.CmdArgs) error {
conf, err := parseConfig(args.StdinData)
if err != nil {
return err
}
_ = conf
var addrs []netlink.Addr
// enter the namespace to grab the list of IPs
_ = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error {
iface, err := netlink.LinkByName(args.IfName)
if err != nil {
return err
}
addrs, err = netlink.AddrList(iface, netlink.FAMILY_V4)
return err
})
registry := &aws.Registry{}
for _, addr := range addrs {
if !conf.SkipDeallocation {
// deallocate IPs outside of the namespace so creds are correct
err := aws.DefaultClient.DeallocateIP(&addr.IP)
if err != nil {
return fmt.Errorf("failed to deallocate ip: %s", err)
}
}
// Mark this IP as free in the registry
err := registry.TrackIP(addr.IP)
if err != nil {
return fmt.Errorf("failed to track ip: %s", err)
}
}
return nil
}
// cmdCheck is called for CHECK requests
func cmdCheck(args *skel.CmdArgs) error {
// TODO: implement this
return nil
}
func main() {
run := func() error {
skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.PluginSupports(version.Current()), "ipam")
return nil
}
_ = lib.LockfileRun(run)
}