diff --git a/docs/dir.md b/docs/dir.md index 61f5efae3a7..bc3e9c8e64d 100644 --- a/docs/dir.md +++ b/docs/dir.md @@ -35,7 +35,7 @@ Files: - `-json.log`: used by `nerdctl logs` - `oci-hook.*.log`: logs of the OCI hook - `lifecycle.json`: used to store stateful information about the container that can only be retrieved through OCI hooks -- `network-config.json`: used to store port mapping information for containers run with the `-p` option. +- `ports.json`: used to store port mapping information for containers run with the `-p` option. ### `//names/` e.g. `/var/lib/nerdctl/1935db59/names/default` diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 0dc2cfdc52e..4a1c941b390 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -392,7 +392,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa err = portutil.GeneratePortMappingsConfig(dataStore, options.GOptions.Namespace, id, netLabelOpts.PortMappings) if err != nil { - return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), fmt.Errorf("Error writing to network-config.json: %v", err) + return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), fmt.Errorf("Error writing to ports.json: %v", err) } opts = append(opts, propagateInternalContainerdLabelsToOCIAnnotations(), diff --git a/pkg/netutil/networkstore/networkstore.go b/pkg/portutil/portstore/portstore.go similarity index 69% rename from pkg/netutil/networkstore/networkstore.go rename to pkg/portutil/portstore/portstore.go index 0faa78ba9cf..63fa3e00724 100644 --- a/pkg/netutil/networkstore/networkstore.go +++ b/pkg/portutil/portstore/portstore.go @@ -14,7 +14,7 @@ limitations under the License. */ -package networkstore +package portstore import ( "encoding/json" @@ -29,15 +29,15 @@ import ( const ( containersDirBaseName = "containers" - networkConfigName = "network-config.json" + portsFileName = "ports.json" ) -var ErrNetworkStore = errors.New("network-store error") +var ErrPortStore = errors.New("port-store error") -func New(dataStore, namespace, containerID string) (ns *NetworkStore, err error) { +func New(dataStore, namespace, containerID string) (ns *PortStore, err error) { defer func() { if err != nil { - err = errors.Join(ErrNetworkStore, err) + err = errors.Join(ErrPortStore, err) } }() @@ -50,21 +50,21 @@ func New(dataStore, namespace, containerID string) (ns *NetworkStore, err error) return nil, err } - return &NetworkStore{ + return &PortStore{ safeStore: st, }, nil } -type NetworkStore struct { +type PortStore struct { safeStore store.Store PortMappings []cni.PortMapping } -func (ns *NetworkStore) Acquire(portMappings []cni.PortMapping) (err error) { +func (ps *PortStore) Acquire(portMappings []cni.PortMapping) (err error) { defer func() { if err != nil { - err = errors.Join(ErrNetworkStore, err) + err = errors.Join(ErrPortStore, err) } }() @@ -73,25 +73,25 @@ func (ns *NetworkStore) Acquire(portMappings []cni.PortMapping) (err error) { return fmt.Errorf("failed to marshal port mappings to JSON: %w", err) } - return ns.safeStore.WithLock(func() error { - return ns.safeStore.Set(portsJSON, networkConfigName) + return ps.safeStore.WithLock(func() error { + return ps.safeStore.Set(portsJSON, portsFileName) }) } -func (ns *NetworkStore) Load() (err error) { +func (ps *PortStore) Load() (err error) { defer func() { if err != nil { - err = errors.Join(ErrNetworkStore, err) + err = errors.Join(ErrPortStore, err) } }() - return ns.safeStore.WithLock(func() error { - doesExist, err := ns.safeStore.Exists(networkConfigName) + return ps.safeStore.WithLock(func() error { + doesExist, err := ps.safeStore.Exists(portsFileName) if err != nil || !doesExist { return err } - data, err := ns.safeStore.Get(networkConfigName) + data, err := ps.safeStore.Get(portsFileName) if err != nil { if errors.Is(err, store.ErrNotFound) { err = nil @@ -103,7 +103,7 @@ func (ns *NetworkStore) Load() (err error) { if err := json.Unmarshal(data, &ports); err != nil { return fmt.Errorf("failed to parse port mappings %v: %w", ports, err) } - ns.PortMappings = ports + ps.PortMappings = ports return err }) diff --git a/pkg/portutil/portutil.go b/pkg/portutil/portutil.go index 681988c654f..0b03bdcbcef 100644 --- a/pkg/portutil/portutil.go +++ b/pkg/portutil/portutil.go @@ -28,7 +28,7 @@ import ( "github.com/containerd/log" "github.com/containerd/nerdctl/v2/pkg/labels" - "github.com/containerd/nerdctl/v2/pkg/netutil/networkstore" + "github.com/containerd/nerdctl/v2/pkg/portutil/portstore" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) @@ -141,25 +141,25 @@ func ParseFlagP(s string) ([]cni.PortMapping, error) { } func GeneratePortMappingsConfig(dataStore, namespace, id string, portMappings []cni.PortMapping) error { - ns, err := networkstore.New(dataStore, namespace, id) + ps, err := portstore.New(dataStore, namespace, id) if err != nil { return err } - return ns.Acquire(portMappings) + return ps.Acquire(portMappings) } func LoadPortMappings(dataStore, namespace, id string, containerLabels map[string]string) ([]cni.PortMapping, error) { var ports []cni.PortMapping - ns, err := networkstore.New(dataStore, namespace, id) + ps, err := portstore.New(dataStore, namespace, id) if err != nil { return ports, err } - if err = ns.Load(); err != nil { + if err = ps.Load(); err != nil { return ports, err } - if len(ns.PortMappings) != 0 { - return ns.PortMappings, nil + if len(ps.PortMappings) != 0 { + return ps.PortMappings, nil } portsJSON := containerLabels[labels.Ports]