Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: move functions from mgr to container #2746

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 5 additions & 61 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ func (mgr *ContainerManager) createContainerdContainer(ctx context.Context, c *C
}

// set container's LogPath
mgr.SetContainerLogPath(c)
c.SetLogPath(mgr.Config.HomeDir)

runtime, err := mgr.getRuntime(c.HostConfig.Runtime)
if err != nil {
Expand Down Expand Up @@ -1150,7 +1150,7 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
c.Unlock()

// update Resources of a container.
if err := mgr.updateContainerResources(c, config.Resources); err != nil {
if err := c.updateResources(config.Resources); err != nil {
restore = true
return errors.Wrapf(err, "failed to update resource of container %s", c.ID)
}
Expand Down Expand Up @@ -1316,63 +1316,6 @@ func (mgr *ContainerManager) updateContainerDiskQuota(ctx context.Context, c *Co
return nil
}

// updateContainerResources update container's resources parameters.
func (mgr *ContainerManager) updateContainerResources(c *Container, resources types.Resources) error {
c.Lock()
defer c.Unlock()
// update resources of container.
cResources := &c.HostConfig.Resources
if resources.BlkioWeight != 0 {
cResources.BlkioWeight = resources.BlkioWeight
}
if len(resources.BlkioDeviceReadBps) != 0 {
cResources.BlkioDeviceReadBps = resources.BlkioDeviceReadBps
}
if len(resources.BlkioDeviceReadIOps) != 0 {
cResources.BlkioDeviceReadIOps = resources.BlkioDeviceReadIOps
}
if len(resources.BlkioDeviceWriteBps) != 0 {
cResources.BlkioDeviceWriteBps = resources.BlkioDeviceWriteBps
}
if len(resources.BlkioDeviceWriteIOps) != 0 {
cResources.BlkioDeviceWriteIOps = resources.BlkioDeviceWriteIOps
}
if resources.CPUPeriod != 0 {
cResources.CPUPeriod = resources.CPUPeriod
}
if resources.CPUQuota == -1 || resources.CPUQuota >= 1000 {
cResources.CPUQuota = resources.CPUQuota
}
if resources.CPUShares != 0 {
cResources.CPUShares = resources.CPUShares
}
if resources.CpusetCpus != "" {
cResources.CpusetCpus = resources.CpusetCpus
}
if resources.CpusetMems != "" {
cResources.CpusetMems = resources.CpusetMems
}
if resources.Memory != 0 {
// if memory limit smaller than already set memoryswap limit and doesn't
// update the memoryswap limit, then error out.
if cResources.MemorySwap != 0 && resources.Memory > cResources.MemorySwap && resources.MemorySwap == 0 {
return fmt.Errorf("Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time")
}
cResources.Memory = resources.Memory
}
if resources.MemorySwap != 0 {
cResources.MemorySwap = resources.MemorySwap
}
if resources.MemoryReservation != 0 {
cResources.MemoryReservation = resources.MemoryReservation
}
if resources.KernelMemory != 0 {
cResources.KernelMemory = resources.KernelMemory
}

return nil
}

// Top lists the processes running inside of the given container
func (mgr *ContainerManager) Top(ctx context.Context, name string, psArgs string) (*types.ContainerProcessList, error) {
if psArgs == "" {
Expand Down Expand Up @@ -1647,8 +1590,9 @@ func (mgr *ContainerManager) initLogDriverBeforeStart(c *Container) error {
}
}

logInfo := mgr.convContainerToLoggerInfo(c)
logDriver, err := logOptionsForContainerio(c, logInfo)
containerRootDir := mgr.Store.Path(c.ID)
logInfo := c.convToLoggerInfo(containerRootDir)
logDriver, err := c.logOptionsForContainerio(logInfo)
if err != nil {
return err
}
Expand Down
65 changes: 0 additions & 65 deletions daemon/mgr/container_logger.go

This file was deleted.

114 changes: 114 additions & 0 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/cri/stream/remotecommand"
"github.com/alibaba/pouch/ctrd"
"github.com/alibaba/pouch/daemon/logger"
"github.com/alibaba/pouch/daemon/logger/jsonfile"
"github.com/alibaba/pouch/daemon/logger/syslog"
"github.com/alibaba/pouch/pkg/meta"
"github.com/alibaba/pouch/pkg/utils"

Expand Down Expand Up @@ -529,6 +532,117 @@ func (c *Container) CleanRootfsSnapshotDirs() error {
return nil
}

// updateResources update container's resources parameters.
func (c *Container) updateResources(resources types.Resources) error {
c.Lock()
defer c.Unlock()
// update resources of container.
cResources := &c.HostConfig.Resources
if resources.BlkioWeight != 0 {
cResources.BlkioWeight = resources.BlkioWeight
}
if len(resources.BlkioDeviceReadBps) != 0 {
cResources.BlkioDeviceReadBps = resources.BlkioDeviceReadBps
}
if len(resources.BlkioDeviceReadIOps) != 0 {
cResources.BlkioDeviceReadIOps = resources.BlkioDeviceReadIOps
}
if len(resources.BlkioDeviceWriteBps) != 0 {
cResources.BlkioDeviceWriteBps = resources.BlkioDeviceWriteBps
}
if len(resources.BlkioDeviceWriteIOps) != 0 {
cResources.BlkioDeviceWriteIOps = resources.BlkioDeviceWriteIOps
}
if resources.CPUPeriod != 0 {
cResources.CPUPeriod = resources.CPUPeriod
}
if resources.CPUQuota == -1 || resources.CPUQuota >= 1000 {
cResources.CPUQuota = resources.CPUQuota
}
if resources.CPUShares != 0 {
cResources.CPUShares = resources.CPUShares
}
if resources.CpusetCpus != "" {
cResources.CpusetCpus = resources.CpusetCpus
}
if resources.CpusetMems != "" {
cResources.CpusetMems = resources.CpusetMems
}
if resources.Memory != 0 {
// if memory limit smaller than already set memoryswap limit and doesn't
// update the memoryswap limit, then error out.
if cResources.MemorySwap != 0 && resources.Memory > cResources.MemorySwap && resources.MemorySwap == 0 {
return fmt.Errorf("Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time")
}
cResources.Memory = resources.Memory
}
if resources.MemorySwap != 0 {
cResources.MemorySwap = resources.MemorySwap
}
if resources.MemoryReservation != 0 {
cResources.MemoryReservation = resources.MemoryReservation
}
if resources.KernelMemory != 0 {
cResources.KernelMemory = resources.KernelMemory
}

return nil
}

func (c *Container) logOptionsForContainerio(info logger.Info) (logger.LogDriver, error) {
cfg := c.HostConfig.LogConfig
if cfg == nil || cfg.LogDriver == types.LogConfigLogDriverNone {
return nil, nil
}

switch cfg.LogDriver {
case types.LogConfigLogDriverJSONFile:
return jsonfile.Init(info)
case types.LogConfigLogDriverSyslog:
return syslog.Init(info)
default:
logrus.Warnf("not support (%v) log driver yet", cfg.LogDriver)
return nil, nil
}
}

// convToLoggerInfo uses logger.Info to wrap container information.
func (c *Container) convToLoggerInfo(containerRootDir string) logger.Info {
logCfg := make(map[string]string)
if cfg := c.HostConfig.LogConfig; cfg != nil && cfg.LogDriver != types.LogConfigLogDriverNone {
logCfg = cfg.LogOpts
}

// TODO(fuwei):
// 1. add more fields into logger.Info
// 2. separate the logic about retrieving container root dir from mgr.
return logger.Info{
LogConfig: logCfg,
ContainerID: c.ID,
ContainerName: c.Name,
ContainerImageID: c.Image,
ContainerLabels: c.Config.Labels,
ContainerEnvs: c.Config.Env,
ContainerRootDir: containerRootDir,
DaemonName: "pouchd",
}
}

// SetLogPath sets the log path of container.
// LogPath would be as a field in `Inspect` response.
func (c *Container) SetLogPath(homeDir string) {
if c.HostConfig.LogConfig == nil {
return
}

// If the logdriver is json-file, the LogPath should be like
// /var/lib/pouch/containers/5804ee42e505a5d9f30128848293fcb72d8cbc7517310bd24895e82a618fa454/json.log
if c.HostConfig.LogConfig.LogDriver == "json-file" {
c.LogPath = filepath.Join(homeDir, "containers", c.ID, "json.log")
}
return
}

// ContainerRestartPolicy represents the policy is used to manage container.
type ContainerRestartPolicy types.RestartPolicy

Expand Down
3 changes: 2 additions & 1 deletion daemon/mgr/container_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ func (mgr *ContainerManager) validateLogConfig(c *Container) error {
case types.LogConfigLogDriverNone, types.LogConfigLogDriverJSONFile:
return jsonfile.ValidateLogOpt(restOpts)
case types.LogConfigLogDriverSyslog:
info := mgr.convContainerToLoggerInfo(c)
containerRootDir := mgr.Store.Path(c.ID)
info := c.convToLoggerInfo(containerRootDir)
return syslog.ValidateSyslogOption(info)
default:
return fmt.Errorf("not support (%v) log driver yet", logCfg.LogDriver)
Expand Down