Skip to content

Commit 5d48116

Browse files
committed
UpdateGuestNetworkInterface -> UpdateGuestNetworkInterfaceRateLimit
UpdateGuestNetworkInterface() takes ifaceID twice, as its second parameter (as a string) and the third parameter (as a part of PartialNetworkInterface). While that is technically coming from Firecracker's OpenAPI spec. This is odd and error-prone. Instead this change introduces UpdateGuestNetworkInterfaceRateLimit() which only takes ifaceID as its second parameter.
1 parent 8e15d83 commit 5d48116

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

machine.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ type NetworkInterface struct {
192192
OutRateLimiter *models.RateLimiter
193193
}
194194

195+
// RateLimiterSet represents a pair of RateLimiters (inbound and outbound)
196+
type RateLimiterSet struct {
197+
// InRateLimiter limits the incoming bytes.
198+
InRateLimiter *models.RateLimiter
199+
// OutRateLimiter limits the outgoing bytes.
200+
OutRateLimiter *models.RateLimiter
201+
}
202+
195203
// VsockDevice represents a vsock connection between the host and the guest
196204
// microVM.
197205
type VsockDevice struct {
@@ -564,8 +572,17 @@ func (m *Machine) createNetworkInterface(ctx context.Context, iface NetworkInter
564572
return err
565573
}
566574

567-
// UpdateGuestNetworkInterface modifies the current guest network interface of ID index
568-
func (m *Machine) UpdateGuestNetworkInterface(ctx context.Context, ifaceID string, iface models.PartialNetworkInterface, opts ...PatchGuestNetworkInterfaceByIDOpt) error {
575+
// UpdateGuestNetworkInterfaceRateLimit modifies the specified network interface's rate limits
576+
func (m *Machine) UpdateGuestNetworkInterfaceRateLimit(ctx context.Context, ifaceID string, rateLimiters RateLimiterSet, opts ...PatchGuestNetworkInterfaceByIDOpt) error {
577+
iface := models.PartialNetworkInterface{
578+
IfaceID: &ifaceID,
579+
}
580+
if rateLimiters.InRateLimiter != nil {
581+
iface.RxRateLimiter = rateLimiters.InRateLimiter
582+
}
583+
if rateLimiters.OutRateLimiter != nil {
584+
iface.TxRateLimiter = rateLimiters.InRateLimiter
585+
}
569586
if _, err := m.client.PatchGuestNetworkInterfaceByID(ctx, ifaceID, &iface, opts...); err != nil {
570587
m.logger.Errorf("Update network interface failed: %s: %v", ifaceID, err)
571588
return err

machine_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,13 @@ func testUpdateGuestDrive(ctx context.Context, t *testing.T, m *Machine) {
499499
}
500500

501501
func testUpdateGuestNetworkInterface(ctx context.Context, t *testing.T, m *Machine) {
502-
if err := m.UpdateGuestNetworkInterface(ctx, "1", models.PartialNetworkInterface{IfaceID: String("1")}); err != nil {
502+
rateLimitSet := RateLimiterSet{
503+
InRateLimiter: NewRateLimiter(
504+
TokenBucketBuilder{}.WithBucketSize(10).WithRefillDuration(10).Build(),
505+
TokenBucketBuilder{}.WithBucketSize(10).WithRefillDuration(10).Build(),
506+
),
507+
}
508+
if err := m.UpdateGuestNetworkInterfaceRateLimit(ctx, "1", rateLimitSet); err != nil {
503509
t.Fatalf("Failed to update the network interface %v", err)
504510
}
505511
}

machineiface.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ package firecracker
1515

1616
import (
1717
"context"
18-
19-
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
2018
)
2119

2220
// This ensures the interface method signatures match that of Machine
@@ -31,5 +29,5 @@ type MachineIface interface {
3129
Wait(context.Context) error
3230
SetMetadata(context.Context, interface{}) error
3331
UpdateGuestDrive(context.Context, string, string, ...PatchGuestDriveByIDOpt) error
34-
UpdateGuestNetworkInterface(context.Context, string, models.PartialNetworkInterface, ...PatchGuestNetworkInterfaceByIDOpt) error
32+
UpdateGuestNetworkInterfaceRateLimit(context.Context, string, RateLimiterSet, ...PatchGuestNetworkInterfaceByIDOpt) error
3533
}

0 commit comments

Comments
 (0)