Repository: kubernetes/minikube
Pull request #14353 (2022-06-20 07:39:52)

Just a little misspelling mistakes
<!-- 🎉 Thank you for contributing to minikube! 🎉 Here are some hints to get your PR merged faster:

1. Your PR title will be included in the release notes, choose it carefully
2. If the PR fixes an issue, add "fixes #<issue number>" to the description.
3. If the PR is a user interface change, please include a "before" and "after" example.
4. If the PR is a large design change, please include an enhancement proposal:
https://github.com/kubernetes/minikube/tree/master/enhancements
-->
occurring not "occuring", a little misspelling mistakes


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c85acc2bb..51ef9ce53 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,7 +9,7 @@ QEMU driver enhancements:
 Features:
 * Add configure option to registry-aliases addon [#13912](https://github.com/kubernetes/minikube/pull/13912)
 
-For a more detailed changelog, including changes occuring in pre-release versions, see [CHANGELOG.md](https://github.com/kubernetes/minikube/blob/master/CHANGELOG.md).
+For a more detailed changelog, including changes occurring in pre-release versions, see [CHANGELOG.md](https://github.com/kubernetes/minikube/blob/master/CHANGELOG.md).
 
 Thank you to our contributors for this release!
 
@@ -75,7 +75,7 @@ Version Upgrades:
 * ISO: Upgrade Podman from 2.2.1 to 3.4.2 [#13126](https://github.com/kubernetes/minikube/pull/13126)
 * ISO: Add packaging for crun [#11679](https://github.com/kubernetes/minikube/pull/11679)
 
-For a more detailed changelog, including changes occuring in pre-release versions, see [CHANGELOG.md](https://github.com/kubernetes/minikube/blob/master/CHANGELOG.md).
+For a more detailed changelog, including changes occurring in pre-release versions, see [CHANGELOG.md](https://github.com/kubernetes/minikube/blob/master/CHANGELOG.md).
 
 Thank you to our contributors for this release!
 
@@ -161,7 +161,7 @@ Version Upgrades:
 * ISO: Add packaging for cri-dockerd [#13191](https://github.com/kubernetes/minikube/pull/13191)
 
 
-For a more detailed changelog, including changes occuring in pre-release versions, see [CHANGELOG.md](https://github.com/kubernetes/minikube/blob/master/CHANGELOG.md).
+For a more detailed changelog, including changes occurring in pre-release versions, see [CHANGELOG.md](https://github.com/kubernetes/minikube/blob/master/CHANGELOG.md).
 
 Thank you to our contributors for this release!
 
diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go
index 6b7b13d08..870518cdd 100644
--- a/cmd/minikube/cmd/root.go
+++ b/cmd/minikube/cmd/root.go
@@ -23,6 +23,7 @@ import (
 	"path/filepath"
 	"runtime"
 	"strings"
+	"time"
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/pflag"
@@ -83,16 +84,8 @@ var RootCmd = &cobra.Command{
 // Execute adds all child commands to the root command sets flags appropriately.
 // This is called by main.main(). It only needs to happen once to the rootCmd.
 func Execute() {
-	auditID, err := audit.LogCommandStart()
-	if err != nil {
-		klog.Errorf("failed to log command start to audit: %v", err)
-	}
-	defer func() {
-		err := audit.LogCommandEnd(auditID)
-		if err != nil {
-			klog.Errorf("failed to log command end to audit: %v", err)
-		}
-	}()
+	defer audit.Log(time.Now())
+
 	// Check whether this is a windows binary (.exe) running inisde WSL.
 	if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() {
 		var found = false
diff --git a/hack/jenkins/cron/cleanup_and_reboot_Linux.sh b/hack/jenkins/cron/cleanup_and_reboot_Linux.sh
index 401924350..34c148882 100755
--- a/hack/jenkins/cron/cleanup_and_reboot_Linux.sh
+++ b/hack/jenkins/cron/cleanup_and_reboot_Linux.sh
@@ -111,6 +111,9 @@ function cleanup() {
 	done
 	echo -e "\nafter the cleanup:"
 	overview
+
+	# clean up /tmp
+	find /tmp -name . -o -prune -exec rm -rf -- {} + >/dev/null 2>&1 || true
 }
 
 # Give 15m for Linux-specific cleanup
diff --git a/pkg/minikube/audit/audit.go b/pkg/minikube/audit/audit.go
index e119429da..eb4b0ead7 100644
--- a/pkg/minikube/audit/audit.go
+++ b/pkg/minikube/audit/audit.go
@@ -17,20 +17,15 @@ limitations under the License.
 package audit
 
 import (
-	"bufio"
-	"encoding/json"
-	"fmt"
 	"os"
 	"os/user"
 	"strings"
 	"time"
 
-	"github.com/google/uuid"
 	"github.com/spf13/pflag"
 	"github.com/spf13/viper"
+	"k8s.io/klog/v2"
 	"k8s.io/minikube/pkg/minikube/config"
-	"k8s.io/minikube/pkg/minikube/constants"
-	"k8s.io/minikube/pkg/minikube/localpath"
 	"k8s.io/minikube/pkg/version"
 )
 
@@ -57,67 +52,14 @@ func args() string {
 }
 
 // Log details about the executed command.
-func LogCommandStart() (string, error) {
-	if len(os.Args) < 2 || !shouldLog() {
-		return "", nil
+func Log(startTime time.Time) {
+	if !shouldLog() {
+		return
 	}
-	id := uuid.New().String()
-	r := newRow(pflag.Arg(0), args(), userName(), version.GetVersion(), time.Now(), id)
+	r := newRow(pflag.Arg(0), args(), userName(), version.GetVersion(), startTime, time.Now())
 	if err := appendToLog(r); err != nil {
-		return "", err
+		klog.Warning(err)
 	}
-	return r.id, nil
-}
-
-func LogCommandEnd(id string) error {
-	if id == "" {
-		return nil
-	}
-	if err := openAuditLog(); err != nil {
-		return err
-	}
-	defer closeAuditLog()
-	var logs []string
-	s := bufio.NewScanner(currentLogFile)
-	for s.Scan() {
-		logs = append(logs, s.Text())
-	}
-	if err := s.Err(); err != nil {
-		return fmt.Errorf("failed to read from audit file: %v", err)
-	}
-	closeAuditLog()
-	rowSlice, err := logsToRows(logs)
-	if err != nil {
-		return fmt.Errorf("failed to convert logs to rows: %v", err)
-	}
-	auditContents := ""
-	var entriesNeedsToUpdate int
-	for _, v := range rowSlice {
-		if v.id == id {
-			v.endTime = time.Now().Format(constants.TimeFormat)
-			v.Data = v.toMap()
-			entriesNeedsToUpdate++
-		}
-		auditLog, err := json.Marshal(v)
-		if err != nil {
-			return err
-		}
-		auditContents += string(auditLog) + "\n"
-	}
-	if entriesNeedsToUpdate == 0 {
-		return fmt.Errorf("failed to find a log row with id equals to %v", id)
-	}
-	// have to truncate the audit log while closed as Windows can't truncate an open file
-	if err := os.Truncate(localpath.AuditLog(), 0); err != nil {
-		return fmt.Errorf("failed to truncate audit log: %v", err)
-	}
-	if err := openAuditLog(); err != nil {
-		return err
-	}
-	if _, err = currentLogFile.Write([]byte(auditContents)); err != nil {
-		return fmt.Errorf("failed to write to audit log: %v", err)
-	}
-	return nil
 }
 
 // shouldLog returns if the command should be logged.
@@ -132,7 +74,7 @@ func shouldLog() bool {
 	}
 
 	// commands that should not be logged.
-	no := []string{"status", "version", "logs", "generate-docs"}
+	no := []string{"status", "version"}
 	a := pflag.Arg(0)
 	for _, c := range no {
 		if a == c {
diff --git a/pkg/minikube/audit/logFile.go b/pkg/minikube/audit/logFile.go
index 437114aa8..1762070f1 100644
--- a/pkg/minikube/audit/logFile.go
+++ b/pkg/minikube/audit/logFile.go
@@ -20,7 +20,6 @@ import (
 	"fmt"
 	"os"
 
-	"k8s.io/klog/v2"
 	"k8s.io/minikube/pkg/minikube/localpath"
 	"k8s.io/minikube/pkg/minikube/out/register"
 )
@@ -28,35 +27,29 @@ import (
 // currentLogFile the file that's used to store audit logs
 var currentLogFile *os.File
 
-// openAuditLog opens the audit log file or creates it if it doesn't exist.
-func openAuditLog() error {
+// setLogFile sets the logPath and creates the log file if it doesn't exist.
+func setLogFile() error {
 	lp := localpath.AuditLog()
 	f, err := os.OpenFile(lp, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
 	if err != nil {
-		return fmt.Errorf("failed to open the audit log: %v", err)
+		return fmt.Errorf("unable to open %s: %v", lp, err)
 	}
 	currentLogFile = f
 	return nil
 }
 
-// closeAuditLog closes the audit log file
-func closeAuditLog() {
-	if err := currentLogFile.Close(); err != nil {
-		klog.Errorf("failed to close the audit log: %v", err)
-	}
-}
-
 // appendToLog appends the row to the log file.
 func appendToLog(row *row) error {
+	if currentLogFile == nil {
+		if err := setLogFile(); err != nil {
+			return err
+		}
+	}
 	ce := register.CloudEvent(row, row.toMap())
 	bs, err := ce.MarshalJSON()
 	if err != nil {
 		return fmt.Errorf("error marshalling event: %v", err)
 	}
-	if err := openAuditLog(); err != nil {
-		return err
-	}
-	defer closeAuditLog()
 	if _, err := currentLogFile.WriteString(string(bs) + "\n"); err != nil {
 		return fmt.Errorf("unable to write to audit log: %v", err)
 	}
diff --git a/pkg/minikube/audit/report.go b/pkg/minikube/audit/report.go
index 1c255086c..681675712 100644
--- a/pkg/minikube/audit/report.go
+++ b/pkg/minikube/audit/report.go
@@ -32,10 +32,11 @@ func Report(lastNLines int) (*RawReport, error) {
 	if lastNLines <= 0 {
 		return nil, fmt.Errorf("last n lines must be 1 or greater")
 	}
-	if err := openAuditLog(); err != nil {
-		return nil, err
+	if currentLogFile == nil {
+		if err := setLogFile(); err != nil {
+			return nil, fmt.Errorf("failed to set the log file: %v", err)
+		}
 	}
-	defer closeAuditLog()
 	var logs []string
 	s := bufio.NewScanner(currentLogFile)
 	for s.Scan() {
diff --git a/pkg/minikube/audit/row.go b/pkg/minikube/audit/row.go
index 29c5d0b49..fb5991a00 100644
--- a/pkg/minikube/audit/row.go
+++ b/pkg/minikube/audit/row.go
@@ -30,20 +30,14 @@ import (
 
 // row is the log of a single command.
 type row struct {
-	SpecVersion     string            `json:"specversion"`
-	ID              string            `json:"id"`
-	Source          string            `json:"source"`
-	TypeField       string            `json:"type"`
-	DataContentType string            `json:"datacontenttype"`
-	Data            map[string]string `json:"data"`
-	args            string
-	command         string
-	endTime         string
-	id              string
-	profile         string
-	startTime       string
-	user            string
-	version         string
+	args      string
+	command   string
+	endTime   string
+	profile   string
+	startTime string
+	user      string
+	version   string
+	Data      map[string]string `json:"data"`
 }
 
 // Type returns the cloud events compatible type of this struct.
@@ -61,7 +55,6 @@ func (e *row) assignFields() {
 	e.startTime = e.Data["startTime"]
 	e.user = e.Data["user"]
 	e.version = e.Data["version"]
-	e.id = e.Data["id"]
 }
 
 // toMap combines fields into a string map,
@@ -75,12 +68,11 @@ func (e *row) toMap() map[string]string {
 		"startTime": e.startTime,
 		"user":      e.user,
 		"version":   e.version,
-		"id":        e.id,
 	}
 }
 
 // newRow creates a new audit row.
-func newRow(command string, args string, user string, version string, startTime time.Time, id string, profile ...string) *row {
+func newRow(command string, args string, user string, version string, startTime time.Time, endTime time.Time, profile ...string) *row {
 	p := viper.GetString(config.ProfileName)
 	if len(profile) > 0 {
 		p = profile[0]
@@ -88,11 +80,11 @@ func newRow(command string, args string, user string, version string, startTime
 	return &row{
 		args:      args,
 		command:   command,
+		endTime:   endTime.Format(constants.TimeFormat),
 		profile:   p,
 		startTime: startTime.Format(constants.TimeFormat),
 		user:      user,
 		version:   version,
-		id:        id,
 	}
 }
 


Test patch:
diff --git a/pkg/minikube/audit/audit_test.go b/pkg/minikube/audit/audit_test.go
index 262ceac82..e91f7d972 100644
--- a/pkg/minikube/audit/audit_test.go
+++ b/pkg/minikube/audit/audit_test.go
@@ -20,6 +20,7 @@ import (
 	"os"
 	"os/user"
 	"testing"
+	"time"
 
 	"github.com/spf13/pflag"
 	"github.com/spf13/viper"
@@ -176,27 +177,7 @@ func TestAudit(t *testing.T) {
 	})
 
 	// Check if logging with limited args causes a panic
-	t.Run("LogCommandStart", func(t *testing.T) {
-		oldArgs := os.Args
-		defer func() { os.Args = oldArgs }()
-		os.Args = []string{"minikube", "start"}
-
-		oldCommandLine := pflag.CommandLine
-		defer func() {
-			pflag.CommandLine = oldCommandLine
-			pflag.Parse()
-		}()
-		mockArgs(t, os.Args)
-		auditID, err := LogCommandStart()
-		if auditID == "" {
-			t.Fatal("audit ID should not be empty")
-		}
-		if err != nil {
-			t.Fatal(err)
-		}
-	})
-
-	t.Run("LogCommandEnd", func(t *testing.T) {
+	t.Run("Log", func(t *testing.T) {
 		oldArgs := os.Args
 		defer func() { os.Args = oldArgs }()
 		os.Args = []string{"minikube"}
@@ -207,32 +188,8 @@ func TestAudit(t *testing.T) {
 			pflag.Parse()
 		}()
 		mockArgs(t, os.Args)
-		auditID, err := LogCommandStart()
-		if err != nil {
-			t.Fatal("start failed")
-		}
-		err = LogCommandEnd(auditID)
 
-		if err != nil {
-			t.Fatal(err)
-		}
-	})
-
-	t.Run("LogCommandEndNonExistingID", func(t *testing.T) {
-		oldArgs := os.Args
-		defer func() { os.Args = oldArgs }()
-		os.Args = []string{"minikube"}
-
-		oldCommandLine := pflag.CommandLine
-		defer func() {
-			pflag.CommandLine = oldCommandLine
-			pflag.Parse()
-		}()
-		mockArgs(t, os.Args)
-		err := LogCommandEnd("non-existing-id")
-		if err == nil {
-			t.Fatal("function LogCommandEnd should return an error when a non-existing id is passed in it as an argument")
-		}
+		Log(time.Now())
 	})
 }
 
diff --git a/pkg/minikube/audit/logFile_test.go b/pkg/minikube/audit/logFile_test.go
index 916791108..617a43b8e 100644
--- a/pkg/minikube/audit/logFile_test.go
+++ b/pkg/minikube/audit/logFile_test.go
@@ -23,23 +23,21 @@ import (
 	"testing"
 	"time"
 
-	"github.com/google/uuid"
 	"k8s.io/minikube/pkg/minikube/localpath"
 )
 
 func TestLogFile(t *testing.T) {
-	t.Run("OpenAuditLog", func(t *testing.T) {
+	t.Run("SetLogFile", func(t *testing.T) {
 		// make sure logs directory exists
 		if err := os.MkdirAll(filepath.Dir(localpath.AuditLog()), 0755); err != nil {
 			t.Fatalf("Error creating logs directory: %v", err)
 		}
-		if err := openAuditLog(); err != nil {
-			t.Fatal(err)
+		if err := setLogFile(); err != nil {
+			t.Error(err)
 		}
 	})
 
 	t.Run("AppendToLog", func(t *testing.T) {
-		defer closeAuditLog()
 		f, err := os.CreateTemp("", "audit.json")
 		if err != nil {
 			t.Fatalf("Error creating temporary file: %v", err)
@@ -50,7 +48,7 @@ func TestLogFile(t *testing.T) {
 		defer func() { currentLogFile = &oldLogFile }()
 		currentLogFile = f
 
-		r := newRow("start", "-v", "user1", "v0.17.1", time.Now(), uuid.New().String())
+		r := newRow("start", "-v", "user1", "v0.17.1", time.Now(), time.Now())
 		if err := appendToLog(r); err != nil {
 			t.Fatalf("Error appendingToLog: %v", err)
 		}
diff --git a/pkg/minikube/audit/row_test.go b/pkg/minikube/audit/row_test.go
index a95123485..88b54174d 100644
--- a/pkg/minikube/audit/row_test.go
+++ b/pkg/minikube/audit/row_test.go
@@ -23,7 +23,6 @@ import (
 	"testing"
 	"time"
 
-	"github.com/google/uuid"
 	"k8s.io/minikube/pkg/minikube/constants"
 )
 
@@ -37,10 +36,8 @@ func TestRow(t *testing.T) {
 	stFormatted := st.Format(constants.TimeFormat)
 	et := time.Now()
 	etFormatted := et.Format(constants.TimeFormat)
-	id := uuid.New().String()
 
-	r := newRow(c, a, u, v, st, id, p)
-	r.endTime = etFormatted
+	r := newRow(c, a, u, v, st, et, p)
 
 	t.Run("NewRow", func(t *testing.T) {
 		tests := []struct {
@@ -54,7 +51,7 @@ func TestRow(t *testing.T) {
 			{"user", r.user, u},
 			{"version", r.version, v},
 			{"startTime", r.startTime, stFormatted},
-			{"id", r.id, id},
+			{"endTime", r.endTime, etFormatted},
 		}
 
 		for _, tt := range tests {
@@ -86,7 +83,7 @@ func TestRow(t *testing.T) {
 			{"user", u},
 			{"version", v},
 			{"startTime", stFormatted},
-			{"id", id},
+			{"endTime", etFormatted},
 		}
 
 		for _, tt := range tests {
@@ -109,7 +106,7 @@ func TestRow(t *testing.T) {
 	})
 
 	t.Run("assignFields", func(t *testing.T) {
-		l := fmt.Sprintf(`{"data":{"args":"%s","command":"%s","id":"%s","profile":"%s","startTime":"%s","user":"%s","version":"v0.17.1"},"datacontenttype":"application/json","id":"bc6ec9d4-0d08-4b57-ac3b-db8d67774768","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.audit"}`, a, c, id, p, stFormatted, u)
+		l := fmt.Sprintf(`{"data":{"args":"%s","command":"%s","endTime":"%s","profile":"%s","startTime":"%s","user":"%s","version":"v0.17.1"},"datacontenttype":"application/json","id":"bc6ec9d4-0d08-4b57-ac3b-db8d67774768","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.audit"}`, a, c, etFormatted, p, stFormatted, u)
 
 		r := &row{}
 		if err := json.Unmarshal([]byte(l), r); err != nil {
@@ -129,7 +126,7 @@ func TestRow(t *testing.T) {
 			{"user", r.user, u},
 			{"version", r.version, v},
 			{"startTime", r.startTime, stFormatted},
-			{"id", r.id, id},
+			{"endTime", r.endTime, etFormatted},
 		}
 
 		for _, tt := range tests {