-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainier.go
223 lines (186 loc) · 5.56 KB
/
rainier.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"runtime"
"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/ip"
"github.com/containernetworking/plugins/pkg/ipam"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/digitalocean/go-openvswitch/ovs"
)
const DefaultMTU = 1500
const HostInterfaceJson = "/tmp/rainier.json"
var hostInterfaces = make(map[string]interface{})
type RainierConfig struct {
types.NetConf
PublicBridgeName string `json:"publicBridgeName"`
}
func cmdAdd(args *skel.CmdArgs) error {
config := &RainierConfig{}
if err := json.Unmarshal(args.StdinData, config); err != nil {
return err
}
// Create OVS bridges
if err := createOvsBr(config.PublicBridgeName); err != nil {
return err
}
// Get name space
netns, err := ns.GetNS(args.Netns)
if err != nil {
return fmt.Errorf("failed to open netns %q: %v", args.Netns, err)
}
defer netns.Close()
// Create veth
hostInterface, containerInterface, err := createVeth(netns, args.IfName)
if err != nil {
return err
}
// Add port to OVS
if err := addOvsPort(config.PublicBridgeName, hostInterface.Name); err != nil {
return err
}
// Invoke IPAM
r, err := ipam.ExecAdd(config.IPAM.Type, args.StdinData)
if err != nil {
return err
}
// Convert IPAM result to current Result type
result, err := current.NewResultFromResult(r)
if err != nil {
return err
}
if len(result.IPs) == 0 {
return fmt.Errorf("IPAM plugin returns no IP address")
}
// Associate all IPs to the first interface
for _, ip := range result.IPs {
ip.Interface = current.Int(0)
}
// Set interface in result
result.Interfaces = []*current.Interface{containerInterface}
// Apply IP address to the container interface
err = netns.Do(func(_ ns.NetNS) error {
return ipam.ConfigureIface(containerInterface.Name, result)
})
if err != nil {
return err
}
// Set DNS in result
result.DNS = config.DNS
// Update JSON file
readHostInterfacesFromFile()
hostInterfaces[args.ContainerID] = hostInterface.Name
writeHostInterfacesToFile()
return types.PrintResult(result, config.NetConf.CNIVersion)
}
func cmdDel(args *skel.CmdArgs) error {
config := &RainierConfig{}
if err := json.Unmarshal(args.StdinData, config); err != nil {
return err
}
if err := ipam.ExecDel(config.IPAM.Type, args.StdinData); err != nil {
return err
}
// Update JSON file and remove port from OVS
readHostInterfacesFromFile()
hostIfName := hostInterfaces[args.ContainerID]
if hostIfName != nil {
if err := deleteOvsPort(config.PublicBridgeName, hostIfName.(string)); err != nil {
return err
}
delete(hostInterfaces, args.ContainerID)
writeHostInterfacesToFile()
}
return nil
}
func cmdGet(args *skel.CmdArgs) error {
return fmt.Errorf("cmdGet is not implemented")
}
func createVeth(netns ns.NetNS, ifName string) (*current.Interface, *current.Interface, error) {
contIface := ¤t.Interface{}
hostIface := ¤t.Interface{}
err := netns.Do(func(hostNS ns.NetNS) error {
// create the veth pair in the container and move host end into host netns
hostVeth, containerVeth, err := ip.SetupVeth(ifName, DefaultMTU, hostNS)
if err != nil {
return err
}
contIface.Name = containerVeth.Name
contIface.Mac = containerVeth.HardwareAddr.String()
contIface.Sandbox = netns.Path()
hostIface.Name = hostVeth.Name
return nil
})
if err != nil {
return nil, nil, err
}
return hostIface, contIface, nil
}
func createOvsBr(bridgeName string) error {
protocols := []string{ovs.ProtocolOpenFlow13}
client := ovs.New(
ovs.Sudo(),
ovs.Protocols(protocols),
)
if err := client.VSwitch.AddBridge(bridgeName); err != nil {
return fmt.Errorf("Failed to add bridge %s. Error = %s", bridgeName, err)
}
return nil
}
func addOvsPort(bridgeName string, hostIfName string) error {
protocols := []string{ovs.ProtocolOpenFlow13}
client := ovs.New(
ovs.Sudo(),
ovs.Protocols(protocols),
)
if err := client.VSwitch.AddPort(bridgeName, hostIfName); err != nil {
return fmt.Errorf("Failed to add port %s to bridge %s. Error = %s", hostIfName, bridgeName, err)
}
return nil
}
func deleteOvsPort(bridgeName string, hostIfName string) error {
protocols := []string{ovs.ProtocolOpenFlow13}
client := ovs.New(
ovs.Sudo(),
ovs.Protocols(protocols),
)
if err := client.VSwitch.DeletePort(bridgeName, hostIfName); err != nil {
fmt.Println("%s %s", bridgeName, hostIfName)
return fmt.Errorf("Failed to delete port %s from bridge %s. Error = %s", hostIfName, bridgeName, err)
}
return nil
}
func readHostInterfacesFromFile() error {
jsonByte, err := ioutil.ReadFile(HostInterfaceJson)
if err == nil {
if err := json.Unmarshal(jsonByte, &hostInterfaces); err != nil {
return fmt.Errorf("Fail to decode host interface JSON")
}
}
return nil
}
func writeHostInterfacesToFile() error {
jsonByte, err := json.Marshal(hostInterfaces)
if err != nil {
return fmt.Errorf("Fail to encode host interface JSON")
}
if err := ioutil.WriteFile(HostInterfaceJson, jsonByte, 0644); err != nil {
return fmt.Errorf("Fail to write host interface JSON")
}
return nil
}
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()
}
func main() {
about := "Rainier CNI"
skel.PluginMain(cmdAdd, cmdGet, cmdDel, version.All, about)
}