@@ -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
99105type 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+
494545type IPAMConfig struct {
495546 Subnet string `json:"Subnet,omitempty"`
496547 Gateway string `json:"Gateway,omitempty"`
0 commit comments