Skip to content

Commit

Permalink
Refactor runrunc package
Browse files Browse the repository at this point in the history
* Split ExecPreparer into several smaller objects. This was necessary
  because the orchestration was totally different for peas and "regular"
  exec processes, so it didn't make sense to keep overloading the
  ExecPreparer. PeaCreator and Execer can now simply orchestrate the
  pieces differently.

* Split exec_test into separate test files for each struct under test.

* Extract Execer into own file.

* Backfill a bunch of unit tests for Execer.

[#151709894]

Signed-off-by: Julia Nedialkova <[email protected]>
  • Loading branch information
Craig Furman authored and yulianedyalkova committed Oct 27, 2017
1 parent 1200fd3 commit ef4208e
Show file tree
Hide file tree
Showing 22 changed files with 969 additions and 1,247 deletions.
26 changes: 15 additions & 11 deletions guardiancmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,7 @@ func (cmd *ServerCommand) Run(signals <-chan os.Signal, ready chan<- struct{}) e
restorer = &gardener.NoopRestorer{}
}

var volumizer gardener.Volumizer = nil
volumizer = cmd.wireVolumizer(logger, cmd.Graph.Dir, cmd.Docker.InsecureRegistries, cmd.Graph.PersistentImages, uidMappings, gidMappings)
volumizer := cmd.wireVolumizer(logger, cmd.Graph.Dir, cmd.Docker.InsecureRegistries, cmd.Graph.PersistentImages, uidMappings, gidMappings)

starters := []gardener.Starter{}
if !cmd.Server.SkipSetup {
Expand All @@ -412,7 +411,7 @@ func (cmd *ServerCommand) Run(signals <-chan os.Signal, ready chan<- struct{}) e
var bulkStarter gardener.BulkStarter = gardener.NewBulkStarter(starters)

backend := &gardener.Gardener{
UidGenerator: cmd.wireUidGenerator(),
UidGenerator: cmd.wireUIDGenerator(),
BulkStarter: bulkStarter,
SysInfoProvider: sysinfo.NewResourcesProvider(cmd.Containers.Dir),
Networker: networker,
Expand Down Expand Up @@ -560,7 +559,7 @@ func (cmd *ServerCommand) saveProperties(logger lager.Logger, propertiesPath str
}
}

func (cmd *ServerCommand) wireUidGenerator() gardener.UidGeneratorFunc {
func (cmd *ServerCommand) wireUIDGenerator() gardener.UidGeneratorFunc {
return gardener.UidGeneratorFunc(func() string { return mustStringify(uuid.NewV4()) })
}

Expand Down Expand Up @@ -609,8 +608,8 @@ func (cmd *ServerCommand) wireNetworker(log lager.Logger, depotPath string, prop
iptRunner := &logging.Runner{CommandRunner: commandRunner(), Logger: log.Session("iptables-runner")}
nonLoggingIptRunner := commandRunner()
ipTables := iptables.New(cmd.Bin.IPTables.Path(), cmd.Bin.IPTablesRestore.Path(), iptRunner, locksmith, chainPrefix)
nonLoggingIpTables := iptables.New(cmd.Bin.IPTables.Path(), cmd.Bin.IPTablesRestore.Path(), nonLoggingIptRunner, locksmith, chainPrefix)
ipTablesStarter := iptables.NewStarter(nonLoggingIpTables, cmd.Network.AllowHostAccess, interfacePrefix, denyNetworksList, cmd.Containers.DestroyContainersOnStartup, log)
nonLoggingIPTables := iptables.New(cmd.Bin.IPTables.Path(), cmd.Bin.IPTablesRestore.Path(), nonLoggingIptRunner, locksmith, chainPrefix)
ipTablesStarter := iptables.NewStarter(nonLoggingIPTables, cmd.Network.AllowHostAccess, interfacePrefix, denyNetworksList, cmd.Containers.DestroyContainersOnStartup, log)
ruleTranslator := iptables.NewRuleTranslator()

containerMtu := cmd.Network.Mtu
Expand Down Expand Up @@ -766,19 +765,24 @@ func (cmd *ServerCommand) wireContainerizer(log lager.Logger,

bundleSaver := &goci.BundleSaver{}
depot := wireDepot(depotPath, template, bundleSaver)
execPreparer := cmd.wireExecPreparer()

bndlLoader := &goci.BndlLoader{}
processBuilder := runrunc.NewProcessBuilder(wireEnvFunc(), nonRootMaxCaps)

runcrunner := runrunc.New(
cmdRunner,
runrunc.NewLogRunner(cmdRunner, runrunc.LogDir(os.TempDir()).GenerateLogFile),
goci.RuncBinary{Path: runtimePath},
dadooPath,
runtimePath,
execPreparer,
bndlLoader,
processBuilder,
wireMkdirer(cmdRunner),
runrunc.LookupFunc(runrunc.LookupUser),
cmd.wireExecRunner(
dadooPath,
runtimePath,
cmd.wireUidGenerator(),
cmd.wireUIDGenerator(),
cmdRunner,
cmd.Containers.CleanupProcessDirsOnWait,
),
Expand All @@ -800,11 +804,11 @@ func (cmd *ServerCommand) wireContainerizer(log lager.Logger,
peaCreator := &peas.PeaCreator{
VolumeCreator: volumeCreator,
BundleGenerator: peaTemplate,
ProcessBuilder: processBuilder,
BundleSaver: bundleSaver,
ExecPreparer: execPreparer,
ContainerCreator: runrunc.NewCreator(runtimePath, "run", cmdRunner),
}
return rundmc.New(depot, runcrunner, &goci.BndlLoader{}, nstar, stopper, eventStore, stateStore, &preparerootfs.SymlinkRefusingFileCreator{}, peaCreator)
return rundmc.New(depot, runcrunner, bndlLoader, nstar, stopper, eventStore, stateStore, &preparerootfs.SymlinkRefusingFileCreator{}, peaCreator)
}

func (cmd *ServerCommand) wireMetricsProvider(log lager.Logger, depotPath, graphRoot string) *metrics.MetricsProvider {
Expand Down
28 changes: 18 additions & 10 deletions guardiancmd/command_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"code.cloudfoundry.org/guardian/rundmc/cgroups"
"code.cloudfoundry.org/guardian/rundmc/depot"
"code.cloudfoundry.org/guardian/rundmc/execrunner/dadoo"
"code.cloudfoundry.org/guardian/rundmc/goci"
"code.cloudfoundry.org/guardian/rundmc/preparerootfs"
"code.cloudfoundry.org/guardian/rundmc/runrunc"
"code.cloudfoundry.org/idmapper"
Expand Down Expand Up @@ -164,6 +163,24 @@ func (cmd *ServerCommand) wireVolumizer(logger lager.Logger, graphRoot string, i
return gardener.NewVolumeProvider(shed, shed)
}

func wireEnvFunc() runrunc.EnvFunc {
return runrunc.EnvFunc(runrunc.UnixEnvFor)
}

func wireMkdirer(cmdRunner commandrunner.CommandRunner) runrunc.Mkdirer {
if runningAsRoot() {
return bundlerules.ChrootMkdir{Command: preparerootfs.Command, CommandRunner: cmdRunner}
}

return NoopMkdirer{}
}

type NoopMkdirer struct{}

func (NoopMkdirer) MkdirAs(rootFSPathFile string, uid, gid int, mode os.FileMode, recreate bool, path ...string) error {
return nil
}

func (cmd *ServerCommand) wireExecRunner(dadooPath, runcPath string, processIDGen runrunc.UidGenerator, commandRunner commandrunner.CommandRunner, shouldCleanup bool) *dadoo.ExecRunner {

pidFileReader := &dadoo.PidFileReader{
Expand Down Expand Up @@ -193,15 +210,6 @@ func wireCgroupsStarter(logger lager.Logger, tag string, chowner cgroups.Chowner
return cgroups.NewStarter(logger, mustOpen("/proc/cgroups"), mustOpen("/proc/self/cgroup"), cgroupsMountpoint, gardenCgroup, allowedDevices, commandRunner(), chowner)
}

func (cmd *ServerCommand) wireExecPreparer() runrunc.ExecPreparer {
cmdRunner := commandRunner()
chrootMkdir := bundlerules.ChrootMkdir{
Command: preparerootfs.Command,
CommandRunner: cmdRunner,
}
return runrunc.NewExecPreparer(&goci.BndlLoader{}, runrunc.LookupFunc(runrunc.LookupUser), runrunc.EnvFunc(runrunc.UnixEnvFor), chrootMkdir, nonRootMaxCaps, runningAsRoot)
}

func wireResolvConfigurer(depotPath string) kawasaki.DnsResolvConfigurer {
return &kawasaki.ResolvConfigurer{
HostsFileCompiler: &dns.HostsFileCompiler{},
Expand Down
10 changes: 6 additions & 4 deletions guardiancmd/command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"code.cloudfoundry.org/guardian/rundmc/cgroups"
"code.cloudfoundry.org/guardian/rundmc/depot"
"code.cloudfoundry.org/guardian/rundmc/execrunner"
"code.cloudfoundry.org/guardian/rundmc/goci"
"code.cloudfoundry.org/guardian/rundmc/runrunc"
"code.cloudfoundry.org/idmapper"
"code.cloudfoundry.org/lager"
Expand Down Expand Up @@ -60,6 +59,10 @@ func wireCgroupsStarter(_ lager.Logger, _ string, _ cgroups.Chowner) gardener.St
return &NoopStarter{}
}

func wireMkdirer(_ commandrunner.CommandRunner) runrunc.Mkdirer {
return mkdirer{}
}

type mkdirer struct{}

func (m mkdirer) MkdirAs(rootFSPathFile string, uid, gid int, mode os.FileMode, recreate bool, paths ...string) error {
Expand All @@ -72,9 +75,8 @@ func (m mkdirer) MkdirAs(rootFSPathFile string, uid, gid int, mode os.FileMode,
return nil
}

func (cmd *ServerCommand) wireExecPreparer() runrunc.ExecPreparer {
runningAsRoot := func() bool { return true }
return runrunc.NewExecPreparer(&goci.BndlLoader{}, runrunc.LookupFunc(runrunc.LookupUser), runrunc.EnvFunc(runrunc.WindowsEnvFor), mkdirer{}, nil, runningAsRoot)
func wireEnvFunc() runrunc.EnvFunc {
return runrunc.EnvFunc(runrunc.WindowsEnvFor)
}

func wireResolvConfigurer(depotPath string) kawasaki.DnsResolvConfigurer {
Expand Down
177 changes: 0 additions & 177 deletions properties/fakes/fake_map_persister.go

This file was deleted.

Loading

0 comments on commit ef4208e

Please sign in to comment.