Skip to content

Commit 98d75b0

Browse files
committed
Adds new constructor for setting up networks
This adds a new constructor, NewSetupNetworkHandler(string), which allows for a networks namespace to be passed in Signed-off-by: xibz <[email protected]>
1 parent 6292bbe commit 98d75b0

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

handlers.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,15 @@ var CreateNetworkInterfacesHandler = Handler{
214214
},
215215
}
216216

217-
// SetupNetworkHandler is a named handler that will setup the network namespace
218-
// and network interface configuration prior to the Firecracker VMM starting.
219-
var SetupNetworkHandler = Handler{
220-
Name: SetupNetworkHandlerName,
221-
Fn: func(ctx context.Context, m *Machine) error {
222-
return m.setupNetwork(ctx)
223-
},
217+
// NewSetupNetworkHandler will construct a new handler that will setup a given
218+
// network with the provided netns
219+
func NewSetupNetworkHandler(netnsPath string) Handler {
220+
return Handler{
221+
Name: SetupNetworkHandlerName,
222+
Fn: func(ctx context.Context, m *Machine) error {
223+
return m.setupNetwork(ctx, netnsPath)
224+
},
225+
}
224226
}
225227

226228
// SetupKernelArgsHandler is a named handler that will update any kernel boot
@@ -253,26 +255,28 @@ func NewSetMetadataHandler(metadata interface{}) Handler {
253255
}
254256
}
255257

256-
var defaultFcInitHandlerList = HandlerList{}.Append(
257-
SetupNetworkHandler,
258-
SetupKernelArgsHandler,
259-
StartVMMHandler,
260-
CreateLogFilesHandler,
261-
BootstrapLoggingHandler,
262-
CreateMachineHandler,
263-
CreateBootSourceHandler,
264-
AttachDrivesHandler,
265-
CreateNetworkInterfacesHandler,
266-
AddVsocksHandler,
267-
)
258+
func newDefaultFcInitHandlerList() HandlerList {
259+
return HandlerList{}.Append(
260+
NewSetupNetworkHandler(""),
261+
SetupKernelArgsHandler,
262+
StartVMMHandler,
263+
CreateLogFilesHandler,
264+
BootstrapLoggingHandler,
265+
CreateMachineHandler,
266+
CreateBootSourceHandler,
267+
AttachDrivesHandler,
268+
CreateNetworkInterfacesHandler,
269+
AddVsocksHandler,
270+
)
271+
}
268272

269273
var defaultValidationHandlerList = HandlerList{}.Append(
270274
NetworkConfigValidationHandler,
271275
)
272276

273277
var defaultHandlers = Handlers{
274278
Validation: defaultValidationHandlerList,
275-
FcInit: defaultFcInitHandlerList,
279+
FcInit: newDefaultFcInitHandlerList(),
276280
}
277281

278282
// Handler represents a named handler that contains a name and a function which

jailer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestJail(t *testing.T) {
145145
t.Run(c.name, func(t *testing.T) {
146146
m := &Machine{
147147
Handlers: Handlers{
148-
FcInit: defaultFcInitHandlerList,
148+
FcInit: newDefaultFcInitHandlerList(),
149149
},
150150
}
151151
cfg := &Config{

machine.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
255255
}
256256

257257
m.Handlers = defaultHandlers
258-
259258
if cfg.JailerCfg != nil {
260259
m.Handlers.Validation = m.Handlers.Validation.Append(JailerConfigValidationHandler)
261260
if err := jail(ctx, m, &cfg); err != nil {
@@ -268,10 +267,6 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
268267
Build(ctx)
269268
}
270269

271-
for _, opt := range opts {
272-
opt(m)
273-
}
274-
275270
if m.logger == nil {
276271
logger := log.New()
277272
if cfg.Debug {
@@ -296,6 +291,12 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
296291
m.machineConfig = cfg.MachineCfg
297292
m.Cfg = cfg
298293

294+
m.Handlers.FcInit = m.Handlers.FcInit.Swap(NewSetupNetworkHandler(m.netNSPath()))
295+
296+
for _, opt := range opts {
297+
opt(m)
298+
}
299+
299300
m.logger.Debug("Called NewMachine()")
300301
return m, nil
301302
}
@@ -369,8 +370,8 @@ func (m *Machine) netNSPath() string {
369370
return ""
370371
}
371372

372-
func (m *Machine) setupNetwork(ctx context.Context) error {
373-
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.netNSPath(), m.logger)
373+
func (m *Machine) setupNetwork(ctx context.Context, netnsPath string) error {
374+
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, netnsPath, m.logger)
374375
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
375376
return err
376377
}

0 commit comments

Comments
 (0)