@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"os"
23
+ "strings"
23
24
"time"
24
25
25
26
timetypes "github.com/docker/docker/api/types/time"
@@ -40,6 +41,10 @@ var logsCommand = cli.Command{
40
41
Name : "follow, f" ,
41
42
Usage : "Follow log output" ,
42
43
},
44
+ cli.BoolFlag {
45
+ Name : "previous, p" ,
46
+ Usage : "Print the logs for the previous instance of the container in a pod if it exists" ,
47
+ },
43
48
cli.Int64Flag {
44
49
Name : "tail" ,
45
50
Value : - 1 ,
@@ -77,6 +82,7 @@ var logsCommand = cli.Command{
77
82
return err
78
83
}
79
84
timestamp := ctx .Bool ("timestamps" )
85
+ previous := ctx .Bool ("previous" )
80
86
logOptions := logs .NewLogOptions (& v1.PodLogOptions {
81
87
Follow : ctx .Bool ("follow" ),
82
88
TailLines : & tailLines ,
@@ -92,6 +98,14 @@ var logsCommand = cli.Command{
92
98
if logPath == "" {
93
99
return fmt .Errorf ("The container has not set log path" )
94
100
}
101
+ if previous {
102
+ containerAttempt := status .GetMetadata ().Attempt
103
+ if containerAttempt == uint32 (0 ) {
104
+ return fmt .Errorf ("Previous terminated container %s not found" , status .GetMetadata ().Name )
105
+ }
106
+ logPath = fmt .Sprintf ("%s%s%s" , logPath [:strings .LastIndex (logPath , "/" )+ 1 ], fmt .Sprint (containerAttempt - 1 ),
107
+ logPath [strings .LastIndex (logPath , "." ):])
108
+ }
95
109
return logs .ReadLogs (context .Background (), logPath , status .GetId (), logOptions , runtimeService , os .Stdout , os .Stderr )
96
110
},
97
111
}
0 commit comments