Skip to content

Commit 07d28ad

Browse files
author
Vipin Jain
committed
Merge branch 'epif_cleanup'
2 parents 4c4cc47 + f231e87 commit 07d28ad

File tree

11 files changed

+365
-269
lines changed

11 files changed

+365
-269
lines changed

core/core.go

-24
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ type EndpointDriver interface {
8585
CreateEndpoint(id string) error
8686
DeleteEndpoint(value string) error
8787
MakeEndpointAddress() (*Address, error)
88-
GetEndpointContainerContext(id string) (*ContainerEpContext, error)
89-
GetContainerEpContextByContName(contName string) ([]ContainerEpContext, error)
90-
UpdateContainerId(id string, contId string) error
9188
}
9289

9390
type StateDriver interface {
@@ -108,24 +105,3 @@ type StateDriver interface {
108105
unmarshal func([]byte, interface{}) error) error
109106
ClearState(key string) error
110107
}
111-
112-
type ContainerEpContext struct {
113-
NewContName string
114-
CurrContName string
115-
InterfaceId string
116-
IpAddress string
117-
SubnetLen uint
118-
DefaultGw string
119-
}
120-
121-
type ContainerDriver interface {
122-
// Container driver provides a mechanism to interface with container
123-
// runtime to handle events create, start, die, stop, pause, etc.
124-
Driver
125-
Init(config *Config) error
126-
Deinit()
127-
AttachEndpoint(ctx *ContainerEpContext) error
128-
DetachEndpoint(ctx *ContainerEpContext) error
129-
GetContainerId(contName string) string
130-
GetContainerName(contName string) (string, error)
131-
}

crt/crt.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/***
2+
Copyright 2014 Cisco Systems Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package crt
17+
18+
import (
19+
"encoding/json"
20+
"errors"
21+
"reflect"
22+
23+
"github.com/contiv/netplugin/crtclient"
24+
"github.com/contiv/netplugin/crtclient/docker"
25+
)
26+
27+
type Crt struct {
28+
ContainerIf crtclient.ContainerIf
29+
}
30+
31+
type CrtConfig struct {
32+
Crt struct {
33+
Type string
34+
}
35+
}
36+
37+
type ContainerIfTypes struct {
38+
CrtType reflect.Type
39+
CrtConfigType reflect.Type
40+
}
41+
42+
var ContainerIfRegistry = map[string]ContainerIfTypes{
43+
"docker": ContainerIfTypes{
44+
CrtType: reflect.TypeOf(docker.Docker{}),
45+
CrtConfigType: reflect.TypeOf(docker.DockerConfig{}),
46+
},
47+
}
48+
49+
func (c *Crt) AttachEndpoint(
50+
contEpContext *crtclient.ContainerEpContext) error {
51+
return c.ContainerIf.AttachEndpoint(contEpContext)
52+
}
53+
54+
func (c *Crt) DetachEndpoint(contEpContext *crtclient.ContainerEpContext) error {
55+
return c.ContainerIf.DetachEndpoint(contEpContext)
56+
}
57+
58+
func (c *Crt) GetContainerId(contName string) string {
59+
return c.ContainerIf.GetContainerId(contName)
60+
}
61+
62+
func (c *Crt) GetContainerName(contId string) (string, error) {
63+
return c.ContainerIf.GetContainerName(contId)
64+
}
65+
66+
func (c *Crt) Deinit() {
67+
c.ContainerIf.Deinit()
68+
}
69+
70+
func (c *Crt) Init(configStr string) error {
71+
72+
cfg := &CrtConfig{}
73+
err := json.Unmarshal([]byte(configStr), cfg)
74+
if err != nil {
75+
return err
76+
}
77+
78+
if _, ok := ContainerIfRegistry[cfg.Crt.Type]; !ok {
79+
return errors.New("unregistered container run time")
80+
}
81+
82+
crtConfigType := ContainerIfRegistry[cfg.Crt.Type].CrtConfigType
83+
crtConfig := reflect.New(crtConfigType).Interface()
84+
err = json.Unmarshal([]byte(configStr), crtConfig)
85+
if err != nil {
86+
return err
87+
}
88+
89+
config := &crtclient.Config{V: crtConfig}
90+
crtType := ContainerIfRegistry[cfg.Crt.Type].CrtType
91+
crtif := reflect.New(crtType).Interface()
92+
c.ContainerIf = crtif.(crtclient.ContainerIf)
93+
err = c.ContainerIf.Init(config)
94+
if err != nil {
95+
return err
96+
}
97+
98+
return nil
99+
}

crt/crt_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/***
2+
Copyright 2014 Cisco Systems Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package crt
17+
18+
import (
19+
"testing"
20+
)
21+
22+
func TestCrtInit(t *testing.T) {
23+
configStr := `{
24+
"docker" : {
25+
"socket" : "unix:///var/run/docker.sock"
26+
},
27+
"crt" : {
28+
"type": "docker"
29+
}
30+
}`
31+
crt := Crt{}
32+
err := crt.Init(configStr)
33+
if err != nil {
34+
t.Fatalf("crt init failed: Error: %s", err)
35+
}
36+
defer func() { crt.Deinit() }()
37+
}
38+
39+
func TestCrtInitInvalidConfigMissingCrt(t *testing.T) {
40+
configStr := `{
41+
"docker" : {
42+
"socket" : "unix:///var/run/docker.sock"
43+
}
44+
}`
45+
crt := Crt{}
46+
err := crt.Init(configStr)
47+
if err == nil {
48+
t.Fatalf("crt init succeeded!")
49+
}
50+
}
51+
52+
func TestCrtInitInvalidConfigMissingCrtIf(t *testing.T) {
53+
configStr := `{
54+
"crt" : {
55+
"type": "docker"
56+
}
57+
}`
58+
crt := Crt{}
59+
err := crt.Init(configStr)
60+
if err == nil {
61+
t.Fatalf("crt init succeeded!")
62+
}
63+
}
64+
65+
func TestCrtInitInvalidConfigInvalidCrt(t *testing.T) {
66+
configStr := `{
67+
"crt" : {
68+
"type": "rocket"
69+
}
70+
}`
71+
crt := Crt{}
72+
err := crt.Init(configStr)
73+
if err == nil {
74+
t.Fatalf("crt init succeeded!")
75+
}
76+
}

crtclient/crtclient.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/***
2+
Copyright 2014 Cisco Systems Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package crtclient
17+
18+
// container runtime client interface implementations
19+
20+
type Config struct {
21+
// Config object parsed from a json styled config
22+
V interface{}
23+
}
24+
25+
type ContainerEpContext struct {
26+
NewContName string
27+
CurrContName string
28+
InterfaceId string
29+
IpAddress string
30+
SubnetLen uint
31+
DefaultGw string
32+
}
33+
34+
type ContainerIf interface {
35+
Init(config *Config) error
36+
Deinit()
37+
AttachEndpoint(ctx *ContainerEpContext) error
38+
DetachEndpoint(ctx *ContainerEpContext) error
39+
GetContainerId(contName string) string
40+
GetContainerName(contName string) (string, error)
41+
}

drivers/dockerdriver.go crtclient/docker/docker.go

+22-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
*/
1515

16-
package drivers
16+
package docker
1717

1818
import (
1919
"errors"
@@ -25,30 +25,34 @@ import (
2525
"path"
2626
"strconv"
2727

28-
"github.com/contiv/netplugin/core"
28+
"github.com/contiv/netplugin/crtclient"
2929
"github.com/vishvananda/netlink"
3030
// "github.com/vishvananda/netns"
3131
)
3232

33-
type DockerDriverConfig struct {
33+
type DockerConfig struct {
3434
Docker struct {
3535
Socket string
3636
}
3737
}
3838

39-
type DockerDriver struct {
39+
type Docker struct {
4040
Client *dockerclient.DockerClient
4141
}
4242

43-
func (d *DockerDriver) Init(config *core.Config) error {
43+
func (d *Docker) Init(config *crtclient.Config) error {
4444
if config == nil {
45-
return &core.Error{Desc: fmt.Sprintf("Invalid arguments. cfg: %v", config)}
45+
return errors.New("null config!")
4646
}
4747

48-
cfg, ok := config.V.(*DockerDriverConfig)
48+
cfg, ok := config.V.(*DockerConfig)
4949

5050
if !ok {
51-
return &core.Error{Desc: "Invalid config type passed!"}
51+
return errors.New("Invalid config type passed!")
52+
}
53+
54+
if cfg.Docker.Socket == "" {
55+
return errors.New(fmt.Sprintf("Invalid arguments. cfg: %v", config))
5256
}
5357

5458
// TODO: ADD TLS support
@@ -57,10 +61,10 @@ func (d *DockerDriver) Init(config *core.Config) error {
5761
return nil
5862
}
5963

60-
func (d *DockerDriver) Deinit() {
64+
func (d *Docker) Deinit() {
6165
}
6266

63-
func (d *DockerDriver) getContPid(contName string) (string, error) {
67+
func (d *Docker) getContPid(contName string) (string, error) {
6468
contInfo, err := d.Client.InspectContainer(contName)
6569
if err != nil {
6670
return "", errors.New("couldn't obtain container info")
@@ -93,7 +97,7 @@ func setIfNs(ifname string, pid int) error {
9397
// Note: most of the work in this function is a temporary workaround for
9498
// what docker daemon would eventually do; in the meanwhile
9599
// the essense of the logic is borrowed from pipework
96-
func (d *DockerDriver) moveIfToContainer(ifId string, contName string) error {
100+
func (d *Docker) moveIfToContainer(ifId string, contName string) error {
97101

98102
// log.Printf("Moving interface '%s' into container '%s' \n", ifId, contName)
99103

@@ -137,7 +141,7 @@ func (d *DockerDriver) moveIfToContainer(ifId string, contName string) error {
137141
return err
138142
}
139143

140-
func (d *DockerDriver) cleanupNetns(contName string) error {
144+
func (d *Docker) cleanupNetns(contName string) error {
141145
contPid, err := d.getContPid(contName)
142146
if err != nil {
143147
return err
@@ -153,7 +157,7 @@ func (d *DockerDriver) cleanupNetns(contName string) error {
153157
}
154158

155159
/*
156-
func (d *DockerDriver) configureIfAddress(ctx *core.ContainerEpContext) error {
160+
func (d *Docker) configureIfAddress(ctx *crtclient.ContainerEpContext) error {
157161
158162
log.Printf("configuring ip: addr -%s/%d- on if %s for container %s\n",
159163
ctx.IpAddress, ctx.SubnetLen, ctx.InterfaceId, ctx.NewContName)
@@ -228,7 +232,7 @@ func (d *DockerDriver) configureIfAddress(ctx *core.ContainerEpContext) error {
228232
return err
229233
}
230234
*/
231-
func (d *DockerDriver) configureIfAddress(ctx *core.ContainerEpContext) error {
235+
func (d *Docker) configureIfAddress(ctx *crtclient.ContainerEpContext) error {
232236

233237
log.Printf("configuring ip: addr -%s/%d- on if %s for container %s\n",
234238
ctx.IpAddress, ctx.SubnetLen, ctx.InterfaceId, ctx.NewContName)
@@ -268,7 +272,7 @@ func (d *DockerDriver) configureIfAddress(ctx *core.ContainerEpContext) error {
268272

269273
// performs funtion to configure the network access and policies
270274
// before the container becomes active
271-
func (d *DockerDriver) AttachEndpoint(ctx *core.ContainerEpContext) error {
275+
func (d *Docker) AttachEndpoint(ctx *crtclient.ContainerEpContext) error {
272276

273277
err := d.moveIfToContainer(ctx.InterfaceId, ctx.NewContName)
274278
if err != nil {
@@ -289,7 +293,7 @@ func (d *DockerDriver) AttachEndpoint(ctx *core.ContainerEpContext) error {
289293
}
290294

291295
// uninstall the policies and configuration during container attach
292-
func (d *DockerDriver) DetachEndpoint(ctx *core.ContainerEpContext) error {
296+
func (d *Docker) DetachEndpoint(ctx *crtclient.ContainerEpContext) error {
293297
var err error
294298

295299
// log.Printf("Detached called for container %s with %s interface\n",
@@ -303,7 +307,7 @@ func (d *DockerDriver) DetachEndpoint(ctx *core.ContainerEpContext) error {
303307
return err
304308
}
305309

306-
func (d *DockerDriver) GetContainerId(contName string) string {
310+
func (d *Docker) GetContainerId(contName string) string {
307311
contInfo, err := d.Client.InspectContainer(contName)
308312
if err != nil {
309313
log.Printf("could not get contId for container %s, err '%s' \n",
@@ -319,7 +323,7 @@ func (d *DockerDriver) GetContainerId(contName string) string {
319323
return contInfo.Id
320324
}
321325

322-
func (d *DockerDriver) GetContainerName(contId string) (string, error) {
326+
func (d *Docker) GetContainerName(contId string) (string, error) {
323327
contInfo, err := d.Client.InspectContainer(contId)
324328
if err != nil {
325329
log.Printf("could not get contName for container %s, err '%s' \n",

0 commit comments

Comments
 (0)