Skip to content

Commit

Permalink
Merge pull request #1647 from zhuangqh/feature_log_driver_option
Browse files Browse the repository at this point in the history
feature: deamon support --log-driver and --log-opt options
  • Loading branch information
Wei Fu authored Jul 9, 2018
2 parents ebf3817 + 725825b commit 7ec009e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cli/common_flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"github.com/alibaba/pouch/apis/types"

"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -43,7 +45,7 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {
flagSet.StringSliceVarP(&c.labels, "label", "l", nil, "Set labels for a container")

// log driver and log options
flagSet.StringVar(&c.logDriver, "log-driver", "json-file", "Logging driver for the container")
flagSet.StringVar(&c.logDriver, "log-driver", types.LogConfigLogDriverJSONFile, "Logging driver for the container")
flagSet.StringSliceVar(&c.logOpts, "log-opt", nil, "Log driver options")

// memory
Expand Down
29 changes: 24 additions & 5 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,7 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
}

// set default log driver and validate for logger driver
if config.HostConfig.LogConfig == nil {
config.HostConfig.LogConfig = &types.LogConfig{
LogDriver: types.LogConfigLogDriverJSONFile,
}
}
config.HostConfig.LogConfig = mgr.getDefaultLogConfigIfMissing(config.HostConfig.LogConfig)

container := &Container{
State: &types.ContainerState{
Expand Down Expand Up @@ -421,6 +417,29 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
}, nil
}

func (mgr *ContainerManager) getDefaultLogConfigIfMissing(logConfig *types.LogConfig) *types.LogConfig {
defaultLogOpts := make(map[string]string)
for k, v := range mgr.Config.DefaultLogConfig.LogOpts {
defaultLogOpts[k] = v
}

if logConfig == nil {
defaultConfig := mgr.Config.DefaultLogConfig
defaultConfig.LogOpts = defaultLogOpts
return &defaultConfig
}

if logConfig.LogDriver == "" {
logConfig.LogDriver = mgr.Config.DefaultLogConfig.LogDriver
}

if len(logConfig.LogOpts) == 0 {
logConfig.LogOpts = defaultLogOpts
}

return logConfig
}

// Get the detailed information of container.
func (mgr *ContainerManager) Get(ctx context.Context, name string) (*Container, error) {
c, err := mgr.container(name)
Expand Down
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"syscall"
"time"

"github.com/alibaba/pouch/apis/opts"
optscfg "github.com/alibaba/pouch/apis/opts/config"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/lxcfs"
Expand All @@ -29,6 +31,7 @@ import (
var (
sigHandles []func() error
printVersion bool
logOpts []string
)

var cfg = &config.Config{}
Expand Down Expand Up @@ -105,6 +108,10 @@ func setupFlags(cmd *cobra.Command) {
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.IPForward, "ipforward", true, "Enable ipforward")
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.UserlandProxy, "userland-proxy", false, "Enable userland proxy")

// log config
flagSet.StringVar(&cfg.DefaultLogConfig.LogDriver, "log-driver", types.LogConfigLogDriverJSONFile, "Set default log driver")
flagSet.StringSliceVar(&logOpts, "log-opt", nil, "Set default log driver options")

// cgroup-path flag is to set parent cgroup for all containers, default is "default" staying with containerd's configuration.
flagSet.StringVar(&cfg.CgroupParent, "cgroup-parent", "default", "Set parent cgroup for all containers")
flagSet.StringVar(&cfg.PluginPath, "plugin", "", "Set the path where plugin shared library file put")
Expand All @@ -122,6 +129,13 @@ func runDaemon(cmd *cobra.Command) error {
return fmt.Errorf("failed to load daemon file: %s", err)
}

// parse log driver config
logOptMap, err := opts.ParseLogOptions(cfg.DefaultLogConfig.LogDriver, logOpts)
if err != nil {
return err
}
cfg.DefaultLogConfig.LogOpts = logOptMap

//user specifies --version or -v, print version and return.
if printVersion {
fmt.Printf("pouchd version: %s, build: %s, build at: %s\n", version.Version, version.GitCommit, version.BuildTime)
Expand Down

0 comments on commit 7ec009e

Please sign in to comment.