-
Notifications
You must be signed in to change notification settings - Fork 950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: refact the setup spec mount #2653
Merged
Ace-Tang
merged 1 commit into
AliyunContainerService:master
from
rudyfly:refect_mount_spec
Jan 8, 2019
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"strings" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
"github.com/pkg/errors" | ||
|
||
specs "github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/sirupsen/logrus" | ||
|
@@ -40,10 +41,7 @@ func clearReadonly(m *specs.Mount) { | |
m.Options = opts | ||
} | ||
|
||
// setupMounts create mount spec. | ||
func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error { | ||
var mounts []specs.Mount | ||
// Override the default mounts which are duplicate with user defined ones. | ||
func overrideDefaultMount(mounts []specs.Mount, c *Container, s *specs.Spec) ([]specs.Mount, error) { | ||
for _, sm := range s.Mounts { | ||
dup := false | ||
for _, cm := range c.Mounts { | ||
|
@@ -55,21 +53,14 @@ func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error { | |
if dup { | ||
continue | ||
} | ||
if sm.Destination == "/dev/shm" && c.HostConfig.ShmSize != nil && | ||
*c.HostConfig.ShmSize != 0 { | ||
for idx, v := range sm.Options { | ||
if strings.Contains(v, "size=") { | ||
sm.Options[idx] = fmt.Sprintf("size=%s", strconv.FormatInt(*c.HostConfig.ShmSize, 10)) | ||
} | ||
} | ||
} | ||
|
||
mounts = append(mounts, sm) | ||
} | ||
|
||
if c.HostConfig == nil { | ||
return nil | ||
} | ||
// user defined mount | ||
return mounts, nil | ||
} | ||
|
||
func mergeContainerMount(mounts []specs.Mount, c *Container, s *specs.Spec) ([]specs.Mount, error) { | ||
for _, mp := range c.Mounts { | ||
if trySetupNetworkMount(mp, c) { | ||
// ignore the network mount, we will handle it later. | ||
|
@@ -79,7 +70,7 @@ func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error { | |
// check duplicate mountpoint | ||
for _, sm := range mounts { | ||
if sm.Destination == mp.Destination { | ||
return fmt.Errorf("duplicate mount point: %s", mp.Destination) | ||
return nil, fmt.Errorf("duplicate mount point: %s", mp.Destination) | ||
} | ||
} | ||
|
||
|
@@ -121,21 +112,54 @@ func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error { | |
mounts = append(mounts, generateNetworkMounts(c)...) | ||
} | ||
|
||
s.Mounts = sortMounts(mounts) | ||
return mounts, nil | ||
} | ||
|
||
// setupMounts create mount spec. | ||
func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error { | ||
var ( | ||
mounts []specs.Mount | ||
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. no need to define this parameter, WDYT? 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. I think it need. |
||
err error | ||
) | ||
|
||
// Override the default mounts which are duplicate with user defined ones. | ||
mounts, err = overrideDefaultMount(mounts, c, s) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to override default spec mounts") | ||
} | ||
|
||
// user defined mount | ||
mounts, err = mergeContainerMount(mounts, c, s) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to merge container mounts") | ||
} | ||
|
||
if c.HostConfig.Privileged { | ||
for i := range s.Mounts { | ||
// modify share memory size, and change rw mode for privileged mode. | ||
for i := range mounts { | ||
if mounts[i].Destination == "/dev/shm" && c.HostConfig.ShmSize != nil && | ||
*c.HostConfig.ShmSize != 0 { | ||
for idx, v := range mounts[i].Options { | ||
if strings.Contains(v, "size=") { | ||
mounts[i].Options[idx] = fmt.Sprintf("size=%s", | ||
strconv.FormatInt(*c.HostConfig.ShmSize, 10)) | ||
} | ||
} | ||
} | ||
|
||
if c.HostConfig.Privileged { | ||
// Clear readonly for /sys. | ||
if s.Mounts[i].Destination == "/sys" && !s.Root.Readonly { | ||
clearReadonly(&s.Mounts[i]) | ||
if mounts[i].Destination == "/sys" && !s.Root.Readonly { | ||
clearReadonly(&mounts[i]) | ||
} | ||
|
||
// Clear readonly for cgroup | ||
if s.Mounts[i].Type == "cgroup" { | ||
clearReadonly(&s.Mounts[i]) | ||
if mounts[i].Type == "cgroup" { | ||
clearReadonly(&mounts[i]) | ||
} | ||
} | ||
} | ||
|
||
s.Mounts = sortMounts(mounts) | ||
return nil | ||
} | ||
|
||
|
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.
Maybe we can add some test cases for the new function.
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.
It isn't complicated logic, I think it no need to add unit test.
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.
Should add, but can be in another pr