Skip to content

Commit 8bf4250

Browse files
authored
make ovs socket file path as configurable property (#142)
* make ovs socket file path as configurable property Signed-off-by: Periyasamy Palanisamy <[email protected]> * address review comments Signed-off-by: Periyasamy Palanisamy <[email protected]> * address review comments #2 Signed-off-by: Periyasamy Palanisamy <[email protected]>
1 parent 6f16405 commit 8bf4250

File tree

3 files changed

+111
-10
lines changed

3 files changed

+111
-10
lines changed

docs/cni-plugin.md

+29
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ Another example with a trunk port and jumbo frames:
4545
* `mtu` (integer, optional): MTU.
4646
* `trunk` (optional): List of VLAN ID's and/or ranges of accepted VLAN
4747
ID's.
48+
* `configuration_path` (optional): configuration file containing ovsdb
49+
socket file path, etc.
50+
51+
### Flatfile Configuation
52+
53+
There is one option for flat file configuration:
54+
55+
* `configuration_path`: A file path to a OVS CNI configuration file.
56+
57+
OVS CNI will look for the configuration in these locations, in this order:
58+
59+
* The location specified by the `configuration_path` option.
60+
* `/etc/kubernetes/cni/net.d/ovs.d/ovs.conf`
61+
* `/etc/cni/net.d/ovs.d/ovs.conf`
62+
63+
You may specify the `configuration_path` to point to another location should it be desired.
64+
65+
Any options added to the `ovs.conf` are overridden by configuration options that are in the
66+
CNI configuration (e.g. in a custom resource `NetworkAttachmentDefinition` used by Multus CNI
67+
or in the first file ASCII-betically in the CNI configuration directory -- which is
68+
`/etc/cni/net.d/` by default).
69+
70+
The sample content of ovs.conf (in JSON format) is as follows:
71+
72+
```json
73+
{
74+
"socket_file": "/usr/local/var/run/openvswitch/db.sock"
75+
}
76+
```
4877

4978
## Manual Testing
5079

pkg/ovsdb/ovsdb.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ func NewOvsDriver(ovsSocket string) (*OvsDriver, error) {
5353
}
5454

5555
// Create a new OVS driver for a bridge with Unix socket
56-
func NewOvsBridgeDriver(bridgeName string) (*OvsBridgeDriver, error) {
56+
func NewOvsBridgeDriver(bridgeName, socketFile string) (*OvsBridgeDriver, error) {
5757
ovsDriver := new(OvsBridgeDriver)
5858

59-
ovsDB, err := libovsdb.ConnectWithUnixSocket("/var/run/openvswitch/db.sock")
59+
if socketFile == "" {
60+
socketFile = "/var/run/openvswitch/db.sock"
61+
}
62+
63+
ovsDB, err := libovsdb.ConnectWithUnixSocket(socketFile)
6064
if err != nil {
61-
return nil, fmt.Errorf("failed to connect to ovsdb error: %v", err)
65+
return nil, fmt.Errorf("failed to connect to ovsdb socket %s: error: %v", socketFile, err)
6266
}
6367

6468
// Setup state

pkg/plugin/plugin.go

+75-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import (
2424
"encoding/json"
2525
"errors"
2626
"fmt"
27+
"io/ioutil"
2728
"log"
2829
"net"
30+
"os"
2931
"runtime"
3032
"sort"
3133
"time"
@@ -36,6 +38,7 @@ import (
3638
"github.com/containernetworking/plugins/pkg/ip"
3739
"github.com/containernetworking/plugins/pkg/ipam"
3840
"github.com/containernetworking/plugins/pkg/ns"
41+
"github.com/imdario/mergo"
3942
"github.com/j-keck/arping"
4043
"github.com/vishvananda/netlink"
4144

@@ -51,11 +54,13 @@ const (
5154

5255
type netConf struct {
5356
types.NetConf
54-
BrName string `json:"bridge,omitempty"`
55-
VlanTag *uint `json:"vlan"`
56-
MTU int `json:"mtu"`
57-
Trunk []*trunk `json:"trunk,omitempty"`
58-
DeviceID string `json:"deviceID"` // PCI address of a VF in valid sysfs format
57+
BrName string `json:"bridge,omitempty"`
58+
VlanTag *uint `json:"vlan"`
59+
MTU int `json:"mtu"`
60+
Trunk []*trunk `json:"trunk,omitempty"`
61+
DeviceID string `json:"deviceID"` // PCI address of a VF in valid sysfs format
62+
ConfigurationPath string `json:"configuration_path"`
63+
SocketFile string `json:"socket_file"`
5964
}
6065

6166
type trunk struct {
@@ -112,6 +117,61 @@ func loadNetConf(bytes []byte) (*netConf, error) {
112117
return netconf, nil
113118
}
114119

120+
func loadFlatNetConf(configPath string) (*netConf, error) {
121+
confFiles := getOvsConfFiles()
122+
if configPath != "" {
123+
confFiles = append([]string{configPath}, confFiles...)
124+
}
125+
126+
// loop through the path and parse the JSON config
127+
flatNetConf := &netConf{}
128+
for _, confFile := range confFiles {
129+
confExists, err := pathExists(confFile)
130+
if err != nil {
131+
return nil, fmt.Errorf("error checking ovs config file: error: %v", err)
132+
}
133+
if confExists {
134+
jsonFile, err := os.Open(confFile)
135+
if err != nil {
136+
return nil, fmt.Errorf("open ovs config file %s error: %v", confFile, err)
137+
}
138+
defer jsonFile.Close()
139+
jsonBytes, err := ioutil.ReadAll(jsonFile)
140+
if err != nil {
141+
return nil, fmt.Errorf("load ovs config file %s: error: %v", confFile, err)
142+
}
143+
if err := json.Unmarshal(jsonBytes, flatNetConf); err != nil {
144+
return nil, fmt.Errorf("parse ovs config file %s: error: %v", confFile, err)
145+
}
146+
break
147+
}
148+
}
149+
150+
return flatNetConf, nil
151+
}
152+
153+
func mergeConf(netconf, flatNetConf *netConf) (*netConf, error) {
154+
if err := mergo.Merge(netconf, flatNetConf); err != nil {
155+
return nil, fmt.Errorf("merge with ovs config file: error: %v", err)
156+
}
157+
return netconf, nil
158+
}
159+
160+
func pathExists(path string) (bool, error) {
161+
_, err := os.Stat(path)
162+
if err == nil {
163+
return true, nil
164+
}
165+
if os.IsNotExist(err) {
166+
return false, nil
167+
}
168+
return false, err
169+
}
170+
171+
func getOvsConfFiles() []string {
172+
return []string{"/etc/kubernetes/cni/net.d/ovs.d/ovs.conf", "/etc/cni/net.d/ovs.d/ovs.conf"}
173+
}
174+
115175
func generateRandomMac() net.HardwareAddr {
116176
prefix := []byte{0x02, 0x00, 0x00} // local unicast prefix
117177
suffix := make([]byte, 3)
@@ -284,6 +344,14 @@ func CmdAdd(args *skel.CmdArgs) error {
284344
if err != nil {
285345
return err
286346
}
347+
flatNetConf, err := loadFlatNetConf(netconf.ConfigurationPath)
348+
if err != nil {
349+
return err
350+
}
351+
netconf, err = mergeConf(netconf, flatNetConf)
352+
if err != nil {
353+
return err
354+
}
287355

288356
var vlanTagNum uint = 0
289357
trunks := make([]uint, 0)
@@ -306,7 +374,7 @@ func CmdAdd(args *skel.CmdArgs) error {
306374
return err
307375
}
308376

309-
ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName)
377+
ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName, netconf.SocketFile)
310378
if err != nil {
311379
return err
312380
}
@@ -480,7 +548,7 @@ func CmdDel(args *skel.CmdArgs) error {
480548
return err
481549
}
482550

483-
ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName)
551+
ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName, netconf.SocketFile)
484552
if err != nil {
485553
return err
486554
}

0 commit comments

Comments
 (0)