Skip to content

Commit

Permalink
reduce some memory for log driver
Browse files Browse the repository at this point in the history
Signed-off-by: ningmingxiao <[email protected]>
  • Loading branch information
ningmingxiao committed Jan 21, 2023
1 parent a038f2d commit 556aecb
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 55 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ help:

nerdctl:
$(GO_BUILD) $(VERBOSE_FLAG) -o $(CURDIR)/_output/nerdctl$(BIN_EXT) $(PACKAGE)/cmd/nerdctl
containerd-logging-driver:
$(GO_BUILD) $(VERBOSE_FLAG) -o $(CURDIR)/_output/containerd-logging-driver$(BIN_EXT) $(PACKAGE)/cmd/containerd-logging-driver

clean:
find . -name \*~ -delete
find . -name \#\* -delete
rm -rf _output/* vendor

binaries: nerdctl
binaries: nerdctl containerd-logging-driver

install:
install -D -m 755 $(CURDIR)/_output/nerdctl $(DESTDIR)$(BINDIR)/nerdctl
install -D -m 755 $(CURDIR)/_output/containerd-logging-driver $(DESTDIR)$(BINDIR)/containerd-logging-driver
install -D -m 755 $(CURDIR)/extras/rootless/containerd-rootless.sh $(DESTDIR)$(BINDIR)/containerd-rootless.sh
install -D -m 755 $(CURDIR)/extras/rootless/containerd-rootless-setuptool.sh $(DESTDIR)$(BINDIR)/containerd-rootless-setuptool.sh

Expand Down Expand Up @@ -106,6 +109,7 @@ artifacts: clean
.PHONY: \
help \
nerdctl \
containerd-logging-driver \
clean \
binaries \
install \
Expand Down
42 changes: 42 additions & 0 deletions cmd/containerd-logging-driver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"flag"
"github.com/containerd/nerdctl/pkg/logging"
"github.com/sirupsen/logrus"
)

var driverConfig logging.DriverConfig

func init() {
flag.StringVar(&driverConfig.LogPath, "log-path", "", "The log path where the logs are written")
flag.StringVar(&driverConfig.MaxSize, "max-size", "-1", "The maximum size of the log before it is rolled")
flag.StringVar(&driverConfig.MaxFile, "max-file", "1", "The maximum number of log files that can be present")
flag.StringVar(&driverConfig.Tag, "tag", "", "A string that is appended to the APP-NAME in the syslog message")
flag.StringVar(&driverConfig.Driver, "driver", "", "A string that is appended to the APP-NAME in the syslog message")
}

func main() {
flag.Parse()
err := logging.Main(driverConfig)

if err != nil {
logrus.Fatal(err)
}
}
6 changes: 0 additions & 6 deletions cmd/nerdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/containerd/nerdctl/pkg/config"
ncdefaults "github.com/containerd/nerdctl/pkg/defaults"
"github.com/containerd/nerdctl/pkg/errutil"
"github.com/containerd/nerdctl/pkg/logging"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/containerd/nerdctl/pkg/version"
"github.com/fatih/color"
Expand Down Expand Up @@ -120,11 +119,6 @@ func main() {
}

func xmain() error {
if len(os.Args) == 3 && os.Args[1] == logging.MagicArgv1 {
// containerd runtime v2 logging plugin mode.
// "binary://BIN?KEY=VALUE" URI is parsed into Args {BIN, KEY, VALUE}.
return logging.Main(os.Args[2])
}
// nerdctl CLI mode
app, err := newApp()
if err != nil {
Expand Down
43 changes: 37 additions & 6 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/logging"
"github.com/containerd/nerdctl/pkg/logging/jsonfile"
"github.com/containerd/nerdctl/pkg/mountutil"
"github.com/containerd/nerdctl/pkg/namestore"
"github.com/containerd/nerdctl/pkg/netutil"
Expand Down Expand Up @@ -548,7 +549,7 @@ func createContainer(ctx context.Context, cmd *cobra.Command, client *containerd
if err = os.WriteFile(logConfigFilePath, logConfigB, 0600); err != nil {
return nil, nil, err
}
if lu, err := generateLogURI(dataStore); err != nil {
if lu, err := generateLogURI(dataStore, globalOptions.Namespace, id); err != nil {
return nil, nil, err
} else if lu != nil {
logrus.Debugf("generated log driver: %s", lu.String())
Expand Down Expand Up @@ -914,16 +915,46 @@ func withBindMountHostIPC(_ context.Context, _ oci.Client, _ *containers.Contain
return nil
}

func generateLogURI(dataStore string) (*url.URL, error) {
selfExe, err := os.Executable()
func generateLogURI(dataStore, ns, id string) (*url.URL, error) {
nlogDriver, err := exec.LookPath("containerd-logging-driver")
if err != nil {
return nil, err
}
args := map[string]string{
logging.MagicArgv1: dataStore,
logConfig, err := logging.LoadLogConfig(dataStore, ns, id)
if err != nil {
return nil, err
}
logPath := jsonfile.Path(dataStore, ns, id)
args := make(map[string]string)
opts := logConfig.Opts
if logPath != "" && logConfig.Driver == "json-file" {
args["-"+logging.LogPath] = logPath
}
maxSize, ok := opts[logging.MaxSize]
if ok {
if maxSize != "" {
args["-"+logging.MaxSize] = maxSize
}
}

maxFile, ok := opts[logging.MaxFile]
if ok {
if maxFile != "" {
args["-"+logging.MaxFile] = maxFile
}
}

tag, ok := opts[logging.Tag]
if ok {
if tag != "" {
args["-"+logging.Tag] = tag
}
}

return cio.LogURIGenerator("binary", selfExe, args)
if logConfig.Driver != "" {
args["-"+logging.LogDriver] = logConfig.Driver
}
return cio.LogURIGenerator("binary", nlogDriver, args)
}

func withNerdctlOCIHook(cmd *cobra.Command, id string) (oci.SpecOpts, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/logging/fluentd_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (f *FluentdLogger) Init(dataStore, ns, id string) error {
return nil
}

func (f *FluentdLogger) PreProcess(_ string, config *logging.Config) error {
func (f *FluentdLogger) PreProcess(config *logging.Config) error {
if runtime.GOOS == "windows" {
// TODO: support fluentd on windows
return fmt.Errorf("logging to fluentd is not supported on windows")
Expand Down
2 changes: 1 addition & 1 deletion pkg/logging/journald_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (journaldLogger *JournaldLogger) Init(dataStore, ns, id string) error {
return nil
}

func (journaldLogger *JournaldLogger) PreProcess(dataStore string, config *logging.Config) error {
func (journaldLogger *JournaldLogger) PreProcess(config *logging.Config) error {
if !journal.Enabled() {
return errors.New("the local systemd journal is not available for logging")
}
Expand Down
26 changes: 14 additions & 12 deletions pkg/logging/json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ package logging
import (
"errors"
"fmt"
"github.com/containerd/containerd/runtime/v2/logging"
"github.com/docker/go-units"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"time"

"github.com/containerd/containerd/runtime/v2/logging"
"github.com/containerd/nerdctl/pkg/logging/jsonfile"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/docker/go-units"
"github.com/fahedouch/go-logrotate"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -73,12 +73,10 @@ func (jsonLogger *JSONLogger) Init(dataStore, ns, id string) error {
return nil
}

func (jsonLogger *JSONLogger) PreProcess(dataStore string, config *logging.Config) error {
func (jsonLogger *JSONLogger) PreProcess(config *logging.Config) error {
var jsonFilePath string
if logPath, ok := jsonLogger.Opts[LogPath]; ok {
jsonFilePath = logPath
} else {
jsonFilePath = jsonfile.Path(dataStore, config.Namespace, config.ID)
}
l := &logrotate.Logger{
Filename: jsonFilePath,
Expand All @@ -87,13 +85,17 @@ func (jsonLogger *JSONLogger) PreProcess(dataStore string, config *logging.Confi
var capVal int64
capVal = -1
if capacity, ok := jsonLogger.Opts[MaxSize]; ok {
var err error
capVal, err = units.FromHumanSize(capacity)
if err != nil {
return err
}
if capVal <= 0 {
return fmt.Errorf("max-size must be a positive number")
if capacity == "-1" {
capVal = -1
} else {
var err error
capVal, err = units.FromHumanSize(capacity)
if err != nil {
return err
}
if capVal <= 0 {
return fmt.Errorf("max-size must be a positive number")
}
}
}
l.MaxBytes = capVal
Expand Down
55 changes: 28 additions & 27 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,24 @@ const (
MaxSize = "max-size"
MaxFile = "max-file"
Tag = "tag"
LogDriver = "driver"
)

type Driver interface {
Init(dataStore, ns, id string) error
PreProcess(dataStore string, config *logging.Config) error
PreProcess(config *logging.Config) error
Process(stdout <-chan string, stderr <-chan string) error
PostProcess() error
}

type DriverConfig struct {
LogPath string
MaxSize string
MaxFile string
Tag string
Driver string
}

type DriverFactory func(map[string]string) (Driver, error)
type LogOpsValidateFunc func(logOptMap map[string]string) error

Expand Down Expand Up @@ -102,8 +111,8 @@ func init() {
// Main is the entrypoint for the containerd runtime v2 logging plugin mode.
//
// Should be called only if argv1 == MagicArgv1.
func Main(argv2 string) error {
fn, err := getLoggerFunc(argv2)
func Main(driverConfig DriverConfig) error {
fn, err := getLoggerFunc(driverConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -139,8 +148,8 @@ func LoadLogConfig(dataStore, ns, id string) (LogConfig, error) {
return logConfig, nil
}

func loggingProcessAdapter(driver Driver, dataStore string, config *logging.Config) error {
if err := driver.PreProcess(dataStore, config); err != nil {
func loggingProcessAdapter(driver Driver, config *logging.Config) error {
if err := driver.PreProcess(config); err != nil {
return err
}
var wg sync.WaitGroup
Expand Down Expand Up @@ -170,33 +179,25 @@ func loggingProcessAdapter(driver Driver, dataStore string, config *logging.Conf
return driver.PostProcess()
}

func getLoggerFunc(dataStore string) (logging.LoggerFunc, error) {
if dataStore == "" {
return nil, errors.New("got empty data store")
}
func getLoggerFunc(driverConfig DriverConfig) (logging.LoggerFunc, error) {
return func(_ context.Context, config *logging.Config, ready func() error) error {
if config.Namespace == "" || config.ID == "" {
return errors.New("got invalid config")
}
logConfigFilePath := LogConfigFilePath(dataStore, config.Namespace, config.ID)
if _, err := os.Stat(logConfigFilePath); err == nil {
logConfig, err := LoadLogConfig(dataStore, config.Namespace, config.ID)
if err != nil {
return err
}
driver, err := GetDriver(logConfig.Driver, logConfig.Opts)
if err != nil {
return err
}
if err := ready(); err != nil {
return err
}

return loggingProcessAdapter(driver, dataStore, config)
} else if !errors.Is(err, os.ErrNotExist) {
// the file does not exist if the container was created with nerdctl < 0.20
var logConfig LogConfig
logConfig.Opts = map[string]string{
"max-file": driverConfig.MaxFile,
"max-size": driverConfig.MaxSize,
"log-path": driverConfig.LogPath,
}
logConfig.Driver = driverConfig.Driver
driver, err := GetDriver(logConfig.Driver, logConfig.Opts)
if err != nil {
return err
}
if err := ready(); err != nil {
return err
}
return nil
return loggingProcessAdapter(driver, config)
}, nil
}
2 changes: 1 addition & 1 deletion pkg/logging/syslog_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (sy *SyslogLogger) Init(dataStore string, ns string, id string) error {
return nil
}

func (sy *SyslogLogger) PreProcess(dataStore string, config *logging.Config) error {
func (sy *SyslogLogger) PreProcess(config *logging.Config) error {
logger, err := parseSyslog(config.ID, sy.Opts)
if err != nil {
return err
Expand Down

0 comments on commit 556aecb

Please sign in to comment.