-
Notifications
You must be signed in to change notification settings - Fork 4
/
ifupdown_scheme.go
98 lines (77 loc) · 1.99 KB
/
ifupdown_scheme.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
pb "github.com/0xef53/kvmrun/api/services/network/v1"
)
var errSchemeNotFound = errors.New("scheme not found")
type Scheme interface {
Configure(pb.NetworkServiceClient, bool) error
Deconfigure(pb.NetworkServiceClient) error
}
func GetNetworkScheme(linkname string, configs ...string) (Scheme, error) {
for _, fname := range configs {
b, err := ioutil.ReadFile(fname)
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
netconf := []json.RawMessage{}
if err := json.Unmarshal(b, &netconf); err != nil {
return nil, err
}
for _, b := range netconf {
scheme := struct {
Type string `json:"scheme"`
Ifname string `json:"ifname"`
}{}
if err := json.Unmarshal(b, &scheme); err != nil {
return nil, err
}
if scheme.Ifname == linkname {
switch strings.ToLower(scheme.Type) {
case "vlan", "bridge-vlan":
opts := vlanSchemeOptions{}
if err := json.Unmarshal(b, &opts); err != nil {
return nil, err
}
return &vlanScheme{linkname, &opts}, nil
case "vxlan", "bridge-vxlan":
opts := vxlanSchemeOptions{}
if err := json.Unmarshal(b, &opts); err != nil {
return nil, err
}
return &vxlanScheme{linkname, &opts}, nil
case "routed":
opts := routerSchemeOptions{}
/*
TODO: cgroup classid dirty hack. Fix it.
*/
if cwd, err := os.Getwd(); err == nil {
opts.MachineName = filepath.Base(cwd)
} else {
return nil, fmt.Errorf("unable to determine machine name: %s", err)
}
if err := json.Unmarshal(b, &opts); err != nil {
return nil, err
}
return &routerScheme{linkname, &opts}, nil
case "bridge":
opts := bridgeSchemeOptions{}
if err := json.Unmarshal(b, &opts); err != nil {
return nil, err
}
return &bridgeScheme{linkname, &opts}, nil
}
}
}
}
return nil, errSchemeNotFound
}