-
Notifications
You must be signed in to change notification settings - Fork 0
/
imds-ipam.go
211 lines (165 loc) · 4.72 KB
/
imds-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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
cniv1 "github.com/containernetworking/cni/pkg/types/100"
cniversion "github.com/containernetworking/cni/pkg/version"
"github.com/anguslees/aws-cni-plugins/internal/metadata"
)
var version string
const trace = false
func main() {
log.SetFlags(0)
log.SetPrefix("CNI imds-ipam: ")
log.SetOutput(os.Stderr) // NB: ends up in kubelet syslog
rand.Seed(time.Now().UnixNano())
skel.PluginMain(cmdAdd, cmdCheck, cmdDel, cniversion.All, fmt.Sprintf("imds-ipam CNI plugin %s", version))
}
type NetConfIgnoreInterfaceTerm struct {
// Ignore DeviceIndex in range [Start,End)
DeviceIndexStart int `json:"deviceIndexStart"`
DeviceIndexEnd int `json:"deviceIndexEnd"`
}
// IPAMConf is our CNI (IPAM) config structure
type IPAMConf struct {
types.IPAM
Routes []*types.Route `json:"routes"`
DataDir string `json:"dataDir"`
IPVersion string `json:"ipVersion"`
// Interfaces to ignore (ignores interfaces matching any term)
IgnoreInterfaces []NetConfIgnoreInterfaceTerm `json:"ignoreInterfaces"`
}
// NetConf is our CNI config structure
type NetConf struct {
CNIVersion string `json:"cniVersion,omitempty"`
Name string `json:"name,omitempty"`
IPAM *IPAMConf `json:"ipam,omitempty"`
}
func loadConf(bytes []byte) (*NetConf, *IPAMConf, error) {
n := &NetConf{}
if err := json.Unmarshal(bytes, n); err != nil {
return nil, nil, err
}
if n.IPAM == nil {
return nil, nil, fmt.Errorf("IPAM config missing 'ipam' key")
}
if n.IPAM.IPVersion == "" {
n.IPAM.IPVersion = "4"
}
return n, n.IPAM, nil
}
func cmdCheck(args *skel.CmdArgs) error {
if trace {
log.Printf("CHECK: %v", args)
}
netConf, ipamConf, err := loadConf(args.StdinData)
if err != nil {
return fmt.Errorf("failed to parse config: %v", err)
}
store := NewStore(filepath.Join(ipamConf.DataDir, netConf.Name))
if err := store.Open(); err != nil {
return err
}
defer store.Close()
ip := store.FindByID(args.ContainerID, args.IfName)
if ip == nil {
return fmt.Errorf("imds-ipam: Failed to find address added by container %s", args.ContainerID)
}
if trace {
log.Printf("CHECK returning success")
}
return nil
}
func cmdAdd(args *skel.CmdArgs) error {
ctx := context.TODO()
if trace {
log.Printf("ADD: %v", args)
}
netConf, ipamConf, err := loadConf(args.StdinData)
if err != nil {
return err
}
session, err := session.NewSession()
if err != nil {
return err
}
awsConfig := aws.NewConfig()
imds := metadata.NewTypedIMDS(metadata.NewCachedIMDS(ec2metadata.New(session, awsConfig)))
result := &cniv1.Result{}
store := NewStore(filepath.Join(ipamConf.DataDir, netConf.Name))
if err := store.Open(); err != nil {
return err
}
defer func() {
if err := store.Close(); err != nil {
panic(err)
}
}()
allocator := NewIMDSAllocator(imds, &store)
ipConf, err := allocator.Get(ctx, args.ContainerID, args.IfName, ipamConf.IPVersion)
if err != nil {
return err
}
result.IPs = append(result.IPs, &ipConf)
result.Routes = ipamConf.Routes
if trace {
log.Printf("ADD returning %v", result)
}
return types.PrintResult(result, netConf.CNIVersion)
}
func cmdDel(args *skel.CmdArgs) error {
ctx := context.TODO()
if trace {
log.Printf("DEL: %v", args)
}
netConf, ipamConf, err := loadConf(args.StdinData)
if err != nil {
return err
}
session, err := session.NewSession()
if err != nil {
return err
}
awsConfig := aws.NewConfig()
imds := metadata.NewTypedIMDS(metadata.NewCachedIMDS(ec2metadata.New(session, awsConfig)))
store := NewStore(filepath.Join(ipamConf.DataDir, netConf.Name))
if err := store.Open(); err != nil {
return err
}
defer func() {
if err := store.Close(); err != nil {
panic(err)
}
}()
allocator := NewIMDSAllocator(imds, &store)
if err := allocator.Put(ctx, args.ContainerID, args.IfName, ipamConf.IPVersion); err != nil {
return err
}
if trace {
log.Printf("DEL returning success")
}
return nil
}