Skip to content

Commit 794bc6c

Browse files
authored
Merge pull request #3780 from afbjorklund/logs
Fix minikube logs for other container runtimes
2 parents 42863bf + df5bbc3 commit 794bc6c

File tree

6 files changed

+40
-18
lines changed

6 files changed

+40
-18
lines changed

pkg/drivers/none/none.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (d *Driver) Kill() error {
134134
}
135135

136136
// First try to gracefully stop containers
137-
containers, err := d.runtime.ListContainers(cruntime.MinikubeContainerPrefix)
137+
containers, err := d.runtime.ListContainers("")
138138
if err != nil {
139139
return errors.Wrap(err, "containers")
140140
}
@@ -146,7 +146,7 @@ func (d *Driver) Kill() error {
146146
return errors.Wrap(err, "stop")
147147
}
148148

149-
containers, err = d.runtime.ListContainers(cruntime.MinikubeContainerPrefix)
149+
containers, err = d.runtime.ListContainers("")
150150
if err != nil {
151151
return errors.Wrap(err, "containers")
152152
}
@@ -196,7 +196,7 @@ func (d *Driver) Stop() error {
196196
if err := stopKubelet(d.exec); err != nil {
197197
return err
198198
}
199-
containers, err := d.runtime.ListContainers(cruntime.MinikubeContainerPrefix)
199+
containers, err := d.runtime.ListContainers("")
200200
if err != nil {
201201
return errors.Wrap(err, "containers")
202202
}

pkg/minikube/cruntime/cri.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ import (
2828

2929
// listCRIContainers returns a list of containers using crictl
3030
func listCRIContainers(cr CommandRunner, filter string) ([]string, error) {
31-
content, err := cr.CombinedOutput(fmt.Sprintf(`sudo crictl ps -a --name=%s --quiet`, filter))
31+
var content string
32+
var err error
33+
state := "Running"
34+
if filter != "" {
35+
content, err = cr.CombinedOutput(fmt.Sprintf(`sudo crictl ps -a --name=%s --state=%s --quiet`, filter, state))
36+
} else {
37+
content, err = cr.CombinedOutput(fmt.Sprintf(`sudo crictl ps -a --state=%s --quiet`, state))
38+
}
3239
if err != nil {
3340
return nil, err
3441
}
@@ -80,7 +87,7 @@ image-endpoint: unix://{{.Socket}}
8087
// criContainerLogCmd returns the command to retrieve the log for a container based on ID
8188
func criContainerLogCmd(id string, len int, follow bool) string {
8289
var cmd strings.Builder
83-
cmd.WriteString("crictl logs ")
90+
cmd.WriteString("sudo crictl logs ")
8491
if len > 0 {
8592
cmd.WriteString(fmt.Sprintf("--tail %d ", len))
8693
}

pkg/minikube/cruntime/cruntime.go

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/pkg/errors"
2525
)
2626

27-
const MinikubeContainerPrefix = "k8s_"
28-
2927
// CommandRunner is the subset of bootstrapper.CommandRunner this package consumes
3028
type CommandRunner interface {
3129
Run(string) error

pkg/minikube/cruntime/cruntime_test.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (f *FakeRunner) docker(args []string, root bool) (string, error) {
190190
func (f *FakeRunner) crictl(args []string, root bool) (string, error) {
191191
switch cmd := args[0]; cmd {
192192
case "ps":
193-
// crictl ps -a --name=apiserver --quiet
193+
// crictl ps -a --name=apiserver --state=Running --quiet
194194
if args[1] == "-a" && strings.HasPrefix(args[2], "--name") {
195195
fname := strings.Split(args[2], "=")[1]
196196
ids := []string{}
@@ -202,6 +202,14 @@ func (f *FakeRunner) crictl(args []string, root bool) (string, error) {
202202
}
203203
f.t.Logf("fake crictl: Found containers: %v", ids)
204204
return strings.Join(ids, "\n"), nil
205+
} else if args[1] == "-a" {
206+
ids := []string{}
207+
for id := range f.containers {
208+
ids = append(ids, id)
209+
}
210+
f.t.Logf("fake crictl: Found containers: %v", ids)
211+
return strings.Join(ids, "\n"), nil
212+
205213
}
206214
case "stop":
207215
for _, id := range args[1:] {
@@ -376,11 +384,17 @@ func TestContainerFunctions(t *testing.T) {
376384
for _, tc := range tests {
377385
t.Run(tc.runtime, func(t *testing.T) {
378386
runner := NewFakeRunner(t)
387+
prefix := ""
388+
if tc.runtime == "docker" {
389+
prefix = "k8s_"
390+
}
379391
runner.containers = map[string]string{
380-
"abc0": "k8s_apiserver",
381-
"fgh1": "k8s_coredns",
382-
"xyz2": "k8s_storage",
383-
"zzz": "unrelated",
392+
"abc0": prefix + "apiserver",
393+
"fgh1": prefix + "coredns",
394+
"xyz2": prefix + "storage",
395+
}
396+
if tc.runtime == "docker" {
397+
runner.containers["zzz"] = "unrelated"
384398
}
385399
cr, err := New(Config{Type: tc.runtime, Runner: runner})
386400
if err != nil {
@@ -409,7 +423,7 @@ func TestContainerFunctions(t *testing.T) {
409423
}
410424

411425
// Get the list of everything else.
412-
got, err = cr.ListContainers(MinikubeContainerPrefix)
426+
got, err = cr.ListContainers("")
413427
if err != nil {
414428
t.Fatalf("ListContainers: %v", err)
415429
}
@@ -420,7 +434,7 @@ func TestContainerFunctions(t *testing.T) {
420434

421435
// Kill the containers and assert that they have disappeared
422436
cr.KillContainers(got)
423-
got, err = cr.ListContainers(MinikubeContainerPrefix)
437+
got, err = cr.ListContainers("")
424438
if err != nil {
425439
t.Fatalf("ListContainers: %v", err)
426440
}

pkg/minikube/cruntime/docker.go

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/golang/glog"
2525
)
2626

27+
const KubernetesContainerPrefix = "k8s_"
28+
2729
// Docker contains Docker runtime state
2830
type Docker struct {
2931
Socket string
@@ -85,6 +87,7 @@ func (r *Docker) KubeletOptions() map[string]string {
8587

8688
// ListContainers returns a list of containers
8789
func (r *Docker) ListContainers(filter string) ([]string, error) {
90+
filter = KubernetesContainerPrefix + filter
8891
content, err := r.Runner.CombinedOutput(fmt.Sprintf(`docker ps -a --filter="name=%s" --format="{{.ID}}"`, filter))
8992
if err != nil {
9093
return nil, err

pkg/minikube/logs/logs.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ var rootCauseRe = regexp.MustCompile(`^error: |eviction manager: pods.* evicted|
3737

3838
// importantPods are a list of pods to retrieve logs for, in addition to the bootstrapper logs.
3939
var importantPods = []string{
40-
"k8s_kube-apiserver",
41-
"k8s_coredns_coredns",
42-
"k8s_kube-scheduler",
40+
"kube-apiserver",
41+
"coredns",
42+
"kube-scheduler",
4343
}
4444

4545
// lookbackwardsCount is how far back to look in a log for problems. This should be large enough to
@@ -143,7 +143,7 @@ func logCommands(r cruntime.Manager, bs bootstrapper.Bootstrapper, length int, f
143143
}
144144
glog.Infof("%d containers: %s", len(ids), ids)
145145
if len(ids) == 0 {
146-
cmds[pod] = fmt.Sprintf("No container was found matching %q", pod)
146+
glog.Warningf("No container was found matching %q", pod)
147147
continue
148148
}
149149
cmds[pod] = r.ContainerLogCmd(ids[0], length, follow)

0 commit comments

Comments
 (0)