forked from ciao-project/ciao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_network.go
170 lines (143 loc) · 4.32 KB
/
docker_network.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
/*
// Copyright (c) 2016 Intel Corporation
//
// 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.
*/
package main
import (
"sync"
"context"
"github.com/01org/ciao/networking/libsnnet"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/network"
"github.com/golang/glog"
)
type dockerNetworkState struct {
done chan struct{}
err error
}
var dockerNetworkMap struct {
sync.Mutex
networks map[string]*dockerNetworkState
}
func init() {
dockerNetworkMap.networks = make(map[string]*dockerNetworkState)
}
func createDockerVnic(vnicCfg *libsnnet.VnicConfig) (*libsnnet.Vnic, *libsnnet.SsntpEventInfo, *libsnnet.ContainerInfo, error) {
dockerNetworkMap.Lock()
state := dockerNetworkMap.networks[vnicCfg.SubnetID]
if state != nil {
dockerNetworkMap.Unlock()
glog.Info("Waiting for Docker network creation")
<-state.done
if state.err != nil {
return nil, nil, nil, state.err
}
return cnNet.CreateVnic(vnicCfg)
}
ch := make(chan struct{})
defer close(ch)
state = &dockerNetworkState{done: ch}
dockerNetworkMap.networks[vnicCfg.SubnetID] = state
dockerNetworkMap.Unlock()
vnic, event, info, err := cnNet.CreateVnic(vnicCfg)
state.err = err
if err != nil {
return vnic, event, info, err
}
if info == nil {
glog.Warning("VNIC information expected")
return vnic, event, info, err
}
// We are going to ignore the error here. Docker network
// might fail as it could already have been created. We
// would not know this if it was created in a previous invocation
// of launcher. Should the network really fail to be created
// the container will not launch.
_ = createDockerNetwork(context.Background(), info)
return vnic, event, info, state.err
}
func destroyDockerVnic(vnicCfg *libsnnet.VnicConfig) (*libsnnet.SsntpEventInfo, error) {
// BUG(markus): We need to pass in a context to destroyVnic
event, info, err := cnNet.DestroyVnic(vnicCfg)
if err != nil {
glog.Errorf("cn.DestroyVnic failed %v", err)
return event, err
}
if info != nil {
// This is one of these weird cases we will have with
// docker in which some launcher and libssnet state gets out of
// sync with docker. Launcher needs a cleanup routine that detects
// these inconsistencies and cleans up:
// https://github.com/01org/ciao/issues/4
_ = destroyDockerNetwork(context.Background(), info.SubnetID)
dockerNetworkMap.Lock()
delete(dockerNetworkMap.networks, vnicCfg.SubnetID)
dockerNetworkMap.Unlock()
}
return event, err
}
func createDockerNetwork(ctx context.Context, info *libsnnet.ContainerInfo) error {
cli, err := getDockerClient()
if err != nil {
return err
}
_, err = cli.NetworkCreate(ctx, types.NetworkCreate{
Name: info.SubnetID,
Driver: "ciao",
IPAM: network.IPAM{
Driver: "ciao",
Config: []network.IPAMConfig{{
Subnet: info.Subnet.String(),
Gateway: info.Gateway.String(),
}}},
Options: map[string]string{
"bridge": info.Bridge,
}})
if err != nil {
glog.Errorf("Unable to create docker network %s: %v", info.SubnetID, err)
}
return err
}
func destroyDockerNetwork(ctx context.Context, bridge string) error {
cli, err := getDockerClient()
if err != nil {
return err
}
err = cli.NetworkRemove(ctx, bridge)
if err != nil {
glog.Errorf("Unable to remove docker network %s: %v", bridge, err)
}
return err
}
func resetDockerNetworking() {
cli, err := getDockerClient()
if err != nil {
return
}
nets, err := cli.NetworkList(context.Background(), types.NetworkListOptions{})
if err != nil {
glog.Errorf("Unable to retrieve list of docker networks: %v", err)
return
}
for i := range nets {
if nets[i].Driver == "ciao" {
glog.Infof("Deleting docker network %s", nets[i].Name)
err = cli.NetworkRemove(context.Background(), nets[i].ID)
if err != nil {
glog.Errorf("Unable to remove docker network %s: %v", nets[i].ID, err)
}
}
}
return
}