@@ -23,7 +23,6 @@ import (
2323 "os/exec"
2424 "path/filepath"
2525 "strings"
26- "sync"
2726 "syscall"
2827
2928 "github.com/firecracker-microvm/firecracker-go-sdk"
@@ -51,13 +50,11 @@ type runcJailer struct {
5150 runcBinaryPath string
5251 uid uint32
5352 gid uint32
54- once sync. Once
53+ configSpec specs. Spec
5554}
5655
5756const firecrackerFileName = "firecracker"
5857
59- var configSpec * specs.Spec
60-
6158func newRuncJailer (ctx context.Context , logger * logrus.Entry , ociBundlePath , runcBinPath string , uid , gid uint32 ) (* runcJailer , error ) {
6259 l := logger .WithField ("ociBundlePath" , ociBundlePath ).
6360 WithField ("runcBinaryPath" , runcBinPath )
@@ -71,6 +68,19 @@ func newRuncJailer(ctx context.Context, logger *logrus.Entry, ociBundlePath, run
7168 gid : gid ,
7269 }
7370
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+
7484 rootPath := j .RootPath ()
7585
7686 const mode = os .FileMode (0700 )
@@ -109,7 +119,7 @@ func (j *runcJailer) BuildJailedMachine(cfg *Config, machineConfig *firecracker.
109119 client := firecracker .NewClient (machineConfig .SocketPath , j .logger , machineConfig .Debug )
110120
111121 if machineConfig .NetNS == "" {
112- if netns := getNetNS (configSpec ); netns != "" {
122+ if netns := getNetNS (j . configSpec ); netns != "" {
113123 machineConfig .NetNS = netns
114124 }
115125 }
@@ -370,47 +380,19 @@ func (j *runcJailer) jailerCommand(containerName string, isDebug bool) *exec.Cmd
370380
371381// overwriteConfig will set the proper default values if a field had not been set.
372382func (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+ )
411391 }
412392
413- spec := * configSpec
393+ spec = j .setDefaultConfigValues (cfg , socketPath , spec )
394+ spec .Root .Path = rootfsFolder
395+ spec .Root .Readonly = false
414396 spec .Process .User .UID = j .uid
415397 spec .Process .User .GID = j .gid
416398
@@ -491,11 +473,7 @@ func mkdirAllWithPermissions(path string, mode os.FileMode, uid, gid uint32) err
491473 return nil
492474}
493475
494- func getNetNS (spec * specs.Spec ) string {
495- if spec == nil {
496- return ""
497- }
498-
476+ func getNetNS (spec specs.Spec ) string {
499477 for _ , ns := range spec .Linux .Namespaces {
500478 if ns .Type == networkNamespaceRuncName {
501479 return ns .Path
0 commit comments