-
Notifications
You must be signed in to change notification settings - Fork 611
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
Add bypass4netns integration #808
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
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
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
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,87 @@ | ||
/* | ||
Copyright The containerd 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 bypass4netnsutil | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
|
||
"github.com/containerd/containerd/errdefs" | ||
gocni "github.com/containerd/go-cni" | ||
b4nnapi "github.com/rootless-containers/bypass4netns/pkg/api" | ||
"github.com/rootless-containers/bypass4netns/pkg/api/daemon/client" | ||
) | ||
|
||
func NewBypass4netnsCNIBypassManager(client client.Client) (*Bypass4netnsCNIBypassManager, error) { | ||
if client == nil { | ||
return nil, errdefs.ErrInvalidArgument | ||
} | ||
pm := &Bypass4netnsCNIBypassManager{ | ||
Client: client, | ||
} | ||
return pm, nil | ||
} | ||
|
||
type Bypass4netnsCNIBypassManager struct { | ||
client.Client | ||
} | ||
|
||
func (b4nnm *Bypass4netnsCNIBypassManager) StartBypass(ctx context.Context, ports []gocni.PortMapping, id, stateDir string) error { | ||
socketPath, err := GetSocketPathByID(id) | ||
if err != nil { | ||
return err | ||
} | ||
pidFilePath, err := GetPidFilePathByID(id) | ||
if err != nil { | ||
return err | ||
} | ||
logFilePath := filepath.Join(stateDir, "bypass4netns.log") | ||
|
||
spec := b4nnapi.BypassSpec{ | ||
ID: id, | ||
SocketPath: socketPath, | ||
PidFilePath: pidFilePath, | ||
LogFilePath: logFilePath, | ||
// TODO: Remove hard-coded subnets | ||
IgnoreSubnets: []string{"127.0.0.0/8", "10.0.0.0/8"}, | ||
} | ||
portMap := []b4nnapi.PortSpec{} | ||
for _, p := range ports { | ||
portMap = append(portMap, b4nnapi.PortSpec{ | ||
ParentIP: p.HostIP, | ||
ParentPort: int(p.HostPort), | ||
ChildPort: int(p.ContainerPort), | ||
Protos: []string{p.Protocol}, | ||
}) | ||
} | ||
spec.PortMapping = portMap | ||
_, err = b4nnm.BypassManager().StartBypass(ctx, spec) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b4nnm *Bypass4netnsCNIBypassManager) StopBypass(ctx context.Context, id string) error { | ||
err := b4nnm.BypassManager().StopBypass(ctx, id) | ||
if err != nil { | ||
return 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,143 @@ | ||
/* | ||
Copyright The containerd 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 bypass4netnsutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/containerd/containerd/containers" | ||
"github.com/containerd/containerd/oci" | ||
"github.com/containerd/nerdctl/pkg/labels" | ||
"github.com/opencontainers/runtime-spec/specs-go" | ||
b4nnoci "github.com/rootless-containers/bypass4netns/pkg/oci" | ||
) | ||
|
||
func generateSecurityOpt(listenerPath string) (oci.SpecOpts, error) { | ||
opt := func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error { | ||
s.Linux.Seccomp = b4nnoci.GetDefaultSeccompProfile(listenerPath) | ||
return nil | ||
} | ||
return opt, nil | ||
} | ||
|
||
func GenerateBypass4netnsOpts(securityOptsMaps map[string]string, labelMaps map[string]string, id string) ([]oci.SpecOpts, error) { | ||
b4nn, ok := labelMaps[labels.Bypass4netns] | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
b4nnEnable, err := strconv.ParseBool(b4nn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !b4nnEnable { | ||
return nil, nil | ||
} | ||
|
||
if _, ok := securityOptsMaps["seccomp"]; ok { | ||
return nil, fmt.Errorf("--security-opt seccomp cannot be specified if bypass4netns enabled") | ||
} | ||
|
||
socketPath, err := GetSocketPathByID(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = CreateSocketDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := []oci.SpecOpts{} | ||
opt, err := generateSecurityOpt(socketPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
opts = append(opts, opt) | ||
|
||
return opts, nil | ||
} | ||
|
||
func getXDGRuntimeDir() (string, error) { | ||
if xrd := os.Getenv("XDG_RUNTIME_DIR"); xrd != "" { | ||
return xrd, nil | ||
} | ||
return "", fmt.Errorf("environment variable XDG_RUNTIME_DIR is not set") | ||
} | ||
|
||
func CreateSocketDir() error { | ||
xdgRuntimeDir, err := getXDGRuntimeDir() | ||
if err != nil { | ||
return err | ||
} | ||
dirPath := filepath.Join(xdgRuntimeDir, "bypass4netns") | ||
if _, err := os.Stat(dirPath); os.IsNotExist(err) { | ||
err = os.MkdirAll(dirPath, 0775) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GetBypass4NetnsdDefaultSocketPath() (string, error) { | ||
xdgRuntimeDir, err := getXDGRuntimeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(xdgRuntimeDir, "bypass4netnsd.sock"), nil | ||
} | ||
|
||
func GetSocketPathByID(id string) (string, error) { | ||
xdgRuntimeDir, err := getXDGRuntimeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
socketPath := filepath.Join(xdgRuntimeDir, "bypass4netns", id[0:15]+".sock") | ||
return socketPath, nil | ||
} | ||
|
||
func GetPidFilePathByID(id string) (string, error) { | ||
xdgRuntimeDir, err := getXDGRuntimeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
socketPath := filepath.Join(xdgRuntimeDir, "bypass4netns", id[0:15]+".pid") | ||
return socketPath, nil | ||
} | ||
|
||
func IsBypass4netnsEnabled(annotations map[string]string) (bool, error) { | ||
if b4nn, ok := annotations[labels.Bypass4netns]; ok { | ||
b4nnEnable, err := strconv.ParseBool(b4nn) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return b4nnEnable, nil | ||
} | ||
|
||
return false, 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
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.
"10.0.0.0/8" shouldn't be hardcoded.
But you may add a documentation about this and call it a day.
Eventually, this should be passed from (equivalent of )
nerdctl network list
andnerdctl network inspect
.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 documentation can be added in
docs/rootless.md
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.
Also please explain that bypass4netns is experimental in https://github.com/containerd/nerdctl/blob/master/docs/experimental.md