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

feature: init syslog functionality in pouchd #1500

Merged
merged 7 commits into from
Jun 19, 2018
Merged

feature: init syslog functionality in pouchd #1500

merged 7 commits into from
Jun 19, 2018

Conversation

fuweid
Copy link
Contributor

@fuweid fuweid commented Jun 9, 2018

Ⅰ. Describe what this PR did

Add syslog driver into PouchContainer so that user can use more log driver, not just jsonfile.

Ⅱ. Does this pull request fix one issue?

NONE

Ⅲ. Describe how you did it

Before add this feature, we have to do the following things:

  1. allow user to specify the log-driver by CLI
  2. add validation for the log options during create container

NOTE: In order to keep change scope, we can add validation into update/upgrade in the following PR if necessary.

After we do the above things, we can handle syslog like jsonfile. With syslog driver, user can write the log data in local syslog or send it to the remote syslog center.

In this PR, I introduce the third-party package srslog, which can handle the local and remote cases.

Ⅳ. Describe how to verify it

The user can use the following options to setup the syslog driver:

  • syslog-facility [priority]
  • syslog-address [for remote syslog center]
  • syslog-format [log format]
  • tag [allow user to log more container context]

only driver

➜  pouch git:(feature_syslog) pouch run --log-driver syslog busybox echo hello
hello

➜  tail -f /var/log/syslog
Jun 10 18:07:49 ubuntu-xenial kernel: [54778.963768] eth0: renamed from veth8ea9817
Jun 10 18:07:49 ubuntu-xenial kernel: [54778.974287] IPv6: ADDRCONF(NETDEV_CHANGE): veth571595e: link becomes ready
Jun 10 18:07:49 ubuntu-xenial kernel: [54778.974358] p0: port 3(veth571595e) entered forwarding state
Jun 10 18:07:49 ubuntu-xenial kernel: [54778.974368] p0: port 3(veth571595e) entered forwarding state
>>>>>> Jun 10 18:07:49 ubuntu-xenial 4d861e786eeb[14458]: hello
Jun 10 18:07:49 ubuntu-xenial kernel: [54779.175524] p0: port 3(veth571595e) entered disabled state
Jun 10 18:07:49 ubuntu-xenial kernel: [54779.175576] veth8ea9817: renamed from eth0
Jun 10 18:07:49 ubuntu-xenial kernel: [54779.218755] p0: port 3(veth571595e) entered disabled state
Jun 10 18:07:49 ubuntu-xenial kernel: [54779.221528] device veth571595e left promiscuous mode
Jun 10 18:07:49 ubuntu-xenial kernel: [54779.221532] p0: port 3(veth571595e) entered disabled state

driver with tag

➜  pouch git:(feature_syslog) pouch run --log-driver syslog \
--log-opt tag="id={{.ID}} - image-id={{.ImageID}} - {{.DaemonName}}" \
busybox echo hello
hello

➜  tail -f /var/log/syslog
....
Jun 10 18:09:21 ubuntu-xenial id=7cd48c5829da - image-id=8c811b4aec35 - pouchd[14458]: hello
....

driver with tag and different format

➜  pouch git:(feature_syslog) pouch run --log-driver syslog \
--log-opt tag="id={{.ID}} - image-id={{.ImageID}} - {{.DaemonName}}" \
--log-opt syslog-format=rfc5424micro \
busybox echo hello
hello

➜  tail -f /var/log/syslog
....
Jun 10 18:13:07 ubuntu-xenial 1 2018-06-10T18:13:07.220202+08:00 ubuntu-xenial id=9933ce8a4c83 - image-id=8c811b4aec35 - pouchd 14458 id=9933ce8a4c83 - image-id=8c811b4aec35 - pouchd - hello

Ⅴ. Special notes for reviews

I submitted several commits so that the reviewer can check commit one by one.
For example, first commit is to show the refactor small changed part. It can avoid to use big change to flood the reviewer. :)

After LGTM, we can use squash and change commit message before merge.

Known issues:

  1. the srslog should provide the timeout option. Just in case that the remote log center set up and the client will hang.
  2. for now, the containerio doesn't return error during initing the backend. I will send following PR to fix this.

@codecov-io
Copy link

codecov-io commented Jun 9, 2018

Codecov Report

Merging #1500 into master will decrease coverage by 2.29%.
The diff coverage is 43.97%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #1500     +/-   ##
=========================================
- Coverage   40.55%   38.25%   -2.3%     
=========================================
  Files         253      262      +9     
  Lines       16481    17874   +1393     
=========================================
+ Hits         6684     6838    +154     
- Misses       8941    10168   +1227     
- Partials      856      868     +12
Impacted Files Coverage Δ
cli/common_flags.go 0% <0%> (ø) ⬆️
daemon/logger/info.go 0% <0%> (ø)
cli/container.go 0% <0%> (ø) ⬆️
daemon/logger/syslog/fmt.go 0% <0%> (ø)
daemon/mgr/container_logs.go 84.21% <100%> (+9.56%) ⬆️
apis/opts/log_options.go 100% <100%> (ø)
daemon/containerio/syslog.go 18.75% <18.75%> (ø)
daemon/logger/syslog/syslog.go 25% <25%> (ø)
daemon/containerio/options.go 50.79% <28.57%> (-4.38%) ⬇️
daemon/mgr/container.go 34.71% <40.74%> (-14.58%) ⬇️
... and 31 more

1. use logger.Info to contain container's information
2. add log driver validation in daemon
3. make the openContainerIO/attachContainerIO friendly

Signed-off-by: Wei Fu <[email protected]>
@pouchrobot pouchrobot added size/XL and removed size/L labels Jun 9, 2018
Wei Fu added 3 commits June 10, 2018 16:40
1. add option in containerio
2. add syslog config validation in container mgr
3. add syslog option during init containerio

Signed-off-by: Wei Fu <[email protected]>
@fuweid fuweid changed the title [WIP] feature: init syslog functionality in pouchd feature: init syslog functionality in pouchd Jun 10, 2018
@fuweid fuweid requested review from allencloud and YaoZengzeng June 10, 2018 10:18
// convertKVStringsToMap converts ["key=value"] into {"key":"value"}
//
// TODO(fuwei): make it common in the opts.ParseXXX().
func convertKVStringsToMap(values []string) (map[string]string, error) {
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make senses.

// ErrInvalidSyslogFormat represents the invalid format.
ErrInvalidSyslogFormat = errors.New("invalid syslog format")

// ErrFailedToLoadX509KeyPair is to used to indicate that it's failed to load x590 key pair.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load x509 key pair? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha find a typo! good catch.

"github.com/sirupsen/logrus"
)

func optionsForContainerio(c *Container) []func(*containerio.Option) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only used for log options and in the file container_logger.go

Maybe the name logOptionsForContainerio is more appropriate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make senses.


// Close closes the Syslog.
func (s *Syslog) Close() error {
return s.writer.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK to close Syslog multiple times?

Copy link
Contributor Author

@fuweid fuweid Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, I don't think we should care the multiple closes. Even if the srslog supports the case.

We should close the syslog once. If we closes it multiple times, we should reconsider that the logic is ok. And that we will introduce sync.Lock things if we support the case. Is it worth? 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I totally agree that the backend should be closed only once.

But now pouchd will include all the backends in both Stdout and Stderr. when being closed, both of them will close all the backends. And this is unreasonable.

So my PR #1388 fixes this problem. The close of Stdout and Stderr only close the corresponding ring buffer. Only when the IO is closed, all the backends will be closed 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YaoZengzeng we need to drain the ringbuffer, right? When we close the IO, we need to wait the data has been drained. 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is exactly what #1388 do. 😄

1. s/optionsForContainerio/logOptionsForContainerio/g
2. make the convertKVStringsToMap into pkg/utils
3. fix typo

Signed-off-by: Wei Fu <[email protected]>
@YaoZengzeng
Copy link
Contributor

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants