Skip to content

Commit 385f12e

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 68534ed commit 385f12e

File tree

11 files changed

+72
-22
lines changed

11 files changed

+72
-22
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: 8 additions & 9 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,15 +85,15 @@ 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 {
91+
return err
92+
}
93+
if !doesExist {
94+
return nil
95+
}
96+
9897
data, err := ns.safeStore.Get(networkConfigName)
9998
if err == nil {
10099
var ports []cni.PortMapping

pkg/ocihook/ocihook.go

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

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

pkg/portutil/portutil.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package portutil
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"net"
2223
"strings"
@@ -26,6 +27,7 @@ import (
2627
"github.com/containerd/go-cni"
2728
"github.com/containerd/log"
2829

30+
"github.com/containerd/nerdctl/v2/pkg/labels"
2931
"github.com/containerd/nerdctl/v2/pkg/netutil/networkstore"
3032
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3133
)
@@ -136,13 +138,26 @@ func GeneratePortMappingsConfig(dataStore, namespace, id string, portMappings []
136138
return ns.Acquire(portMappings)
137139
}
138140

139-
func LoadPortMappings(dataStore, namespace, id string) ([]cni.PortMapping, error) {
141+
func LoadPortMappings(dataStore, namespace, id, portsJSON string) ([]cni.PortMapping, error) {
142+
var ports []cni.PortMapping
143+
140144
ns, err := networkstore.New(dataStore, namespace, id)
141145
if err != nil {
142-
return []cni.PortMapping{}, err
146+
return ports, err
143147
}
144148
if err = ns.Load(); err != nil {
145-
return []cni.PortMapping{}, err
149+
return ports, err
150+
}
151+
if len(ns.PortMappings) != 0 {
152+
return ns.PortMappings, nil
153+
}
154+
155+
if portsJSON == "" {
156+
return ports, nil
157+
}
158+
if err := json.Unmarshal([]byte(portsJSON), &ports); err != nil {
159+
return ports, fmt.Errorf("failed to parse label %q=%q: %s", labels.Ports, portsJSON, err.Error())
146160
}
147-
return ns.PortMappings, nil
161+
log.L.Warnf("container %s is using legacy port mapping configuration. To ensure compatibility with the new port mapping logic, please recreate this container. For more details, see: https://github.com/containerd/nerdctl/pull/4290", id[:12])
162+
return ports, nil
148163
}

0 commit comments

Comments
 (0)