Skip to content

Commit 556aecb

Browse files
committed
reduce some memory for log driver
Signed-off-by: ningmingxiao <[email protected]>
1 parent a038f2d commit 556aecb

File tree

9 files changed

+129
-55
lines changed

9 files changed

+129
-55
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,19 @@ help:
4949

5050
nerdctl:
5151
$(GO_BUILD) $(VERBOSE_FLAG) -o $(CURDIR)/_output/nerdctl$(BIN_EXT) $(PACKAGE)/cmd/nerdctl
52+
containerd-logging-driver:
53+
$(GO_BUILD) $(VERBOSE_FLAG) -o $(CURDIR)/_output/containerd-logging-driver$(BIN_EXT) $(PACKAGE)/cmd/containerd-logging-driver
5254

5355
clean:
5456
find . -name \*~ -delete
5557
find . -name \#\* -delete
5658
rm -rf _output/* vendor
5759

58-
binaries: nerdctl
60+
binaries: nerdctl containerd-logging-driver
5961

6062
install:
6163
install -D -m 755 $(CURDIR)/_output/nerdctl $(DESTDIR)$(BINDIR)/nerdctl
64+
install -D -m 755 $(CURDIR)/_output/containerd-logging-driver $(DESTDIR)$(BINDIR)/containerd-logging-driver
6265
install -D -m 755 $(CURDIR)/extras/rootless/containerd-rootless.sh $(DESTDIR)$(BINDIR)/containerd-rootless.sh
6366
install -D -m 755 $(CURDIR)/extras/rootless/containerd-rootless-setuptool.sh $(DESTDIR)$(BINDIR)/containerd-rootless-setuptool.sh
6467

@@ -106,6 +109,7 @@ artifacts: clean
106109
.PHONY: \
107110
help \
108111
nerdctl \
112+
containerd-logging-driver \
109113
clean \
110114
binaries \
111115
install \

cmd/containerd-logging-driver/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"flag"
21+
"github.com/containerd/nerdctl/pkg/logging"
22+
"github.com/sirupsen/logrus"
23+
)
24+
25+
var driverConfig logging.DriverConfig
26+
27+
func init() {
28+
flag.StringVar(&driverConfig.LogPath, "log-path", "", "The log path where the logs are written")
29+
flag.StringVar(&driverConfig.MaxSize, "max-size", "-1", "The maximum size of the log before it is rolled")
30+
flag.StringVar(&driverConfig.MaxFile, "max-file", "1", "The maximum number of log files that can be present")
31+
flag.StringVar(&driverConfig.Tag, "tag", "", "A string that is appended to the APP-NAME in the syslog message")
32+
flag.StringVar(&driverConfig.Driver, "driver", "", "A string that is appended to the APP-NAME in the syslog message")
33+
}
34+
35+
func main() {
36+
flag.Parse()
37+
err := logging.Main(driverConfig)
38+
39+
if err != nil {
40+
logrus.Fatal(err)
41+
}
42+
}

cmd/nerdctl/main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/containerd/nerdctl/pkg/config"
2929
ncdefaults "github.com/containerd/nerdctl/pkg/defaults"
3030
"github.com/containerd/nerdctl/pkg/errutil"
31-
"github.com/containerd/nerdctl/pkg/logging"
3231
"github.com/containerd/nerdctl/pkg/rootlessutil"
3332
"github.com/containerd/nerdctl/pkg/version"
3433
"github.com/fatih/color"
@@ -120,11 +119,6 @@ func main() {
120119
}
121120

122121
func xmain() error {
123-
if len(os.Args) == 3 && os.Args[1] == logging.MagicArgv1 {
124-
// containerd runtime v2 logging plugin mode.
125-
// "binary://BIN?KEY=VALUE" URI is parsed into Args {BIN, KEY, VALUE}.
126-
return logging.Main(os.Args[2])
127-
}
128122
// nerdctl CLI mode
129123
app, err := newApp()
130124
if err != nil {

cmd/nerdctl/run.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
5252
"github.com/containerd/nerdctl/pkg/labels"
5353
"github.com/containerd/nerdctl/pkg/logging"
54+
"github.com/containerd/nerdctl/pkg/logging/jsonfile"
5455
"github.com/containerd/nerdctl/pkg/mountutil"
5556
"github.com/containerd/nerdctl/pkg/namestore"
5657
"github.com/containerd/nerdctl/pkg/netutil"
@@ -548,7 +549,7 @@ func createContainer(ctx context.Context, cmd *cobra.Command, client *containerd
548549
if err = os.WriteFile(logConfigFilePath, logConfigB, 0600); err != nil {
549550
return nil, nil, err
550551
}
551-
if lu, err := generateLogURI(dataStore); err != nil {
552+
if lu, err := generateLogURI(dataStore, globalOptions.Namespace, id); err != nil {
552553
return nil, nil, err
553554
} else if lu != nil {
554555
logrus.Debugf("generated log driver: %s", lu.String())
@@ -914,16 +915,46 @@ func withBindMountHostIPC(_ context.Context, _ oci.Client, _ *containers.Contain
914915
return nil
915916
}
916917

917-
func generateLogURI(dataStore string) (*url.URL, error) {
918-
selfExe, err := os.Executable()
918+
func generateLogURI(dataStore, ns, id string) (*url.URL, error) {
919+
nlogDriver, err := exec.LookPath("containerd-logging-driver")
919920
if err != nil {
920921
return nil, err
921922
}
922-
args := map[string]string{
923-
logging.MagicArgv1: dataStore,
923+
logConfig, err := logging.LoadLogConfig(dataStore, ns, id)
924+
if err != nil {
925+
return nil, err
926+
}
927+
logPath := jsonfile.Path(dataStore, ns, id)
928+
args := make(map[string]string)
929+
opts := logConfig.Opts
930+
if logPath != "" && logConfig.Driver == "json-file" {
931+
args["-"+logging.LogPath] = logPath
932+
}
933+
maxSize, ok := opts[logging.MaxSize]
934+
if ok {
935+
if maxSize != "" {
936+
args["-"+logging.MaxSize] = maxSize
937+
}
938+
}
939+
940+
maxFile, ok := opts[logging.MaxFile]
941+
if ok {
942+
if maxFile != "" {
943+
args["-"+logging.MaxFile] = maxFile
944+
}
945+
}
946+
947+
tag, ok := opts[logging.Tag]
948+
if ok {
949+
if tag != "" {
950+
args["-"+logging.Tag] = tag
951+
}
924952
}
925953

926-
return cio.LogURIGenerator("binary", selfExe, args)
954+
if logConfig.Driver != "" {
955+
args["-"+logging.LogDriver] = logConfig.Driver
956+
}
957+
return cio.LogURIGenerator("binary", nlogDriver, args)
927958
}
928959

929960
func withNerdctlOCIHook(cmd *cobra.Command, id string) (oci.SpecOpts, error) {

pkg/logging/fluentd_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (f *FluentdLogger) Init(dataStore, ns, id string) error {
9797
return nil
9898
}
9999

100-
func (f *FluentdLogger) PreProcess(_ string, config *logging.Config) error {
100+
func (f *FluentdLogger) PreProcess(config *logging.Config) error {
101101
if runtime.GOOS == "windows" {
102102
// TODO: support fluentd on windows
103103
return fmt.Errorf("logging to fluentd is not supported on windows")

pkg/logging/journald_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (journaldLogger *JournaldLogger) Init(dataStore, ns, id string) error {
6464
return nil
6565
}
6666

67-
func (journaldLogger *JournaldLogger) PreProcess(dataStore string, config *logging.Config) error {
67+
func (journaldLogger *JournaldLogger) PreProcess(config *logging.Config) error {
6868
if !journal.Enabled() {
6969
return errors.New("the local systemd journal is not available for logging")
7070
}

pkg/logging/json_logger.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ package logging
1919
import (
2020
"errors"
2121
"fmt"
22+
"github.com/containerd/containerd/runtime/v2/logging"
23+
"github.com/docker/go-units"
2224
"io"
2325
"os"
2426
"os/exec"
2527
"path/filepath"
2628
"strconv"
2729
"time"
2830

29-
"github.com/containerd/containerd/runtime/v2/logging"
3031
"github.com/containerd/nerdctl/pkg/logging/jsonfile"
3132
"github.com/containerd/nerdctl/pkg/strutil"
32-
"github.com/docker/go-units"
3333
"github.com/fahedouch/go-logrotate"
3434
"github.com/sirupsen/logrus"
3535
)
@@ -73,12 +73,10 @@ func (jsonLogger *JSONLogger) Init(dataStore, ns, id string) error {
7373
return nil
7474
}
7575

76-
func (jsonLogger *JSONLogger) PreProcess(dataStore string, config *logging.Config) error {
76+
func (jsonLogger *JSONLogger) PreProcess(config *logging.Config) error {
7777
var jsonFilePath string
7878
if logPath, ok := jsonLogger.Opts[LogPath]; ok {
7979
jsonFilePath = logPath
80-
} else {
81-
jsonFilePath = jsonfile.Path(dataStore, config.Namespace, config.ID)
8280
}
8381
l := &logrotate.Logger{
8482
Filename: jsonFilePath,
@@ -87,13 +85,17 @@ func (jsonLogger *JSONLogger) PreProcess(dataStore string, config *logging.Confi
8785
var capVal int64
8886
capVal = -1
8987
if capacity, ok := jsonLogger.Opts[MaxSize]; ok {
90-
var err error
91-
capVal, err = units.FromHumanSize(capacity)
92-
if err != nil {
93-
return err
94-
}
95-
if capVal <= 0 {
96-
return fmt.Errorf("max-size must be a positive number")
88+
if capacity == "-1" {
89+
capVal = -1
90+
} else {
91+
var err error
92+
capVal, err = units.FromHumanSize(capacity)
93+
if err != nil {
94+
return err
95+
}
96+
if capVal <= 0 {
97+
return fmt.Errorf("max-size must be a positive number")
98+
}
9799
}
98100
}
99101
l.MaxBytes = capVal

pkg/logging/logging.go

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,24 @@ const (
4040
MaxSize = "max-size"
4141
MaxFile = "max-file"
4242
Tag = "tag"
43+
LogDriver = "driver"
4344
)
4445

4546
type Driver interface {
4647
Init(dataStore, ns, id string) error
47-
PreProcess(dataStore string, config *logging.Config) error
48+
PreProcess(config *logging.Config) error
4849
Process(stdout <-chan string, stderr <-chan string) error
4950
PostProcess() error
5051
}
5152

53+
type DriverConfig struct {
54+
LogPath string
55+
MaxSize string
56+
MaxFile string
57+
Tag string
58+
Driver string
59+
}
60+
5261
type DriverFactory func(map[string]string) (Driver, error)
5362
type LogOpsValidateFunc func(logOptMap map[string]string) error
5463

@@ -102,8 +111,8 @@ func init() {
102111
// Main is the entrypoint for the containerd runtime v2 logging plugin mode.
103112
//
104113
// Should be called only if argv1 == MagicArgv1.
105-
func Main(argv2 string) error {
106-
fn, err := getLoggerFunc(argv2)
114+
func Main(driverConfig DriverConfig) error {
115+
fn, err := getLoggerFunc(driverConfig)
107116
if err != nil {
108117
return err
109118
}
@@ -139,8 +148,8 @@ func LoadLogConfig(dataStore, ns, id string) (LogConfig, error) {
139148
return logConfig, nil
140149
}
141150

142-
func loggingProcessAdapter(driver Driver, dataStore string, config *logging.Config) error {
143-
if err := driver.PreProcess(dataStore, config); err != nil {
151+
func loggingProcessAdapter(driver Driver, config *logging.Config) error {
152+
if err := driver.PreProcess(config); err != nil {
144153
return err
145154
}
146155
var wg sync.WaitGroup
@@ -170,33 +179,25 @@ func loggingProcessAdapter(driver Driver, dataStore string, config *logging.Conf
170179
return driver.PostProcess()
171180
}
172181

173-
func getLoggerFunc(dataStore string) (logging.LoggerFunc, error) {
174-
if dataStore == "" {
175-
return nil, errors.New("got empty data store")
176-
}
182+
func getLoggerFunc(driverConfig DriverConfig) (logging.LoggerFunc, error) {
177183
return func(_ context.Context, config *logging.Config, ready func() error) error {
178184
if config.Namespace == "" || config.ID == "" {
179185
return errors.New("got invalid config")
180186
}
181-
logConfigFilePath := LogConfigFilePath(dataStore, config.Namespace, config.ID)
182-
if _, err := os.Stat(logConfigFilePath); err == nil {
183-
logConfig, err := LoadLogConfig(dataStore, config.Namespace, config.ID)
184-
if err != nil {
185-
return err
186-
}
187-
driver, err := GetDriver(logConfig.Driver, logConfig.Opts)
188-
if err != nil {
189-
return err
190-
}
191-
if err := ready(); err != nil {
192-
return err
193-
}
194-
195-
return loggingProcessAdapter(driver, dataStore, config)
196-
} else if !errors.Is(err, os.ErrNotExist) {
197-
// the file does not exist if the container was created with nerdctl < 0.20
187+
var logConfig LogConfig
188+
logConfig.Opts = map[string]string{
189+
"max-file": driverConfig.MaxFile,
190+
"max-size": driverConfig.MaxSize,
191+
"log-path": driverConfig.LogPath,
192+
}
193+
logConfig.Driver = driverConfig.Driver
194+
driver, err := GetDriver(logConfig.Driver, logConfig.Opts)
195+
if err != nil {
196+
return err
197+
}
198+
if err := ready(); err != nil {
198199
return err
199200
}
200-
return nil
201+
return loggingProcessAdapter(driver, config)
201202
}, nil
202203
}

pkg/logging/syslog_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (sy *SyslogLogger) Init(dataStore string, ns string, id string) error {
121121
return nil
122122
}
123123

124-
func (sy *SyslogLogger) PreProcess(dataStore string, config *logging.Config) error {
124+
func (sy *SyslogLogger) PreProcess(config *logging.Config) error {
125125
logger, err := parseSyslog(config.ID, sy.Opts)
126126
if err != nil {
127127
return err

0 commit comments

Comments
 (0)