@@ -46,6 +46,7 @@ import (
46
46
"github.com/containerd/nerdctl/v2/pkg/imgutil"
47
47
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
48
48
"github.com/containerd/nerdctl/v2/pkg/labels"
49
+ "github.com/containerd/nerdctl/v2/pkg/logging"
49
50
"github.com/containerd/nerdctl/v2/pkg/ocihook/state"
50
51
)
51
52
@@ -94,6 +95,11 @@ type ImageMetadata struct {
94
95
LastTagTime time.Time `json:",omitempty"`
95
96
}
96
97
98
+ type LogConfig struct {
99
+ Type string
100
+ Config logging.LogConfig
101
+ }
102
+
97
103
// Container mimics a `docker container inspect` object.
98
104
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L340-L374
99
105
type Container struct {
@@ -116,7 +122,7 @@ type Container struct {
116
122
// TODO: ProcessLabel string
117
123
AppArmorProfile string
118
124
// TODO: ExecIDs []string
119
- // TODO: HostConfig *container. HostConfig
125
+ HostConfig * HostConfig
120
126
// TODO: GraphDriver GraphDriverData
121
127
SizeRw * int64 `json:",omitempty"`
122
128
SizeRootFs * int64 `json:",omitempty"`
@@ -126,6 +132,15 @@ type Container struct {
126
132
NetworkSettings * NetworkSettings
127
133
}
128
134
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
+
129
144
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
130
145
// MountPoint represents a mount point configuration inside the container.
131
146
// This is used for reporting the mountpoints in use by a container.
@@ -282,6 +297,32 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
282
297
c .Mounts = mounts
283
298
}
284
299
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
+
285
326
cs := new (ContainerState )
286
327
cs .Restarting = n .Labels [restart .StatusLabel ] == string (containerd .Running )
287
328
cs .Error = n .Labels [labels .Error ]
@@ -308,6 +349,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
308
349
return nil , err
309
350
}
310
351
c .NetworkSettings = nSettings
352
+ c .HostConfig .PortBindings = * nSettings .Ports
311
353
}
312
354
c .State = cs
313
355
c .Config = & Config {
@@ -491,6 +533,15 @@ func convertToNatPort(portMappings []cni.PortMapping) (*nat.PortMap, error) {
491
533
return & portMap , nil
492
534
}
493
535
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
+
494
545
type IPAMConfig struct {
495
546
Subnet string `json:"Subnet,omitempty"`
496
547
Gateway string `json:"Gateway,omitempty"`
0 commit comments