Skip to content

Commit

Permalink
Merge pull request #2306 from ZYecho/add-logopt-check
Browse files Browse the repository at this point in the history
bugfix: add check for json-file log opt
  • Loading branch information
fuweid authored Oct 15, 2018
2 parents 35ffa0f + dee059d commit abff661
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
20 changes: 20 additions & 0 deletions daemon/logger/jsonfile/jsonfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsonfile

import (
"fmt"
"os"
"sync"

Expand Down Expand Up @@ -58,3 +59,22 @@ func (lf *JSONLogFile) Close() error {
lf.closed = true
return nil
}

var validLogOpt = []string{"max-file", "max-size", "compress", "labels", "env", "env-regex", "tag"}

// ValidateLogOpt validate log options for json-file log driver
func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
isValid := false
for _, opt := range validLogOpt {
if key == opt {
isValid = true
break
}
}
if !isValid {
return fmt.Errorf("unknown log opt '%s' for json-file log driver", key)
}
}
return nil
}
3 changes: 2 additions & 1 deletion daemon/mgr/container_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon/logger/jsonfile"
"github.com/alibaba/pouch/daemon/logger/syslog"
"github.com/alibaba/pouch/pkg/system"

Expand Down Expand Up @@ -202,7 +203,7 @@ func (mgr *ContainerManager) validateLogConfig(c *Container) error {

switch logCfg.LogDriver {
case types.LogConfigLogDriverNone, types.LogConfigLogDriverJSONFile:
return nil
return jsonfile.ValidateLogOpt(logCfg.LogOpts)
case types.LogConfigLogDriverSyslog:
info := mgr.convContainerToLoggerInfo(c)
return syslog.ValidateSyslogOption(info)
Expand Down
22 changes: 22 additions & 0 deletions test/cli_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ func (suite *PouchLogsSuite) TestFollowMode(c *check.C) {
}
}

// TestLogsOpt tests if log options could work.
func (suite *PouchLogsSuite) TestLogsOpt(c *check.C) {
cname := "TestCLILogs_LogsOpt"
command.PouchRun(
"run",
"--log-opt", "env=test",
"--name", cname,
busyboxImage,
).Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, cname)

cnameOfUnsupported := "TestCLILogs_LogsOpt_Unsupported"
result := command.PouchRun(
"run",
"--log-opt", "env1=test",
"--name", cnameOfUnsupported,
busyboxImage,
)
c.Assert(result.Error, check.NotNil)
defer DelContainerForceMultyTime(c, cnameOfUnsupported)
}

func (suite *PouchLogsSuite) syncLogs(c *check.C, cname string, flags ...string) []string {
args := append([]string{"logs"}, flags...)

Expand Down

0 comments on commit abff661

Please sign in to comment.