forked from contiv/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetplugin.go
137 lines (115 loc) · 3.65 KB
/
netplugin.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
/***
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 plugin
import (
"sync"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/utils"
)
// implements the generic Plugin interface
// Config has the configuration for the plugin
type Config struct {
Drivers struct {
Network string `json:"network"`
Endpoint string `json:"endpoint"`
State string `json:"state"`
}
Instance core.InstanceInfo `json:"plugin-instance"`
}
// NetPlugin is the configuration struct for the plugin bus. Network and
// Endpoint drivers are all present in `drivers/` and state drivers are present
// in `state/`.
type NetPlugin struct {
sync.Mutex
ConfigFile string
NetworkDriver core.NetworkDriver
StateDriver core.StateDriver
}
// Init initializes the NetPlugin instance via the configuration string passed.
func (p *NetPlugin) Init(pluginConfig Config, configStr string) error {
var err error
if pluginConfig.Instance.HostLabel == "" {
return core.Errorf("empty host-label passed")
}
// initialize state driver
p.StateDriver, err = utils.NewStateDriver(pluginConfig.Drivers.State, configStr)
if err != nil {
return err
}
defer func() {
if err != nil {
utils.ReleaseStateDriver()
}
}()
instanceInfo := &core.InstanceInfo{
HostLabel: pluginConfig.Instance.HostLabel,
VtepIP: pluginConfig.Instance.VtepIP,
VlanIntf: pluginConfig.Instance.VlanIntf,
StateDriver: p.StateDriver,
}
// initialize network driver
p.NetworkDriver, err = utils.NewNetworkDriver(pluginConfig.Drivers.Network,
configStr, instanceInfo)
if err != nil {
return err
}
defer func() {
if err != nil {
p.NetworkDriver.Deinit()
}
}()
return nil
}
// Deinit is a destructor for the NetPlugin configuration.
func (p *NetPlugin) Deinit() {
if p.NetworkDriver != nil {
p.NetworkDriver.Deinit()
p.NetworkDriver = nil
}
if p.StateDriver != nil {
utils.ReleaseStateDriver()
p.StateDriver = nil
}
}
// CreateNetwork creates a network for a given ID.
func (p *NetPlugin) CreateNetwork(id string) error {
return p.NetworkDriver.CreateNetwork(id)
}
// DeleteNetwork deletes a network provided by the ID.
func (p *NetPlugin) DeleteNetwork(id string) error {
return p.NetworkDriver.DeleteNetwork(id)
}
// FetchNetwork retrieves a network's state given an ID.
func (p *NetPlugin) FetchNetwork(id string) (core.State, error) {
return nil, core.Errorf("Not implemented")
}
// CreateEndpoint creates an endpoint for a given ID.
func (p *NetPlugin) CreateEndpoint(id string) error {
return p.NetworkDriver.CreateEndpoint(id)
}
// DeleteEndpoint destroys an endpoint for an ID.
func (p *NetPlugin) DeleteEndpoint(id string) error {
return p.NetworkDriver.DeleteEndpoint(id)
}
// FetchEndpoint retrieves an endpoint's state for a given ID
func (p *NetPlugin) FetchEndpoint(id string) (core.State, error) {
return nil, core.Errorf("Not implemented")
}
// CreatePeerHost creates an peer host for a given ID.
func (p *NetPlugin) CreatePeerHost(id string) error {
return p.NetworkDriver.CreatePeerHost(id)
}
// DeletePeerHost destroys a peer host for an ID.
func (p *NetPlugin) DeletePeerHost(id string) error {
return p.NetworkDriver.DeletePeerHost(id)
}