Skip to content

cluster up: print last 10 lines of logs on errors after container started #9256

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

Merged
merged 1 commit into from
Jun 10, 2016
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
18 changes: 18 additions & 0 deletions pkg/bootstrap/docker/dockerhelper/helper.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package dockerhelper

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/url"
"strconv"
"strings"

"github.com/blang/semver"
Expand Down Expand Up @@ -207,6 +209,22 @@ func (h *Helper) HostIP() string {
return ""
}

func (h *Helper) ContainerLog(container string, numLines int) string {
output := &bytes.Buffer{}
err := h.client.Logs(docker.LogsOptions{
Container: container,
Tail: strconv.Itoa(numLines),
OutputStream: output,
ErrorStream: output,
Stdout: true,
Stderr: true,
})
if err != nil {
glog.V(1).Infof("Error getting container %q log: %v", container, err)
}
return output.String()
}

func (h *Helper) StopAndRemoveContainer(container string) error {
err := h.client.StopContainer(container, 10)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/bootstrap/docker/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Error interface {
error
WithCause(error) Error
WithSolution(string, ...interface{}) Error
WithDetails(string) Error
}

func NewError(msg string, args ...interface{}) Error {
Expand Down Expand Up @@ -42,6 +43,10 @@ func (e *internalError) Solution() string {
return e.solution
}

func (e *internalError) Details() string {
return e.details
}

func (e *internalError) WithCause(err error) Error {
e.cause = err
return e
Expand Down
21 changes: 15 additions & 6 deletions pkg/bootstrap/docker/openshift/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (h *Helper) InstallRegistry(kubeClient kclient.Interface, f *clientcmd.Fact
return nil
}
if !apierrors.IsNotFound(err) {
return errors.NewError("error retrieving docker registry service").WithCause(err)
return errors.NewError("error retrieving docker registry service").WithCause(err).WithDetails(h.OriginLog())
}
imageTemplate := variable.NewDefaultImageTemplate()
imageTemplate.Format = images
Expand All @@ -53,7 +53,10 @@ func (h *Helper) InstallRegistry(kubeClient kclient.Interface, f *clientcmd.Fact
output := &bytes.Buffer{}
err = registry.RunCmdRegistry(f, cmd, output, cfg, []string{})
glog.V(4).Infof("Registry command output:\n%s", output.String())
return err
if err != nil {
return errors.NewError("cannot install registry").WithCause(err).WithDetails(h.OriginLog())
}
return nil
}

// InstallRouter installs a default router on the OpenShift server
Expand All @@ -63,6 +66,9 @@ func (h *Helper) InstallRouter(kubeClient kclient.Interface, f *clientcmd.Factor
// Router service already exists, nothing to do
return nil
}
if !apierrors.IsNotFound(err) {
return errors.NewError("error retrieving router service").WithCause(err).WithDetails(h.OriginLog())
}

masterDir := filepath.Join(configDir, "master")

Expand All @@ -71,18 +77,18 @@ func (h *Helper) InstallRouter(kubeClient kclient.Interface, f *clientcmd.Factor
routerSA.Name = "router"
_, err = kubeClient.ServiceAccounts("default").Create(routerSA)
if err != nil {
return errors.NewError("cannot create router service account").WithCause(err)
return errors.NewError("cannot create router service account").WithCause(err).WithDetails(h.OriginLog())
}

// Add router SA to privileged SCC
privilegedSCC, err := kubeClient.SecurityContextConstraints().Get("privileged")
if err != nil {
return errors.NewError("cannot retrieve privileged SCC").WithCause(err)
return errors.NewError("cannot retrieve privileged SCC").WithCause(err).WithDetails(h.OriginLog())
}
privilegedSCC.Users = append(privilegedSCC.Users, serviceaccount.MakeUsername("default", "router"))
_, err = kubeClient.SecurityContextConstraints().Update(privilegedSCC)
if err != nil {
return errors.NewError("cannot update privileged SCC").WithCause(err)
return errors.NewError("cannot update privileged SCC").WithCause(err).WithDetails(h.OriginLog())
}

// Create router cert
Expand Down Expand Up @@ -134,7 +140,10 @@ func (h *Helper) InstallRouter(kubeClient kclient.Interface, f *clientcmd.Factor
cmd.SetOutput(output)
err = router.RunCmdRouter(f, cmd, output, cfg, []string{})
glog.V(4).Infof("Router command output:\n%s", output.String())
return err
if err != nil {
return errors.NewError("cannot install router").WithCause(err).WithDetails(h.OriginLog())
}
return nil
}

// catFiles concatenates multiple source files into a single destination file
Expand Down
10 changes: 6 additions & 4 deletions pkg/bootstrap/docker/openshift/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package openshift

import (
"fmt"

"github.com/openshift/origin/pkg/bootstrap/docker/errors"
)

// ErrOpenShiftFailedToStart is thrown when the OpenShift server failed to start
func ErrOpenShiftFailedToStart(container string) error {
return fmt.Errorf("Could not start OpenShift container %q", container)
func ErrOpenShiftFailedToStart(container string) errors.Error {
return errors.NewError("could not start OpenShift container %q", container)
}

// ErrTimedOutWaitingForStart is thrown when the OpenShift server can't be pinged after reasonable
// amount of time.
func ErrTimedOutWaitingForStart(container string) error {
return fmt.Errorf("Could not start OpenShift container %q", container)
func ErrTimedOutWaitingForStart(container string) errors.Error {
return errors.NewError("timed out waiting for OpenShift container %q", container)
}

type errPortsNotAvailable struct {
Expand Down
16 changes: 12 additions & 4 deletions pkg/bootstrap/docker/openshift/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,14 @@ func (h *Helper) Start(opt *StartOptions, out io.Writer) (string, error) {
return "", errors.NewError("cannot get state of OpenShift container %s", h.containerName).WithCause(err)
}
if !running {
return "", ErrOpenShiftFailedToStart(h.containerName)
return "", ErrOpenShiftFailedToStart(h.containerName).WithDetails(h.OriginLog())
}

// Wait until the API server is listening
fmt.Fprintf(out, "Waiting for API server to start listening\n")
masterHost := fmt.Sprintf("%s:8443", opt.ServerIP)
if err = cmdutil.WaitForSuccessfulDial(true, "tcp", masterHost, 200*time.Millisecond, 1*time.Second, serverUpTimeout); err != nil {
return "", ErrTimedOutWaitingForStart(h.containerName)
return "", ErrTimedOutWaitingForStart(h.containerName).WithDetails(h.OriginLog())
}
// Check for healthz endpoint to be ready
client, err := masterHTTPClient(configDir)
Expand All @@ -285,7 +285,7 @@ func (h *Helper) Start(opt *StartOptions, out io.Writer) (string, error) {
for {
resp, ierr := client.Get(h.healthzReadyURL(opt.ServerIP))
if ierr != nil {
return "", errors.NewError("cannot access master readiness URL %s", h.healthzReadyURL(opt.ServerIP)).WithCause(err)
return "", errors.NewError("cannot access master readiness URL %s", h.healthzReadyURL(opt.ServerIP)).WithCause(err).WithDetails(h.OriginLog())
}
if resp.StatusCode == http.StatusOK {
break
Expand All @@ -300,12 +300,20 @@ func (h *Helper) Start(opt *StartOptions, out io.Writer) (string, error) {
if rerr == nil {
responseBody = string(body)
}
return "", errors.NewError("server is not ready. Response (%d): %s", resp.StatusCode, responseBody).WithCause(ierr)
return "", errors.NewError("server is not ready. Response (%d): %s", resp.StatusCode, responseBody).WithCause(ierr).WithDetails(h.OriginLog())
}
fmt.Fprintf(out, "OpenShift server started\n")
return configDir, nil
}

func (h *Helper) OriginLog() string {
log := h.dockerHelper.ContainerLog(h.containerName, 10)
if len(log) > 0 {
return fmt.Sprintf("Last 10 lines of %q container log:\n%s\n", h.containerName, log)
}
return fmt.Sprintf("No log available from %q container\n", h.containerName)
}

func (h *Helper) healthzReadyURL(ip string) string {
return fmt.Sprintf("%s/healthz/ready", h.Master(ip))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/bootstrap/docker/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func (c *ClientStartConfig) importObjects(out io.Writer, locations map[string]st
glog.V(2).Infof("Importing %s from %s", name, location)
err = openshift.ImportObjects(f, openShiftNamespace, location)
if err != nil {
return err
return errors.NewError("cannot import %s", name).WithCause(err).WithDetails(c.OpenShiftHelper().OriginLog())
}
}
return nil
Expand Down