Skip to content

Commit

Permalink
Merge pull request #10465 from spowelljr/addLastStart
Browse files Browse the repository at this point in the history
Add last start logs to minikube logs output
  • Loading branch information
spowelljr authored Feb 24, 2021
2 parents 59cca69 + f0902d4 commit 34db47e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
24 changes: 24 additions & 0 deletions cmd/minikube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"

"github.com/spf13/pflag"
"k8s.io/klog/v2"

"k8s.io/minikube/pkg/minikube/localpath"
// Register drivers
_ "k8s.io/minikube/pkg/minikube/registry/drvs"

Expand Down Expand Up @@ -142,6 +144,7 @@ func setFlags() {
klog.Warningf("Unable to set default flag value for alsologtostderr: %v", err)
}
}
setLastStartFlags()

// make sure log_dir exists if log_file is not also set - the log_dir is mutually exclusive with the log_file option
// ref: https://github.com/kubernetes/klog/blob/52c62e3b70a9a46101f33ebaf0b100ec55099975/klog.go#L491
Expand All @@ -152,3 +155,24 @@ func setFlags() {
}
}
}

// setLastStartFlags sets the log_file flag to lastStart.txt if start command and user doesn't specify log_file or log_dir flags.
func setLastStartFlags() {
if os.Args[1] != "start" {
return
}
if pflag.CommandLine.Changed("log_file") || pflag.CommandLine.Changed("log_dir") {
return
}
fp := localpath.LastStartLog()
dp := filepath.Dir(fp)
if err := os.MkdirAll(dp, 0755); err != nil {
klog.Warningf("Unable to make log dir %s: %v", dp, err)
}
if _, err := os.Create(fp); err != nil {
klog.Warningf("Unable to create/truncate file %s: %v", fp, err)
}
if err := pflag.Set("log_file", fp); err != nil {
klog.Warningf("Unable to set default flag value for log_file: %v", err)
}
}
5 changes: 5 additions & 0 deletions pkg/minikube/localpath/localpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func AuditLog() string {
return filepath.Join(MiniPath(), "logs", "audit.json")
}

// LastStartLog returns the path to the last start log.
func LastStartLog() string {
return filepath.Join(MiniPath(), "logs", "lastStart.txt")
}

// ClientCert returns client certificate path, used by kubeconfig
func ClientCert(name string) string {
new := filepath.Join(Profile(name), "client.crt")
Expand Down
33 changes: 32 additions & 1 deletion pkg/minikube/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/style"
)
Expand Down Expand Up @@ -190,10 +191,15 @@ func Output(r cruntime.Manager, bs bootstrapper.Bootstrapper, cfg config.Cluster
}

if err := outputAudit(lines); err != nil {
klog.Error(err)
klog.Errorf("failed to output audit logs: %v", err)
failed = append(failed, "audit")
}

if err := outputLastStart(); err != nil {
klog.Errorf("failed to output last start logs: %v", err)
failed = append(failed, "last start")
}

if len(failed) > 0 {
return fmt.Errorf("unable to fetch logs for: %s", strings.Join(failed, ", "))
}
Expand All @@ -212,6 +218,31 @@ func outputAudit(lines int) error {
return nil
}

// outputLastStart outputs the last start logs.
func outputLastStart() error {
out.Step(style.Empty, "")
out.Step(style.Empty, "==> Last Start <==")
fp := localpath.LastStartLog()
f, err := os.Open(fp)
if os.IsNotExist(err) {
msg := fmt.Sprintf("Last start log file not found at %s", fp)
out.Step(style.Empty, msg)
return nil
}
if err != nil {
return fmt.Errorf("failed to open file %s: %v", fp, err)
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
out.Step(style.Empty, s.Text())
}
if err := s.Err(); err != nil {
return fmt.Errorf("failed to read file %s: %v", fp, err)
}
return nil
}

// logCommands returns a list of commands that would be run to receive the anticipated logs
func logCommands(r cruntime.Manager, bs bootstrapper.Bootstrapper, cfg config.ClusterConfig, length int, follow bool) map[string]string {
cmds := bs.LogCommands(cfg, bootstrapper.LogOptions{Lines: length, Follow: follow})
Expand Down
2 changes: 1 addition & 1 deletion test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ func validateLogsCmd(ctx context.Context, t *testing.T, profile string) {
if err != nil {
t.Errorf("%s failed: %v", rr.Command(), err)
}
expectedWords := []string{"apiserver", "Linux", "kubelet", "Audit"}
expectedWords := []string{"apiserver", "Linux", "kubelet", "Audit", "Last Start"}
switch ContainerRuntime() {
case "docker":
expectedWords = append(expectedWords, "Docker")
Expand Down

0 comments on commit 34db47e

Please sign in to comment.