-
Notifications
You must be signed in to change notification settings - Fork 241
/
dockerclient.go
197 lines (159 loc) · 5.24 KB
/
dockerclient.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
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package dockerclient
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/Azure/azure-container-networking/platform"
"github.com/Azure/azure-container-networking/cns/imdsclient"
"github.com/Azure/azure-container-networking/log"
)
const (
defaultDockerConnectionURL = "http://127.0.0.1:2375"
defaultIpamPlugin = "azure-vnet"
networkMode = "com.microsoft.azure.network.mode"
bridgeMode = "bridge"
)
// DockerClient specifies a client to connect to docker.
type DockerClient struct {
connectionURL string
imdsClient *imdsclient.ImdsClient
}
// NewDockerClient create a new docker client.
func NewDockerClient(url string) (*DockerClient, error) {
return &DockerClient{
connectionURL: url,
imdsClient: &imdsclient.ImdsClient{},
}, nil
}
// NewDefaultDockerClient create a new docker client.
func NewDefaultDockerClient(imdsClient *imdsclient.ImdsClient) (*DockerClient, error) {
return &DockerClient{
connectionURL: defaultDockerConnectionURL,
imdsClient: imdsClient,
}, nil
}
// NetworkExists tries to retrieve a network from docker (if it exists).
func (dockerClient *DockerClient) NetworkExists(networkName string) error {
log.Printf("[Azure CNS] NetworkExists")
res, err := http.Get(
dockerClient.connectionURL + inspectNetworkPath + networkName)
if err != nil {
log.Printf("[Azure CNS] Error received from http Post for docker network inspect %v %v", networkName, err.Error())
return err
}
defer res.Body.Close()
// network exists
if res.StatusCode == 200 {
log.Debugf("[Azure CNS] Network with name %v already exists. Docker return code: %v", networkName, res.StatusCode)
return nil
}
// network not found
if res.StatusCode == 404 {
log.Debugf("[Azure CNS] Network with name %v does not exist. Docker return code: %v", networkName, res.StatusCode)
return fmt.Errorf("Network not found")
}
return fmt.Errorf("Unknown return code from docker inspect %d", res.StatusCode)
}
// CreateNetwork creates a network using docker network create.
func (dockerClient *DockerClient) CreateNetwork(networkName string, nicInfo *imdsclient.InterfaceInfo, options map[string]interface{}) error {
log.Printf("[Azure CNS] CreateNetwork")
enableSnat := true
config := &Config{
Subnet: nicInfo.Subnet,
}
configs := make([]Config, 1)
configs[0] = *config
ipamConfig := &IPAM{
Driver: defaultIpamPlugin,
Config: configs,
}
netConfig := &NetworkConfiguration{
Name: networkName,
Driver: defaultNetworkPlugin,
IPAM: *ipamConfig,
Internal: true,
}
if options != nil {
if _, ok := options[OptDisableSnat]; ok {
enableSnat = false
}
}
if enableSnat {
netConfig.Options = make(map[string]interface{})
netConfig.Options[networkMode] = bridgeMode
}
log.Printf("[Azure CNS] Going to create network with config: %+v", netConfig)
netConfigJSON := new(bytes.Buffer)
err := json.NewEncoder(netConfigJSON).Encode(netConfig)
if err != nil {
return err
}
res, err := http.Post(
dockerClient.connectionURL+createNetworkPath,
"application/json; charset=utf-8",
netConfigJSON)
if err != nil {
log.Printf("[Azure CNS] Error received from http Post for docker network create %v", networkName)
return err
}
defer res.Body.Close()
if res.StatusCode != 201 {
var createNetworkResponse DockerErrorResponse
err = json.NewDecoder(res.Body).Decode(&createNetworkResponse)
var ermsg string
ermsg = ""
if err != nil {
ermsg = err.Error()
}
return fmt.Errorf("[Azure CNS] Create docker network failed with error code %v - %v - %v",
res.StatusCode, createNetworkResponse.message, ermsg)
}
if enableSnat {
err = platform.SetOutboundSNAT(nicInfo.Subnet)
if err != nil {
log.Printf("[Azure CNS] Error setting up SNAT outbound rule %v", err)
}
}
return nil
}
// DeleteNetwork creates a network using docker network create.
func (dockerClient *DockerClient) DeleteNetwork(networkName string) error {
log.Printf("[Azure CNS] DeleteNetwork")
url := dockerClient.connectionURL + inspectNetworkPath + networkName
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
log.Printf("[Azure CNS] Error received while creating http DELETE request for network delete %v %v", networkName, err.Error())
return err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Printf("[Azure CNS] HTTP Post returned error %v", err.Error())
return err
}
defer res.Body.Close()
// network successfully deleted.
if res.StatusCode == 204 {
primaryNic, err := dockerClient.imdsClient.GetPrimaryInterfaceInfoFromHost()
if err != nil {
return err
}
cmd := fmt.Sprintf("iptables -t nat -D POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d %v -j MASQUERADE",
primaryNic.Subnet)
_, err = platform.ExecuteCommand(cmd)
if err != nil {
log.Printf("[Azure CNS] Error Removing Outbound SNAT rule %v", err)
}
return nil
}
// network not found.
if res.StatusCode == 404 {
return fmt.Errorf("[Azure CNS] Network not found %v", networkName)
}
return fmt.Errorf("[Azure CNS] Unknown return code from docker delete network %v: ret = %d",
networkName, res.StatusCode)
}