forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
openshift-sdn_linux.go
260 lines (224 loc) · 7.1 KB
/
openshift-sdn_linux.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
// build +linux
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/openshift/origin/pkg/network/node/cniserver"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/020"
"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/vishvananda/netlink"
)
type cniPlugin struct {
socketPath string
hostNS ns.NetNS
}
func NewCNIPlugin(socketPath string, hostNS ns.NetNS) *cniPlugin {
return &cniPlugin{socketPath: socketPath, hostNS: hostNS}
}
// Create and fill a CNIRequest with this plugin's environment and stdin which
// contain the CNI variables and configuration
func newCNIRequest(args *skel.CmdArgs) *cniserver.CNIRequest {
envMap := make(map[string]string)
for _, item := range os.Environ() {
idx := strings.Index(item, "=")
if idx > 0 {
envMap[strings.TrimSpace(item[:idx])] = item[idx+1:]
}
}
return &cniserver.CNIRequest{
Env: envMap,
Config: args.StdinData,
}
}
// Send a CNI request to the CNI server via JSON + HTTP over a root-owned unix socket,
// and return the result
func (p *cniPlugin) doCNI(url string, req *cniserver.CNIRequest) ([]byte, error) {
data, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal CNI request %v: %v", req, err)
}
client := &http.Client{
Transport: &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
return net.Dial("unix", p.socketPath)
},
},
}
var resp *http.Response
err = p.hostNS.Do(func(ns.NetNS) error {
resp, err = client.Post(url, "application/json", bytes.NewReader(data))
return err
})
if err != nil {
return nil, fmt.Errorf("failed to send CNI request: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read CNI result: %v", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("CNI request failed with status %v: '%s'", resp.StatusCode, string(body))
}
return body, nil
}
// Send the ADD command environment and config to the CNI server, returning
// the IPAM result to the caller
func (p *cniPlugin) doCNIServerAdd(req *cniserver.CNIRequest, hostVeth string) (types.Result, error) {
req.HostVeth = hostVeth
body, err := p.doCNI("http://dummy/", req)
if err != nil {
return nil, err
}
// We currently expect CNI version 0.2.0 results, because that's the
// CNIVersion we pass in our config JSON
result, err := types020.NewResult(body)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response '%s': %v", string(body), err)
}
return result, nil
}
func (p *cniPlugin) testCmdAdd(args *skel.CmdArgs) (types.Result, error) {
return p.doCNIServerAdd(newCNIRequest(args), "dummy0")
}
func (p *cniPlugin) CmdAdd(args *skel.CmdArgs) error {
req := newCNIRequest(args)
config, err := cniserver.ReadConfig(cniserver.CNIServerConfigFilePath)
if err != nil {
return err
}
var hostVeth, contVeth net.Interface
err = ns.WithNetNSPath(args.Netns, func(hostNS ns.NetNS) error {
hostVeth, contVeth, err = ip.SetupVeth(args.IfName, int(config.MTU), hostNS)
if err != nil {
return fmt.Errorf("failed to create container veth: %v", err)
}
return nil
})
if err != nil {
return err
}
result, err := p.doCNIServerAdd(req, hostVeth.Name)
if err != nil {
return err
}
// current.NewResultFromResult and ipam.ConfigureIface both think that
// a route with no gateway specified means to pass the default gateway
// as the next hop to ip.AddRoute, but that's not what we want; we want
// to pass nil as the next hop. So we need to clear the default gateway.
result020, err := types020.GetResult(result)
if err != nil {
return fmt.Errorf("failed to convert IPAM result: %v", err)
}
defaultGW := result020.IP4.Gateway
result020.IP4.Gateway = nil
result030, err := current.NewResultFromResult(result020)
if err != nil || len(result030.IPs) != 1 || result030.IPs[0].Version != "4" {
return fmt.Errorf("failed to convert IPAM result: %v", err)
}
// Add a sandbox interface record which ConfigureInterface expects.
// The only interface we report is the pod interface.
result030.Interfaces = []*current.Interface{
{
Name: args.IfName,
Mac: contVeth.HardwareAddr.String(),
Sandbox: args.Netns,
},
}
result030.IPs[0].Interface = current.Int(0)
err = ns.WithNetNSPath(args.Netns, func(hostNS ns.NetNS) error {
// Set up eth0
if err := ip.SetHWAddrByIP(args.IfName, result030.IPs[0].Address.IP, nil); err != nil {
return fmt.Errorf("failed to set pod interface MAC address: %v", err)
}
if err := ipam.ConfigureIface(args.IfName, result030); err != nil {
return fmt.Errorf("failed to configure container IPAM: %v", err)
}
// Set up lo
link, err := netlink.LinkByName("lo")
if err == nil {
err = netlink.LinkSetUp(link)
}
if err != nil {
return fmt.Errorf("failed to configure container loopback: %v", err)
}
// Set up macvlan0 (if it exists)
link, err = netlink.LinkByName("macvlan0")
if err == nil {
err = netlink.LinkSetUp(link)
if err != nil {
return fmt.Errorf("failed to enable macvlan device: %v", err)
}
// A macvlan can't reach its parent interface's IP, so we need to
// add a route to that via the SDN
var addrs []netlink.Addr
err = hostNS.Do(func(ns.NetNS) error {
parent, err := netlink.LinkByIndex(link.Attrs().ParentIndex)
if err != nil {
return err
}
addrs, err = netlink.AddrList(parent, netlink.FAMILY_V4)
return err
})
if err != nil {
return fmt.Errorf("failed to configure macvlan device: %v", err)
}
var dsts []*net.IPNet
for _, addr := range addrs {
dsts = append(dsts, &net.IPNet{IP: addr.IP, Mask: net.CIDRMask(32, 32)})
}
_, serviceIPNet, err := net.ParseCIDR(config.ServiceNetworkCIDR)
if err != nil {
return fmt.Errorf("failed to parse ServiceNetworkCIDR: %v", err)
}
dsts = append(dsts, serviceIPNet)
dnsIP := net.ParseIP(config.DNSIP)
if dnsIP == nil {
return fmt.Errorf("failed to parse dns IP: %v", err)
}
dsts = append(dsts, &net.IPNet{IP: dnsIP, Mask: net.CIDRMask(32, 32)})
for _, dst := range dsts {
route := &netlink.Route{
Dst: dst,
Gw: defaultGW,
}
if err := netlink.RouteAdd(route); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to add route to dst: %v via SDN: %v", dst, err)
}
}
}
return nil
})
if err != nil {
return err
}
return result.Print()
}
func (p *cniPlugin) CmdDel(args *skel.CmdArgs) error {
_, err := p.doCNI("http://dummy/", newCNIRequest(args))
return err
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
hostNS, err := ns.GetCurrentNS()
if err != nil {
panic(fmt.Sprintf("could not get current kernel netns: %v", err))
}
defer hostNS.Close()
p := NewCNIPlugin(cniserver.CNIServerSocketPath, hostNS)
skel.PluginMain(p.CmdAdd, p.CmdDel, version.Legacy)
}