Skip to content

Commit 0404c65

Browse files
committed
Initial CNI integration commit
This commit contains the initial set of changes to add CNI sevice to libnetwork drivers. This is done using a cniserver which acts as a side car for libnetwork daemon to process CNI requests. Signed-off-by: Abhinandan Prativadi <abhi@docker.com>
1 parent 9d71b5b commit 0404c65

30 files changed

Lines changed: 2960 additions & 22 deletions

File tree

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ build-local:
3636
@mkdir -p "bin"
3737
go build -tags experimental -o "bin/dnet" ./cmd/dnet
3838
go build -o "bin/docker-proxy" ./cmd/proxy
39-
39+
go build -o "bin/cniserver" ./pkg/server
40+
go build -o "bin/cnictl" ./cni/
4041
clean:
4142
@echo "🐳 $@"
4243
@if [ -d bin ]; then \

api/api.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,9 @@ func procPublishService(c libnetwork.NetworkController, vars map[string]string,
650650
if err != nil {
651651
return "", endpointToService(convertNetworkError(err))
652652
}
653+
epResp := getEndpointInfo(ep)
653654

654-
return ep.ID(), &createdResponse
655+
return epResp, &createdResponse
655656
}
656657

657658
func procUnpublishService(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
@@ -707,6 +708,12 @@ func procAttachBackend(c libnetwork.NetworkController, vars map[string]string, b
707708
if err != nil {
708709
return nil, convertNetworkError(err)
709710
}
711+
if bk.SandboxKey != "" {
712+
err = sb.SetKey(bk.SandboxKey)
713+
if err != nil {
714+
return nil, convertNetworkError(err)
715+
}
716+
}
710717

711718
return sb.Key(), &successResponse
712719
}

api/types.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package api
22

3-
import "github.com/docker/libnetwork/types"
3+
import (
4+
"net"
5+
6+
"github.com/docker/libnetwork"
7+
"github.com/docker/libnetwork/types"
8+
)
49

510
/***********
611
Resources
@@ -72,8 +77,9 @@ type sandboxCreate struct {
7277

7378
// endpointJoin represents the expected body of the "join endpoint" or "leave endpoint" http request messages
7479
type endpointJoin struct {
75-
SandboxID string `json:"sandbox_id"`
76-
Aliases []string `json:"aliases"`
80+
SandboxID string `json:"sandbox_id"`
81+
Aliases []string `json:"aliases"`
82+
SandboxKey string `json:"sandbox_key"`
7783
}
7884

7985
// servicePublish represents the body of the "publish service" http request message
@@ -94,3 +100,34 @@ type extraHost struct {
94100
Name string `json:"name"`
95101
Address string `json:"address"`
96102
}
103+
104+
// endpointInfo contants the endpoint info for https response message on endpoint creation
105+
type endpointInfo struct {
106+
ID string `json:"id"`
107+
Address net.IPNet `json:"address"`
108+
AddressIPv6 net.IPNet `json:"address_ipv6"`
109+
MacAddress net.HardwareAddr `json:"mac_address"`
110+
Gateway net.IP `json:"gateway"`
111+
GatewayIPv6 net.IP `json:"gateway_ipv6"`
112+
}
113+
114+
func getEndpointInfo(ep libnetwork.Endpoint) endpointInfo {
115+
epInfo := endpointInfo{ID: ep.ID()}
116+
117+
if ipv4 := ep.Info().Iface().Address(); ipv4 != nil {
118+
epInfo.Address = *ipv4
119+
}
120+
if ipv6 := ep.Info().Iface().AddressIPv6(); ipv6 != nil {
121+
epInfo.AddressIPv6 = *ipv6
122+
}
123+
if mac := ep.Info().Iface().MacAddress(); mac != nil {
124+
epInfo.MacAddress = mac
125+
}
126+
if gw := ep.Info().Gateway(); gw != nil {
127+
epInfo.Gateway = gw
128+
}
129+
if gw6 := ep.Info().GatewayIPv6(); gw6 != nil {
130+
epInfo.GatewayIPv6 = gw6
131+
}
132+
return epInfo
133+
}

client/service.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/docker/docker/opts"
1313
"github.com/docker/docker/pkg/stringid"
14+
"github.com/docker/libnetwork"
1415
flag "github.com/docker/libnetwork/client/mflag"
1516
"github.com/docker/libnetwork/netutils"
1617
)
@@ -173,19 +174,19 @@ func (cli *NetworkCli) CmdServicePublish(chain string, args ...string) error {
173174
}
174175

175176
sn, nn := parseServiceName(cmd.Arg(0))
176-
sc := serviceCreate{Name: sn, Network: nn, MyAliases: flAlias.GetAll()}
177+
sc := ServiceCreate{Name: sn, Network: nn, MyAliases: flAlias.GetAll()}
177178
obj, _, err := readBody(cli.call("POST", "/services", sc, nil))
178179
if err != nil {
179180
return err
180181
}
181182

182-
var replyID string
183-
err = json.Unmarshal(obj, &replyID)
183+
var ep libnetwork.Endpoint
184+
err = json.Unmarshal(obj, &ep)
184185
if err != nil {
185186
return err
186187
}
187188

188-
fmt.Fprintf(cli.out, "%s\n", replyID)
189+
fmt.Fprintf(cli.out, "%s\n", ep.ID())
189190
return nil
190191
}
191192

@@ -205,7 +206,7 @@ func (cli *NetworkCli) CmdServiceUnpublish(chain string, args ...string) error {
205206
return err
206207
}
207208

208-
sd := serviceDelete{Name: sn, Force: *force}
209+
sd := ServiceDelete{Name: sn, Force: *force}
209210
_, _, err = readBody(cli.call("DELETE", "/services/"+serviceID, sd, nil))
210211

211212
return err
@@ -350,7 +351,7 @@ func (cli *NetworkCli) CmdServiceAttach(chain string, args ...string) error {
350351
return err
351352
}
352353

353-
nc := serviceAttach{SandboxID: sandboxID, Aliases: flAlias.GetAll()}
354+
nc := ServiceAttach{SandboxID: sandboxID, Aliases: flAlias.GetAll()}
354355

355356
_, _, err = readBody(cli.call("POST", "/services/"+serviceID+"/backend", nc, nil))
356357

client/types.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package client
22

3-
import "github.com/docker/libnetwork/types"
3+
import (
4+
"net"
5+
6+
"github.com/docker/libnetwork/types"
7+
)
48

59
/***********
610
Resources
@@ -48,23 +52,24 @@ type networkCreate struct {
4852
NetworkOpts map[string]string `json:"network_opts"`
4953
}
5054

51-
// serviceCreate represents the body of the "publish service" http request message
52-
type serviceCreate struct {
55+
// ServiceCreate represents the body of the "publish service" http request message
56+
type ServiceCreate struct {
5357
Name string `json:"name"`
5458
MyAliases []string `json:"my_aliases"`
5559
Network string `json:"network_name"`
5660
}
5761

58-
// serviceDelete represents the body of the "unpublish service" http request message
59-
type serviceDelete struct {
62+
// ServiceDelete represents the body of the "unpublish service" http request message
63+
type ServiceDelete struct {
6064
Name string `json:"name"`
6165
Force bool `json:"force"`
6266
}
6367

64-
// serviceAttach represents the expected body of the "attach/detach sandbox to/from service" http request messages
65-
type serviceAttach struct {
66-
SandboxID string `json:"sandbox_id"`
67-
Aliases []string `json:"aliases"`
68+
// ServiceAttach represents the expected body of the "attach/detach sandbox to/from service" http request messages
69+
type ServiceAttach struct {
70+
SandboxID string `json:"sandbox_id"`
71+
Aliases []string `json:"aliases"`
72+
SandboxKey string `json:"sandbox_key"`
6873
}
6974

7075
// SandboxCreate is the body of the "post /sandboxes" http request message
@@ -76,6 +81,7 @@ type SandboxCreate struct {
7681
ResolvConfPath string `json:"resolv_conf_path"`
7782
DNS []string `json:"dns"`
7883
ExtraHosts []extraHost `json:"extra_hosts"`
84+
UseExternalKey bool `json:"use_external_key"`
7985
UseDefaultSandbox bool `json:"use_default_sandbox"`
8086
ExposedPorts []types.TransportPort `json:"exposed_ports"`
8187
PortMapping []types.PortBinding `json:"port_mapping"`
@@ -94,3 +100,13 @@ type sandboxParentUpdate struct {
94100
Name string `json:"name"`
95101
Address string `json:"address"`
96102
}
103+
104+
// endpointInfo contants the endpoint info for http response message on endpoint creation
105+
type EndpointInfo struct {
106+
ID string `json:"id"`
107+
Address net.IPNet `json:"address"`
108+
AddressIPv6 net.IPNet `json:"address_ipv6"`
109+
MacAddress net.HardwareAddr `json:"mac_address"`
110+
Gateway net.IP `json:"gateway"`
111+
GatewayIPv6 net.IP `json:"gateway_ipv6"`
112+
}

cmd/dnet/dnet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ func (d *dnetConnection) httpCall(method, path string, data interface{}, headers
447447

448448
req.URL.Host = d.addr
449449
req.URL.Scheme = "http"
450-
450+
fmt.Printf("Requesting http: %+v",req)
451451
httpClient := &http.Client{}
452452
resp, err := httpClient.Do(req)
453453
statusCode := -1

cni/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/containernetworking/cni/pkg/skel"
7+
"github.com/containernetworking/cni/pkg/types"
8+
"github.com/containernetworking/cni/pkg/version"
9+
"github.com/docker/libnetwork/pkg/cniapi"
10+
log "github.com/sirupsen/logrus"
11+
)
12+
13+
func cmdAdd(args *skel.CmdArgs) error {
14+
libClient := cniapi.NewLibNetCniClient()
15+
result, err := libClient.SetupPod(args)
16+
if err != nil {
17+
return fmt.Errorf("Failed to setup Pod , %v", err)
18+
}
19+
return types.PrintResult(result, version.Current())
20+
}
21+
22+
func cmdDel(args *skel.CmdArgs) error {
23+
libClient := cniapi.NewLibNetCniClient()
24+
if err := libClient.TearDownPod(args); err != nil {
25+
return fmt.Errorf("Failed to tear down pod, %v", err)
26+
}
27+
return nil
28+
}
29+
30+
func main() {
31+
log.Infof("Starting Libnetwork CNI plugin")
32+
skel.PluginMain(cmdAdd, cmdDel, version.PluginSupports("", "0.1.0", "0.2.0", version.Current()))
33+
}

0 commit comments

Comments
 (0)