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: include support to metadata service #258

Merged
merged 2 commits into from
Mar 15, 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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/fsnotify/fsnotify v1.4.9
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/google/go-cmp v0.5.2
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/intel/sriov-network-device-plugin v0.0.0-20200924101303-b7f6d3e06797
github.com/jaypipes/ghw v0.6.1
github.com/k8snetworkplumbingwg/network-attachment-definition-client v0.0.0-20200626054723-37f83d1996bc
Expand Down Expand Up @@ -82,6 +83,7 @@ require (
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.1 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,15 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down
8 changes: 2 additions & 6 deletions pkg/daemon/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ func (writer *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Me
var err error

if platformType == utils.VirtualOpenStack {
writer.metaData, err = utils.ReadOpenstackMetaData()
writer.metaData, writer.networkData, err = utils.GetOpenstackData()
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)
glog.Errorf("Run(): failed to get OpenStack data: %v", err)
}
}

Expand Down
105 changes: 78 additions & 27 deletions pkg/utils/utils_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"

"github.com/golang/glog"
"github.com/hashicorp/go-retryablehttp"
dputils "github.com/intel/sriov-network-device-plugin/pkg/utils"
"github.com/jaypipes/ghw"
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
Expand Down Expand Up @@ -42,9 +44,12 @@ var (
)

const (
ospMetaDataDir = "/host/var/config/openstack/2018-08-27"
ospNetworkData = ospMetaDataDir + "/network_data.json"
ospMetaData = ospMetaDataDir + "/meta_data.json"
ospMetaDataDir = "/host/var/config/openstack/2018-08-27"
ospMetaDataBaseUrl = "http://169.254.169.254/openstack/2018-08-27"
ospNetworkDataFile = ospMetaDataDir + "/network_data.json"
ospMetaDataFile = ospMetaDataDir + "/meta_data.json"
ospNetworkDataUrl = ospMetaDataBaseUrl + "/network_data.json"
ospMetaDataUrl = ospMetaDataBaseUrl + "/meta_data.json"
)

// OSPMetaDataDevice -- Device structure within meta_data.json
Expand Down Expand Up @@ -93,43 +98,89 @@ type OSPNetworkData struct {
// Omit Services
}

// 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)
// GetOpenstackData gets the metadata and network_data
func GetOpenstackData() (metaData *OSPMetaData, networkData *OSPNetworkData, err error) {
metaData, networkData, err = getOpenstackDataFromConfigDrive()
if err != nil {
err = fmt.Errorf("error opening file %s: %w", ospMetaData, err)
metaData, networkData, err = getOpenstackDataFromMetadataService()
}
return metaData, networkData, err
}

// getOpenstackDataFromConfigDrive reads the meta_data and network_data files
func getOpenstackDataFromConfigDrive() (metaData *OSPMetaData, networkData *OSPNetworkData, err error) {
metaData = &OSPMetaData{}
networkData = &OSPNetworkData{}
glog.Infof("reading OpenStack meta_data from config-drive")
var metadataf *os.File
metadataf, err = os.Open(ospMetaDataFile)
if err != nil {
return metaData, networkData, fmt.Errorf("error opening file %s: %w", ospMetaDataFile, err)
}
defer func() {
if e := f.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospMetaData, e)
if e := metadataf.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospMetaDataFile, e)
}
}()
if err = json.NewDecoder(f).Decode(&metaData); err != nil {
err = fmt.Errorf("error unmarshalling metadata from file %s: %w", ospMetaData, err)
if err = json.NewDecoder(metadataf).Decode(&metaData); err != nil {
MaysaMacedo marked this conversation as resolved.
Show resolved Hide resolved
return metaData, networkData, fmt.Errorf("error unmarshalling metadata from file %s: %w", ospMetaDataFile, err)
}
return metaData, err
}

// 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)
glog.Infof("reading OpenStack network_data from config-drive")
var networkDataf *os.File
networkDataf, err = os.Open(ospNetworkDataFile)
if err != nil {
err = fmt.Errorf("error opening file %s: %w", ospNetworkData, err)
return
return metaData, networkData, fmt.Errorf("error opening file %s: %w", ospNetworkDataFile, err)
}
MaysaMacedo marked this conversation as resolved.
Show resolved Hide resolved
defer func() {
if e := f.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospNetworkData, e)
if e := networkDataf.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospNetworkDataFile, e)
}
}()
if err = json.NewDecoder(f).Decode(&networkData); err != nil {
err = fmt.Errorf("error unmarshalling metadata from file %s: %w", ospNetworkData, err)
if err = json.NewDecoder(networkDataf).Decode(&networkData); err != nil {
return metaData, networkData, fmt.Errorf("error unmarshalling metadata from file %s: %w", ospNetworkDataFile, err)
}
return metaData, networkData, err
}

func getBodyFromUrl(url string) ([]byte, error) {
glog.V(2).Infof("Getting body from %s", url)
resp, err := retryablehttp.Get(url)
MaysaMacedo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
rawBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return rawBytes, nil
}

// getOpenstackDataFromMetadataService fetchs the metadata and network_data from the metadata service
func getOpenstackDataFromMetadataService() (metaData *OSPMetaData, networkData *OSPNetworkData, err error) {
metaData = &OSPMetaData{}
networkData = &OSPNetworkData{}
glog.Infof("getting OpenStack meta_data from metadata server")
metaDataRawBytes, err := getBodyFromUrl(ospMetaDataUrl)
if err != nil {
return metaData, networkData, fmt.Errorf("error getting OpenStack meta_data from %s: %v", ospMetaDataUrl, err)
}
err = json.Unmarshal(metaDataRawBytes, metaData)
if err != nil {
return metaData, networkData, fmt.Errorf("error unmarshalling raw bytes %v from %s", err, ospMetaDataUrl)
}

glog.Infof("getting OpenStack network_data from metadata server")
networkDataRawBytes, err := getBodyFromUrl(ospNetworkDataUrl)
if err != nil {
return metaData, networkData, fmt.Errorf("error getting OpenStack network_data from %s: %v", ospNetworkDataUrl, err)
}
err = json.Unmarshal(networkDataRawBytes, networkData)
if err != nil {
return metaData, networkData, fmt.Errorf("error unmarshalling raw bytes %v from %s", err, ospNetworkDataUrl)
}
return networkData, err
return metaData, networkData, nil
}

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