Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minikube logs for other container runtimes #3780

Merged
merged 1 commit into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/drivers/none/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (d *Driver) Kill() error {
}

// First try to gracefully stop containers
containers, err := d.runtime.ListContainers(cruntime.MinikubeContainerPrefix)
containers, err := d.runtime.ListContainers("")
if err != nil {
return errors.Wrap(err, "containers")
}
Expand All @@ -146,7 +146,7 @@ func (d *Driver) Kill() error {
return errors.Wrap(err, "stop")
}

containers, err = d.runtime.ListContainers(cruntime.MinikubeContainerPrefix)
containers, err = d.runtime.ListContainers("")
if err != nil {
return errors.Wrap(err, "containers")
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (d *Driver) Stop() error {
if err := stopKubelet(d.exec); err != nil {
return err
}
containers, err := d.runtime.ListContainers(cruntime.MinikubeContainerPrefix)
containers, err := d.runtime.ListContainers("")
if err != nil {
return errors.Wrap(err, "containers")
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/minikube/cruntime/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ import (

// listCRIContainers returns a list of containers using crictl
func listCRIContainers(cr CommandRunner, filter string) ([]string, error) {
content, err := cr.CombinedOutput(fmt.Sprintf(`sudo crictl ps -a --name=%s --quiet`, filter))
var content string
var err error
state := "Running"
if filter != "" {
content, err = cr.CombinedOutput(fmt.Sprintf(`sudo crictl ps -a --name=%s --state=%s --quiet`, filter, state))
} else {
content, err = cr.CombinedOutput(fmt.Sprintf(`sudo crictl ps -a --state=%s --quiet`, state))
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -80,7 +87,7 @@ image-endpoint: unix://{{.Socket}}
// criContainerLogCmd returns the command to retrieve the log for a container based on ID
func criContainerLogCmd(id string, len int, follow bool) string {
var cmd strings.Builder
cmd.WriteString("crictl logs ")
cmd.WriteString("sudo crictl logs ")
if len > 0 {
cmd.WriteString(fmt.Sprintf("--tail %d ", len))
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/minikube/cruntime/cruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"github.com/pkg/errors"
)

const MinikubeContainerPrefix = "k8s_"

// CommandRunner is the subset of bootstrapper.CommandRunner this package consumes
type CommandRunner interface {
Run(string) error
Expand Down
28 changes: 21 additions & 7 deletions pkg/minikube/cruntime/cruntime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (f *FakeRunner) docker(args []string, root bool) (string, error) {
func (f *FakeRunner) crictl(args []string, root bool) (string, error) {
switch cmd := args[0]; cmd {
case "ps":
// crictl ps -a --name=apiserver --quiet
// crictl ps -a --name=apiserver --state=Running --quiet
if args[1] == "-a" && strings.HasPrefix(args[2], "--name") {
fname := strings.Split(args[2], "=")[1]
ids := []string{}
Expand All @@ -202,6 +202,14 @@ func (f *FakeRunner) crictl(args []string, root bool) (string, error) {
}
f.t.Logf("fake crictl: Found containers: %v", ids)
return strings.Join(ids, "\n"), nil
} else if args[1] == "-a" {
ids := []string{}
for id := range f.containers {
ids = append(ids, id)
}
f.t.Logf("fake crictl: Found containers: %v", ids)
return strings.Join(ids, "\n"), nil

}
case "stop":
for _, id := range args[1:] {
Expand Down Expand Up @@ -376,11 +384,17 @@ func TestContainerFunctions(t *testing.T) {
for _, tc := range tests {
t.Run(tc.runtime, func(t *testing.T) {
runner := NewFakeRunner(t)
prefix := ""
if tc.runtime == "docker" {
prefix = "k8s_"
}
runner.containers = map[string]string{
"abc0": "k8s_apiserver",
"fgh1": "k8s_coredns",
"xyz2": "k8s_storage",
"zzz": "unrelated",
"abc0": prefix + "apiserver",
"fgh1": prefix + "coredns",
"xyz2": prefix + "storage",
}
if tc.runtime == "docker" {
runner.containers["zzz"] = "unrelated"
}
cr, err := New(Config{Type: tc.runtime, Runner: runner})
if err != nil {
Expand Down Expand Up @@ -409,7 +423,7 @@ func TestContainerFunctions(t *testing.T) {
}

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

// Kill the containers and assert that they have disappeared
cr.KillContainers(got)
got, err = cr.ListContainers(MinikubeContainerPrefix)
got, err = cr.ListContainers("")
if err != nil {
t.Fatalf("ListContainers: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/minikube/cruntime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/golang/glog"
)

const KubernetesContainerPrefix = "k8s_"

// Docker contains Docker runtime state
type Docker struct {
Socket string
Expand Down Expand Up @@ -85,6 +87,7 @@ func (r *Docker) KubeletOptions() map[string]string {

// ListContainers returns a list of containers
func (r *Docker) ListContainers(filter string) ([]string, error) {
filter = KubernetesContainerPrefix + filter
content, err := r.Runner.CombinedOutput(fmt.Sprintf(`docker ps -a --filter="name=%s" --format="{{.ID}}"`, filter))
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/minikube/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ var rootCauseRe = regexp.MustCompile(`^error: |eviction manager: pods.* evicted|

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

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