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

Added metadata cloud config support to CD ROM and openstack config drive providers #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 19 additions & 11 deletions providers/cdrom_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package providers

import (
"fmt"
"github.com/diskfs/go-diskfs"
log "github.com/sirupsen/logrus"
"os"
"path"
"path/filepath"
"strings"
"syscall"

"github.com/diskfs/go-diskfs"
log "github.com/sirupsen/logrus"
)

const (
Expand All @@ -18,11 +19,11 @@ const (

// ProviderCDROM is the type implementing the Provider interface for CDROMs (nocloud/config-drive)
type ProviderCDROM struct {
providerType string
device string
mountPoint string
err error
userdata []byte
providerType string
device string
mountPoint string
err error
userdata, metadata []byte
}

func (p *ProviderCDROM) String() string {
Expand Down Expand Up @@ -77,15 +78,15 @@ func uniqueString(input []string) []string {
return u
}

func NewProviderCDROM(device string, datafiles []string, providerType string) *ProviderCDROM {
func NewProviderCDROM(device string, userdataFiles []string, metadataFile string, providerType string) *ProviderCDROM {
mountPoint, err := os.MkdirTemp("", "cd")
p := ProviderCDROM{providerType, device, mountPoint, err, []byte{}}
p := ProviderCDROM{providerType, device, mountPoint, err, []byte{}, []byte{}}
if err == nil {
if p.err = p.mount(); p.err == nil {
defer p.unmount()
// read the userdata - we read the spec file and the fallback, but eventually
// will remove the fallback
for _, f := range datafiles {
for _, f := range userdataFiles {
userdata, err := os.ReadFile(path.Join(p.mountPoint, f))
// did we find a file?
if err == nil && userdata != nil {
Expand All @@ -94,8 +95,15 @@ func NewProviderCDROM(device string, datafiles []string, providerType string) *P
}
}
if p.userdata == nil {
log.Debugf("no userdata file found at any of %v", datafiles)
log.Debugf("no userdata file found at any of %v", userdataFiles)
}
// read the metadata
metadata, err := os.ReadFile(path.Join(p.mountPoint, metadataFile))
// did we find a file?
if err == nil && metadata != nil {
p.metadata = metadata
}
p.unmount()
}
}
return &p
Expand Down
3 changes: 2 additions & 1 deletion providers/provider_cdrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
func ListCDROMs() []Provider {
// UserdataFiles is where to find the user data
var userdataFiles = []string{"user-data", "config"}
var metadataFile = "meta-data"
cdroms, err := filepath.Glob(cdromDevs)
if err != nil {
// Glob can only error on invalid pattern
Expand All @@ -44,7 +45,7 @@ func ListCDROMs() []Provider {
log.Debugf("unique devices to be checked: %v", cdroms)
var providers []Provider
for _, device := range cdroms {
providers = append(providers, NewProviderCDROM(device, userdataFiles, "CDROM"))
providers = append(providers, NewProviderCDROM(device, userdataFiles, metadataFile, "CDROM"))
}
return providers
}
3 changes: 2 additions & 1 deletion providers/provider_openstack_config_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
func ListConfigDrives() []Provider {
// UserdataFiles is where to find the user data
var userdataFiles = []string{"/openstack/latest/user_data"}
var metadataFile = "/openstack/latest/meta_data.json"
cdroms, err := filepath.Glob(cdromDevs)
if err != nil {
// Glob can only error on invalid pattern
Expand All @@ -44,7 +45,7 @@ func ListConfigDrives() []Provider {
log.Debugf("unique devices to be checked: %v", cdroms)
var providers []Provider
for _, device := range cdroms {
providers = append(providers, NewProviderCDROM(device, userdataFiles, "CONFIG_DRIVE"))
providers = append(providers, NewProviderCDROM(device, userdataFiles, metadataFile, "CONFIG_DRIVE"))
}
return providers
}