forked from contiv/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrt.go
107 lines (88 loc) · 2.92 KB
/
crt.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
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
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 crt
import (
"encoding/json"
"reflect"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/crtclient"
"github.com/contiv/netplugin/crtclient/docker"
)
// CRT is an abstraction for Container Runtimes.
type CRT struct {
ContainerIf crtclient.ContainerIf
}
// Config is the configuration for the container runtype. The type is
// polymorphic to allow for multiple runtimes to be supported.
type Config struct {
CRT struct {
Type string
}
}
type containerIfTypes struct {
CRTType reflect.Type
ConfigType reflect.Type
}
var containerIfRegistry = map[string]containerIfTypes{
"docker": containerIfTypes{
CRTType: reflect.TypeOf(docker.Docker{}),
ConfigType: reflect.TypeOf(docker.Config{}),
},
}
// AttachEndpoint attaches an endpoint to a container.
func (c *CRT) AttachEndpoint(
contEpContext *crtclient.ContainerEPContext) error {
return c.ContainerIf.AttachEndpoint(contEpContext)
}
// DetachEndpoint detaches an endpoint from a container.
func (c *CRT) DetachEndpoint(contEpContext *crtclient.ContainerEPContext) error {
return c.ContainerIf.DetachEndpoint(contEpContext)
}
// GetContainerID obtains the container identifier for the given name.
func (c *CRT) GetContainerID(contName string) string {
return c.ContainerIf.GetContainerID(contName)
}
// GetContainerName obtains the container name from the identifier.
func (c *CRT) GetContainerName(contID string) (string, error) {
return c.ContainerIf.GetContainerName(contID)
}
// Deinit deinitializes the container interface.
func (c *CRT) Deinit() {
c.ContainerIf.Deinit()
}
// Init initializes the container runtime given a JSON configuration that
// conforms to the Config set type.
func (c *CRT) Init(configStr string) error {
cfg := &Config{}
err := json.Unmarshal([]byte(configStr), cfg)
if err != nil {
return err
}
if _, ok := containerIfRegistry[cfg.CRT.Type]; !ok {
return core.Errorf("unregistered container run time")
}
crtConfigType := containerIfRegistry[cfg.CRT.Type].ConfigType
crtConfig := reflect.New(crtConfigType).Interface()
err = json.Unmarshal([]byte(configStr), crtConfig)
if err != nil {
return err
}
crtType := containerIfRegistry[cfg.CRT.Type].CRTType
crtif := reflect.New(crtType).Interface()
c.ContainerIf = crtif.(crtclient.ContainerIf)
err = c.ContainerIf.Init(&crtclient.Config{V: crtConfig})
if err != nil {
return err
}
return nil
}