Skip to content

Commit 8626a34

Browse files
committed
Add support for the MMDS configuration.
Signed-off-by: Yutaka Juba <[email protected]>
1 parent 7f1b74b commit 8626a34

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

firecracker.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ func (f *Client) PatchMmds(ctx context.Context, metadata interface{}, opts ...Pa
332332
return f.client.Operations.PatchMmds(params)
333333
}
334334

335+
// PutMmdsConfig is a wrapper for the swagger generated client to make calling of the
336+
// API easier.
337+
func (f *Client) PutMmdsConfig(ctx context.Context, config *models.MmdsConfig) (*ops.PutMmdsConfigNoContent, error) {
338+
params := ops.NewPutMmdsConfigParams()
339+
params.SetContext(ctx)
340+
params.SetBody(config)
341+
342+
return f.client.Operations.PutMmdsConfig(params)
343+
}
344+
335345
// GetMachineConfigurationOpt is a functional option to be used for the
336346
// GetMachineConfiguration API in setting any additional optional fields.
337347
type GetMachineConfigurationOpt func(*ops.GetMachineConfigurationParams)

handlers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package firecracker
1616
import (
1717
"context"
1818
"fmt"
19+
"net"
1920
"os"
2021
)
2122

@@ -30,6 +31,7 @@ const (
3031
CreateNetworkInterfacesHandlerName = "fcinit.CreateNetworkInterfaces"
3132
AddVsocksHandlerName = "fcinit.AddVsocks"
3233
SetMetadataHandlerName = "fcinit.SetMetadata"
34+
ConfigMmdsHandlerName = "fcinit.ConfigMmds"
3335
LinkFilesToRootFSHandlerName = "fcinit.LinkFilesToRootFS"
3436
SetupNetworkHandlerName = "fcinit.SetupNetwork"
3537
SetupKernelArgsHandlerName = "fcinit.SetupKernelArgs"
@@ -258,6 +260,17 @@ func NewSetMetadataHandler(metadata interface{}) Handler {
258260
}
259261
}
260262

263+
// NewConfigMmdsHandler is a named handler that puts the MMDS config into the
264+
// firecracker process.
265+
func NewConfigMmdsHandler(address net.IP) Handler {
266+
return Handler{
267+
Name: ConfigMmdsHandlerName,
268+
Fn: func(ctx context.Context, m *Machine) error {
269+
return m.setMmdsConfig(ctx, address)
270+
},
271+
}
272+
}
273+
261274
var defaultFcInitHandlerList = HandlerList{}.Append(
262275
SetupNetworkHandler,
263276
SetupKernelArgsHandler,

handlers_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package firecracker
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"os"
78
"path/filepath"
89
"reflect"
@@ -515,6 +516,8 @@ func TestHandlers(t *testing.T) {
515516
"foo": "bar",
516517
"baz": "qux",
517518
}
519+
mmdsAddress := net.IPv4(169, 254, 169, 254)
520+
mmdsConfig := &models.MmdsConfig{IPV4Address: String(mmdsAddress.String())}
518521

519522
cases := []struct {
520523
Handler Handler
@@ -619,6 +622,19 @@ func TestHandlers(t *testing.T) {
619622
},
620623
Config: Config{},
621624
},
625+
{
626+
Handler: NewConfigMmdsHandler(mmdsAddress),
627+
Client: fctesting.MockClient{
628+
PutMmdsConfigFn: func(params *ops.PutMmdsConfigParams) (*ops.PutMmdsConfigNoContent, error) {
629+
called = ConfigMmdsHandlerName
630+
if !reflect.DeepEqual(mmdsConfig, params.Body) {
631+
return nil, fmt.Errorf("incorrect mmds config value: %v", params.Body)
632+
}
633+
return &ops.PutMmdsConfigNoContent{}, nil
634+
},
635+
},
636+
Config: Config{},
637+
},
622638
}
623639

624640
ctx := context.Background()

machine.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"io"
21+
"net"
2122
"os"
2223
"os/exec"
2324
"os/signal"
@@ -870,6 +871,19 @@ func (m *Machine) sendCtrlAltDel(ctx context.Context) error {
870871
return err
871872
}
872873

874+
func (m *Machine) setMmdsConfig(ctx context.Context, address net.IP) error {
875+
mmdsCfg := models.MmdsConfig{
876+
IPV4Address: String(address.String()),
877+
}
878+
if _, err := m.client.PutMmdsConfig(ctx, &mmdsCfg); err != nil {
879+
m.logger.Errorf("Setting mmds configuration failed: %s: %v", address, err)
880+
return err
881+
}
882+
883+
m.logger.Debug("SetMmdsConfig successful")
884+
return nil
885+
}
886+
873887
// SetMetadata sets the machine's metadata for MDDS
874888
func (m *Machine) SetMetadata(ctx context.Context, metadata interface{}) error {
875889
if _, err := m.client.PutMmds(ctx, metadata); err != nil {

0 commit comments

Comments
 (0)