Skip to content

Commit

Permalink
fix: add loggerLogConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Arjun Raja Yogidas <[email protected]>
  • Loading branch information
coderbirju committed Jan 15, 2025
1 parent 99acf7f commit cfdf044
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
27 changes: 23 additions & 4 deletions pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa

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

internalLabels.cpusetCpus = options.CPUSetCPUs
internalLabels.cpusetMems = options.CPUSetMems
internalLabels.blkioWeight = options.BlkioWeight

// TODO: abolish internal labels and only use annotations
ilOpt, err := withInternalLabels(internalLabels)
if err != nil {
Expand Down Expand Up @@ -617,10 +621,13 @@ func withStop(stopSignal string, stopTimeout int, ensuredImage *imgutil.EnsuredI

type internalLabels struct {
// labels from cmd options
namespace string
platform string
extraHosts []string
pidFile string
namespace string
platform string
extraHosts []string
pidFile string
blkioWeight uint16
cpusetCpus string
cpusetMems string
// labels from cmd options or automatically set
name string
hostname string
Expand Down Expand Up @@ -730,6 +737,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
m[labels.ContainerAutoRemove] = internalLabels.rm
}

if internalLabels.blkioWeight > 0 {
m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight)
}

if internalLabels.cpusetMems != "" {
m[labels.CPUSetMems] = internalLabels.cpusetMems
}

if internalLabels.cpusetCpus != "" {
m[labels.CPUSetCPUs] = internalLabels.cpusetCpus
}

return containerd.WithAdditionalContainerLabels(m), nil
}

Expand Down
36 changes: 30 additions & 6 deletions pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"github.com/containerd/nerdctl/v2/pkg/imgutil"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/logging"
"github.com/containerd/nerdctl/v2/pkg/ocihook/state"
)

Expand Down Expand Up @@ -97,7 +96,14 @@ type ImageMetadata struct {

type LogConfig struct {
Type string
Config logging.LogConfig
Config loggerLogConfig
}

type loggerLogConfig struct {
Driver string `json:"driver"`
Opts map[string]string `json:"opts,omitempty"`
LogURI string `json:"-"`
Address string `json:"address"`
}

// Container mimics a `docker container inspect` object.
Expand Down Expand Up @@ -138,7 +144,9 @@ type HostConfig struct {
ExtraHosts []string // List of extra hosts
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
LogConfig LogConfig // Configuration of the logs for this container

BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
CpusetMems string // CpusetMems 0-2, 0,1
CpusetCpus string // CpusetCpus 0-2, 0,1
}

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

if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" {
c.HostConfig.LogConfig.Type = nerdctlLoguri
// c.HostConfig.LogConfig.Config = map[string]string{}
}
if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok {
var logConfig logging.LogConfig
var logConfig loggerLogConfig
err := json.Unmarshal([]byte(logConfigJSON), &logConfig)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal log config: %v", err)
Expand All @@ -317,12 +324,29 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
c.HostConfig.LogConfig.Config = logConfig
} else {
// If LogConfig label is not present, set default values
c.HostConfig.LogConfig.Config = logging.LogConfig{
c.HostConfig.LogConfig.Config = loggerLogConfig{
Driver: "json-file",
Opts: make(map[string]string),
}
}

if blkioWeightSet := n.Labels[labels.BlkioWeight]; blkioWeightSet != "" {
var blkioWeight uint16
_, err := fmt.Sscanf(blkioWeightSet, "%d", &blkioWeight)
if err != nil {
return nil, fmt.Errorf("failed to convert string to uint: %v", err)
}
c.HostConfig.BlkioWeight = blkioWeight
}

if cpusetmems := n.Labels[labels.CPUSetMems]; cpusetmems != "" {
c.HostConfig.CpusetMems = cpusetmems
}

if cpusetcpus := n.Labels[labels.CPUSetCPUs]; cpusetcpus != "" {
c.HostConfig.CpusetCpus = cpusetcpus
}

cs := new(ContainerState)
cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running)
cs.Error = n.Labels[labels.Error]
Expand Down
11 changes: 10 additions & 1 deletion pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ const (
// (like "nerdctl/default-network=true" or "nerdctl/default-network=false")
NerdctlDefaultNetwork = Prefix + "default-network"

// LogConfig defines the loggin configuration passed to the container
// LogConfig defines the logging configuration passed to the container
LogConfig = Prefix + "log-config"

// ContainerAutoRemove is to check whether the --rm option is specified.
ContainerAutoRemove = Prefix + "auto-remove"

// BlkioWeight to check if the --blkio-weight is specified
BlkioWeight = Prefix + "blkio-weight"

// CPUSetCPUs to check if the --cpuset-cpus is specified
CPUSetCPUs = Prefix + "cpuset-cpus"

// CPUSetMems to check if the --cpuset-mems is specified
CPUSetMems = Prefix + "cpuset-mems"
)

0 comments on commit cfdf044

Please sign in to comment.