Skip to content

Commit cfdf044

Browse files
committed
fix: add loggerLogConfig
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 99acf7f commit cfdf044

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

pkg/cmd/container/create.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
326326

327327
internalLabels.rm = containerutil.EncodeContainerRmOptLabel(options.Rm)
328328

329+
internalLabels.cpusetCpus = options.CPUSetCPUs
330+
internalLabels.cpusetMems = options.CPUSetMems
331+
internalLabels.blkioWeight = options.BlkioWeight
332+
329333
// TODO: abolish internal labels and only use annotations
330334
ilOpt, err := withInternalLabels(internalLabels)
331335
if err != nil {
@@ -617,10 +621,13 @@ func withStop(stopSignal string, stopTimeout int, ensuredImage *imgutil.EnsuredI
617621

618622
type internalLabels struct {
619623
// labels from cmd options
620-
namespace string
621-
platform string
622-
extraHosts []string
623-
pidFile string
624+
namespace string
625+
platform string
626+
extraHosts []string
627+
pidFile string
628+
blkioWeight uint16
629+
cpusetCpus string
630+
cpusetMems string
624631
// labels from cmd options or automatically set
625632
name string
626633
hostname string
@@ -730,6 +737,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
730737
m[labels.ContainerAutoRemove] = internalLabels.rm
731738
}
732739

740+
if internalLabels.blkioWeight > 0 {
741+
m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight)
742+
}
743+
744+
if internalLabels.cpusetMems != "" {
745+
m[labels.CPUSetMems] = internalLabels.cpusetMems
746+
}
747+
748+
if internalLabels.cpusetCpus != "" {
749+
m[labels.CPUSetCPUs] = internalLabels.cpusetCpus
750+
}
751+
733752
return containerd.WithAdditionalContainerLabels(m), nil
734753
}
735754

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ 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"
5049
"github.com/containerd/nerdctl/v2/pkg/ocihook/state"
5150
)
5251

@@ -97,7 +96,14 @@ type ImageMetadata struct {
9796

9897
type LogConfig struct {
9998
Type string
100-
Config logging.LogConfig
99+
Config loggerLogConfig
100+
}
101+
102+
type loggerLogConfig struct {
103+
Driver string `json:"driver"`
104+
Opts map[string]string `json:"opts,omitempty"`
105+
LogURI string `json:"-"`
106+
Address string `json:"address"`
101107
}
102108

103109
// Container mimics a `docker container inspect` object.
@@ -138,7 +144,9 @@ type HostConfig struct {
138144
ExtraHosts []string // List of extra hosts
139145
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
140146
LogConfig LogConfig // Configuration of the logs for this container
141-
147+
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
148+
CpusetMems string // CpusetMems 0-2, 0,1
149+
CpusetCpus string // CpusetCpus 0-2, 0,1
142150
}
143151

144152
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
@@ -304,10 +312,9 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
304312

305313
if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" {
306314
c.HostConfig.LogConfig.Type = nerdctlLoguri
307-
// c.HostConfig.LogConfig.Config = map[string]string{}
308315
}
309316
if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok {
310-
var logConfig logging.LogConfig
317+
var logConfig loggerLogConfig
311318
err := json.Unmarshal([]byte(logConfigJSON), &logConfig)
312319
if err != nil {
313320
return nil, fmt.Errorf("failed to unmarshal log config: %v", err)
@@ -317,12 +324,29 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
317324
c.HostConfig.LogConfig.Config = logConfig
318325
} else {
319326
// If LogConfig label is not present, set default values
320-
c.HostConfig.LogConfig.Config = logging.LogConfig{
327+
c.HostConfig.LogConfig.Config = loggerLogConfig{
321328
Driver: "json-file",
322329
Opts: make(map[string]string),
323330
}
324331
}
325332

333+
if blkioWeightSet := n.Labels[labels.BlkioWeight]; blkioWeightSet != "" {
334+
var blkioWeight uint16
335+
_, err := fmt.Sscanf(blkioWeightSet, "%d", &blkioWeight)
336+
if err != nil {
337+
return nil, fmt.Errorf("failed to convert string to uint: %v", err)
338+
}
339+
c.HostConfig.BlkioWeight = blkioWeight
340+
}
341+
342+
if cpusetmems := n.Labels[labels.CPUSetMems]; cpusetmems != "" {
343+
c.HostConfig.CpusetMems = cpusetmems
344+
}
345+
346+
if cpusetcpus := n.Labels[labels.CPUSetCPUs]; cpusetcpus != "" {
347+
c.HostConfig.CpusetCpus = cpusetcpus
348+
}
349+
326350
cs := new(ContainerState)
327351
cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running)
328352
cs.Error = n.Labels[labels.Error]

pkg/labels/labels.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,18 @@ 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
104+
// LogConfig defines the logging configuration passed to the container
105105
LogConfig = Prefix + "log-config"
106106

107107
// ContainerAutoRemove is to check whether the --rm option is specified.
108108
ContainerAutoRemove = Prefix + "auto-remove"
109+
110+
// BlkioWeight to check if the --blkio-weight is specified
111+
BlkioWeight = Prefix + "blkio-weight"
112+
113+
// CPUSetCPUs to check if the --cpuset-cpus is specified
114+
CPUSetCPUs = Prefix + "cpuset-cpus"
115+
116+
// CPUSetMems to check if the --cpuset-mems is specified
117+
CPUSetMems = Prefix + "cpuset-mems"
109118
)

0 commit comments

Comments
 (0)