Skip to content

Commit 3ca429a

Browse files
committed
Refactor and Restore changes
Signed-off-by: Abhinandan Prativadi <[email protected]>
1 parent 5fa7fd9 commit 3ca429a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+975
-3938
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +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-
go build -o "bin/cniserver" ./cmd/cni_
40-
go build -o "bin/cnictl" ./cni/
39+
go build -o "bin/cniserver" ./cmd/cni_server
40+
go build -o "bin/cnictl" ./cmd/dnet_cni/
4141
clean:
4242
@echo "🐳 $@"
4343
@if [ -d bin ]; then \

api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ func procAttachBackend(c libnetwork.NetworkController, vars map[string]string, b
712712
if err != nil {
713713
return nil, convertNetworkError(err)
714714
}
715+
715716
if bk.SandboxKey != "" {
716717
err = sb.SetKey(bk.SandboxKey)
717718
if err != nil {
@@ -738,7 +739,6 @@ func procDetachBackend(c libnetwork.NetworkController, vars map[string]string, b
738739
if err != nil {
739740
return nil, convertNetworkError(err)
740741
}
741-
742742
return nil, &successResponse
743743
}
744744

api/types.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ type serviceDelete struct {
9696
Force bool `json:"force"`
9797
}
9898

99-
// extraHost represents the extra host object
100-
type extraHost struct {
101-
Name string `json:"name"`
102-
Address string `json:"address"`
103-
}
104-
10599
// endpointInfo contants the endpoint info for https response message on endpoint creation
106100
type endpointInfo struct {
107101
ID string `json:"id"`
@@ -112,6 +106,28 @@ type endpointInfo struct {
112106
GatewayIPv6 net.IP `json:"gateway_ipv6"`
113107
}
114108

109+
// extraHost represents the extra host object
110+
type extraHost struct {
111+
Name string `json:"name"`
112+
Address string `json:"address"`
113+
}
114+
115+
// SandboxMetadata holds the metadata related to sandox (config)
116+
type SandboxMetadata struct {
117+
ContainerID string `json:"container_id"`
118+
HostName string `json:"host_name"`
119+
DomainName string `json:"domain_name"`
120+
HostsPath string `json:"hosts_path"`
121+
ResolvConfPath string `json:"resolv_conf_path"`
122+
DNS []string `json:"dns"`
123+
ExtraHosts []extraHost `json:"extra_hosts"`
124+
UseExternalKey bool `json:"use_external_key"`
125+
UseDefaultSandbox bool `json:"use_default_sandbox"`
126+
ExposedPorts []types.TransportPort `json:"exposed_ports"`
127+
PortMapping []types.PortBinding `json:"port_mapping"`
128+
ExternalKey string `json:"external_key"`
129+
}
130+
115131
func getEndpointInfo(ep libnetwork.Endpoint) endpointInfo {
116132
epInfo := endpointInfo{ID: ep.ID()}
117133

client/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
7171
}
7272
}
7373

74-
var icList []ipamConf
74+
var icList []IPAMConf
7575
if *flSubnet != "" {
76-
ic := ipamConf{
76+
ic := IPAMConf{
7777
PreferredPool: *flSubnet,
7878
}
7979

client/types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ type SandboxResource struct {
3535
/***********
3636
Body types
3737
************/
38-
type ipamConf struct {
38+
39+
// IPAMConf is the ipam configution used during network create
40+
type IPAMConf struct {
3941
PreferredPool string
4042
SubPool string
4143
Gateway string
@@ -47,7 +49,7 @@ type NetworkCreate struct {
4749
Name string `json:"name"`
4850
ID string `json:"id"`
4951
NetworkType string `json:"network_type"`
50-
IPv4Conf []ipamConf `json:"ipv4_configuration"`
52+
IPv4Conf []IPAMConf `json:"ipv4_configuration"`
5153
DriverOpts map[string]string `json:"driver_opts"`
5254
NetworkOpts map[string]string `json:"network_opts"`
5355
}
@@ -102,7 +104,7 @@ type sandboxParentUpdate struct {
102104
Address string `json:"address"`
103105
}
104106

105-
// endpointInfo contants the endpoint info for http response message on endpoint creation
107+
// EndpointInfo contants the endpoint info for http response message on endpoint creation
106108
type EndpointInfo struct {
107109
ID string `json:"id"`
108110
Address net.IPNet `json:"address"`

cmd/cni_/cni_.go renamed to cmd/cni_server/cni_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func cniApp(stdout, stderr io.Writer) error {
2929
app.Usage = "A cni side car for libnetwork daemon."
3030
app.Flags = cniserverFlags
3131
app.Before = processFlags
32-
//app.Commands = cniCommands
32+
//app.Commands = cniCommands // TODO: Add cni commands to improve debuggability
3333

3434
app.Run(os.Args)
3535
return nil

cmd/cni_/flags_.go renamed to cmd/cni_server/flags.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"os"
66

77
"github.com/codegangsta/cli"
8-
cniserver "github.com/docker/libnetwork/pkg/server/cniserver"
98
"github.com/sirupsen/logrus"
9+
10+
cniserver "github.com/docker/libnetwork/provider/cni/server"
1011
)
1112

1213
var (
@@ -42,13 +43,14 @@ func processFlags(c *cli.Context) error {
4243

4344
cniService, err := cniserver.NewCniService(c.String("sock"), c.String("dnet-address"), c.String("dnet-port"))
4445
if err != nil {
45-
return fmt.Errorf("faile to create cni service: %v", err)
46+
return fmt.Errorf("failed to create cni service: %v", err)
4647
}
4748
serverCloseChan := make(chan struct{})
48-
if err := cniService.InitCniService(serverCloseChan); err != nil {
49-
fmt.Printf("Failed to initialize CNI server: \n", err)
49+
if err := cniService.Init(serverCloseChan); err != nil {
50+
logrus.Errorf("Failed to initialize CNI server: %v", err)
5051
os.Exit(1)
5152
}
53+
// Wait on till the server closes
5254
<-serverCloseChan
5355
return nil
5456
}

cmd/dnet/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func runContainerCreate(c *cli.Context) {
4747
}
4848

4949
sc := client.SandboxCreate{ContainerID: c.Args()[0]}
50-
obj, _, err := netutils.ReadBody(epConn.conn.HttpCall("POST", "/sandboxes", sc, nil))
50+
obj, _, err := netutils.ReadBody(epConn.conn.HTTPCall("POST", "/sandboxes", sc, nil))
5151
if err != nil {
5252
fmt.Printf("POST failed during create container: %v\n", err)
5353
os.Exit(1)
@@ -72,7 +72,7 @@ func runContainerRm(c *cli.Context) {
7272
os.Exit(1)
7373
}
7474

75-
obj, _, err := netutils.ReadBody(epConn.conn.HttpCall("GET", "/sandboxes?partial-container-id="+c.Args()[0], nil, nil))
75+
obj, _, err := netutils.ReadBody(epConn.conn.HTTPCall("GET", "/sandboxes?partial-container-id="+c.Args()[0], nil, nil))
7676
if err != nil {
7777
fmt.Printf("GET failed during container id lookup: %v\n", err)
7878
os.Exit(1)
@@ -89,7 +89,7 @@ func runContainerRm(c *cli.Context) {
8989
os.Exit(1)
9090
}
9191

92-
_, _, err = netutils.ReadBody(epConn.conn.HttpCall("DELETE", "/sandboxes/"+sbList[0].ID, nil, nil))
92+
_, _, err = netutils.ReadBody(epConn.conn.HTTPCall("DELETE", "/sandboxes/"+sbList[0].ID, nil, nil))
9393
if err != nil {
9494
fmt.Printf("DELETE of sandbox id %s failed: %v", sbList[0].ID, err)
9595
os.Exit(1)
@@ -98,7 +98,7 @@ func runContainerRm(c *cli.Context) {
9898

9999
func runDockerCommand(c *cli.Context, cmd string) {
100100
_, stdout, stderr := term.StdStreams()
101-
oldcli := client.NewNetworkCli(stdout, stderr, epConn.conn.HttpCall)
101+
oldcli := client.NewNetworkCli(stdout, stderr, epConn.conn.HTTPCall)
102102
var args []string
103103
args = append(args, cmd)
104104
if c.Bool("h") {

cmd/dnet/dnet.go

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import (
1616

1717
"github.com/BurntSushi/toml"
1818
"github.com/codegangsta/cli"
19+
"github.com/docker/docker/api/types/network"
1920
"github.com/docker/docker/opts"
2021
"github.com/docker/docker/pkg/discovery"
2122
"github.com/docker/docker/pkg/reexec"
22-
23-
"github.com/docker/docker/api/types/network"
2423
"github.com/docker/docker/pkg/term"
24+
"github.com/gorilla/mux"
25+
"github.com/sirupsen/logrus"
26+
"golang.org/x/net/context"
27+
2528
"github.com/docker/libnetwork"
2629
"github.com/docker/libnetwork/api"
2730
"github.com/docker/libnetwork/cluster"
@@ -31,10 +34,9 @@ import (
3134
"github.com/docker/libnetwork/netlabel"
3235
"github.com/docker/libnetwork/netutils"
3336
"github.com/docker/libnetwork/options"
37+
"github.com/docker/libnetwork/provider"
38+
"github.com/docker/libnetwork/provider/cni/cniapi"
3439
"github.com/docker/libnetwork/types"
35-
"github.com/gorilla/mux"
36-
"github.com/sirupsen/logrus"
37-
"golang.org/x/net/context"
3840
)
3941

4042
const (
@@ -43,11 +45,12 @@ const (
4345
// DefaultHTTPPort is the default http port used by dnet
4446
DefaultHTTPPort = 2389
4547
// DefaultUnixSocket exported
46-
DefaultUnixSocket = "/var/run/dnet.sock"
47-
cfgFileEnv = "LIBNETWORK_CFG"
48-
defaultCfgFile = "/etc/default/libnetwork.toml"
49-
defaultHeartbeat = time.Duration(10) * time.Second
50-
ttlFactor = 2
48+
DefaultUnixSocket = "/var/run/dnet.sock"
49+
cfgFileEnv = "LIBNETWORK_CFG"
50+
defaultCfgFile = "/etc/default/libnetwork.toml"
51+
defaultHeartbeat = time.Duration(10) * time.Second
52+
ttlFactor = 2
53+
defaultProviderTimeout = 120 // default time to fetch state from provider
5154
)
5255

5356
var epConn *dnetConnection
@@ -130,18 +133,21 @@ func processConfig(cfg *config.Config) ([]config.Option, error) {
130133
}
131134

132135
// Retry discovery for 2 minutes before exiting
133-
for {
134-
select {
135-
case <-time.After(2 * time.Minute):
136-
return nil, fmt.Errorf("failed to initialize discovery")
137-
default:
138-
dOptions, err := startDiscovery(&cfg.Cluster)
139-
if err == nil {
140-
options = append(options, dOptions...)
141-
return options, nil
136+
if cfg.Cluster.Discovery != "" {
137+
for {
138+
select {
139+
case <-time.After(2 * time.Minute):
140+
return nil, fmt.Errorf("failed to initialize discovery")
141+
default:
142+
dOptions, err := startDiscovery(&cfg.Cluster)
143+
if err == nil {
144+
options = append(options, dOptions...)
145+
return options, nil
146+
}
142147
}
143148
}
144149
}
150+
return options, nil
145151
}
146152

147153
func startDiscovery(cfg *config.ClusterCfg) ([]config.Option, error) {
@@ -241,7 +247,7 @@ func createDefaultNetwork(c libnetwork.NetworkController) {
241247
}
242248

243249
type dnetConnection struct {
244-
conn *netutils.HttpConnection
250+
conn *netutils.HTTPConnection
245251
Orchestration *NetworkOrchestration
246252
configEvent chan cluster.ConfigEventType
247253
}
@@ -254,7 +260,8 @@ type NetworkOrchestration struct {
254260
Peer string
255261
}
256262

257-
func (d *dnetConnection) dnetDaemon(cfgFile string) error {
263+
func (d *dnetConnection) dnetDaemon(cfgFile string, provider string) error {
264+
logrus.Infof("Starting DnetDaemon")
258265
if err := startTestDriver(); err != nil {
259266
return fmt.Errorf("failed to start test driver: %v", err)
260267
}
@@ -264,10 +271,14 @@ func (d *dnetConnection) dnetDaemon(cfgFile string) error {
264271
if err == nil {
265272
cOptions, err = processConfig(cfg)
266273
if err != nil {
267-
fmt.Errorf("failed to process config: %v", err)
274+
return fmt.Errorf("failed to process config: %v", err)
268275
}
269276
} else {
270-
logrus.Errorf("failed to parse config: %v", err)
277+
return fmt.Errorf("failed to parse config: %v", err)
278+
}
279+
280+
if provider != "" {
281+
cfg.Daemon.Provider = attachDnetProvider(provider)
271282
}
272283

273284
bridgeConfig := options.Generic{
@@ -279,9 +290,19 @@ func (d *dnetConnection) dnetDaemon(cfgFile string) error {
279290

280291
cOptions = append(cOptions, config.OptionDriverConfig("bridge", bridgeOption))
281292

293+
// If this is a restore ,then fetch active sandboxes from api server.
294+
if cfg.Daemon.Provider != nil {
295+
sbOptions, err := fetchActiveSandboxes(cfg.Daemon.Provider)
296+
if err != nil {
297+
return err
298+
}
299+
if sbOptions != nil {
300+
cOptions = append(cOptions, sbOptions)
301+
}
302+
}
282303
controller, err := libnetwork.New(cOptions...)
283304
if err != nil {
284-
fmt.Println("Error starting dnetDaemon :", err)
305+
fmt.Println("Error starting DnetDaemon :", err)
285306
return err
286307
}
287308
controller.SetClusterProvider(d)
@@ -440,7 +461,7 @@ func newDnetConnection(val string) (*dnetConnection, error) {
440461
}
441462

442463
return &dnetConnection{
443-
&netutils.HttpConnection{
464+
&netutils.HTTPConnection{
444465
Proto: protoAddrParts[0],
445466
Addr: protoAddrParts[1],
446467
},
@@ -460,3 +481,34 @@ func ipamOption(bridgeName string) libnetwork.NetworkOption {
460481
}
461482
return nil
462483
}
484+
485+
func attachDnetProvider(provider string) provider.DnetProvider {
486+
switch provider {
487+
case "cni":
488+
return cniapi.NewDnetCniClient()
489+
default:
490+
return nil
491+
}
492+
}
493+
494+
func fetchActiveSandboxes(provider provider.DnetProvider) (config.Option, error) {
495+
x := time.Duration(2 * time.Second)
496+
var err error
497+
var sbOptions map[string]interface{}
498+
for x < defaultProviderTimeout {
499+
sbOptions, err = provider.FetchActiveSandboxes()
500+
if err == nil {
501+
goto success
502+
}
503+
logrus.Errorf("Retry:failed to fetch active sandbox: %b", err)
504+
time.Sleep(x * time.Second)
505+
x = x * 2
506+
}
507+
return nil, fmt.Errorf("failed to fetch active sandbox: %b", err)
508+
success:
509+
logrus.Infof("Active sandboxes are: {%+v}", sbOptions)
510+
if len(sbOptions) != 0 {
511+
return config.OptionActiveSandboxes(sbOptions), nil
512+
}
513+
return nil, nil
514+
}

cmd/dnet/flags.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ var (
3333
Value: "/etc/default/libnetwork.toml",
3434
Usage: "Configuration file",
3535
},
36+
cli.StringFlag{
37+
Name: "p, -provider-interface",
38+
Usage: "Provider for Dnet. Accepted values cni",
39+
},
3640
}
3741
)
3842

@@ -75,7 +79,9 @@ func processFlags(c *cli.Context) error {
7579
}
7680

7781
if c.Bool("d") {
78-
err = epConn.dnetDaemon(c.String("c"))
82+
cfgFile := c.String("c")
83+
provider := c.String("p")
84+
err = epConn.dnetDaemon(cfgFile, provider)
7985
if err != nil {
8086
logrus.Errorf("dnet Daemon exited with an error : %v", err)
8187
os.Exit(1)

0 commit comments

Comments
 (0)