Commit Graph

21 Commits

Author SHA1 Message Date
Kazuyoshi Kato
bf26140d94 Fix LogURIGenerator on Windows
Checking / is not the right way to distinguish an absolute path in
Windows.

Fixes #5786.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
2022-09-19 10:00:18 -07:00
Sebastiaan van Stijn
6a2d3990d1
cio: FIFOSet.Close() check if FIFOSet is nill to prevent NPE
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-02-01 11:17:51 +01:00
Akshat Kumar
7a9fbec5fb Add logging binary support when terminal is true
Currently the shims only support starting the logging binary process if the
io.Creator Config does not specify Terminal: true. This means that the program
using containerd will only be able to specify FIFO io when Terminal: true,
rather than allowing the shim to fork the logging binary process. Hence,
containerd consumers face an inconsistent behavior regarding logging binary
management depending on the Terminal option.

Allowing the shim to fork the logging binary process will introduce consistency
between the running container and the logging process. Otherwise, the logging
process may die if its parent process dies whereas the container will keep
running, resulting in the loss of container logs.

Signed-off-by: Akshat Kumar <kshtku@amazon.com>
2020-08-25 17:28:29 -07:00
Wei Fu
d656fa38ca restart plugin: support binary log uri
Introduce LogURIGenerator helper function in cio package. It is used in
the restart options, like WithBinaryLogURI and WithFileLogURI.

And restart.LogPathLabel might be used in production and work well. In
order to reduce breaking change, the LogPathLabel is still recognized if
new LogURILabel is not set. In next release 1.5, the LogPathLabel will
be removed.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
2020-06-10 00:09:24 +08:00
Maksym Pavlenko
fbf96d302a Fix path in LogFile creator
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
2019-06-19 16:53:33 -07:00
Maksym Pavlenko
5e0d793801 Fix bugs in BinaryIO creator
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
2019-06-19 11:15:17 -07:00
Michael Crosby
e6ae9cc64f Shim pluggable logging
Closes #603

This adds logging facilities at the shim level to provide minimal I/O
overhead and pluggable logging options.  Log handling is done within the
shim so that all I/O, cpu, and memory can be charged to the container.

A sample logging driver setting up logging for a container the systemd
journal looks like this:

```go
package main

import (
	"bufio"
	"context"
	"fmt"
	"io"
	"sync"

	"github.com/containerd/containerd/runtime/v2/logging"
	"github.com/coreos/go-systemd/journal"
)

func main() {
	logging.Run(log)
}

func log(ctx context.Context, config *logging.Config, ready func() error) error {
	// construct any log metadata for the container
	vars := map[string]string{
		"SYSLOG_IDENTIFIER": fmt.Sprintf("%s:%s", config.Namespace, config.ID),
	}
	var wg sync.WaitGroup
	wg.Add(2)
	// forward both stdout and stderr to the journal
	go copy(&wg, config.Stdout, journal.PriInfo, vars)
	go copy(&wg, config.Stderr, journal.PriErr, vars)

	// signal that we are ready and setup for the container to be started
	if err := ready(); err != nil {
		return err
	}
	wg.Wait()
	return nil
}

func copy(wg *sync.WaitGroup, r io.Reader, pri journal.Priority, vars map[string]string) {
	defer wg.Done()
	s := bufio.NewScanner(r)
	for s.Scan() {
		if s.Err() != nil {
			return
		}
		journal.Send(s.Text(), pri, vars)
	}
}
```

A `logging` package has been created to assist log developers create
logging plugins for containerd.

This uses a URI based approach for logging drivers that can be expanded
in the future.

Supported URI scheme's are:

* binary
* fifo
* file

You can pass the log url via ctr on the command line:

```bash
> ctr run --rm --runtime io.containerd.runc.v2 --log-uri binary://shim-journald docker.io/library/redis:alpine redis
```

```bash
> journalctl -f -t default:redis

-- Logs begin at Tue 2018-12-11 16:29:51 EST. --
Mar 08 16:08:22 deathstar default:redis[120760]: 1:C 08 Mar 2019 21:08:22.703 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.704 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.704 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.704 # Current maximum open files is 1024. maxclients has been reduced to 992 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.705 * Running mode=standalone, port=6379.
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.705 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.705 # Server initialized
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.705 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.705 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
Mar 08 16:08:22 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:22.705 * Ready to accept connections
Mar 08 16:08:50 deathstar default:redis[120760]: 1:signal-handler (1552079330) Received SIGINT scheduling shutdown...
Mar 08 16:08:50 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:50.405 # User requested shutdown...
Mar 08 16:08:50 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:50.406 * Saving the final RDB snapshot before exiting.
Mar 08 16:08:50 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:50.452 * DB saved on disk
Mar 08 16:08:50 deathstar default:redis[120760]: 1:M 08 Mar 2019 21:08:50.453 # Redis is now ready to exit, bye bye...
```

The following client side Opts are added:

```go
// LogURI provides the raw logging URI
func LogURI(uri *url.URL) Creator { }
// BinaryIO forwards contianer STDOUT|STDERR directly to a logging binary
func BinaryIO(binary string, args map[string]string) Creator {}
```

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
2019-03-12 12:18:28 -04:00
John Howard
90caf6f6a7 Windows:NewDirectIOFromFIFOSet
Signed-off-by: John Howard <jhoward@microsoft.com>
2019-01-15 10:04:43 -08:00
Michael Crosby
906acb18b6 Don't provide IO when it's not set
This makes sure that runc does not get any valid IO for the pipe.  Some
builds and other containers will be stuck if they inspect stdin
expecially and its a pipe but not connected to any user input.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
2018-09-07 18:30:31 -04:00
Michael Crosby
fdceb13b14 Add cio.Load for loading io set
This adds a `Load` Opt for cio to load a tasks io/fifos without
attaching or starting the copy routines.

It adds the load method in `ctr` by default so that fifos or other IO
are removed from disk on delete methods inbetween command runs.  It is
not the default for all task loads for backwards compat. and a user may
want to keep io around to reuse or if log files are used.

Fixes #2421

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
2018-06-26 11:48:26 -04:00
Michael Crosby
1c263a7d5e Add LogFile as a cio IO option
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
2018-04-26 17:04:01 -04:00
Kunal Kushwaha
b12c3215a0 Licence header added
Signed-off-by: Kunal Kushwaha <kushwaha_kunal_v7@lab.ntt.co.jp>
2018-02-19 10:32:26 +09:00
Phil Estes
d7efcbc083
Merge pull request #2036 from stevvooe/use-buffer-pools
archive, cio, cmd, linux: use buffer pools
2018-01-23 15:00:41 -05:00
Stephen J Day
cd72819b53
archive, cio, cmd, linux: use buffer pools
To avoid buffer bloat in long running processes, we try to use buffer
pools where possible. This is meant to address shim memory usage issues,
but may not be the root cause.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
2018-01-22 13:52:06 -08:00
Akihiro Suda
507a149488 cio: add WithFIFODir opt
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2018-01-18 14:33:56 +09:00
Daniel Nephin
7d4337e738 Reduce the number of IO constructors
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-12-11 15:07:09 -05:00
Daniel Nephin
a901091f7c Rename cio.Creation to cio.Creator
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-12-08 14:26:10 -05:00
Daniel Nephin
3146019918 Refactor cio.DirectIO
New code duplication
Better re-use from consumers of the cio package

TODO: io_windows.go

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-12-08 14:26:10 -05:00
Daniel Nephin
f79ec5b55f Cleanup cio.FIFOSet interface
Remove duplication with cio.Config
unexport newFIFOSetTempDir() since it includes hardcoded paths
Expose os.RemoveAll() as part of FIFOSet instead of a Dir

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-12-08 14:26:10 -05:00
Daniel Nephin
65665ab807 Always Cancel before Close
Document this contract.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-12-08 10:30:36 -05:00
Daniel Nephin
298dabc6c2 Move io.go into cio package
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2017-11-17 17:04:45 -05:00