This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 882
stage1: move more logic out of AppUnit #3496
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
// Copyright 2016 The rkt Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//+build linux | ||
|
||
package common | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/appc/spec/schema" | ||
"github.com/appc/spec/schema/types" | ||
"github.com/hashicorp/errwrap" | ||
|
||
"github.com/coreos/rkt/common" | ||
"github.com/coreos/rkt/common/cgroup" | ||
stage1commontypes "github.com/coreos/rkt/stage1/common/types" | ||
) | ||
|
||
// preparedApp contains some internal state needed to actually run an app. | ||
// We add this intermediate step to prevent unit file generation from being | ||
// totally unwieldly. | ||
type preparedApp struct { | ||
app *schema.RuntimeApp | ||
uid uint32 | ||
gid uint32 | ||
env types.Environment | ||
resources appResources | ||
mounts []mountWrapper | ||
noNewPrivileges bool | ||
capabilities []string | ||
seccomp *seccompFilter | ||
|
||
// Path restrictions | ||
roPaths []string | ||
hiddenPaths []string | ||
hiddenDirs []string | ||
} | ||
|
||
type appResources struct { | ||
MemoryLimit *uint64 // Memory limit in bytes | ||
CPUQuota *uint64 // The hard (absolute) CPU quota as a percent (100 = 1 core) | ||
LinuxCPUShares *uint64 // The relative CPU weight in the app's cgroup. | ||
LinuxOOMScoreAdjust *int // OOMScoreAdjust knob | ||
} | ||
|
||
/* | ||
* Paths to protect for non-provileged applications | ||
* AKA protectKernelTunables | ||
*/ | ||
var protectKernelROPaths = []string{ | ||
"/proc/bus/", | ||
"/proc/sys/kernel/core_pattern", | ||
"/proc/sys/kernel/modprobe", | ||
"/proc/sys/vm/panic_on_oom", | ||
"/proc/sysrq-trigger", | ||
"/sys/block/", | ||
"/sys/bus/", | ||
"/sys/class/", | ||
"/sys/dev/", | ||
"/sys/devices/", | ||
"/sys/kernel/", | ||
} | ||
var protectKernelHiddenDirs = []string{ | ||
"/sys/firmware/", | ||
"/sys/fs/", | ||
"/sys/hypervisor/", | ||
"/sys/module/", | ||
"/sys/power/", | ||
} | ||
|
||
// This is separate because systemd <231 didn't support masking files, | ||
// only directories | ||
var protectKernelHiddenPaths = []string{ | ||
"/proc/config.gz", | ||
"/proc/kallsyms", | ||
"/proc/sched_debug", | ||
"/proc/kcore", | ||
"/proc/kmem", | ||
"/proc/mem", | ||
} | ||
|
||
// prepareApp sets up the internal runtime context for a specific app. | ||
func prepareApp(p *stage1commontypes.Pod, ra *schema.RuntimeApp) (*preparedApp, error) { | ||
pa := preparedApp{ | ||
app: ra, | ||
env: ra.App.Environment, | ||
noNewPrivileges: getAppNoNewPrivileges(ra.App.Isolators), | ||
} | ||
var err error | ||
|
||
// Determine numeric uid and gid | ||
u, g, err := parseUserGroup(p, ra) | ||
if err != nil { | ||
return nil, errwrap.Wrap(errors.New("unable to determine app's uid and gid"), err) | ||
} | ||
if u < 0 || g < 0 { | ||
return nil, errors.New("Invalid uid or gid") | ||
} | ||
pa.uid = uint32(u) | ||
pa.gid = uint32(g) | ||
|
||
// Set some rkt-provided environment variables | ||
pa.env.Set("AC_APP_NAME", ra.Name.String()) | ||
if p.MetadataServiceURL != "" { | ||
pa.env.Set("AC_METADATA_URL", p.MetadataServiceURL) | ||
} | ||
|
||
// Determine capability set | ||
pa.capabilities, err = getAppCapabilities(ra.App.Isolators) | ||
if err != nil { | ||
return nil, errwrap.Wrap(errors.New("unable to construct capabilities"), err) | ||
} | ||
|
||
// Determine mounts | ||
cfd := ConvertedFromDocker(p.Images[ra.Name.String()]) | ||
pa.mounts, err = GenerateMounts(ra, p.Manifest.Volumes, cfd) | ||
if err != nil { | ||
return nil, errwrap.Wrap(errors.New("unable to compute mounts"), err) | ||
} | ||
|
||
// Compute resources | ||
pa.resources, err = computeAppResources(ra.App.Isolators) | ||
if err != nil { | ||
return nil, errwrap.Wrap(errors.New("unable to compute resources"), err) | ||
} | ||
|
||
// Protect kernel tunables by default | ||
if !p.InsecureOptions.DisablePaths { | ||
pa.roPaths = append(pa.roPaths, protectKernelROPaths...) | ||
pa.hiddenPaths = append(pa.hiddenDirs, protectKernelHiddenPaths...) | ||
pa.hiddenDirs = append(pa.hiddenDirs, protectKernelHiddenDirs...) | ||
} | ||
|
||
// Seccomp | ||
if !p.InsecureOptions.DisableSeccomp { | ||
pa.seccomp, err = generateSeccompFilter(p, &pa) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if pa.seccomp != nil && pa.seccomp.forceNoNewPrivileges { | ||
pa.noNewPrivileges = true | ||
} | ||
} | ||
|
||
// Write the systemd-sysusers config file | ||
if err := generateSysusers(p, pa.app, int(pa.uid), int(pa.gid), &p.UidRange); err != nil { | ||
return nil, errwrap.Wrapf("unable to generate sysusers file", err) | ||
} | ||
|
||
return &pa, nil | ||
} | ||
|
||
// computeAppResources processes any isolators that manipulate cgroups. | ||
func computeAppResources(isolators types.Isolators) (appResources, error) { | ||
res := appResources{} | ||
var err error | ||
|
||
withIsolator := func(name string, f func() error) error { | ||
ok, err := cgroup.IsIsolatorSupported(name) | ||
if err != nil { | ||
return errwrap.Wrapf("could not check for isolator "+name, err) | ||
} | ||
|
||
if !ok { | ||
fmt.Fprintf(os.Stderr, "warning: resource/%s isolator set but support disabled in the kernel, skipping\n", name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, this is still here, and it was there also in the old code. I'll submit an issue to get rid of all |
||
return nil | ||
} | ||
|
||
return f() | ||
} | ||
|
||
for _, isolator := range isolators { | ||
if err != nil { | ||
return res, err | ||
} | ||
|
||
switch v := isolator.Value().(type) { | ||
case *types.ResourceMemory: | ||
err = withIsolator("memory", func() error { | ||
if v.Limit() == nil { | ||
return nil | ||
} | ||
|
||
val := uint64(v.Limit().Value()) | ||
res.MemoryLimit = &val | ||
return nil | ||
}) | ||
case *types.ResourceCPU: | ||
err = withIsolator("cpu", func() error { | ||
if v.Limit() == nil { | ||
return nil | ||
} | ||
if v.Limit().Value() > MaxMilliValue { | ||
return fmt.Errorf("cpu limit exceeds the maximum millivalue: %v", v.Limit().String()) | ||
} | ||
|
||
val := uint64(v.Limit().MilliValue() / 10) | ||
res.CPUQuota = &val | ||
return nil | ||
}) | ||
case *types.LinuxCPUShares: | ||
err = withIsolator("cpu", func() error { | ||
val := uint64(*v) | ||
res.LinuxCPUShares = &val | ||
return nil | ||
}) | ||
case *types.LinuxOOMScoreAdj: | ||
val := int(*v) | ||
res.LinuxOOMScoreAdjust = &val | ||
} | ||
} | ||
|
||
return res, err | ||
} | ||
|
||
// relAppPaths prepends the relative app path (/opt/stage1/rootfs/) to a list | ||
// of paths. Useful for systemd unit directives. | ||
func (pa *preparedApp) relAppPaths(paths []string) []string { | ||
out := make([]string, 0, len(paths)) | ||
for _, p := range paths { | ||
out = append(out, filepath.Join(common.RelAppRootfsPath(pa.app.Name), p)) | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's return a named or typed Error and do the warn printing in the
stage1
entrypoint code, not here incommon
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. This is tricky because this could possibly be the case for multiple isolators. What's the idiomatic way of representing multiple warnings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The consumer would then do a type assertion if it is interested which isolator is unsupported: