@@ -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,11 @@ type runcJailer struct {
51
50
runcBinaryPath string
52
51
uid uint32
53
52
gid uint32
54
- once sync. Once
53
+ configSpec specs. Spec
55
54
}
56
55
57
56
const firecrackerFileName = "firecracker"
58
57
59
- var configSpec * specs.Spec
60
-
61
58
func newRuncJailer (ctx context.Context , logger * logrus.Entry , ociBundlePath , runcBinPath string , uid , gid uint32 ) (* runcJailer , error ) {
62
59
l := logger .WithField ("ociBundlePath" , ociBundlePath ).
63
60
WithField ("runcBinaryPath" , runcBinPath )
@@ -71,6 +68,19 @@ func newRuncJailer(ctx context.Context, logger *logrus.Entry, ociBundlePath, run
71
68
gid : gid ,
72
69
}
73
70
71
+ spec := specs.Spec {}
72
+ var configBytes []byte
73
+ configBytes , err := ioutil .ReadFile (runcConfigPath )
74
+ if err != nil {
75
+ return nil , errors .Wrapf (err , "failed to read %s" , runcConfigPath )
76
+ }
77
+
78
+ if err = json .Unmarshal (configBytes , & spec ); err != nil {
79
+ return nil , errors .Wrapf (err , "failed to unmarshal %s" , runcConfigPath )
80
+ }
81
+
82
+ j .configSpec = spec
83
+
74
84
rootPath := j .RootPath ()
75
85
76
86
const mode = os .FileMode (0700 )
@@ -109,7 +119,7 @@ func (j *runcJailer) BuildJailedMachine(cfg *Config, machineConfig *firecracker.
109
119
client := firecracker .NewClient (machineConfig .SocketPath , j .logger , machineConfig .Debug )
110
120
111
121
if machineConfig .NetNS == "" {
112
- if netns := getNetNS (configSpec ); netns != "" {
122
+ if netns := getNetNS (j . configSpec ); netns != "" {
113
123
machineConfig .NetNS = netns
114
124
}
115
125
}
@@ -370,47 +380,19 @@ func (j *runcJailer) jailerCommand(containerName string, isDebug bool) *exec.Cmd
370
380
371
381
// overwriteConfig will set the proper default values if a field had not been set.
372
382
func (j * runcJailer ) overwriteConfig (cfg * Config , machineConfig * firecracker.Config , socketPath , configPath string ) error {
373
- 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
383
+ spec := j .configSpec
384
+ if spec .Process .User .UID != 0 ||
385
+ spec .Process .User .GID != 0 {
386
+ return fmt .Errorf (
387
+ "using UID %d and GID %d, these values must not be set" ,
388
+ spec .Process .User .UID ,
389
+ spec .Process .User .GID ,
390
+ )
411
391
}
412
392
413
- spec := * configSpec
393
+ spec = j .setDefaultConfigValues (cfg , socketPath , spec )
394
+ spec .Root .Path = rootfsFolder
395
+ spec .Root .Readonly = false
414
396
spec .Process .User .UID = j .uid
415
397
spec .Process .User .GID = j .gid
416
398
@@ -491,11 +473,7 @@ func mkdirAllWithPermissions(path string, mode os.FileMode, uid, gid uint32) err
491
473
return nil
492
474
}
493
475
494
- func getNetNS (spec * specs.Spec ) string {
495
- if spec == nil {
496
- return ""
497
- }
498
-
476
+ func getNetNS (spec specs.Spec ) string {
499
477
for _ , ns := range spec .Linux .Namespaces {
500
478
if ns .Type == networkNamespaceRuncName {
501
479
return ns .Path
0 commit comments