Skip to content

Commit 6effd83

Browse files
committed
fix: allow containers to start using a large numbers of ports
This commit adds logic to maintain backward compatibility. Signed-off-by: Hayato Kiwata <[email protected]>
1 parent 20a06bb commit 6effd83

File tree

11 files changed

+79
-29
lines changed

11 files changed

+79
-29
lines changed

cmd/nerdctl/compose/compose_ps.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ func composeContainerPrintableTab(ctx context.Context, container containerd.Cont
255255
if err != nil {
256256
return composeContainerPrintable{}, err
257257
}
258-
ports, err := portutil.LoadPortMappings(dataStore, gOptions.Namespace, info.ID)
258+
containerLabels, err := container.Labels(ctx)
259+
if err != nil {
260+
return composeContainerPrintable{}, err
261+
}
262+
ports, err := portutil.LoadPortMappings(dataStore, gOptions.Namespace, info.ID, containerLabels[labels.Ports])
259263
if err != nil {
260264
return composeContainerPrintable{}, err
261265
}
@@ -306,7 +310,11 @@ func composeContainerPrintableJSON(ctx context.Context, container containerd.Con
306310
if err != nil {
307311
return composeContainerPrintable{}, err
308312
}
309-
portMappings, err := portutil.LoadPortMappings(dataStore, gOptions.Namespace, info.ID)
313+
containerLabels, err := container.Labels(ctx)
314+
if err != nil {
315+
return composeContainerPrintable{}, err
316+
}
317+
portMappings, err := portutil.LoadPortMappings(dataStore, gOptions.Namespace, info.ID, containerLabels[labels.Ports])
310318
if err != nil {
311319
return composeContainerPrintable{}, err
312320
}

cmd/nerdctl/container/container_port.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containerd/nerdctl/v2/pkg/clientutil"
3030
"github.com/containerd/nerdctl/v2/pkg/containerutil"
3131
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
32+
"github.com/containerd/nerdctl/v2/pkg/labels"
3233
"github.com/containerd/nerdctl/v2/pkg/portutil"
3334
)
3435

@@ -93,7 +94,11 @@ func portAction(cmd *cobra.Command, args []string) error {
9394
if found.MatchCount > 1 {
9495
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
9596
}
96-
ports, err := portutil.LoadPortMappings(dataStore, globalOptions.Namespace, found.Container.ID())
97+
containerLabels, err := found.Container.Labels(ctx)
98+
if err != nil {
99+
return err
100+
}
101+
ports, err := portutil.LoadPortMappings(dataStore, globalOptions.Namespace, found.Container.ID(), containerLabels[labels.Ports])
97102
if err != nil {
98103
return err
99104
}

pkg/cmd/container/inspect.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
3232
"github.com/containerd/nerdctl/v2/pkg/imgutil"
3333
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
34+
"github.com/containerd/nerdctl/v2/pkg/labels"
3435
"github.com/containerd/nerdctl/v2/pkg/portutil"
3536
)
3637

@@ -80,7 +81,11 @@ func (x *containerInspector) Handler(ctx context.Context, found containerwalker.
8081
return err
8182
}
8283

83-
ports, err := portutil.LoadPortMappings(x.dataStore, x.namespace, n.ID)
84+
containerLabels, err := found.Container.Labels(ctx)
85+
if err != nil {
86+
return err
87+
}
88+
ports, err := portutil.LoadPortMappings(x.dataStore, x.namespace, n.ID, containerLabels[labels.Ports])
8489
if err != nil {
8590
return err
8691
}

pkg/cmd/container/kill.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ func cleanupNetwork(ctx context.Context, container containerd.Container, globalO
128128
if err != nil {
129129
return err
130130
}
131-
ports, err := portutil.LoadPortMappings(dataStore, globalOpts.Namespace, container.ID())
131+
containerLabels, err := container.Labels(ctx)
132+
if err != nil {
133+
return err
134+
}
135+
ports, err := portutil.LoadPortMappings(dataStore, globalOpts.Namespace, container.ID(), containerLabels[labels.Ports])
132136
if err != nil {
133137
return fmt.Errorf("no oci spec: %q", err)
134138
}

pkg/cmd/container/list.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container
168168
if err != nil {
169169
return nil, err
170170
}
171-
ports, err := portutil.LoadPortMappings(dataStore, options.GOptions.Namespace, c.ID())
171+
containerLabels, err := c.Labels(ctx)
172+
if err != nil {
173+
return nil, err
174+
}
175+
ports, err := portutil.LoadPortMappings(dataStore, options.GOptions.Namespace, c.ID(), containerLabels[labels.Ports])
172176
if err != nil {
173177
return nil, err
174178
}

pkg/cmd/container/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func RemoveContainer(ctx context.Context, c containerd.Container, globalOptions
197197
return
198198
}
199199

200-
portSlice, err := portutil.LoadPortMappings(dataStore, globalOptions.Namespace, id)
200+
portSlice, err := portutil.LoadPortMappings(dataStore, globalOptions.Namespace, id, containerLabels[labels.Ports])
201201
if err != nil {
202202
retErr = err
203203
return

pkg/composer/port.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io"
2323

2424
"github.com/containerd/nerdctl/v2/pkg/containerutil"
25+
"github.com/containerd/nerdctl/v2/pkg/labels"
2526
"github.com/containerd/nerdctl/v2/pkg/portutil"
2627
)
2728

@@ -51,7 +52,11 @@ func (c *Composer) Port(ctx context.Context, writer io.Writer, po PortOptions) e
5152
po.Index, len(containers), po.ServiceName)
5253
}
5354
container := containers[po.Index-1]
54-
ports, err := portutil.LoadPortMappings(po.DataStore, po.Namespace, container.ID())
55+
containerLabels, err := container.Labels(ctx)
56+
if err != nil {
57+
return err
58+
}
59+
ports, err := portutil.LoadPortMappings(po.DataStore, po.Namespace, container.ID(), containerLabels[labels.Ports])
5560
if err != nil {
5661
return err
5762
}

pkg/labels/labels.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ const (
5757
// Currently, the length of the slice must be 1.
5858
Networks = Prefix + "networks"
5959

60+
// DEPRECATED : https://github.com/containerd/nerdctl/pull/4290
61+
// Ports is a JSON-marshalled string of []cni.PortMapping .
62+
Ports = Prefix + "ports"
63+
6064
// IPAddress is the static IP address of the container assigned by the user
6165
IPAddress = Prefix + "ip"
6266

pkg/netutil/networkstore/networkstore.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"fmt"
23-
"os"
2423
"path/filepath"
2524

2625
"github.com/containerd/go-cni"
@@ -86,25 +85,25 @@ func (ns *NetworkStore) Load() (err error) {
8685
}
8786
}()
8887

89-
loc, err := ns.safeStore.Location(networkConfigName)
90-
if err != nil {
91-
return err
92-
}
93-
if _, err := os.Stat(loc); err != nil {
94-
return nil
95-
}
96-
9788
return ns.safeStore.WithLock(func() error {
89+
doesExist, err := ns.safeStore.Exists(networkConfigName)
90+
if err != nil || !doesExist {
91+
return err
92+
}
93+
9894
data, err := ns.safeStore.Get(networkConfigName)
99-
if err == nil {
100-
var ports []cni.PortMapping
101-
if err := json.Unmarshal(data, &ports); err != nil {
102-
return fmt.Errorf("failed to parse port mappings %v: %w", ports, err)
95+
if err != nil {
96+
if errors.Is(err, store.ErrNotFound) {
97+
err = nil
10398
}
104-
ns.PortMappings = ports
105-
} else if errors.Is(err, store.ErrNotFound) {
106-
err = nil
99+
return err
100+
}
101+
102+
var ports []cni.PortMapping
103+
if err := json.Unmarshal(data, &ports); err != nil {
104+
return fmt.Errorf("failed to parse port mappings %v: %w", ports, err)
107105
}
106+
ns.PortMappings = ports
108107

109108
return err
110109
})

pkg/ocihook/ocihook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ func newHandlerOpts(state *specs.State, dataStore, cniPath, cniNetconfPath, brid
209209
}
210210
}
211211

212-
ports, err := portutil.LoadPortMappings(o.dataStore, namespace, o.state.ID)
212+
portsJSON := o.state.Annotations[labels.Ports]
213+
ports, err := portutil.LoadPortMappings(o.dataStore, namespace, o.state.ID, portsJSON)
213214
if err != nil {
214215
return nil, err
215216
}

0 commit comments

Comments
 (0)