Skip to content

Commit 70bc5a9

Browse files
authored
Merge pull request #1377 from yuchanns/support-1353
Support `syslog` driver for nerdctl
2 parents f3c54ab + 0a5016d commit 70bc5a9

File tree

7 files changed

+716
-1
lines changed

7 files changed

+716
-1
lines changed

README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ Metadata flags:
493493
- :nerd_face: `--pidfile`: file path to write the task's pid. The CLI syntax conforms to Podman convention.
494494

495495
Logging flags:
496-
- :whale: `--log-driver=(json-file|journald|fluentd)`: Logging driver for the container (default `json-file`).
496+
- :whale: `--log-driver=(json-file|journald|fluentd|syslog)`: Logging driver for the container (default `json-file`).
497497
- :whale: `--log-driver=json-file`: The logs are formatted as JSON. The default logging driver for nerdctl.
498498
- The `json-file` logging driver supports the following logging options:
499499
- :whale: `--log-opt=max-size=<MAX-SIZE>`: The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g). Defaults to unlimited.
@@ -510,6 +510,37 @@ Logging flags:
510510
- :whale: `--log-opt=fluentd-sub-second-precision=<true|false>`: Enable sub-second precision for fluentd. The default value is false.
511511
- :nerd_face: `--log-opt=fluentd-async-reconnect-interval=<1s|1ms>`: The time to wait before retrying to reconnect to fluentd. The default value is 0s.
512512
- :nerd_face: `--log-opt=fluentd-request-ack=<true|false>`: Enable request ack for fluentd. The default value is false.
513+
- :whale: `--log-driver=syslog`: Writes log messages to `syslog`. The
514+
`syslog` daemon must be running on either the host machine or remote.
515+
- The `syslog` logging driver supports the following logging options:
516+
- :whale: `--log-opt=syslog-address=<ADDRESS>`: The address of an
517+
external `syslog` server. The URI specifier may be
518+
`tcp|udp|tcp+tls]://host:port`, `unix://path`, or `unixgram://path`.
519+
If the transport is `tcp`, `udp`, or `tcp+tls`, the default port is
520+
`514`.
521+
- :whale: `--log-opt=syslog-facility=<FACILITY>`: The `syslog` facility to
522+
use. Can be the number or name for any valid syslog facility. See the
523+
[syslog documentation](https://www.rfc-editor.org/rfc/rfc5424#section-6.2.1).
524+
- :whale: `--log-opt=syslog-tls-ca-cert=<VALUE>`: The absolute path to
525+
the trust certificates signed by the CA. **Ignored if the address
526+
protocol is not `tcp+tls`**.
527+
- :whale: `--log-opt=syslog-tls-cert=<VALUE>`: The absolute path to
528+
the TLS certificate file. **Ignored if the address protocol is not
529+
`tcp+tls`**.
530+
- :whale: `--log-opt=syslog-tls-key=<VALUE>`:The absolute path to
531+
the TLS key file. **Ignored if the address protocol is not `tcp+tls`**.
532+
- :whale: `--log-opt=syslog-tls-skip-verify=<VALUE>`: If set to `true`,
533+
TLS verification is skipped when connecting to the daemon.
534+
**Ignored if the address protocol is not `tcp+tls`**.
535+
- :whale: `--log-opt=syslog-format=<VALUE>`: The `syslog` message format
536+
to use. If not specified the local UNIX syslog format is used,
537+
without a specified hostname. Specify `rfc3164` for the RFC-3164
538+
compatible format, `rfc5424` for RFC-5424 compatible format, or
539+
`rfc5424micro` for RFC-5424 compatible format with microsecond
540+
timestamp resolution.
541+
- :whale: `--log-opt=tag=<VALUE>`: A string that is appended to the
542+
`APP-NAME` in the `syslog` message. By default, nerdctl uses the first
543+
12 characters of the container ID to tag log messages.
513544
- :nerd_face: Accepts a LogURI which is a containerd shim logger. A scheme must be specified for the URI. Example: `nerdctl run -d --log-driver binary:///usr/bin/ctr-journald-shim docker.io/library/hello-world:latest`. An implementation of shim logger can be found at (https://github.com/containerd/containerd/tree/dbef1d56d7ebc05bc4553d72c419ed5ce025b05d/runtime/v2#logging)
514545

515546

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+
"fmt"
21+
"os"
22+
"runtime"
23+
"strings"
24+
"testing"
25+
"time"
26+
27+
"github.com/containerd/nerdctl/pkg/rootlessutil"
28+
"github.com/containerd/nerdctl/pkg/testutil"
29+
"github.com/containerd/nerdctl/pkg/testutil/testca"
30+
"github.com/containerd/nerdctl/pkg/testutil/testsyslog"
31+
syslog "github.com/yuchanns/srslog"
32+
)
33+
34+
func runSyslogTest(t *testing.T, networks []string, syslogFacilities map[string]syslog.Priority, fmtValidFuncs map[string]func(string, string, string, string, syslog.Priority, bool) error) {
35+
base := testutil.NewBase(t)
36+
base.Cmd("pull", testutil.CommonImage).AssertOK()
37+
hostname, err := os.Hostname()
38+
if err != nil {
39+
t.Fatalf("Error retrieving hostname")
40+
}
41+
ca := testca.New(base.T)
42+
cert := ca.NewCert("127.0.0.1")
43+
t.Cleanup(func() {
44+
cert.Close()
45+
ca.Close()
46+
})
47+
rI := 0
48+
for _, network := range networks {
49+
for rFK, rFV := range syslogFacilities {
50+
fPriV := rFV
51+
// test both string and number facility
52+
for _, fPriK := range []string{rFK, fmt.Sprintf("%d", int(fPriV)>>3)} {
53+
for fmtK, fmtValidFunc := range fmtValidFuncs {
54+
fmtKT := "empty"
55+
if fmtK != "" {
56+
fmtKT = fmtK
57+
}
58+
subTestName := fmt.Sprintf("%s_%s_%s", strings.ReplaceAll(network, "+", "_"), fPriK, fmtKT)
59+
i := rI
60+
rI++
61+
t.Run(subTestName, func(t *testing.T) {
62+
tID := testutil.Identifier(t)
63+
tag := tID + "_syslog_driver"
64+
msg := "hello, " + tID + "_syslog_driver"
65+
if !testsyslog.TestableNetwork(network) {
66+
if rootlessutil.IsRootless() {
67+
t.Skipf("skipping on %s/%s; '%s' for rootless containers are not supported", runtime.GOOS, runtime.GOARCH, network)
68+
}
69+
t.Skipf("skipping on %s/%s; '%s' is not supported", runtime.GOOS, runtime.GOARCH, network)
70+
}
71+
testContainerName := fmt.Sprintf("%s-%d-%s", tID, i, fPriK)
72+
done := make(chan string)
73+
addr, closer := testsyslog.StartServer(network, "", done, cert)
74+
args := []string{
75+
"run",
76+
"-d",
77+
"--name",
78+
testContainerName,
79+
"--restart=no",
80+
"--log-driver=syslog",
81+
"--log-opt=syslog-facility=" + fPriK,
82+
"--log-opt=tag=" + tag,
83+
"--log-opt=syslog-format=" + fmtK,
84+
"--log-opt=syslog-address=" + fmt.Sprintf("%s://%s", network, addr),
85+
}
86+
if network == "tcp+tls" {
87+
args = append(args,
88+
"--log-opt=syslog-tls-cert="+cert.CertPath,
89+
"--log-opt=syslog-tls-key="+cert.KeyPath,
90+
"--log-opt=syslog-tls-ca-cert="+ca.CertPath,
91+
)
92+
}
93+
args = append(args, testutil.CommonImage, "echo", msg)
94+
base.Cmd(args...).AssertOK()
95+
t.Cleanup(func() {
96+
base.Cmd("rm", "-f", testContainerName).AssertOK()
97+
})
98+
defer closer.Close()
99+
defer close(done)
100+
select {
101+
case rcvd := <-done:
102+
if err := fmtValidFunc(rcvd, msg, tag, hostname, fPriV, network == "tcp+tls"); err != nil {
103+
t.Error(err)
104+
}
105+
case <-time.Tick(time.Second * 3):
106+
t.Errorf("timeout with %s", subTestName)
107+
}
108+
})
109+
}
110+
}
111+
}
112+
}
113+
}
114+
115+
func TestSyslogNetwork(t *testing.T) {
116+
var syslogFacilities = map[string]syslog.Priority{
117+
"user": syslog.LOG_USER,
118+
}
119+
120+
networks := []string{
121+
"udp",
122+
"tcp",
123+
"tcp+tls",
124+
"unix",
125+
"unixgram",
126+
}
127+
fmtValidFuncs := map[string]func(string, string, string, string, syslog.Priority, bool) error{
128+
"rfc5424": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error {
129+
var parsedHostname, timestamp string
130+
var length, version, pid int
131+
if !isTLS {
132+
exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
133+
if n, err := fmt.Sscanf(rcvd, exp, &version, &timestamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname {
134+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
135+
}
136+
} else {
137+
exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
138+
if n, err := fmt.Sscanf(rcvd, exp, &length, &version, &timestamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname {
139+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
140+
}
141+
}
142+
return nil
143+
},
144+
}
145+
runSyslogTest(t, networks, syslogFacilities, fmtValidFuncs)
146+
}
147+
148+
func TestSyslogFacilities(t *testing.T) {
149+
var syslogFacilities = map[string]syslog.Priority{
150+
"kern": syslog.LOG_KERN,
151+
"user": syslog.LOG_USER,
152+
"mail": syslog.LOG_MAIL,
153+
"daemon": syslog.LOG_DAEMON,
154+
"auth": syslog.LOG_AUTH,
155+
"syslog": syslog.LOG_SYSLOG,
156+
"lpr": syslog.LOG_LPR,
157+
"news": syslog.LOG_NEWS,
158+
"uucp": syslog.LOG_UUCP,
159+
"cron": syslog.LOG_CRON,
160+
"authpriv": syslog.LOG_AUTHPRIV,
161+
"ftp": syslog.LOG_FTP,
162+
"local0": syslog.LOG_LOCAL0,
163+
"local1": syslog.LOG_LOCAL1,
164+
"local2": syslog.LOG_LOCAL2,
165+
"local3": syslog.LOG_LOCAL3,
166+
"local4": syslog.LOG_LOCAL4,
167+
"local5": syslog.LOG_LOCAL5,
168+
"local6": syslog.LOG_LOCAL6,
169+
"local7": syslog.LOG_LOCAL7,
170+
}
171+
172+
networks := []string{"unix"}
173+
fmtValidFuncs := map[string]func(string, string, string, string, syslog.Priority, bool) error{
174+
"rfc5424": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error {
175+
var parsedHostname, timestamp string
176+
var length, version, pid int
177+
if !isTLS {
178+
exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
179+
if n, err := fmt.Sscanf(rcvd, exp, &version, &timestamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname {
180+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
181+
}
182+
} else {
183+
exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
184+
if n, err := fmt.Sscanf(rcvd, exp, &length, &version, &timestamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname {
185+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
186+
}
187+
}
188+
return nil
189+
},
190+
}
191+
runSyslogTest(t, networks, syslogFacilities, fmtValidFuncs)
192+
}
193+
194+
func TestSyslogFormat(t *testing.T) {
195+
var syslogFacilities = map[string]syslog.Priority{
196+
"user": syslog.LOG_USER,
197+
}
198+
199+
networks := []string{"unix"}
200+
fmtValidFuncs := map[string]func(string, string, string, string, syslog.Priority, bool) error{
201+
"": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isSTLS bool) error {
202+
var mon, day, hrs string
203+
var pid int
204+
exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%s %s %s " + tag + "[%d]: " + msg + "\n"
205+
if n, err := fmt.Sscanf(rcvd, exp, &mon, &day, &hrs, &pid); n != 4 || err != nil {
206+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
207+
}
208+
return nil
209+
},
210+
"rfc3164": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error {
211+
var parsedHostname, mon, day, hrs string
212+
var pid int
213+
exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%s %s %s %s " + tag + "[%d]: " + msg + "\n"
214+
if n, err := fmt.Sscanf(rcvd, exp, &mon, &day, &hrs, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname {
215+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
216+
}
217+
return nil
218+
},
219+
"rfc5424": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error {
220+
var parsedHostname, timestamp string
221+
var length, version, pid int
222+
if !isTLS {
223+
exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
224+
if n, err := fmt.Sscanf(rcvd, exp, &version, &timestamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname {
225+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
226+
}
227+
} else {
228+
exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
229+
if n, err := fmt.Sscanf(rcvd, exp, &length, &version, &timestamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname {
230+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
231+
}
232+
}
233+
return nil
234+
},
235+
"rfc5424micro": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error {
236+
var parsedHostname, timestamp string
237+
var length, version, pid int
238+
if !isTLS {
239+
exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
240+
if n, err := fmt.Sscanf(rcvd, exp, &version, &timestamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname {
241+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
242+
}
243+
} else {
244+
exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n"
245+
if n, err := fmt.Sscanf(rcvd, exp, &length, &version, &timestamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname {
246+
return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err)
247+
}
248+
}
249+
return nil
250+
},
251+
}
252+
runSyslogTest(t, networks, syslogFacilities, fmtValidFuncs)
253+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ require (
4747
github.com/tidwall/gjson v1.14.3
4848
github.com/vishvananda/netlink v1.2.1-beta.2
4949
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74
50+
github.com/yuchanns/srslog v1.1.0
5051
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
5152
golang.org/x/net v0.0.0-20220615171555-694bf12d69de
5253
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,8 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17
15101510
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
15111511
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
15121512
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
1513+
github.com/yuchanns/srslog v1.1.0 h1:CEm97Xxxd8XpJThE0gc/XsqUGgPufh5u5MUjC27/KOk=
1514+
github.com/yuchanns/srslog v1.1.0/go.mod h1:HsLjdv3XV02C3kgBW2bTyW6i88OQE+VYJZIxrPKPPak=
15131515
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
15141516
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
15151517
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

pkg/logging/logging.go

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func init() {
8787
RegisterDriver("fluentd", func(opts map[string]string) (Driver, error) {
8888
return &FluentdLogger{Opts: opts}, nil
8989
}, FluentdLogOptsValidate)
90+
RegisterDriver("syslog", func(opts map[string]string) (Driver, error) {
91+
return &SyslogLogger{Opts: opts}, nil
92+
}, SyslogOptsValidate)
9093
}
9194

9295
// Main is the entrypoint for the containerd runtime v2 logging plugin mode.

0 commit comments

Comments
 (0)