Skip to content

Commit c52c4b3

Browse files
committed
Creates the driver socker
1 parent be79714 commit c52c4b3

File tree

120 files changed

+11224
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+11224
-2
lines changed

cmd/server.go

+11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"github.com/Mirantis/cri-dockerd/cmd/version"
2626
"github.com/Mirantis/cri-dockerd/config"
2727
"github.com/Mirantis/cri-dockerd/core"
28+
"github.com/Mirantis/cri-dockerd/internal/criLogDriver"
2829
"github.com/Mirantis/cri-dockerd/streaming"
30+
"github.com/docker/go-plugins-helpers/sdk"
2931
"github.com/sirupsen/logrus"
3032

3133
"net"
@@ -217,6 +219,15 @@ func RunCriDockerd(f *options.DockerCRIFlags, stopCh <-chan struct{}) error {
217219
return err
218220
}
219221

222+
h := sdk.NewHandler(`{"Implements": ["LoggingDriver"]}`)
223+
criLogDriver.Handlers(&h, criLogDriver.NewDriver())
224+
go func() {
225+
logrus.Info("Starting the logging driver for Docker CRI interface.")
226+
if err := h.ServeUnix("CRILogger", 0); err != nil {
227+
panic(err)
228+
}
229+
}()
230+
220231
logrus.Info("Starting the GRPC backend for the Docker CRI interface.")
221232
server := backend.NewCriDockerServer(f.RemoteRuntimeEndpoint, ds)
222233
if err := server.Start(); err != nil {

go.mod

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ require (
66
github.com/Microsoft/hcsshim v0.8.22
77
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e
88
github.com/blang/semver v3.5.1+incompatible
9+
github.com/containerd/fifo v1.0.0
910
github.com/containernetworking/cni v1.1.2
1011
github.com/coreos/go-systemd/v22 v22.5.0
1112
github.com/davecgh/go-spew v1.1.1
1213
github.com/docker/distribution v2.8.3+incompatible
1314
github.com/docker/docker v23.0.3+incompatible
1415
github.com/docker/go-connections v0.4.0
16+
github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651
17+
github.com/docker/go-units v0.5.0
1518
github.com/emicklei/go-restful v2.16.0+incompatible
19+
github.com/gogo/protobuf v1.3.2
1620
github.com/golang/mock v1.6.0
1721
github.com/opencontainers/go-digest v1.0.0
1822
github.com/opencontainers/image-spec v1.1.0-rc5
@@ -46,9 +50,11 @@ require (
4650
github.com/cespare/xxhash/v2 v2.2.0 // indirect
4751
github.com/containerd/cgroups v1.0.1 // indirect
4852
github.com/coreos/go-semver v0.3.0 // indirect
53+
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e // indirect
4954
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
5055
github.com/distribution/reference v0.5.0 // indirect
5156
github.com/docker/go-units v0.5.0 // indirect
57+
github.com/docker/go-metrics v0.0.1 // indirect
5258
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
5359
github.com/felixge/httpsnoop v1.0.1 // indirect
5460
github.com/fsnotify/fsnotify v1.4.9 // indirect
@@ -57,7 +63,6 @@ require (
5763
github.com/go-openapi/jsonreference v0.20.0 // indirect
5864
github.com/go-openapi/swag v0.19.14 // indirect
5965
github.com/godbus/dbus/v5 v5.1.0 // indirect
60-
github.com/gogo/protobuf v1.3.2 // indirect
6166
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
6267
github.com/golang/protobuf v1.5.3 // indirect
6368
github.com/google/btree v1.1.2 // indirect
@@ -93,6 +98,7 @@ require (
9398
github.com/prometheus/client_model v0.3.0 // indirect
9499
github.com/prometheus/common v0.39.0 // indirect
95100
github.com/prometheus/procfs v0.9.0 // indirect
101+
github.com/rootless-containers/rootlesskit v1.1.1 // indirect
96102
github.com/stretchr/objx v0.5.0 // indirect
97103
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 // indirect
98104
github.com/vishvananda/netns v0.0.1 // indirect

go.sum

+14-1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ github.com/containerd/containerd v1.4.12/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtM
151151
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
152152
github.com/containerd/continuity v0.1.0/go.mod h1:ICJu0PwR54nI0yPEnJ6jcS+J7CZAUXrLh8lPo2knzsM=
153153
github.com/containerd/fifo v0.0.0-20190226154929-a9fb20d87448/go.mod h1:ODA38xgv3Kuk8dQz2ZQXpnv/UZZUHUCL7pnLehbXgQI=
154+
github.com/containerd/fifo v1.0.0 h1:6PirWBr9/L7GDamKr+XM0IeUFXu5mf3M/BPpH9gaLBU=
154155
github.com/containerd/fifo v1.0.0/go.mod h1:ocF/ME1SX5b1AOlWi9r677YJmCPSwwWnQ9O123vzpE4=
155156
github.com/containerd/go-runc v0.0.0-20180907222934-5a6d9f37cfa3/go.mod h1:IV7qH3hrUgRmyYrtgEeGWJfWbgcHL9CSRruz2Vqcph0=
156157
github.com/containerd/go-runc v1.0.0/go.mod h1:cNU0ZbCgCQVZK4lgG3P+9tn9/PaJNmoDXPpoJhDR+Ok=
@@ -171,6 +172,7 @@ github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHo
171172
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
172173
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
173174
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
175+
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e h1:Wf6HqHfScWJN9/ZjdUKyjop4mf3Qdd+1TvvltAvM3m8=
174176
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
175177
github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
176178
github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
@@ -208,6 +210,10 @@ github.com/docker/docker v23.0.3+incompatible h1:9GhVsShNWz1hO//9BNg/dpMnZW25Kyd
208210
github.com/docker/docker v23.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
209211
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
210212
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
213+
github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQV8=
214+
github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw=
215+
github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651 h1:YcvzLmdrP/b8kLAGJ8GT7bdncgCAiWxJZIlt84D+RJg=
216+
github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651/go.mod h1:LFyLie6XcDbyKGeVK6bHe+9aJTYCxWLBg5IrJZOaXKA=
211217
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
212218
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
213219
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
@@ -442,6 +448,7 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
442448
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
443449
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
444450
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
451+
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
445452
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
446453
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
447454
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -592,6 +599,7 @@ github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prY
592599
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
593600
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
594601
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
602+
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
595603
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
596604
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
597605
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
@@ -605,6 +613,7 @@ github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3d
605613
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
606614
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
607615
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
616+
github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc=
608617
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
609618
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
610619
github.com/prometheus/common v0.28.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
@@ -615,6 +624,7 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
615624
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
616625
github.com/prometheus/procfs v0.0.0-20190522114515-bc1a522cf7b1/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
617626
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
627+
github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
618628
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
619629
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
620630
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
@@ -629,6 +639,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
629639
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
630640
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
631641
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
642+
github.com/rootless-containers/rootlesskit v1.1.1 h1:F5psKWoWY9/VjZ3ifVcaosjvFZJOagX85U22M0/EQZE=
643+
github.com/rootless-containers/rootlesskit v1.1.1/go.mod h1:UD5GoA3dqKCJrnvnhVgQQnweMF2qZnf9KLw8EewcMZI=
632644
github.com/rubiojr/go-vhd v0.0.0-20200706105327-02e210299021/go.mod h1:DM5xW0nvfNNm2uytzsvhI3OnX8uzaRAg8UX/CnDqbto=
633645
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
634646
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -952,6 +964,7 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w
952964
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
953965
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
954966
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
967+
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
955968
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
956969
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
957970
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -1284,8 +1297,8 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12841297
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
12851298
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
12861299
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
1287-
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
12881300
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
1301+
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
12891302
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
12901303
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
12911304
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

internal/criLogDriver/config.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"description": "CRI format logger for Docker",
3+
"documentation": "",
4+
"interface": {
5+
"types": ["docker.logdriver/1.0"],
6+
"socket": "CRILogger.sock"
7+
}
8+
}

internal/criLogDriver/crifilelog.go

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Package crifilelog provides the Logger implementation for CRI logging. This
2+
// logger logs to files on the host server in the CRI format.
3+
package criLogDriver
4+
5+
import (
6+
"bytes"
7+
"encoding/json"
8+
"fmt"
9+
"strconv"
10+
"sync"
11+
"time"
12+
13+
"github.com/docker/docker/daemon/logger"
14+
"github.com/docker/docker/daemon/logger/loggerutils"
15+
units "github.com/docker/go-units"
16+
"github.com/pkg/errors"
17+
)
18+
19+
// Name of the driver
20+
const Name = "cri-file"
21+
22+
const initialBufSize = 256
23+
24+
var buffersPool = sync.Pool{New: func() interface{} { return bytes.NewBuffer(make([]byte, 0, initialBufSize)) }}
25+
26+
// CRIFileLogger is the logger implementation for CRI Docker logging
27+
type CRIFileLogger struct {
28+
writer *loggerutils.LogFile
29+
tag string // tag values requested by the user to log
30+
extra []byte
31+
}
32+
33+
func init() {
34+
if err := logger.RegisterLogDriver(Name, NewCRIFileLogger); err != nil {
35+
panic(err)
36+
}
37+
if err := logger.RegisterLogOptValidator(Name, ValidateLogOpt); err != nil {
38+
panic(err)
39+
}
40+
}
41+
42+
// NewCRIFileLogger creates new CRIFileLogger which writes to filename passed in
43+
// on given context.
44+
func NewCRIFileLogger(info logger.Info) (logger.Logger, error) {
45+
var capval int64 = -1
46+
if capacity, ok := info.Config["max-size"]; ok {
47+
var err error
48+
capval, err = units.FromHumanSize(capacity)
49+
if err != nil {
50+
return nil, err
51+
}
52+
if capval <= 0 {
53+
return nil, fmt.Errorf("max-size must be a positive number")
54+
}
55+
}
56+
var maxFiles = 1
57+
if maxFileString, ok := info.Config["max-file"]; ok {
58+
var err error
59+
maxFiles, err = strconv.Atoi(maxFileString)
60+
if err != nil {
61+
return nil, err
62+
}
63+
if maxFiles < 1 {
64+
return nil, fmt.Errorf("max-file cannot be less than 1")
65+
}
66+
}
67+
68+
var compress bool
69+
if compressString, ok := info.Config["compress"]; ok {
70+
var err error
71+
compress, err = strconv.ParseBool(compressString)
72+
if err != nil {
73+
return nil, err
74+
}
75+
if compress && (maxFiles == 1 || capval == -1) {
76+
return nil, fmt.Errorf("compress cannot be true when max-file is less than 2 or max-size is not set")
77+
}
78+
}
79+
80+
attrs, err := info.ExtraAttributes(nil)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
// no default template. only use a tag if the user asked for it
86+
tag, err := loggerutils.ParseLogTag(info, "")
87+
if err != nil {
88+
return nil, err
89+
}
90+
if tag != "" {
91+
attrs["tag"] = tag
92+
}
93+
94+
var extra json.RawMessage
95+
if len(attrs) > 0 {
96+
var err error
97+
extra, err = json.Marshal(attrs)
98+
if err != nil {
99+
return nil, err
100+
}
101+
}
102+
103+
// No read option from CRI. Just read from the JSON file.
104+
writer, err := loggerutils.NewLogFile(info.LogPath, capval, maxFiles, compress, nil, 0640, nil)
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
return &CRIFileLogger{
110+
writer: writer,
111+
tag: tag,
112+
extra: extra,
113+
}, nil
114+
}
115+
116+
// Log converts logger.Message to CRI format and serializes it to file.
117+
func (c *CRIFileLogger) Log(msg *logger.Message) error {
118+
buf := buffersPool.Get().(*bytes.Buffer)
119+
buf.Reset()
120+
defer buffersPool.Put(buf)
121+
122+
timestamp := msg.Timestamp
123+
err := marshalMessage(msg, c.extra, buf)
124+
logger.PutMessage(msg)
125+
126+
if err != nil {
127+
return err
128+
}
129+
130+
return c.writer.WriteLogEntry(timestamp, buf.Bytes())
131+
}
132+
133+
func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffer) error {
134+
logLine := msg.Line
135+
if msg.PLogMetaData == nil || (msg.PLogMetaData != nil && msg.PLogMetaData.Last) {
136+
logLine = append(msg.Line, '\n')
137+
}
138+
139+
_, err := buf.WriteString(fmt.Sprintf("%s %s %s %s\n", msg.Timestamp.Format(time.RFC3339), msg.Source, extra, logLine))
140+
return errors.Wrap(err, "error writing log message to buffer")
141+
}
142+
143+
// ValidateLogOpt looks for specific log options
144+
func ValidateLogOpt(cfg map[string]string) error {
145+
for key := range cfg {
146+
switch key {
147+
case "max-file":
148+
case "max-size":
149+
case "compress":
150+
case "labels":
151+
case "labels-regex":
152+
case "env":
153+
case "env-regex":
154+
case "tag":
155+
default:
156+
return fmt.Errorf("unknown log opt '%s' for json-file log driver", key)
157+
}
158+
}
159+
return nil
160+
}
161+
162+
// Close closes underlying file and signals all the readers
163+
// that the logs producer is gone.
164+
func (c *CRIFileLogger) Close() error {
165+
return c.writer.Close()
166+
}
167+
168+
// Name returns name of this logger.
169+
func (c *CRIFileLogger) Name() string {
170+
return Name
171+
}

0 commit comments

Comments
 (0)