-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydriver.go
177 lines (140 loc) · 4.14 KB
/
mydriver.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
package mydriver
import (
"encoding/json"
"fmt"
"net"
"github.com/docker/go-plugins-helpers/network"
"github.com/fsouza/go-dockerclient"
"github.com/vishvananda/netlink"
)
type MyDriver struct {
networks map[string]Network
}
type Network struct {
bridge string
endpoints map[string]Endpoint
}
type Endpoint struct {
vethHost string
vethCont string
}
func NewDriver() *MyDriver {
d := new(MyDriver)
return d
}
func (d *MyDriver) GetCapabilities() (*network.CapabilitiesResponse, error) {
return &network.CapabilitiesResponse{
Scope: network.LocalScope,
}, nil
}
func (d *MyDriver) CreateNetwork(request *network.CreateNetworkRequest) error {
options := request.Options["com.docker.network.generic"]
bridge, ok := options.(map[string]interface{})
if !ok {
return fmt.Errorf("invalid data in request: %v", options)
}
interfaces, err := net.Interfaces()
if err != nil {
return fmt.Errorf("cannot get network interfaces")
}
for _, i := range interfaces {
if i.Name == bridge["bridge"] { // bridge exists
d.networks[request.NetworkID] = Network{
bridge: i.Name,
endpoints: map[string]Endpoint{},
}
return nil
}
}
return fmt.Errorf(`bridge "%s" does not exist`, bridge["bridge"])
}
func (d *MyDriver) DeleteNetwork(request *network.DeleteNetworkRequest) error {
return nil
}
func (d *MyDriver) CreateEndpoint(request *network.CreateEndpointRequest) (*network.CreateEndpointResponse, error) {
vethHost := generateVethName()
vethCont := generateVethName()
veth := &netlink.Veth{}
veth.LinkAttrs.Name = vethCont
veth.PeerName = vethHost
err := netlink.LinkAdd(veth)
if err != nil {
return nil, fmt.Errorf(`cannot add link "%s"`, vethHost)
}
err = d.LinkSetUpByName(vethHost)
if err != nil {
return nil, err
}
err = d.LinkSetUpByName(vethCont)
if err != nil {
return nil, err
}
d.networks[request.NetworkID].endpoints[request.EndpointID] = Endpoint{
vethCont: vethCont,
vethHost: vethHost,
}
return &network.CreateEndpointResponse{}, nil
}
func (d *MyDriver) DeleteEndpoint(request *network.DeleteEndpointRequest) error {
return nil
}
func (d *MyDriver) EndpointInfo(request *network.InfoRequest) (*network.InfoResponse, error) {
endpointInfo := make(map[string]string)
docker := Docker{
client: &docker.Client{},
}
endpoint, err := docker.GetContainerInfo(request.EndpointID)
if err != nil {
return nil, err
}
out, err := json.Marshal(endpoint)
if err != nil {
return nil, err
}
endpointInfo[request.EndpointID] = string(out)
return &network.InfoResponse{
Value: endpointInfo,
}, nil
}
func (d *MyDriver) Join(request *network.JoinRequest) (*network.JoinResponse, error) {
vethHost, err := d.getLinkByName(d.networks[request.NetworkID].endpoints[request.EndpointID].vethHost)
if err != nil {
return nil, err
}
bridge, err := d.getLinkByName(d.networks[request.NetworkID].bridge)
b, ok := bridge.(*netlink.Bridge)
if !ok {
return nil, fmt.Errorf(`bridge "%s" incorrect`, d.networks[request.NetworkID].bridge)
}
err = netlink.LinkSetMaster(vethHost, b)
if err != nil {
return nil, fmt.Errorf(`cannot add veth "%s" to bridge "%s"`, vethHost.Attrs().Name, d.networks[request.NetworkID].bridge)
}
return &network.JoinResponse{
InterfaceName: network.InterfaceName{
SrcName: d.networks[request.NetworkID].endpoints[request.EndpointID].vethCont,
DstPrefix: "eth",
},
}, nil
}
func (d *MyDriver) Leave(request *network.LeaveRequest) error {
return d.deleteLinkByName(d.networks[request.NetworkID].endpoints[request.EndpointID].vethHost)
}
func (d *MyDriver) DiscoverNew(request *network.DiscoveryNotification) error {
return nil
}
func (d *MyDriver) DiscoverDelete(request *network.DiscoveryNotification) error {
return nil
}
func (d *MyDriver) ProgramExternalConnectivity(request *network.ProgramExternalConnectivityRequest) error {
return nil
}
func (d *MyDriver) RevokeExternalConnectivity(request *network.RevokeExternalConnectivityRequest) error {
return nil
}
func (d *MyDriver) AllocateNetwork(*network.AllocateNetworkRequest) (*network.AllocateNetworkResponse, error) {
return nil, nil
}
func (d *MyDriver) FreeNetwork(*network.FreeNetworkRequest) error {
return nil
}