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 health checker #119

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 9 additions & 0 deletions firecracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,12 @@ func (f *Client) PatchGuestDriveByID(ctx context.Context, driveID, pathOnHost st

return f.client.Operations.PatchGuestDriveByID(params)
}

// DescribeInstance is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) DescribeInstance(ctx context.Context) (*ops.DescribeInstanceOK, error) {
params := ops.NewDescribeInstanceParams()
params.SetContext(ctx)

return f.client.Operations.DescribeInstance(params)
}
4 changes: 4 additions & 0 deletions firecracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ func TestClient(t *testing.T) {
if _, err := client.PutGuestDriveByID(ctx, "test", drive); err != nil {
t.Errorf("unexpected error on PutGuestDriveByID, %v", err)
}

if _, err := client.DescribeInstance(ctx); err != nil {
t.Errorf("unexpected error on DescribeInstance, %v", err)
}
}
20 changes: 19 additions & 1 deletion machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
"syscall"
"time"

models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
log "github.com/sirupsen/logrus"

models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
)

const (
Expand Down Expand Up @@ -707,6 +708,23 @@ func (m *Machine) UpdateGuestDrive(ctx context.Context, driveID, pathOnHost stri
return nil
}

// IsRunning makes sure that a Firecracker instance is running and responds via API by
// calling `DescribeInstance` endpoint behind.
func (m *Machine) IsRunning(ctx context.Context) error {
instance, err := m.client.DescribeInstance(ctx)
if err != nil {
return err
}

state := StringValue(instance.Payload.State)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to verify the state if the purpose of this method is to see whether Firecracker responds to an API. I think getting any non-error response would meet the success criteria.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the motivation behind the change is to test the responsiveness of the API, then agreed and also think IsVMMResponsive is a good name.

If the motivation is instead to test that the Machine is in the running state, then can we just add a State function that returns the state value, whatever it may be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haikuoliu which behavior would suite better here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I've renamed it to IsRunning which tells whether a VM is in Starting or Running state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit confusing to see that we have Running as a state, and IsRunning actually means IsRunningOrStarting. Naming is hard...

I'm inclined to expose Firecracker's state as is instead of introducing our own notion of the state (running, responsive, alive, ...).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status type could provide rich methods later, like https://godoc.org/inet.af/http#Status.IsClientError (inet.af is bradfitz's net/http rework proposal, which is referenced from golang/go#29652)

switch state {
case models.InstanceInfoStateStarting, models.InstanceInfoStateRunning:
return nil
default:
return fmt.Errorf("invalid instance state: %q", state)
}
}

// refreshMachineConfiguration synchronizes our cached representation of the machine configuration
// with that reported by the Firecracker API
func (m *Machine) refreshMachineConfiguration() error {
Expand Down
8 changes: 8 additions & 0 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func TestMicroVMExecution(t *testing.T) {
t.Run("TestUpdateGuestDrive", func(t *testing.T) { testUpdateGuestDrive(ctx, t, m) })
t.Run("TestUpdateGuestNetworkInterface", func(t *testing.T) { testUpdateGuestNetworkInterface(ctx, t, m) })
t.Run("TestStartInstance", func(t *testing.T) { testStartInstance(ctx, t, m) })
t.Run("IsRunning", func(t *testing.T) { testIsRunning(ctx, t, m) })

// Let the VMM start and stabilize...
timer := time.NewTimer(5 * time.Second)
Expand Down Expand Up @@ -691,6 +692,13 @@ func testGetMetadata(ctx context.Context, t *testing.T, m *Machine) {
}
}

func testIsRunning(ctx context.Context, t *testing.T, m *Machine) {
err := m.IsRunning(ctx)
if err != nil {
t.Errorf("machine is expected to be running: %v", err)
}
}

func TestLogFiles(t *testing.T) {
cfg := Config{
Debug: true,
Expand Down
1 change: 1 addition & 0 deletions machineiface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ type MachineIface interface {
SetMetadata(context.Context, interface{}) error
UpdateGuestDrive(context.Context, string, string, ...PatchGuestDriveByIDOpt) error
UpdateGuestNetworkInterfaceRateLimit(context.Context, string, RateLimiterSet, ...PatchGuestNetworkInterfaceByIDOpt) error
IsRunning(ctx context.Context) error
}