Skip to content

rename network-config.json to ports.json to match reality #4376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/dir.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Files:
- `<CID>-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.

### `<DATAROOT>/<ADDRHASH>/names/<NAMESPACE>`
e.g. `/var/lib/nerdctl/1935db59/names/default`
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package networkstore
package portstore

import (
"encoding/json"
Expand All @@ -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)
}
}()

Expand All @@ -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)
}
}()

Expand All @@ -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
Expand All @@ -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
})
Expand Down
14 changes: 7 additions & 7 deletions pkg/portutil/portutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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]
Expand Down
Loading