Skip to content

Commit 99acf7f

Browse files
committed
feat: add hostConfig to nerdctl inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent b854127 commit 99acf7f

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

pkg/cmd/container/create.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
222222
return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err
223223
}
224224
internalLabels.logURI = logConfig.LogURI
225+
internalLabels.logConfig = logConfig
225226

226227
restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun)
227228
if err != nil {
@@ -641,7 +642,8 @@ type internalLabels struct {
641642
// log
642643
logURI string
643644
// a label to check whether the --rm option is specified.
644-
rm string
645+
rm string
646+
logConfig logging.LogConfig
645647
}
646648

647649
// WithInternalLabels sets the internal labels for a container.
@@ -672,6 +674,11 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
672674
}
673675
if internalLabels.logURI != "" {
674676
m[labels.LogURI] = internalLabels.logURI
677+
logConfigJSON, err := json.Marshal(internalLabels.logConfig)
678+
if err != nil {
679+
return nil, err
680+
}
681+
m[labels.LogConfig] = string(logConfigJSON)
675682
}
676683
if len(internalLabels.anonVolumes) > 0 {
677684
anonVolumeJSON, err := json.Marshal(internalLabels.anonVolumes)

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/containerd/nerdctl/v2/pkg/imgutil"
4747
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
4848
"github.com/containerd/nerdctl/v2/pkg/labels"
49+
"github.com/containerd/nerdctl/v2/pkg/logging"
4950
"github.com/containerd/nerdctl/v2/pkg/ocihook/state"
5051
)
5152

@@ -94,6 +95,11 @@ type ImageMetadata struct {
9495
LastTagTime time.Time `json:",omitempty"`
9596
}
9697

98+
type LogConfig struct {
99+
Type string
100+
Config logging.LogConfig
101+
}
102+
97103
// Container mimics a `docker container inspect` object.
98104
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L340-L374
99105
type Container struct {
@@ -116,7 +122,7 @@ type Container struct {
116122
// TODO: ProcessLabel string
117123
AppArmorProfile string
118124
// TODO: ExecIDs []string
119-
// TODO: HostConfig *container.HostConfig
125+
HostConfig *HostConfig
120126
// TODO: GraphDriver GraphDriverData
121127
SizeRw *int64 `json:",omitempty"`
122128
SizeRootFs *int64 `json:",omitempty"`
@@ -126,6 +132,15 @@ type Container struct {
126132
NetworkSettings *NetworkSettings
127133
}
128134

135+
// From https://github.com/moby/moby/blob/8dbd90ec00daa26dc45d7da2431c965dec99e8b4/api/types/container/host_config.go#L391
136+
// HostConfig the non-portable Config structure of a container.
137+
type HostConfig struct {
138+
ExtraHosts []string // List of extra hosts
139+
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
140+
LogConfig LogConfig // Configuration of the logs for this container
141+
142+
}
143+
129144
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
130145
// MountPoint represents a mount point configuration inside the container.
131146
// This is used for reporting the mountpoints in use by a container.
@@ -282,6 +297,32 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
282297
c.Mounts = mounts
283298
}
284299

300+
c.HostConfig = new(HostConfig)
301+
if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" {
302+
c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts)
303+
}
304+
305+
if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" {
306+
c.HostConfig.LogConfig.Type = nerdctlLoguri
307+
// c.HostConfig.LogConfig.Config = map[string]string{}
308+
}
309+
if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok {
310+
var logConfig logging.LogConfig
311+
err := json.Unmarshal([]byte(logConfigJSON), &logConfig)
312+
if err != nil {
313+
return nil, fmt.Errorf("failed to unmarshal log config: %v", err)
314+
}
315+
316+
// Assign the parsed LogConfig to c.HostConfig.LogConfig
317+
c.HostConfig.LogConfig.Config = logConfig
318+
} else {
319+
// If LogConfig label is not present, set default values
320+
c.HostConfig.LogConfig.Config = logging.LogConfig{
321+
Driver: "json-file",
322+
Opts: make(map[string]string),
323+
}
324+
}
325+
285326
cs := new(ContainerState)
286327
cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running)
287328
cs.Error = n.Labels[labels.Error]
@@ -308,6 +349,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
308349
return nil, err
309350
}
310351
c.NetworkSettings = nSettings
352+
c.HostConfig.PortBindings = *nSettings.Ports
311353
}
312354
c.State = cs
313355
c.Config = &Config{
@@ -491,6 +533,15 @@ func convertToNatPort(portMappings []cni.PortMapping) (*nat.PortMap, error) {
491533
return &portMap, nil
492534
}
493535

536+
func parseExtraHosts(extraHostsJSON string) []string {
537+
var extraHosts []string
538+
if err := json.Unmarshal([]byte(extraHostsJSON), &extraHosts); err != nil {
539+
// Handle error or return empty slice
540+
return []string{}
541+
}
542+
return extraHosts
543+
}
544+
494545
type IPAMConfig struct {
495546
Subnet string `json:"Subnet,omitempty"`
496547
Gateway string `json:"Gateway,omitempty"`

pkg/labels/labels.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ const (
101101
// (like "nerdctl/default-network=true" or "nerdctl/default-network=false")
102102
NerdctlDefaultNetwork = Prefix + "default-network"
103103

104+
// LogConfig defines the loggin configuration passed to the container
105+
LogConfig = Prefix + "log-config"
106+
104107
// ContainerAutoRemove is to check whether the --rm option is specified.
105108
ContainerAutoRemove = Prefix + "auto-remove"
106109
)

0 commit comments

Comments
 (0)