Skip to content

Commit b44b4b9

Browse files
Made changes so that we get logs when a command starts no matter that it is finished or not
1 parent 5641f9a commit b44b4b9

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

cmd/minikube/cmd/root.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"path/filepath"
2424
"runtime"
2525
"strings"
26-
"time"
2726

2827
"github.com/spf13/cobra"
2928
"github.com/spf13/pflag"
@@ -78,8 +77,16 @@ var RootCmd = &cobra.Command{
7877
// Execute adds all child commands to the root command sets flags appropriately.
7978
// This is called by main.main(). It only needs to happen once to the rootCmd.
8079
func Execute() {
81-
defer audit.Log(time.Now())
82-
80+
auditID, err := audit.LogCommandStart()
81+
if err != nil{
82+
klog.Errorf("%v", err)
83+
}
84+
defer func(){
85+
err := audit.LogCommandEnd(auditID)
86+
if err != nil{
87+
klog.Errorf("%v", err)
88+
}
89+
}()
8390
// Check whether this is a windows binary (.exe) running inisde WSL.
8491
if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() {
8592
var found = false

pkg/minikube/audit/audit.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"os/user"
2222
"strings"
2323
"time"
24+
"fmt"
2425

26+
"github.com/google/uuid"
2527
"github.com/spf13/viper"
2628
"k8s.io/klog/v2"
2729
"k8s.io/minikube/pkg/minikube/config"
@@ -51,14 +53,34 @@ func args() string {
5153
}
5254

5355
// Log details about the executed command.
54-
func Log(startTime time.Time) {
56+
func LogCommandStart() (string, err) {
5557
if len(os.Args) < 2 || !shouldLog() {
56-
return
58+
return "", fmt.Errorf("This command should not be logged and len(os.Args) : %v, should be less than 2", len(os.Args))
5759
}
58-
r := newRow(os.Args[1], args(), userName(), version.GetVersion(), startTime, time.Now())
60+
id := uuid.New().String()
61+
r := newRow(os.Args[1], args(), userName(), version.GetVersion(), time.Now(), id)
5962
if err := appendToLog(r); err != nil {
60-
klog.Warning(err)
63+
return "", klog.Warning(err)
6164
}
65+
return r.id, nil
66+
}
67+
68+
func LogCommandEnd(id string) err{
69+
if currentLogFile == nil {
70+
if err := setLogFile(); err != nil {
71+
return fmt.Errorf("failed to set the log file: %v", err)
72+
}
73+
}
74+
auditLogs, err := logsToRows(currentLogFile)
75+
if err != nil{
76+
return fmt.Errorf("%v", err)
77+
}
78+
for _, v := range auditLogs {
79+
if v.id == id {
80+
v.endTime = time.Now()
81+
}
82+
}
83+
return nil
6284
}
6385

6486
// shouldLog returns if the command should be logged.

pkg/minikube/audit/row.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type row struct {
3737
startTime string
3838
user string
3939
version string
40+
id string
4041
Data map[string]string `json:"data"`
4142
}
4243

@@ -72,19 +73,19 @@ func (e *row) toMap() map[string]string {
7273
}
7374

7475
// newRow creates a new audit row.
75-
func newRow(command string, args string, user string, version string, startTime time.Time, endTime time.Time, profile ...string) *row {
76+
func newRow(command string, args string, user string, version string, startTime time.Time, id string, profile ...string) *row {
7677
p := viper.GetString(config.ProfileName)
7778
if len(profile) > 0 {
7879
p = profile[0]
7980
}
8081
return &row{
8182
args: args,
8283
command: command,
83-
endTime: endTime.Format(constants.TimeFormat),
8484
profile: p,
8585
startTime: startTime.Format(constants.TimeFormat),
8686
user: user,
8787
version: version,
88+
id: id,
8889
}
8990
}
9091

0 commit comments

Comments
 (0)