-
Notifications
You must be signed in to change notification settings - Fork 944
feature: init syslog functionality in pouchd #1500
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
Merged
allencloud
merged 7 commits into
AliyunContainerService:master
from
fuweid:feature_syslog
Jun 19, 2018
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fbe9222
refactor: allow container mgr to use custom log driver
39f8a4d
feature: allow cli to use custom log driver and opts
bb7318b
feature: add github.com/RackSec/srslog dependence
9e2bbf4
feature: add syslog driver and add some fields in logger.Info
26f3405
feature: allow user to use syslog log driver
cf0e2e7
feature: limit the logs API which only can be used for jsonfile
5d9dc50
refactor: change for the reviewer
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package opts | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ParseLogOptions parses [key=value] slice-type log options into map. | ||
| func ParseLogOptions(driver string, logOpts []string) (map[string]string, error) { | ||
| opts, err := convertKVStringsToMap(logOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if driver == "none" && len(opts) > 0 { | ||
| return nil, fmt.Errorf("don't allow to set logging opts for driver %s", driver) | ||
| } | ||
| return opts, nil | ||
| } | ||
|
|
||
| // convertKVStringsToMap converts ["key=value"] into {"key":"value"} | ||
| // | ||
| // TODO(fuwei): make it common in the opts.ParseXXX(). | ||
| func convertKVStringsToMap(values []string) (map[string]string, error) { | ||
| kvs := make(map[string]string, len(values)) | ||
|
|
||
| for _, value := range values { | ||
| terms := strings.SplitN(value, "=", 2) | ||
| if len(terms) != 2 { | ||
| return nil, errors.New("the format must be key=value") | ||
| } | ||
| kvs[terms[0]] = terms[1] | ||
| } | ||
| return kvs, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package opts | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseLogOptions(t *testing.T) { | ||
| type tCases struct { | ||
| driver string | ||
| logOpts []string | ||
|
|
||
| hasError bool | ||
| expected map[string]string | ||
| } | ||
|
|
||
| for idx, tc := range []tCases{ | ||
| { | ||
| driver: "", | ||
| logOpts: nil, | ||
| hasError: false, | ||
| expected: map[string]string{}, | ||
| }, { | ||
| driver: "none", | ||
| logOpts: nil, | ||
| hasError: false, | ||
| expected: map[string]string{}, | ||
| }, { | ||
| driver: "none", | ||
| logOpts: []string{"haha"}, | ||
| hasError: true, | ||
| expected: nil, | ||
| }, { | ||
| driver: "none", | ||
| logOpts: []string{"test=1"}, | ||
| hasError: true, | ||
| expected: nil, | ||
| }, { | ||
| driver: "json-file", | ||
| logOpts: []string{"test=1"}, | ||
| hasError: false, | ||
| expected: map[string]string{ | ||
| "test": "1", | ||
| }, | ||
| }, { | ||
| driver: "json-file", | ||
| logOpts: []string{"test=1=1"}, | ||
| hasError: false, | ||
| expected: map[string]string{ | ||
| "test": "1=1", | ||
| }, | ||
| }, { | ||
| driver: "json-file", | ||
| logOpts: []string{"test=1", "flag=oops", "test=2"}, | ||
| hasError: false, | ||
| expected: map[string]string{ | ||
| "test": "2", | ||
| "flag": "oops", | ||
| }, | ||
| }, | ||
| } { | ||
| got, err := ParseLogOptions(tc.driver, tc.logOpts) | ||
| if err == nil && tc.hasError { | ||
| t.Fatalf("[%d case] should have error here, but got nothing", idx) | ||
| } | ||
| if err != nil && !tc.hasError { | ||
| t.Fatalf("[%d case] should have no error here, but got error(%v)", idx, err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(got, tc.expected) { | ||
| t.Fatalf("[%d case] should have (%v), but got (%v)", idx, tc.expected, got) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package containerio | ||
|
|
||
| import ( | ||
| "io" | ||
|
|
||
| "github.com/alibaba/pouch/daemon/logger" | ||
| "github.com/alibaba/pouch/daemon/logger/syslog" | ||
| ) | ||
|
|
||
| func init() { | ||
| Register(func() Backend { | ||
| return &syslogging{} | ||
| }) | ||
| } | ||
|
|
||
| type customWriter struct { | ||
| w func(p []byte) (int, error) | ||
| } | ||
|
|
||
| func (cw *customWriter) Write(p []byte) (int, error) { | ||
| return cw.w(p) | ||
| } | ||
|
|
||
| type syslogging struct { | ||
| w *syslog.Syslog | ||
| } | ||
|
|
||
| func (s *syslogging) Init(opt *Option) error { | ||
| w, err := syslog.NewSyslog(opt.info) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| s.w = w | ||
| return nil | ||
| } | ||
|
|
||
| func (s *syslogging) Name() string { | ||
| return "syslog" | ||
| } | ||
|
|
||
| func (s *syslogging) In() io.Reader { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *syslogging) Out() io.Writer { | ||
| return &customWriter{w: s.sourceWriteFunc("stdout")} | ||
| } | ||
|
|
||
| func (s *syslogging) Err() io.Writer { | ||
| return &customWriter{w: s.sourceWriteFunc("stderr")} | ||
| } | ||
|
|
||
| func (s *syslogging) Close() error { | ||
| return s.w.Close() | ||
| } | ||
|
|
||
| func (s *syslogging) sourceWriteFunc(source string) func([]byte) (int, error) { | ||
| return func(p []byte) (int, error) { | ||
| if len(p) == 0 { | ||
| return 0, nil | ||
| } | ||
|
|
||
| msg := &logger.LogMessage{ | ||
| Source: source, | ||
| Line: p, | ||
| } | ||
|
|
||
| if err := s.w.WriteLogMessage(msg); err != nil { | ||
| return 0, err | ||
| } | ||
| return len(p), nil | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package logger | ||
|
|
||
| import ( | ||
| "github.com/alibaba/pouch/pkg/utils" | ||
| ) | ||
|
|
||
| // Info provides container information for log driver. | ||
| // | ||
| // TODO(fuwei): add more fields. | ||
| type Info struct { | ||
| LogConfig map[string]string | ||
|
|
||
| ContainerID string | ||
| ContainerName string | ||
| ContainerImageID string | ||
| ContainerLabels map[string]string | ||
| ContainerRootDir string | ||
|
|
||
| DaemonName string | ||
| } | ||
|
|
||
| // ID returns the container truncated ID. | ||
| func (i *Info) ID() string { | ||
| return utils.TruncateID(i.ContainerID) | ||
| } | ||
|
|
||
| // FullID returns the container ID. | ||
| func (i *Info) FullID() string { | ||
| return i.ContainerID | ||
| } | ||
|
|
||
| // Name returns the container name. | ||
| func (i *Info) Name() string { | ||
| return i.ContainerName | ||
| } | ||
|
|
||
| // ImageID returns the container's image truncated ID. | ||
| func (i *Info) ImageID() string { | ||
| return utils.TruncateID(i.ContainerImageID) | ||
| } | ||
|
|
||
| // ImageFullID returns the container's image ID. | ||
| func (i *Info) ImageFullID() string { | ||
| return i.ContainerImageID | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package loggerutils | ||
|
|
||
| import ( | ||
| "bytes" | ||
|
|
||
| "github.com/alibaba/pouch/daemon/logger" | ||
| "github.com/alibaba/pouch/pkg/utils/templates" | ||
| ) | ||
|
|
||
| // GenerateLogTag returns a tag which can be used for different log drivers | ||
| // based on the container. | ||
| func GenerateLogTag(info logger.Info, defaultTemplate string) (string, error) { | ||
| tagTemplate := info.LogConfig["tag"] | ||
| if tagTemplate == "" { | ||
| tagTemplate = defaultTemplate | ||
| } | ||
|
|
||
| tmpl, err := templates.NewParse("logtag", tagTemplate) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| buf := bytes.Buffer{} | ||
| if err := tmpl.Execute(&buf, &info); err != nil { | ||
| return "", err | ||
| } | ||
| return buf.String(), nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about encapsulating the function into pkg?
Since I think this is a common function there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make senses.