Skip to content

Commit c8c2a66

Browse files
committed
add support logs for namespace k8s.io
1 parent 40d341d commit c8c2a66

File tree

8 files changed

+546
-15
lines changed

8 files changed

+546
-15
lines changed

cmd/nerdctl/logs.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"syscall"
2626

2727
"github.com/containerd/containerd"
28+
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
2829
"github.com/containerd/nerdctl/pkg/clientutil"
2930
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
3031
"github.com/containerd/nerdctl/pkg/labels"
@@ -33,6 +34,10 @@ import (
3334
"github.com/spf13/cobra"
3435
)
3536

37+
const (
38+
containerMetadataExtension = "io.cri-containerd.container.metadata"
39+
)
40+
3641
func newLogsCommand() *cobra.Command {
3742
var logsCommand = &cobra.Command{
3843
Use: "logs [flags] CONTAINER",
@@ -62,8 +67,8 @@ func logsAction(cmd *cobra.Command, args []string) error {
6267
}
6368

6469
switch globalOptions.Namespace {
65-
case "moby", "k8s.io":
66-
logrus.Warn("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d`")
70+
case "moby":
71+
logrus.Warn("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d` and CRI")
6772
}
6873
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
6974
if err != nil {
@@ -113,6 +118,12 @@ func logsAction(cmd *cobra.Command, args []string) error {
113118
if err != nil {
114119
return err
115120
}
121+
122+
logPath, err := getLogPath(ctx, found.Container)
123+
if err != nil {
124+
return err
125+
}
126+
116127
task, err := found.Container.Task(ctx, nil)
117128
if err != nil {
118129
return err
@@ -143,13 +154,14 @@ func logsAction(cmd *cobra.Command, args []string) error {
143154
ContainerID: found.Container.ID(),
144155
Namespace: l[labels.Namespace],
145156
DatastoreRootPath: dataStore,
157+
LogPath: logPath,
146158
Follow: follow,
147159
Timestamps: timestamps,
148160
Tail: tail,
149161
Since: since,
150162
Until: until,
151163
}
152-
logViewer, err := logging.InitContainerLogViewer(logViewOpts, stopChannel)
164+
logViewer, err := logging.InitContainerLogViewer(l, logViewOpts, stopChannel)
153165
if err != nil {
154166
return err
155167
}
@@ -167,6 +179,23 @@ func logsAction(cmd *cobra.Command, args []string) error {
167179
return nil
168180
}
169181

182+
func getLogPath(ctx context.Context, container containerd.Container) (string, error) {
183+
extention, err := container.Extensions(ctx)
184+
if err != nil {
185+
return "", fmt.Errorf("get extention for container %s,failed: %#v", container.ID(), err)
186+
}
187+
metaData := extention[containerMetadataExtension]
188+
var meta containerstore.Metadata
189+
if metaData != nil {
190+
err = meta.UnmarshalJSON(metaData.GetValue())
191+
if err != nil {
192+
return "", fmt.Errorf("unmarshal extention for container %s,failed: %#v", container.ID(), err)
193+
}
194+
}
195+
196+
return meta.LogPath, nil
197+
}
198+
170199
func logsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
171200
// show container names (TODO: only show containers with logs)
172201
return shellCompleteContainerNames(cmd, nil)

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
github.com/fahedouch/go-logrotate v0.1.3
3030
github.com/fatih/color v1.13.0
3131
github.com/fluent/fluent-logger-golang v1.9.0
32+
github.com/fsnotify/fsnotify v1.6.0
3233
github.com/hashicorp/go-multierror v1.1.1
3334
github.com/ipfs/go-cid v0.3.2
3435
github.com/mattn/go-isatty v0.0.17
@@ -57,10 +58,21 @@ require (
5758
golang.org/x/text v0.6.0
5859
gopkg.in/yaml.v3 v3.0.1
5960
gotest.tools/v3 v3.4.0
61+
k8s.io/cri-api v0.26.0-beta.0
62+
k8s.io/klog/v2 v2.80.1
6063
)
6164

6265
require (
66+
github.com/beorn7/perks v1.0.1 // indirect
67+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
6368
github.com/containerd/cgroups/v3 v3.0.0-20221112182753-e8802a182774 // indirect
69+
github.com/docker/go-metrics v0.0.1 // indirect
70+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
71+
github.com/prometheus/client_golang v1.14.0 // indirect
72+
github.com/prometheus/client_model v0.3.0 // indirect
73+
github.com/prometheus/common v0.37.0 // indirect
74+
github.com/prometheus/procfs v0.8.0 // indirect
75+
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
6476
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
6577
google.golang.org/genproto v0.0.0-20221206210731-b1a01be3a5f6 // indirect
6678
)

go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiU
136136
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
137137
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
138138
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
139+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
139140
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
140141
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
141142
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
@@ -158,6 +159,7 @@ github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6
158159
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
159160
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
160161
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
162+
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
161163
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
162164
github.com/checkpoint-restore/go-criu/v4 v4.1.0/go.mod h1:xUQBLp4RLc5zJtWY++yjOoMoB5lihDt7fai+75m+rGw=
163165
github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M=
@@ -392,6 +394,7 @@ github.com/docker/go-events v0.0.0-20170721190031-9461782956ad/go.mod h1:Uw6Uezg
392394
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c h1:+pKlWGMw7gf6bQ+oDZB4KHQFypsfjYlq/C4rfL7D3g8=
393395
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA=
394396
github.com/docker/go-metrics v0.0.0-20180209012529-399ea8c73916/go.mod h1:/u0gXw0Gay3ceNrsHubL3BtdOL2fHf93USgMTe0W5dI=
397+
github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQV8=
395398
github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw=
396399
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
397400
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
@@ -434,6 +437,7 @@ github.com/frankban/quicktest v1.14.2/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUork
434437
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
435438
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
436439
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
440+
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
437441
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
438442
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
439443
github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
@@ -736,6 +740,8 @@ github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebG
736740
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
737741
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
738742
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
743+
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
744+
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
739745
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
740746
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
741747
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
@@ -907,12 +913,14 @@ github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP
907913
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
908914
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
909915
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
916+
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
910917
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
911918
github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
912919
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
913920
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
914921
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
915922
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
923+
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
916924
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
917925
github.com/prometheus/common v0.0.0-20180110214958-89604d197083/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
918926
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
@@ -923,6 +931,7 @@ github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB8
923931
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
924932
github.com/prometheus/common v0.30.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
925933
github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
934+
github.com/prometheus/common v0.37.0 h1:ccBbHCgIiT9uSoFY0vX8H3zsNR5eLt17/RQLUvn8pXE=
926935
github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA=
927936
github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
928937
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
@@ -1025,6 +1034,8 @@ github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG
10251034
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
10261035
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
10271036
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
1037+
github.com/tchap/go-patricia/v2 v2.3.1 h1:6rQp39lgIYZ+MHmdEq4xzuk1t7OdC35z/xm0BGhTkes=
1038+
github.com/tchap/go-patricia/v2 v2.3.1/go.mod h1:VZRHKAb53DLaG+nA9EaYYiaEx6YztwDlLElMsnSHD4k=
10281039
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
10291040
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
10301041
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
@@ -1427,6 +1438,7 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc
14271438
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14281439
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14291440
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1441+
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14301442
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14311443
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14321444
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
@@ -1776,6 +1788,8 @@ k8s.io/cri-api v0.20.6/go.mod h1:ew44AjNXwyn1s0U4xCKGodU7J1HzBeZ1MpGrpa5r8Yc=
17761788
k8s.io/cri-api v0.23.1/go.mod h1:REJE3PSU0h/LOV1APBrupxrEJqnoxZC8KWzkBUHwrK4=
17771789
k8s.io/cri-api v0.25.0/go.mod h1:J1rAyQkSJ2Q6I+aBMOVgg2/cbbebso6FNa0UagiR0kc=
17781790
k8s.io/cri-api v0.26.0-alpha.3/go.mod h1:E49tenyB7esgfIguEd7+g9qYhHOr9peyyBcSaeH6Gxw=
1791+
k8s.io/cri-api v0.26.0-beta.0 h1:mVt2/80VZy2dRN4mpVYJbPB9L/Zt/EvVev++zPTV9Tw=
1792+
k8s.io/cri-api v0.26.0-beta.0/go.mod h1:E49tenyB7esgfIguEd7+g9qYhHOr9peyyBcSaeH6Gxw=
17791793
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
17801794
k8s.io/gengo v0.0.0-20200428234225-8167cfdcfc14/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
17811795
k8s.io/gengo v0.0.0-20201113003025-83324d819ded/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
@@ -1786,6 +1800,8 @@ k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
17861800
k8s.io/klog/v2 v2.9.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
17871801
k8s.io/klog/v2 v2.30.0/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
17881802
k8s.io/klog/v2 v2.70.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
1803+
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
1804+
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
17891805
k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6/go.mod h1:UuqjUnNftUyPE5H64/qeyjQoUZhGpeFDVdxjTeEVN2o=
17901806
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
17911807
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=

pkg/containerutil/containerutil.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ func ContainerStatus(ctx context.Context, c containerd.Container) (containerd.St
6868

6969
return task.Status(ctx)
7070
}
71+
72+
func ContainerCreatedByCRI(ctx context.Context, c containerd.Container) (bool, error) {
73+
return false, nil
74+
}

pkg/logging/log_viewer.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import (
2626
"github.com/sirupsen/logrus"
2727
)
2828

29+
const (
30+
containerType = "io.cri-containerd.kind"
31+
)
32+
2933
// Type alias for functions which write out logs to the provided stdout/stderr Writers.
3034
// Depending on the provided `LogViewOptions.Follow` option, the function may block
3135
// indefinitely until something is sent through the `stopChannel`.
@@ -44,6 +48,7 @@ func RegisterLogViewer(driverName string, lvfn LogViewerFunc) {
4448
func init() {
4549
RegisterLogViewer("json-file", viewLogsJSONFile)
4650
RegisterLogViewer("journald", viewLogsJournald)
51+
RegisterLogViewer("text", viewLogsTextFile)
4752
}
4853

4954
// Returns a LogViewerFunc for the provided logging driver name.
@@ -64,6 +69,8 @@ type LogViewOptions struct {
6469
// Absolute path to the nerdctl datastore's root.
6570
DatastoreRootPath string
6671

72+
LogPath string
73+
6774
// Whether or not to follow the output of the container logs.
6875
Follow bool
6976

@@ -98,8 +105,8 @@ func (lvo *LogViewOptions) Validate() error {
98105
// Implements functionality for loading the logging configuration and
99106
// fetching/outputting container logs based on its internal LogViewOptions.
100107
type ContainerLogViewer struct {
101-
// Logging configuration.
102-
loggingConfig LogConfig
108+
// Logging logDriver.
109+
logDriver string
103110

104111
// Log viewing options and filters.
105112
logViewingOptions LogViewOptions
@@ -110,17 +117,24 @@ type ContainerLogViewer struct {
110117

111118
// Validates the given LogViewOptions, loads the logging config for the
112119
// given container and returns a ContainerLogViewer.
113-
func InitContainerLogViewer(lvopts LogViewOptions, stopChannel chan os.Signal) (*ContainerLogViewer, error) {
114-
if err := lvopts.Validate(); err != nil {
115-
return nil, fmt.Errorf("invalid LogViewOptions provided (%#v): %s", lvopts, err)
116-
}
120+
func InitContainerLogViewer(containerLabels map[string]string, lvopts LogViewOptions, stopChannel chan os.Signal) (*ContainerLogViewer, error) {
121+
var logDriver string
122+
if _, ok := containerLabels[containerType]; ok {
123+
logDriver = "text"
124+
} else {
125+
if err := lvopts.Validate(); err != nil {
126+
return nil, fmt.Errorf("invalid LogViewOptions provided (%#v): %s", lvopts, err)
127+
}
117128

118-
lcfg, err := LoadLogConfig(lvopts.DatastoreRootPath, lvopts.Namespace, lvopts.ContainerID)
119-
if err != nil {
120-
return nil, fmt.Errorf("failed to load logging config: %s", err)
129+
lcfg, err := LoadLogConfig(lvopts.DatastoreRootPath, lvopts.Namespace, lvopts.ContainerID)
130+
if err != nil {
131+
return nil, fmt.Errorf("failed to load logging config: %s", err)
132+
}
133+
logDriver = lcfg.Driver
121134
}
135+
122136
lv := &ContainerLogViewer{
123-
loggingConfig: lcfg,
137+
logDriver: logDriver,
124138
logViewingOptions: lvopts,
125139
stopChannel: stopChannel,
126140
}
@@ -130,7 +144,7 @@ func InitContainerLogViewer(lvopts LogViewOptions, stopChannel chan os.Signal) (
130144

131145
// Prints all logs for this LogViewer's containers to the provided io.Writers.
132146
func (lv *ContainerLogViewer) PrintLogsTo(stdout, stderr io.Writer) error {
133-
viewerFunc, err := getLogViewer(lv.loggingConfig.Driver)
147+
viewerFunc, err := getLogViewer(lv.logDriver)
134148
if err != nil {
135149
return err
136150
}

pkg/logging/logging.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"path/filepath"
2828
"sort"
2929
"sync"
30+
"time"
3031

3132
"github.com/containerd/containerd/errdefs"
3233
"github.com/containerd/containerd/runtime/v2/logging"
@@ -40,6 +41,9 @@ const (
4041
MaxSize = "max-size"
4142
MaxFile = "max-file"
4243
Tag = "tag"
44+
45+
// logForceCheckPeriod is the period to check for a new read
46+
logForceCheckPeriod = 1 * time.Second
4347
)
4448

4549
type Driver interface {
@@ -86,7 +90,7 @@ func GetDriver(name string, opts map[string]string) (Driver, error) {
8690

8791
func init() {
8892
RegisterDriver("json-file", func(opts map[string]string) (Driver, error) {
89-
return &JSONLogger{Opts: opts}, nil
93+
return &TextLogger{Opts: opts}, nil
9094
}, JSONFileLogOptsValidate)
9195
RegisterDriver("journald", func(opts map[string]string) (Driver, error) {
9296
return &JournaldLogger{Opts: opts}, nil
@@ -97,6 +101,9 @@ func init() {
97101
RegisterDriver("syslog", func(opts map[string]string) (Driver, error) {
98102
return &SyslogLogger{Opts: opts}, nil
99103
}, SyslogOptsValidate)
104+
RegisterDriver("text", func(opts map[string]string) (Driver, error) {
105+
return &SyslogLogger{Opts: opts}, nil
106+
}, SyslogOptsValidate)
100107
}
101108

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

pkg/logging/text/text.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package text

0 commit comments

Comments
 (0)