@@ -25,9 +25,11 @@ import (
2525	"syscall" 
2626
2727	"github.com/containerd/containerd" 
28+ 	"github.com/containerd/nerdctl/pkg/api/types/cri" 
2829	"github.com/containerd/nerdctl/pkg/clientutil" 
2930	"github.com/containerd/nerdctl/pkg/idutil/containerwalker" 
3031	"github.com/containerd/nerdctl/pkg/labels" 
32+ 	"github.com/containerd/nerdctl/pkg/labels/k8slabels" 
3133	"github.com/containerd/nerdctl/pkg/logging" 
3234	"github.com/sirupsen/logrus" 
3335	"github.com/spf13/cobra" 
@@ -62,8 +64,8 @@ func logsAction(cmd *cobra.Command, args []string) error {
6264	}
6365
6466	switch  globalOptions .Namespace  {
65- 	case  "moby" ,  "k8s.io" :
66- 		logrus .Warn ("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d`" )
67+ 	case  "moby" :
68+ 		logrus .Warn ("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d` or CRI " )
6769	}
6870	client , ctx , cancel , err  :=  clientutil .NewClient (cmd .Context (), globalOptions .Namespace , globalOptions .Address )
6971	if  err  !=  nil  {
@@ -113,6 +115,12 @@ func logsAction(cmd *cobra.Command, args []string) error {
113115			if  err  !=  nil  {
114116				return  err 
115117			}
118+ 
119+ 			logPath , err  :=  getLogPath (ctx , found .Container )
120+ 			if  err  !=  nil  {
121+ 				return  err 
122+ 			}
123+ 
116124			task , err  :=  found .Container .Task (ctx , nil )
117125			if  err  !=  nil  {
118126				return  err 
@@ -143,13 +151,14 @@ func logsAction(cmd *cobra.Command, args []string) error {
143151				ContainerID :       found .Container .ID (),
144152				Namespace :         l [labels .Namespace ],
145153				DatastoreRootPath : dataStore ,
154+ 				LogPath :           logPath ,
146155				Follow :            follow ,
147156				Timestamps :        timestamps ,
148157				Tail :              tail ,
149158				Since :             since ,
150159				Until :             until ,
151160			}
152- 			logViewer , err  :=  logging .InitContainerLogViewer (logViewOpts , stopChannel )
161+ 			logViewer , err  :=  logging .InitContainerLogViewer (l ,  logViewOpts , stopChannel )
153162			if  err  !=  nil  {
154163				return  err 
155164			}
@@ -167,6 +176,23 @@ func logsAction(cmd *cobra.Command, args []string) error {
167176	return  nil 
168177}
169178
179+ func  getLogPath (ctx  context.Context , container  containerd.Container ) (string , error ) {
180+ 	extensions , err  :=  container .Extensions (ctx )
181+ 	if  err  !=  nil  {
182+ 		return  "" , fmt .Errorf ("get extensions for container %s,failed: %#v" , container .ID (), err )
183+ 	}
184+ 	metaData  :=  extensions [k8slabels .ContainerMetadataExtension ]
185+ 	var  meta  cri.ContainerMetadata 
186+ 	if  metaData  !=  nil  {
187+ 		err  =  meta .UnmarshalJSON (metaData .GetValue ())
188+ 		if  err  !=  nil  {
189+ 			return  "" , fmt .Errorf ("unmarshal extensions for container %s,failed: %#v" , container .ID (), err )
190+ 		}
191+ 	}
192+ 
193+ 	return  meta .LogPath , nil 
194+ }
195+ 
170196func  logsShellComplete (cmd  * cobra.Command , args  []string , toComplete  string ) ([]string , cobra.ShellCompDirective ) {
171197	// show container names (TODO: only show containers with logs) 
172198	return  shellCompleteContainerNames (cmd , nil )
0 commit comments