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

Add metadata import from JSON file option #102

Open
wants to merge 1 commit 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
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type options struct {
FcCPUTemplate string `long:"cpu-template" description:"Firecracker CPU Template (C3 or T2)"`
FcMemSz int64 `long:"memory" short:"m" description:"VM memory, in MiB" default:"512"`
FcMetadata string `long:"metadata" description:"Firecracker Metadata for MMDS (json)"`
FcMetadataFile string `long:"metadata-file" description:"Firecracker Metadata for MMDS (json file). json file is ignored if metadata is set."`
FcFifoLogFile string `long:"firecracker-log" short:"l" description:"pipes the fifo contents to the specified file"`
FcSocketPath string `long:"socket-path" short:"s" description:"path to use for firecracker socket, defaults to a unique file in in the first existing directory from {$HOME, $TMPDIR, or /tmp}"`
Debug bool `long:"debug" short:"d" description:"Enable debug output"`
Expand Down Expand Up @@ -83,6 +84,15 @@ func (opts *options) getFirecrackerConfig() (firecracker.Config, error) {
return firecracker.Config{}, fmt.Errorf("%s: %v", errInvalidMetadata.Error(), err)
}
}
if opts.FcMetadataFile != "" && opts.FcMetadata == "" {
file, err := os.ReadFile(opts.FcMetadataFile)
if err != nil {
return firecracker.Config{}, err
}
if err := json.Unmarshal(file, &opts.validMetadata); err != nil {
return firecracker.Config{}, fmt.Errorf("%s: %v", errInvalidMetadata.Error(), err)
}
}
//setup NICs
NICs, err := opts.getNetwork()
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,43 @@ func TestGetFirecrackerConfig(t *testing.T) {
},
},
},
{
name: "Valid metadata file config",
opts: &options{
FcMetadataFile: "testdata/valid_metadata.json",
FcSocketPath: "valid/path",
},
expectedErr: func(e error) (bool, error) {
return e == nil, nil
},
outConfig: firecracker.Config{
SocketPath: "valid/path",
Drives: []models.Drive{
{
DriveID: firecracker.String("1"),
PathOnHost: firecracker.String(""),
IsRootDevice: firecracker.Bool(true),
IsReadOnly: firecracker.Bool(false),
},
},
MachineCfg: models.MachineConfiguration{
VcpuCount: firecracker.Int64(0),
MemSizeMib: firecracker.Int64(0),
Smt: firecracker.Bool(true),
},
},
},
{
name: "Invalid metadata file config",
opts: &options{
FcMetadataFile: "testdata/invalid_metadata.json",
FcSocketPath: "valid/path",
},
expectedErr: func(e error) (bool, error) {
return strings.HasPrefix(e.Error(), errInvalidMetadata.Error()), errInvalidMetadata
},
outConfig: firecracker.Config{},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions testdata/invalid_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"valid": false,
3 changes: 3 additions & 0 deletions testdata/valid_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"valid": true
}