Skip to content

Commit b31cbf7

Browse files
authored
Merge pull request containerd#4385 from haytok/followup_of_issue_4027
fix: allow storing additional network info in network-config.json
2 parents c555e02 + 59890cb commit b31cbf7

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

docs/dir.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Files:
3535
- `<CID>-json.log`: used by `nerdctl logs`
3636
- `oci-hook.*.log`: logs of the OCI hook
3737
- `lifecycle.json`: used to store stateful information about the container that can only be retrieved through OCI hooks
38-
- `network-config.json`: used to store port mapping information for containers run with the `-p` option.
38+
- `network-config.json`: used to store container-specific network configuration, such as port mappings.
3939

4040
### `<DATAROOT>/<ADDRHASH>/names/<NAMESPACE>`
4141
e.g. `/var/lib/nerdctl/1935db59/names/default`

pkg/cmd/container/create.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import (
5959
"github.com/containerd/nerdctl/v2/pkg/maputil"
6060
"github.com/containerd/nerdctl/v2/pkg/mountutil"
6161
"github.com/containerd/nerdctl/v2/pkg/namestore"
62+
"github.com/containerd/nerdctl/v2/pkg/netutil/networkstore"
6263
"github.com/containerd/nerdctl/v2/pkg/platformutil"
6364
"github.com/containerd/nerdctl/v2/pkg/portutil"
6465
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
@@ -390,7 +391,10 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
390391
}
391392
cOpts = append(cOpts, ilOpt)
392393

393-
err = portutil.GeneratePortMappingsConfig(dataStore, options.GOptions.Namespace, id, netLabelOpts.PortMappings)
394+
netConf := networkstore.NetworkConfig{
395+
PortMappings: netLabelOpts.PortMappings,
396+
}
397+
err = portutil.StoreNetworkConfig(dataStore, options.GOptions.Namespace, id, netConf)
394398
if err != nil {
395399
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), fmt.Errorf("Error writing to network-config.json: %v", err)
396400
}

pkg/netutil/networkstore/networkstore.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,30 @@ func New(dataStore, namespace, containerID string) (ns *NetworkStore, err error)
5555
}, nil
5656
}
5757

58+
type NetworkConfig struct {
59+
PortMappings []cni.PortMapping `json:"portMappings,omitempty"`
60+
}
61+
5862
type NetworkStore struct {
5963
safeStore store.Store
6064

61-
PortMappings []cni.PortMapping
65+
NetConf NetworkConfig
6266
}
6367

64-
func (ns *NetworkStore) Acquire(portMappings []cni.PortMapping) (err error) {
68+
func (ns *NetworkStore) Acquire(netConf NetworkConfig) (err error) {
6569
defer func() {
6670
if err != nil {
6771
err = errors.Join(ErrNetworkStore, err)
6872
}
6973
}()
7074

71-
portsJSON, err := json.Marshal(portMappings)
75+
netConfJSON, err := json.Marshal(netConf)
7276
if err != nil {
73-
return fmt.Errorf("failed to marshal port mappings to JSON: %w", err)
77+
return fmt.Errorf("failed to marshal network config to JSON: %w", err)
7478
}
7579

7680
return ns.safeStore.WithLock(func() error {
77-
return ns.safeStore.Set(portsJSON, networkConfigName)
81+
return ns.safeStore.Set(netConfJSON, networkConfigName)
7882
})
7983
}
8084

@@ -99,11 +103,11 @@ func (ns *NetworkStore) Load() (err error) {
99103
return err
100104
}
101105

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)
106+
var netConf NetworkConfig
107+
if err := json.Unmarshal(data, &netConf); err != nil {
108+
return fmt.Errorf("failed to parse network config %v: %w", netConf, err)
105109
}
106-
ns.PortMappings = ports
110+
ns.NetConf = netConf
107111

108112
return err
109113
})

pkg/portutil/portutil.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ func ParseFlagP(s string) ([]cni.PortMapping, error) {
140140
return mr, nil
141141
}
142142

143-
func GeneratePortMappingsConfig(dataStore, namespace, id string, portMappings []cni.PortMapping) error {
143+
func StoreNetworkConfig(dataStore, namespace, id string, netConf networkstore.NetworkConfig) error {
144144
ns, err := networkstore.New(dataStore, namespace, id)
145145
if err != nil {
146146
return err
147147
}
148-
return ns.Acquire(portMappings)
148+
return ns.Acquire(netConf)
149149
}
150150

151151
func LoadPortMappings(dataStore, namespace, id string, containerLabels map[string]string) ([]cni.PortMapping, error) {
@@ -158,8 +158,8 @@ func LoadPortMappings(dataStore, namespace, id string, containerLabels map[strin
158158
if err = ns.Load(); err != nil {
159159
return ports, err
160160
}
161-
if len(ns.PortMappings) != 0 {
162-
return ns.PortMappings, nil
161+
if len(ns.NetConf.PortMappings) != 0 {
162+
return ns.NetConf.PortMappings, nil
163163
}
164164

165165
portsJSON := containerLabels[labels.Ports]

0 commit comments

Comments
 (0)