@@ -23,7 +23,6 @@ import (
23
23
"os/exec"
24
24
"path/filepath"
25
25
"strings"
26
- "sync"
27
26
"syscall"
28
27
29
28
"github.com/firecracker-microvm/firecracker-go-sdk"
@@ -51,13 +50,12 @@ type runcJailer struct {
51
50
runcBinaryPath string
52
51
uid uint32
53
52
gid uint32
54
- once sync.Once
53
+
54
+ configSpec specs.Spec
55
55
}
56
56
57
57
const firecrackerFileName = "firecracker"
58
58
59
- var configSpec * specs.Spec
60
-
61
59
func newRuncJailer (ctx context.Context , logger * logrus.Entry , ociBundlePath , runcBinPath string , uid , gid uint32 ) (* runcJailer , error ) {
62
60
l := logger .WithField ("ociBundlePath" , ociBundlePath ).
63
61
WithField ("runcBinaryPath" , runcBinPath )
@@ -71,6 +69,19 @@ func newRuncJailer(ctx context.Context, logger *logrus.Entry, ociBundlePath, run
71
69
gid : gid ,
72
70
}
73
71
72
+ spec := specs.Spec {}
73
+ var configBytes []byte
74
+ configBytes , err := ioutil .ReadFile (runcConfigPath )
75
+ if err != nil {
76
+ return nil , errors .Wrapf (err , "failed to read firecracker-runc-config.json" )
77
+ }
78
+
79
+ if err = json .Unmarshal (configBytes , & spec ); err != nil {
80
+ return nil , errors .Wrapf (err , "failed to unmarshal firecracker-runc-config.json" )
81
+ }
82
+
83
+ j .configSpec = spec
84
+
74
85
rootPath := j .RootPath ()
75
86
76
87
const mode = os .FileMode (0700 )
@@ -109,7 +120,7 @@ func (j *runcJailer) BuildJailedMachine(cfg *Config, machineConfig *firecracker.
109
120
client := firecracker .NewClient (machineConfig .SocketPath , j .logger , machineConfig .Debug )
110
121
111
122
if machineConfig .NetNS == "" {
112
- if netns := getNetNS (configSpec ); netns != "" {
123
+ if netns := getNetNS (j . configSpec ); netns != "" {
113
124
machineConfig .NetNS = netns
114
125
}
115
126
}
@@ -371,46 +382,21 @@ func (j *runcJailer) jailerCommand(containerName string, isDebug bool) *exec.Cmd
371
382
// overwriteConfig will set the proper default values if a field had not been set.
372
383
func (j * runcJailer ) overwriteConfig (cfg * Config , machineConfig * firecracker.Config , socketPath , configPath string ) error {
373
384
var err error
374
- j .once .Do (func () {
375
- // here we attempt to cache the runc config. If the config has already been
376
- // cached, we will return immediately
377
- if configSpec != nil {
378
- return
379
- }
380
-
381
- spec := specs.Spec {}
382
- var configBytes []byte
383
- configBytes , err = ioutil .ReadFile (configPath )
384
- if err != nil {
385
- return
386
- }
387
-
388
- if err = json .Unmarshal (configBytes , & spec ); err != nil {
389
- return
390
- }
391
-
392
- configSpec = & spec
393
-
394
- if spec .Process .User .UID != 0 ||
395
- spec .Process .User .GID != 0 {
396
- err = fmt .Errorf (
397
- "using UID %d and GID %d, these values must not be set" ,
398
- spec .Process .User .UID ,
399
- spec .Process .User .GID ,
400
- )
401
- return
402
- }
403
-
404
- spec = j .setDefaultConfigValues (cfg , socketPath , spec )
405
- spec .Root .Path = rootfsFolder
406
- spec .Root .Readonly = false
407
- })
408
-
409
- if err != nil {
410
- return err
385
+ // here we attempt to cache the runc config. If the config has already been
386
+ // cached, we will return immediately
387
+ spec := j .configSpec
388
+ if spec .Process .User .UID != 0 ||
389
+ spec .Process .User .GID != 0 {
390
+ return fmt .Errorf (
391
+ "using UID %d and GID %d, these values must not be set" ,
392
+ spec .Process .User .UID ,
393
+ spec .Process .User .GID ,
394
+ )
411
395
}
412
396
413
- spec := * configSpec
397
+ spec = j .setDefaultConfigValues (cfg , socketPath , spec )
398
+ spec .Root .Path = rootfsFolder
399
+ spec .Root .Readonly = false
414
400
spec .Process .User .UID = j .uid
415
401
spec .Process .User .GID = j .gid
416
402
@@ -491,11 +477,7 @@ func mkdirAllWithPermissions(path string, mode os.FileMode, uid, gid uint32) err
491
477
return nil
492
478
}
493
479
494
- func getNetNS (spec * specs.Spec ) string {
495
- if spec == nil {
496
- return ""
497
- }
498
-
480
+ func getNetNS (spec specs.Spec ) string {
499
481
for _ , ns := range spec .Linux .Namespaces {
500
482
if ns .Type == networkNamespaceRuncName {
501
483
return ns .Path
0 commit comments