-
Notifications
You must be signed in to change notification settings - Fork 0
/
cni_controller.go
74 lines (58 loc) · 1.69 KB
/
cni_controller.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
package cni
import (
"fmt"
"github.com/containernetworking/cni/libcni"
"github.com/containernetworking/cni/pkg/types"
)
//go:generate counterfeiter -o ../fakes/cni_library.go --fake-name CNILibrary . cniLibrary
type cniLibrary interface {
libcni.CNI
}
type CNIController struct {
CNIConfig libcni.CNI
NetworkConfigList *libcni.NetworkConfigList
}
func (c *CNIController) Up(namespacePath, handle string, metadata map[string]interface{}, legacyNetConf map[string]interface{}) (types.Result, error) {
var result types.Result
var err error
if c.NetworkConfigList == nil {
return result, nil
}
runtimeConfig := &libcni.RuntimeConf{
ContainerID: handle,
NetNS: namespacePath,
IfName: "eth0",
}
extraKeys := map[string]interface{}{}
if len(metadata) > 0 {
extraKeys["metadata"] = metadata
}
if len(legacyNetConf) > 0 {
extraKeys["runtimeConfig"] = legacyNetConf
}
for i, networkConfig := range c.NetworkConfigList.Plugins {
networkConfig, err = libcni.InjectConf(networkConfig, extraKeys)
if err != nil {
return nil, fmt.Errorf("adding extra data to CNI config: %s", err)
}
c.NetworkConfigList.Plugins[i] = networkConfig
}
result, err = c.CNIConfig.AddNetworkList(c.NetworkConfigList, runtimeConfig)
if err != nil {
return nil, fmt.Errorf("add network list failed: %s", err)
}
return result, nil
}
func (c *CNIController) Down(namespacePath, handle string) error {
var err error
runtimeConfig := &libcni.RuntimeConf{
ContainerID: handle,
NetNS: namespacePath,
IfName: "eth0",
}
err = c.CNIConfig.DelNetworkList(c.NetworkConfigList, runtimeConfig)
if err != nil {
return fmt.Errorf("del network failed: %s", err)
}
return nil
}