Skip to content

Commit 34db47e

Browse files
authored
Merge pull request #10465 from spowelljr/addLastStart
Add last start logs to minikube logs output
2 parents 59cca69 + f0902d4 commit 34db47e

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

cmd/minikube/main.go

+24
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import (
2222
"fmt"
2323
"log"
2424
"os"
25+
"path/filepath"
2526
"regexp"
2627
"strconv"
2728

2829
"github.com/spf13/pflag"
2930
"k8s.io/klog/v2"
3031

32+
"k8s.io/minikube/pkg/minikube/localpath"
3133
// Register drivers
3234
_ "k8s.io/minikube/pkg/minikube/registry/drvs"
3335

@@ -142,6 +144,7 @@ func setFlags() {
142144
klog.Warningf("Unable to set default flag value for alsologtostderr: %v", err)
143145
}
144146
}
147+
setLastStartFlags()
145148

146149
// make sure log_dir exists if log_file is not also set - the log_dir is mutually exclusive with the log_file option
147150
// ref: https://github.com/kubernetes/klog/blob/52c62e3b70a9a46101f33ebaf0b100ec55099975/klog.go#L491
@@ -152,3 +155,24 @@ func setFlags() {
152155
}
153156
}
154157
}
158+
159+
// setLastStartFlags sets the log_file flag to lastStart.txt if start command and user doesn't specify log_file or log_dir flags.
160+
func setLastStartFlags() {
161+
if os.Args[1] != "start" {
162+
return
163+
}
164+
if pflag.CommandLine.Changed("log_file") || pflag.CommandLine.Changed("log_dir") {
165+
return
166+
}
167+
fp := localpath.LastStartLog()
168+
dp := filepath.Dir(fp)
169+
if err := os.MkdirAll(dp, 0755); err != nil {
170+
klog.Warningf("Unable to make log dir %s: %v", dp, err)
171+
}
172+
if _, err := os.Create(fp); err != nil {
173+
klog.Warningf("Unable to create/truncate file %s: %v", fp, err)
174+
}
175+
if err := pflag.Set("log_file", fp); err != nil {
176+
klog.Warningf("Unable to set default flag value for log_file: %v", err)
177+
}
178+
}

pkg/minikube/localpath/localpath.go

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func AuditLog() string {
7474
return filepath.Join(MiniPath(), "logs", "audit.json")
7575
}
7676

77+
// LastStartLog returns the path to the last start log.
78+
func LastStartLog() string {
79+
return filepath.Join(MiniPath(), "logs", "lastStart.txt")
80+
}
81+
7782
// ClientCert returns client certificate path, used by kubeconfig
7883
func ClientCert(name string) string {
7984
new := filepath.Join(Profile(name), "client.crt")

pkg/minikube/logs/logs.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"k8s.io/minikube/pkg/minikube/command"
3535
"k8s.io/minikube/pkg/minikube/config"
3636
"k8s.io/minikube/pkg/minikube/cruntime"
37+
"k8s.io/minikube/pkg/minikube/localpath"
3738
"k8s.io/minikube/pkg/minikube/out"
3839
"k8s.io/minikube/pkg/minikube/style"
3940
)
@@ -190,10 +191,15 @@ func Output(r cruntime.Manager, bs bootstrapper.Bootstrapper, cfg config.Cluster
190191
}
191192

192193
if err := outputAudit(lines); err != nil {
193-
klog.Error(err)
194+
klog.Errorf("failed to output audit logs: %v", err)
194195
failed = append(failed, "audit")
195196
}
196197

198+
if err := outputLastStart(); err != nil {
199+
klog.Errorf("failed to output last start logs: %v", err)
200+
failed = append(failed, "last start")
201+
}
202+
197203
if len(failed) > 0 {
198204
return fmt.Errorf("unable to fetch logs for: %s", strings.Join(failed, ", "))
199205
}
@@ -212,6 +218,31 @@ func outputAudit(lines int) error {
212218
return nil
213219
}
214220

221+
// outputLastStart outputs the last start logs.
222+
func outputLastStart() error {
223+
out.Step(style.Empty, "")
224+
out.Step(style.Empty, "==> Last Start <==")
225+
fp := localpath.LastStartLog()
226+
f, err := os.Open(fp)
227+
if os.IsNotExist(err) {
228+
msg := fmt.Sprintf("Last start log file not found at %s", fp)
229+
out.Step(style.Empty, msg)
230+
return nil
231+
}
232+
if err != nil {
233+
return fmt.Errorf("failed to open file %s: %v", fp, err)
234+
}
235+
defer f.Close()
236+
s := bufio.NewScanner(f)
237+
for s.Scan() {
238+
out.Step(style.Empty, s.Text())
239+
}
240+
if err := s.Err(); err != nil {
241+
return fmt.Errorf("failed to read file %s: %v", fp, err)
242+
}
243+
return nil
244+
}
245+
215246
// logCommands returns a list of commands that would be run to receive the anticipated logs
216247
func logCommands(r cruntime.Manager, bs bootstrapper.Bootstrapper, cfg config.ClusterConfig, length int, follow bool) map[string]string {
217248
cmds := bs.LogCommands(cfg, bootstrapper.LogOptions{Lines: length, Follow: follow})

test/integration/functional_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ func validateLogsCmd(ctx context.Context, t *testing.T, profile string) {
794794
if err != nil {
795795
t.Errorf("%s failed: %v", rr.Command(), err)
796796
}
797-
expectedWords := []string{"apiserver", "Linux", "kubelet", "Audit"}
797+
expectedWords := []string{"apiserver", "Linux", "kubelet", "Audit", "Last Start"}
798798
switch ContainerRuntime() {
799799
case "docker":
800800
expectedWords = append(expectedWords, "Docker")

0 commit comments

Comments
 (0)