Skip to content

Commit

Permalink
make ovs socket file path as configurable property (#142)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
pperiyasamy authored Nov 27, 2020
1 parent 6f16405 commit 8bf4250
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
29 changes: 29 additions & 0 deletions docs/cni-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ Another example with a trunk port and jumbo frames:
* `mtu` (integer, optional): MTU.
* `trunk` (optional): List of VLAN ID's and/or ranges of accepted VLAN
ID's.
* `configuration_path` (optional): configuration file containing ovsdb
socket file path, etc.

### Flatfile Configuation

There is one option for flat file configuration:

* `configuration_path`: A file path to a OVS CNI configuration file.

OVS CNI will look for the configuration in these locations, in this order:

* The location specified by the `configuration_path` option.
* `/etc/kubernetes/cni/net.d/ovs.d/ovs.conf`
* `/etc/cni/net.d/ovs.d/ovs.conf`

You may specify the `configuration_path` to point to another location should it be desired.

Any options added to the `ovs.conf` are overridden by configuration options that are in the
CNI configuration (e.g. in a custom resource `NetworkAttachmentDefinition` used by Multus CNI
or in the first file ASCII-betically in the CNI configuration directory -- which is
`/etc/cni/net.d/` by default).

The sample content of ovs.conf (in JSON format) is as follows:

```json
{
"socket_file": "/usr/local/var/run/openvswitch/db.sock"
}
```

## Manual Testing

Expand Down
10 changes: 7 additions & 3 deletions pkg/ovsdb/ovsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ func NewOvsDriver(ovsSocket string) (*OvsDriver, error) {
}

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

ovsDB, err := libovsdb.ConnectWithUnixSocket("/var/run/openvswitch/db.sock")
if socketFile == "" {
socketFile = "/var/run/openvswitch/db.sock"
}

ovsDB, err := libovsdb.ConnectWithUnixSocket(socketFile)
if err != nil {
return nil, fmt.Errorf("failed to connect to ovsdb error: %v", err)
return nil, fmt.Errorf("failed to connect to ovsdb socket %s: error: %v", socketFile, err)
}

// Setup state
Expand Down
82 changes: 75 additions & 7 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"runtime"
"sort"
"time"
Expand All @@ -36,6 +38,7 @@ import (
"github.com/containernetworking/plugins/pkg/ip"
"github.com/containernetworking/plugins/pkg/ipam"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/imdario/mergo"
"github.com/j-keck/arping"
"github.com/vishvananda/netlink"

Expand All @@ -51,11 +54,13 @@ const (

type netConf struct {
types.NetConf
BrName string `json:"bridge,omitempty"`
VlanTag *uint `json:"vlan"`
MTU int `json:"mtu"`
Trunk []*trunk `json:"trunk,omitempty"`
DeviceID string `json:"deviceID"` // PCI address of a VF in valid sysfs format
BrName string `json:"bridge,omitempty"`
VlanTag *uint `json:"vlan"`
MTU int `json:"mtu"`
Trunk []*trunk `json:"trunk,omitempty"`
DeviceID string `json:"deviceID"` // PCI address of a VF in valid sysfs format
ConfigurationPath string `json:"configuration_path"`
SocketFile string `json:"socket_file"`
}

type trunk struct {
Expand Down Expand Up @@ -112,6 +117,61 @@ func loadNetConf(bytes []byte) (*netConf, error) {
return netconf, nil
}

func loadFlatNetConf(configPath string) (*netConf, error) {
confFiles := getOvsConfFiles()
if configPath != "" {
confFiles = append([]string{configPath}, confFiles...)
}

// loop through the path and parse the JSON config
flatNetConf := &netConf{}
for _, confFile := range confFiles {
confExists, err := pathExists(confFile)
if err != nil {
return nil, fmt.Errorf("error checking ovs config file: error: %v", err)
}
if confExists {
jsonFile, err := os.Open(confFile)
if err != nil {
return nil, fmt.Errorf("open ovs config file %s error: %v", confFile, err)
}
defer jsonFile.Close()
jsonBytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("load ovs config file %s: error: %v", confFile, err)
}
if err := json.Unmarshal(jsonBytes, flatNetConf); err != nil {
return nil, fmt.Errorf("parse ovs config file %s: error: %v", confFile, err)
}
break
}
}

return flatNetConf, nil
}

func mergeConf(netconf, flatNetConf *netConf) (*netConf, error) {
if err := mergo.Merge(netconf, flatNetConf); err != nil {
return nil, fmt.Errorf("merge with ovs config file: error: %v", err)
}
return netconf, nil
}

func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func getOvsConfFiles() []string {
return []string{"/etc/kubernetes/cni/net.d/ovs.d/ovs.conf", "/etc/cni/net.d/ovs.d/ovs.conf"}
}

func generateRandomMac() net.HardwareAddr {
prefix := []byte{0x02, 0x00, 0x00} // local unicast prefix
suffix := make([]byte, 3)
Expand Down Expand Up @@ -284,6 +344,14 @@ func CmdAdd(args *skel.CmdArgs) error {
if err != nil {
return err
}
flatNetConf, err := loadFlatNetConf(netconf.ConfigurationPath)
if err != nil {
return err
}
netconf, err = mergeConf(netconf, flatNetConf)
if err != nil {
return err
}

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

ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName)
ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName, netconf.SocketFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -480,7 +548,7 @@ func CmdDel(args *skel.CmdArgs) error {
return err
}

ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName)
ovsDriver, err := ovsdb.NewOvsBridgeDriver(bridgeName, netconf.SocketFile)
if err != nil {
return err
}
Expand Down

0 comments on commit 8bf4250

Please sign in to comment.