提交 12e41aea 编写于 作者: M Matt Rickard 提交者: GitHub

Merge pull request #1273 from r2d4/logs

Add -f --folow option to minikube logs
......@@ -27,6 +27,10 @@ import (
"k8s.io/minikube/pkg/minikube/machine"
)
var (
follow bool
)
// logsCmd represents the logs command
var logsCmd = &cobra.Command{
Use: "logs",
......@@ -39,7 +43,7 @@ var logsCmd = &cobra.Command{
os.Exit(1)
}
defer api.Close()
s, err := cluster.GetHostLogs(api)
s, err := cluster.GetHostLogs(api, follow)
if err != nil {
log.Println("Error getting machine logs:", err)
cmdUtil.MaybeReportErrorAndExit(err)
......@@ -49,5 +53,6 @@ var logsCmd = &cobra.Command{
}
func init() {
logsCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Show only the most recent journal entries, and continuously print new entries as they are appended to the journal.")
RootCmd.AddCommand(logsCmd)
}
......@@ -706,6 +706,9 @@ _minikube_logs()
flags_with_completion=()
flags_completion=()
flags+=("--follow")
flags+=("-f")
local_nonpersistent_flags+=("--follow")
flags+=("--alsologtostderr")
flags+=("--log_backtrace_at=")
flags+=("--log_dir=")
......
......@@ -11,6 +11,12 @@ Gets the logs of the running localkube instance, used for debugging minikube, no
minikube logs
```
### Options
```
-f, --follow Show only the most recent journal entries, and continuously print new entries as they are appended to the journal.
```
### Options inherited from parent commands
```
......
......@@ -370,11 +370,27 @@ func GetHostDockerEnv(api libmachine.API) (map[string]string, error) {
}
// GetHostLogs gets the localkube logs of the host VM.
func GetHostLogs(api libmachine.API) (string, error) {
// If follow is specified, it will tail the logs
func GetHostLogs(api libmachine.API, follow bool) (string, error) {
h, err := CheckIfApiExistsAndLoad(api)
if err != nil {
return "", errors.Wrap(err, "Error checking that api exists and loading it")
}
logsCommand, err := GetLogsCommand(follow)
if err != nil {
return "", errors.Wrap(err, "Error getting logs command")
}
if follow {
c, err := h.CreateSSHClient()
if err != nil {
return "", errors.Wrap(err, "Error creating ssh client")
}
err = c.Shell(logsCommand)
if err != nil {
return "", errors.Wrap(err, "error ssh shell")
}
return "", err
}
s, err := h.RunSSHCommand(logsCommand)
if err != nil {
return "", err
......
......@@ -495,12 +495,34 @@ func TestHostGetLogs(t *testing.T) {
}
api.Hosts[constants.MachineName] = &host.Host{Driver: d}
if _, err := GetHostLogs(api); err != nil {
t.Fatalf("Error getting host logs: %s", err)
tests := []struct {
description string
follow bool
}{
{
description: "logs",
follow: false,
},
{
description: "logs -f",
follow: true,
},
}
if _, ok := s.Commands[logsCommand]; !ok {
t.Fatalf("Expected command not run: %s", logsCommand)
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
t.Parallel()
cmd, err := GetLogsCommand(test.follow)
if err != nil {
t.Errorf("Error getting the logs command: %s", err)
}
if _, err = GetHostLogs(api, test.follow); err != nil {
t.Errorf("Error getting host logs: %s", err)
}
if _, ok := s.Commands[cmd]; !ok {
t.Errorf("Expected command to run but did not: %s", cmd)
}
})
}
}
......
......@@ -180,13 +180,39 @@ func GenLocalkubeStartCmd(kubernetesConfig KubernetesConfig) (string, error) {
return buf.String(), nil
}
var logsCommand = fmt.Sprintf(`
const logsTemplate = `
if which systemctl 2>&1 1>/dev/null; then
sudo journalctl -u localkube
sudo journalctl {{.Flags}} -u localkube
else
tail -n +1 %s %s
tail -n +1 {{.Flags}} {{.RemoteLocalkubeErrPath}} {{.RemoteLocalkubeOutPath}} %s
fi
`, constants.RemoteLocalKubeErrPath, constants.RemoteLocalKubeOutPath)
`
func GetLogsCommand(follow bool) (string, error) {
t, err := template.New("logsTemplate").Parse(logsTemplate)
if err != nil {
return "", err
}
var flags []string
if follow {
flags = append(flags, "-f")
}
buf := bytes.Buffer{}
data := struct {
RemoteLocalkubeErrPath string
RemoteLocalkubeOutPath string
Flags string
}{
RemoteLocalkubeErrPath: constants.RemoteLocalKubeErrPath,
RemoteLocalkubeOutPath: constants.RemoteLocalKubeOutPath,
Flags: strings.Join(flags, " "),
}
if err := t.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
var localkubeStatusCommand = fmt.Sprintf(`
if which systemctl 2>&1 1>/dev/null; then
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册