-
Notifications
You must be signed in to change notification settings - Fork 651
/
Copy pathlogging.go
150 lines (131 loc) · 4.25 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
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 logging
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/runtime/v2/logging"
)
const (
// MagicArgv1 is the magic argv1 for the containerd runtime v2 logging plugin mode.
MagicArgv1 = "_NERDCTL_INTERNAL_LOGGING"
MaxSize = "max-size"
MaxFile = "max-file"
Tag = "tag"
)
type Driver interface {
Init(dataStore, ns, id string) error
Process(dataStore string, config *logging.Config) error
}
type DriverFactory func(map[string]string) (Driver, error)
type LogOpsValidateFunc func(logOptMap map[string]string) error
var drivers = make(map[string]DriverFactory)
var driversLogOptsValidateFunctions = make(map[string]LogOpsValidateFunc)
func ValidateLogOpts(logDriver string, logOpts map[string]string) error {
if value, ok := driversLogOptsValidateFunctions[logDriver]; ok && value != nil {
return value(logOpts)
}
return nil
}
func RegisterDriver(name string, f DriverFactory, validateFunc LogOpsValidateFunc) {
drivers[name] = f
driversLogOptsValidateFunctions[name] = validateFunc
}
func Drivers() []string {
var ss []string // nolint: prealloc
for f := range drivers {
ss = append(ss, f)
}
sort.Strings(ss)
return ss
}
func GetDriver(name string, opts map[string]string) (Driver, error) {
driverFactory, ok := drivers[name]
if !ok {
return nil, fmt.Errorf("unknown logging driver %q: %w", name, errdefs.ErrNotFound)
}
return driverFactory(opts)
}
func init() {
RegisterDriver("json-file", func(opts map[string]string) (Driver, error) {
return &JSONLogger{Opts: opts}, nil
}, JSONFileLogOptsValidate)
RegisterDriver("journald", func(opts map[string]string) (Driver, error) {
return &JournaldLogger{Opts: opts}, nil
}, JournalLogOptsValidate)
RegisterDriver("fluentd", func(opts map[string]string) (Driver, error) {
return &FluentdLogger{Opts: opts}, nil
}, FluentdLogOptsValidate)
RegisterDriver("syslog", func(opts map[string]string) (Driver, error) {
return &SyslogLogger{Opts: opts}, nil
}, SyslogOptsValidate)
}
// 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)
if err != nil {
return err
}
logging.Run(fn)
return nil
}
// LogConfig is marshalled as "log-config.json"
type LogConfig struct {
Driver string `json:"driver"`
Opts map[string]string `json:"opts,omitempty"`
}
// LogConfigFilePath returns the path of log-config.json
func LogConfigFilePath(dataStore, ns, id string) string {
return filepath.Join(dataStore, "containers", ns, id, "log-config.json")
}
func getLoggerFunc(dataStore string) (logging.LoggerFunc, error) {
if dataStore == "" {
return nil, errors.New("got empty data store")
}
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 {
var logConfig LogConfig
logConfigFileB, err := os.ReadFile(logConfigFilePath)
if err != nil {
return err
}
if err = json.Unmarshal(logConfigFileB, &logConfig); err != nil {
return err
}
driver, err := GetDriver(logConfig.Driver, logConfig.Opts)
if err != nil {
return err
}
if err := ready(); err != nil {
return err
}
return driver.Process(dataStore, config)
} else if !errors.Is(err, os.ErrNotExist) {
// the file does not exist if the container was created with nerdctl < 0.20
return err
}
return nil
}, nil
}