-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
pkg/ns: refactored so that builds succeed on non-linux platforms #375
Merged
dcbw
merged 4 commits into
containernetworking:master
from
aaithal:complieOnNonLinuxPlatforms
Feb 27, 2017
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c3cde44
pkg/ns: refactored so that builds succeed on non-linux platforms
aaithal 0dc0845
pkg/ns, pkg/types: refactored non linux build fix code to
aaithal 7eeb21a
pkg/ip: refactored so that builds succeed on non-linux platforms
aaithal 460e1c5
vendor: Update vishvanana/netlink dependency
aaithal 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,144 @@ | ||
// Copyright 2015-2017 CNI 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. | ||
|
||
package ns | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"os" | ||
"path" | ||
"runtime" | ||
"sync" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func getCurrentThreadNetNSPath() string { | ||
// /proc/self/ns/net returns the namespace of the main thread, not | ||
// of whatever thread this goroutine is running on. Make sure we | ||
// use the thread's net namespace since the thread is switching around | ||
return fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid()) | ||
} | ||
|
||
// Creates a new persistent network namespace and returns an object | ||
// representing that namespace, without switching to it | ||
func NewNS() (NetNS, error) { | ||
const nsRunDir = "/var/run/netns" | ||
|
||
b := make([]byte, 16) | ||
_, err := rand.Reader.Read(b) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to generate random netns name: %v", err) | ||
} | ||
|
||
err = os.MkdirAll(nsRunDir, 0755) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// create an empty file at the mount point | ||
nsName := fmt.Sprintf("cni-%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) | ||
nsPath := path.Join(nsRunDir, nsName) | ||
mountPointFd, err := os.Create(nsPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
mountPointFd.Close() | ||
|
||
// Ensure the mount point is cleaned up on errors; if the namespace | ||
// was successfully mounted this will have no effect because the file | ||
// is in-use | ||
defer os.RemoveAll(nsPath) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
// do namespace work in a dedicated goroutine, so that we can safely | ||
// Lock/Unlock OSThread without upsetting the lock/unlock state of | ||
// the caller of this function | ||
var fd *os.File | ||
go (func() { | ||
defer wg.Done() | ||
runtime.LockOSThread() | ||
|
||
var origNS NetNS | ||
origNS, err = GetNS(getCurrentThreadNetNSPath()) | ||
if err != nil { | ||
return | ||
} | ||
defer origNS.Close() | ||
|
||
// create a new netns on the current thread | ||
err = unix.Unshare(unix.CLONE_NEWNET) | ||
if err != nil { | ||
return | ||
} | ||
defer origNS.Set() | ||
|
||
// bind mount the new netns from the current thread onto the mount point | ||
err = unix.Mount(getCurrentThreadNetNSPath(), nsPath, "none", unix.MS_BIND, "") | ||
if err != nil { | ||
return | ||
} | ||
|
||
fd, err = os.Open(nsPath) | ||
if err != nil { | ||
return | ||
} | ||
})() | ||
wg.Wait() | ||
|
||
if err != nil { | ||
unix.Unmount(nsPath, unix.MNT_DETACH) | ||
return nil, fmt.Errorf("failed to create namespace: %v", err) | ||
} | ||
|
||
return &netNS{file: fd, mounted: true}, nil | ||
} | ||
|
||
func (ns *netNS) Close() error { | ||
if err := ns.errorIfClosed(); err != nil { | ||
return err | ||
} | ||
|
||
if err := ns.file.Close(); err != nil { | ||
return fmt.Errorf("Failed to close %q: %v", ns.file.Name(), err) | ||
} | ||
ns.closed = true | ||
|
||
if ns.mounted { | ||
if err := unix.Unmount(ns.file.Name(), unix.MNT_DETACH); err != nil { | ||
return fmt.Errorf("Failed to unmount namespace %s: %v", ns.file.Name(), err) | ||
} | ||
if err := os.RemoveAll(ns.file.Name()); err != nil { | ||
return fmt.Errorf("Failed to clean up namespace %s: %v", ns.file.Name(), err) | ||
} | ||
ns.mounted = false | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ns *netNS) Set() error { | ||
if err := ns.errorIfClosed(); err != nil { | ||
return err | ||
} | ||
|
||
if _, _, err := unix.Syscall(unix.SYS_SETNS, ns.Fd(), uintptr(unix.CLONE_NEWNET), 0); err != 0 { | ||
return fmt.Errorf("Error switching to ns %v: %v", ns.file.Name(), err) | ||
} | ||
|
||
return nil | ||
} |
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,33 @@ | ||
// Copyright 2015-2017 CNI 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 ns | ||
|
||
func getCurrentThreadNetNSPath() string { | ||
return "" | ||
} | ||
|
||
func NewNS() (NetNS, error) { | ||
return nil, NotImplementedError | ||
} | ||
|
||
func (ns *netNS) Close() error { | ||
return NotImplementedError | ||
} | ||
|
||
func (ns *netNS) Set() error { | ||
return NotImplementedError | ||
} |
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.
This makes me a bit uncomfortable: this is used in
ns.Do()
. I know that it currently won't cause problems, but...Could you refactor
Do()
to useGetCurrentNS()
, moveGetCurrentNS()
to the per-os files, and movegetCurrentThreadNetNSPath
tons_linux.go
?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.
Yes, sure. That's cleaner than how I did it.