Skip to content

Commit ab7456e

Browse files
committed
annotations: add nerdctl/bypass4netns-ignore-subnets ([]string)
For experiments of additional `bypass4netns --ignore` Signed-off-by: Akihiro Suda <[email protected]>
1 parent f42ab2f commit ab7456e

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

pkg/annotations/annotations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const (
2525
// Boolean value which can be parsed with strconv.ParseBool() is required.
2626
// (like "nerdctl/bypass4netns=true" or "nerdctl/bypass4netns=false")
2727
Bypass4netns = Prefix + "bypass4netns"
28+
29+
// Bypass4netnsIgnoreSubnets is a JSON of []string that is appended to
30+
// the `bypass4netns --ignore` list.
31+
Bypass4netnsIgnoreSubnets = Bypass4netns + "-ignore-subnets"
2832
)
2933

3034
var ShellCompletions = []string{

pkg/bypass4netnsutil/bypass.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,41 @@ package bypass4netnsutil
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"net"
2324
"path/filepath"
2425

2526
"github.com/containerd/containerd/errdefs"
2627
gocni "github.com/containerd/go-cni"
28+
"github.com/containerd/nerdctl/v2/pkg/annotations"
2729
b4nnapi "github.com/rootless-containers/bypass4netns/pkg/api"
2830
"github.com/rootless-containers/bypass4netns/pkg/api/daemon/client"
2931
rlkclient "github.com/rootless-containers/rootlesskit/v2/pkg/api/client"
3032
)
3133

32-
func NewBypass4netnsCNIBypassManager(client client.Client, rlkClient rlkclient.Client) (*Bypass4netnsCNIBypassManager, error) {
34+
func NewBypass4netnsCNIBypassManager(client client.Client, rlkClient rlkclient.Client, annotationsMap map[string]string) (*Bypass4netnsCNIBypassManager, error) {
3335
if client == nil || rlkClient == nil {
3436
return nil, errdefs.ErrInvalidArgument
3537
}
38+
var ignoreSubnets []string
39+
if v := annotationsMap[annotations.Bypass4netnsIgnoreSubnets]; v != "" {
40+
if err := json.Unmarshal([]byte(v), &ignoreSubnets); err != nil {
41+
return nil, fmt.Errorf("failed to unmarshal annotation %q: %q: %w", annotations.Bypass4netnsIgnoreSubnets, v, err)
42+
}
43+
}
3644
pm := &Bypass4netnsCNIBypassManager{
37-
Client: client,
38-
rlkClient: rlkClient,
45+
Client: client,
46+
rlkClient: rlkClient,
47+
ignoreSubnets: ignoreSubnets,
3948
}
4049
return pm, nil
4150
}
4251

4352
type Bypass4netnsCNIBypassManager struct {
4453
client.Client
45-
rlkClient rlkclient.Client
54+
rlkClient rlkclient.Client
55+
ignoreSubnets []string
4656
}
4757

4858
func (b4nnm *Bypass4netnsCNIBypassManager) StartBypass(ctx context.Context, ports []gocni.PortMapping, id, stateDir string) error {
@@ -73,7 +83,7 @@ func (b4nnm *Bypass4netnsCNIBypassManager) StartBypass(ctx context.Context, port
7383
PidFilePath: pidFilePath,
7484
LogFilePath: logFilePath,
7585
// "auto" can detect CNI CIDRs automatically
76-
IgnoreSubnets: []string{"127.0.0.0/8", rlkCIDR, "auto"},
86+
IgnoreSubnets: append([]string{"127.0.0.0/8", rlkCIDR, "auto"}, b4nnm.ignoreSubnets...),
7787
}
7888
portMap := []b4nnapi.PortSpec{}
7989
for _, p := range ports {

pkg/ocihook/ocihook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func applyNetworkSettings(opts *handlerOpts) error {
449449

450450
if rootlessutil.IsRootlessChild() {
451451
if b4nnEnabled {
452-
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient)
452+
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient, opts.state.Annotations)
453453
if err != nil {
454454
return err
455455
}
@@ -493,7 +493,7 @@ func onPostStop(opts *handlerOpts) error {
493493
}
494494
if rootlessutil.IsRootlessChild() {
495495
if b4nnEnabled {
496-
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient)
496+
bm, err := bypass4netnsutil.NewBypass4netnsCNIBypassManager(opts.bypassClient, opts.rootlessKitClient, opts.state.Annotations)
497497
if err != nil {
498498
return err
499499
}

0 commit comments

Comments
 (0)