Skip to content

Commit ca2a753

Browse files
committed
Add describeInstance functionality for reading vm info
This will help use cases where we need to check guest's running state and other information before taking another action. Signed-off-by: Alakesh Haloi [email protected]
1 parent 5a97663 commit ca2a753

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

firecracker.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,22 @@ func (f *Client) GetMachineConfiguration(opts ...GetMachineConfigurationOpt) (*o
358358
return f.client.Operations.GetMachineConfiguration(p)
359359
}
360360

361+
// DescribeInstanceOpt is a functional option to be used for the DescribeInstance API
362+
// for any additional optional fields
363+
type DescribeInstanceOpt func(*ops.DescribeInstanceParams)
364+
365+
// GetInstanceInfo is a wrapper for the swagger generated client to make calling of
366+
// the API easier
367+
func (f *Client) GetInstanceInfo(ctx context.Context, opts ...DescribeInstanceOpt) (*ops.DescribeInstanceOK, error) {
368+
params := ops.NewDescribeInstanceParams()
369+
params.SetContext(ctx)
370+
for _, opt := range opts {
371+
opt(params)
372+
}
373+
374+
return f.client.Operations.DescribeInstance(params)
375+
}
376+
361377
// PatchGuestDriveByIDOpt is a functional option to be used for the PutMmds API in setting
362378
// any additional optional fields.
363379
type PatchGuestDriveByIDOpt func(*ops.PatchGuestDriveByIDParams)

machine.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,23 @@ func (m *Machine) UpdateGuestDrive(ctx context.Context, driveID, pathOnHost stri
946946
return nil
947947
}
948948

949+
func (m *Machine) DescribeInstanceInfo(ctx context.Context) (models.InstanceInfo, error) {
950+
var instanceInfo models.InstanceInfo
951+
resp, err := m.client.GetInstanceInfo(ctx)
952+
if err != nil {
953+
m.logger.Errorf("Getting Instance Info: %s", err)
954+
return instanceInfo, err
955+
}
956+
957+
instanceInfo = *resp.Payload
958+
if err != nil {
959+
m.logger.Errorf("Getting Instance info failed parsing payload: %s", err)
960+
}
961+
962+
m.logger.Printf("GetInstanceInfo successful")
963+
return instanceInfo, err
964+
}
965+
949966
// refreshMachineConfiguration synchronizes our cached representation of the machine configuration
950967
// with that reported by the Firecracker API
951968
func (m *Machine) refreshMachineConfiguration() error {

machine_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ func TestMicroVMExecution(t *testing.T) {
375375
t.Run("UpdateMetadata", func(t *testing.T) { testUpdateMetadata(ctx, t, m) })
376376
t.Run("GetMetadata", func(t *testing.T) { testGetMetadata(ctx, t, m) }) // Should be after testSetMetadata and testUpdateMetadata
377377
t.Run("TestStartInstance", func(t *testing.T) { testStartInstance(ctx, t, m) })
378+
t.Run("TestGetInstanceInfo", func(t *testing.T) { testGetInstanceInfo(ctx, t, m) })
378379

379380
// Let the VMM start and stabilize...
380381
timer := time.NewTimer(5 * time.Second)
@@ -836,6 +837,29 @@ func testGetMetadata(ctx context.Context, t *testing.T, m *Machine) {
836837
}
837838
}
838839

840+
func testGetInstanceInfo(ctx context.Context, t *testing.T, m *Machine) {
841+
instance, err := m.DescribeInstanceInfo(ctx)
842+
if err != nil {
843+
t.Error("failed to get instance info")
844+
}
845+
846+
if instance.AppName == nil || *instance.AppName == "" {
847+
t.Error("Invalid instance App name")
848+
}
849+
850+
if instance.ID == nil || *instance.ID == "" {
851+
t.Error("Invalid instance ID")
852+
}
853+
854+
if instance.State == nil || *instance.State == "" {
855+
t.Error("Invalid instance state")
856+
}
857+
858+
if instance.VmmVersion == nil || *instance.VmmVersion == "" {
859+
t.Error("Invalid VmmVersion")
860+
}
861+
}
862+
839863
func TestLogFiles(t *testing.T) {
840864
cfg := Config{
841865
KernelImagePath: filepath.Join(testDataPath, "vmlinux"), SocketPath: filepath.Join(testDataPath, "socket-path"),

0 commit comments

Comments
 (0)