Skip to content

fix: allow storing additional network info in network-config.json #4385

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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.
- `network-config.json`: used to store container-specific network configuration, such as port mappings.

### `<DATAROOT>/<ADDRHASH>/names/<NAMESPACE>`
e.g. `/var/lib/nerdctl/1935db59/names/default`
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/containerd/nerdctl/v2/pkg/maputil"
"github.com/containerd/nerdctl/v2/pkg/mountutil"
"github.com/containerd/nerdctl/v2/pkg/namestore"
"github.com/containerd/nerdctl/v2/pkg/netutil/networkstore"
"github.com/containerd/nerdctl/v2/pkg/platformutil"
"github.com/containerd/nerdctl/v2/pkg/portutil"
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
Expand Down Expand Up @@ -390,7 +391,10 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
}
cOpts = append(cOpts, ilOpt)

err = portutil.GeneratePortMappingsConfig(dataStore, options.GOptions.Namespace, id, netLabelOpts.PortMappings)
netConf := networkstore.NetworkConfig{
PortMappings: netLabelOpts.PortMappings,
}
err = portutil.StoreNetworkConfig(dataStore, options.GOptions.Namespace, id, netConf)
if err != nil {
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), fmt.Errorf("Error writing to network-config.json: %v", err)
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/netutil/networkstore/networkstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,30 @@ func New(dataStore, namespace, containerID string) (ns *NetworkStore, err error)
}, nil
}

type NetworkConfig struct {
PortMappings []cni.PortMapping `json:"portMappings,omitempty"`
}

type NetworkStore struct {
safeStore store.Store

PortMappings []cni.PortMapping
NetConf NetworkConfig
}

func (ns *NetworkStore) Acquire(portMappings []cni.PortMapping) (err error) {
func (ns *NetworkStore) Acquire(netConf NetworkConfig) (err error) {
defer func() {
if err != nil {
err = errors.Join(ErrNetworkStore, err)
}
}()

portsJSON, err := json.Marshal(portMappings)
netConfJSON, err := json.Marshal(netConf)
if err != nil {
return fmt.Errorf("failed to marshal port mappings to JSON: %w", err)
return fmt.Errorf("failed to marshal network config to JSON: %w", err)
}

return ns.safeStore.WithLock(func() error {
return ns.safeStore.Set(portsJSON, networkConfigName)
return ns.safeStore.Set(netConfJSON, networkConfigName)
})
}

Expand All @@ -99,11 +103,11 @@ func (ns *NetworkStore) Load() (err error) {
return err
}

var ports []cni.PortMapping
if err := json.Unmarshal(data, &ports); err != nil {
return fmt.Errorf("failed to parse port mappings %v: %w", ports, err)
var netConf NetworkConfig
if err := json.Unmarshal(data, &netConf); err != nil {
return fmt.Errorf("failed to parse network config %v: %w", netConf, err)
}
ns.PortMappings = ports
ns.NetConf = netConf

return err
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/portutil/portutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ func ParseFlagP(s string) ([]cni.PortMapping, error) {
return mr, nil
}

func GeneratePortMappingsConfig(dataStore, namespace, id string, portMappings []cni.PortMapping) error {
func StoreNetworkConfig(dataStore, namespace, id string, netConf networkstore.NetworkConfig) error {
ns, err := networkstore.New(dataStore, namespace, id)
if err != nil {
return err
}
return ns.Acquire(portMappings)
return ns.Acquire(netConf)
}

func LoadPortMappings(dataStore, namespace, id string, containerLabels map[string]string) ([]cni.PortMapping, error) {
Expand All @@ -158,8 +158,8 @@ func LoadPortMappings(dataStore, namespace, id string, containerLabels map[strin
if err = ns.Load(); err != nil {
return ports, err
}
if len(ns.PortMappings) != 0 {
return ns.PortMappings, nil
if len(ns.NetConf.PortMappings) != 0 {
return ns.NetConf.PortMappings, nil
}

portsJSON := containerLabels[labels.Ports]
Expand Down