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

openstack: move metadata fetch under daemon writer and reduce runs #248

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion pkg/daemon/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type NodeStateStatusWriter struct {
node string
status sriovnetworkv1.SriovNetworkNodeStateStatus
OnHeartbeatFailure func()
metaData *utils.OSPMetaData
networkData *utils.OSPNetworkData
}

// NewNodeStateStatusWriter Create a new NodeStateStatusWriter
Expand All @@ -43,6 +45,20 @@ func NewNodeStateStatusWriter(c snclientset.Interface, n string, f func()) *Node
func (writer *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message, syncCh chan<- struct{}, destDir string, runonce bool, platformType utils.PlatformType) {
glog.V(0).Infof("Run(): start writer")
msg := Message{}

var err error

if platformType == utils.VirtualOpenStack {
writer.metaData, err = utils.ReadOpenstackMetaData()
if err != nil {
glog.Errorf("Run(): failed to read OpenStack meta_data: %v", err)
}
writer.networkData, err = utils.ReadOpenstackNetworkData()
if err != nil {
glog.Errorf("Run(): failed to read OpenStack network_data: %v", err)
}
}

if runonce {
glog.V(0).Info("Run(): once")
if err := writer.pollNicStatus(platformType); err != nil {
Expand Down Expand Up @@ -82,7 +98,7 @@ func (writer *NodeStateStatusWriter) pollNicStatus(platformType utils.PlatformTy
var err error

if platformType == utils.VirtualOpenStack {
iface, err = utils.DiscoverSriovDevicesVirtual(platformType)
iface, err = utils.DiscoverSriovDevicesVirtual(platformType, writer.metaData, writer.networkData)
} else {
iface, err = utils.DiscoverSriovDevices()
}
Expand Down
71 changes: 33 additions & 38 deletions pkg/utils/utils_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"

"github.com/golang/glog"
Expand Down Expand Up @@ -42,7 +42,7 @@ var (
)

const (
ospMetaDataDir = "/host/var/config/openstack/latest/"
ospMetaDataDir = "/host/var/config/openstack/2018-08-27"
zshi-redhat marked this conversation as resolved.
Show resolved Hide resolved
ospNetworkData = ospMetaDataDir + "/network_data.json"
ospMetaData = ospMetaDataDir + "/meta_data.json"
)
Expand Down Expand Up @@ -93,51 +93,46 @@ type OSPNetworkData struct {
// Omit Services
}

func metaData(platformType PlatformType, address string) (netFilter string, macAddress string) {
switch platformType {
case VirtualOpenStack:
metaData, networkData := readOpenstackMetaData()
netFilter, macAddress = parseOpenstackMetaData(address, metaData, networkData)

default:
glog.V(2).Infof("Unknown PlatformType: %v", platformType)
}

return
}

func readOpenstackMetaData() (metaData *OSPMetaData, networkData *OSPNetworkData) {
networkData = &OSPNetworkData{}

rawBytes, err := ioutil.ReadFile(ospNetworkData)
// ReadOpenstackMetaData reads the meta data from the openstack metadata file
func ReadOpenstackMetaData() (metaData *OSPMetaData, err error) {
glog.Infof("ReadOpenstackMetaData(): read OpenStack meta_data")
var f *os.File
f, err = os.Open(ospMetaData)
if err != nil {
glog.Errorf("error reading file %s, %v", ospNetworkData, err)
return
err = fmt.Errorf("error opening file %s: %w", ospMetaData, err)
}

if err = json.Unmarshal(rawBytes, networkData); err != nil {
glog.Errorf("error unmarshalling raw bytes %v from %s", err, ospNetworkData)
return
defer func() {
if e := f.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospMetaData, e)
}
}()
if err = json.NewDecoder(f).Decode(&metaData); err != nil {
err = fmt.Errorf("error unmarshalling metadata from file %s: %w", ospMetaData, err)
}
return metaData, err
}

metaData = &OSPMetaData{}

rawBytes, err = ioutil.ReadFile(ospMetaData)
// ReadOpenstackNetworkData reads the network data from the openstack metadata file
func ReadOpenstackNetworkData() (networkData *OSPNetworkData, err error) {
glog.Infof("ReadOpenstackNetworkData(): read OpenStack network_data")
var f *os.File
f, err = os.Open(ospNetworkData)
if err != nil {
glog.Errorf("error reading file %s, %v", ospMetaData, err)
err = fmt.Errorf("error opening file %s: %w", ospNetworkData, err)
return
}

if err = json.Unmarshal(rawBytes, metaData); err != nil {
glog.Errorf("error unmarshalling raw bytes %v from %s", err, ospNetworkData)
return
defer func() {
if e := f.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospNetworkData, e)
}
}()
if err = json.NewDecoder(f).Decode(&networkData); err != nil {
err = fmt.Errorf("error unmarshalling metadata from file %s: %w", ospNetworkData, err)
}

return
return networkData, err
}

func parseOpenstackMetaData(pciAddr string, metaData *OSPMetaData, networkData *OSPNetworkData) (networkID string, macAddress string) {

if metaData == nil || networkData == nil {
return
}
Expand All @@ -161,7 +156,7 @@ func parseOpenstackMetaData(pciAddr string, metaData *OSPMetaData, networkData *
}

// DiscoverSriovDevicesVirtual discovers VFs on a virtual platform
func DiscoverSriovDevicesVirtual(platformType PlatformType) ([]sriovnetworkv1.InterfaceExt, error) {
func DiscoverSriovDevicesVirtual(platformType PlatformType, metaData *OSPMetaData, networkData *OSPNetworkData) ([]sriovnetworkv1.InterfaceExt, error) {
glog.V(2).Info("DiscoverSriovDevicesVirtual")
pfList := []sriovnetworkv1.InterfaceExt{}

Expand All @@ -186,7 +181,7 @@ func DiscoverSriovDevicesVirtual(platformType PlatformType) ([]sriovnetworkv1.In
continue
}

netFilter, metaMac := metaData(platformType, device.Address)
netFilter, metaMac := parseOpenstackMetaData(device.Address, metaData, networkData)

driver, err := dputils.GetDriverName(device.Address)
if err != nil {
Expand Down