Skip to content

Commit

Permalink
annotations: add nerdctl/bypass4netns-ignore-subnets ([]string)
Browse files Browse the repository at this point in the history
For experiments of additional `bypass4netns --ignore`

Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Mar 31, 2024
1 parent f42ab2f commit fc4c8e7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion pkg/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ const (
// Boolean value which can be parsed with strconv.ParseBool() is required.
// (like "nerdctl/bypass4netns=true" or "nerdctl/bypass4netns=false")
Bypass4netns = Prefix + "bypass4netns"

// Bypass4netnsIgnoreSubnets is a JSON of []string that is appended to
// the `bypass4netns --ignore` list.
Bypass4netnsIgnoreSubnets = Bypass4netns + "-ignore-subnets"
)

var ShellCompletions = []string{
Bypass4netns + "=true",
Bypass4netns + "=false",
// Other annotations should not be set via CLI
Bypass4netnsIgnoreSubnets + "=",
}
20 changes: 15 additions & 5 deletions pkg/bypass4netnsutil/bypass.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@ package bypass4netnsutil

import (
"context"
"encoding/json"
"fmt"
"net"
"path/filepath"

"github.com/containerd/containerd/errdefs"
gocni "github.com/containerd/go-cni"
"github.com/containerd/nerdctl/v2/pkg/annotations"
b4nnapi "github.com/rootless-containers/bypass4netns/pkg/api"
"github.com/rootless-containers/bypass4netns/pkg/api/daemon/client"
rlkclient "github.com/rootless-containers/rootlesskit/v2/pkg/api/client"
)

func NewBypass4netnsCNIBypassManager(client client.Client, rlkClient rlkclient.Client) (*Bypass4netnsCNIBypassManager, error) {
func NewBypass4netnsCNIBypassManager(client client.Client, rlkClient rlkclient.Client, annotationsMap map[string]string) (*Bypass4netnsCNIBypassManager, error) {
if client == nil || rlkClient == nil {
return nil, errdefs.ErrInvalidArgument
}
var ignoreSubnets []string
if v := annotationsMap[annotations.Bypass4netnsIgnoreSubnets]; v != "" {
if err := json.Unmarshal([]byte(v), &ignoreSubnets); err != nil {
return nil, fmt.Errorf("failed to unmarshal annotation %q: %q: %w", annotations.Bypass4netnsIgnoreSubnets, v, err)
}
}
pm := &Bypass4netnsCNIBypassManager{
Client: client,
rlkClient: rlkClient,
Client: client,
rlkClient: rlkClient,
ignoreSubnets: ignoreSubnets,
}
return pm, nil
}

type Bypass4netnsCNIBypassManager struct {
client.Client
rlkClient rlkclient.Client
rlkClient rlkclient.Client
ignoreSubnets []string
}

func (b4nnm *Bypass4netnsCNIBypassManager) StartBypass(ctx context.Context, ports []gocni.PortMapping, id, stateDir string) error {
Expand Down Expand Up @@ -73,7 +83,7 @@ func (b4nnm *Bypass4netnsCNIBypassManager) StartBypass(ctx context.Context, port
PidFilePath: pidFilePath,
LogFilePath: logFilePath,
// "auto" can detect CNI CIDRs automatically
IgnoreSubnets: []string{"127.0.0.0/8", rlkCIDR, "auto"},
IgnoreSubnets: append([]string{"127.0.0.0/8", rlkCIDR, "auto"}, b4nnm.ignoreSubnets...),
}
portMap := []b4nnapi.PortSpec{}
for _, p := range ports {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ocihook/ocihook.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func applyNetworkSettings(opts *handlerOpts) error {

if rootlessutil.IsRootlessChild() {
if b4nnEnabled {
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient)
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient, opts.state.Annotations)
if err != nil {
return err
}
Expand Down Expand Up @@ -493,7 +493,7 @@ func onPostStop(opts *handlerOpts) error {
}
if rootlessutil.IsRootlessChild() {
if b4nnEnabled {
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient)
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient, opts.state.Annotations)
if err != nil {
return err
}
Expand Down

0 comments on commit fc4c8e7

Please sign in to comment.