Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make ovs socket file path as configurable property #142

Merged
merged 3 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
confDirs := getOvsConfDir()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not directories but files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that makes sense. changed it.

if configPath != "" {
confDirs = append([]string{configPath}, confDirs...)
}

// loop through the path and parse the JSON config
flatNetConf := &netConf{}
for _, confPath := range confDirs {
confExists, err := pathExists(confPath)
if err != nil {
return nil, fmt.Errorf("error checking ovs config file: error: %v", err)
}
if confExists {
jsonFile, err := os.Open(confPath)
if err != nil {
return nil, fmt.Errorf("open ovs config file %s error: %v", confPath, err)
}
defer jsonFile.Close()
jsonBytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("load ovs config file %s: error: %v", confPath, err)
}
if err := json.Unmarshal(jsonBytes, flatNetConf); err != nil {
return nil, fmt.Errorf("parse ovs config file %s: error: %v", confPath, 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 getOvsConfDir() []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