adjust status on container start failure
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
3f56193a15
commit
0b2f6b5354
4 changed files with 39 additions and 7 deletions
|
@ -50,6 +50,7 @@ type ContainerState struct {
|
||||||
Finished time.Time `json:"finished,omitempty"`
|
Finished time.Time `json:"finished,omitempty"`
|
||||||
ExitCode int32 `json:"exitCode,omitempty"`
|
ExitCode int32 `json:"exitCode,omitempty"`
|
||||||
OOMKilled bool `json:"oomKilled,omitempty"`
|
OOMKilled bool `json:"oomKilled,omitempty"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContainer creates a container object.
|
// NewContainer creates a container object.
|
||||||
|
|
|
@ -545,6 +545,15 @@ func (r *Runtime) DeleteContainer(c *Container) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStartFailed sets the container state appropriately after a start failure
|
||||||
|
func (r *Runtime) SetStartFailed(c *Container, err error) {
|
||||||
|
c.opLock.Lock()
|
||||||
|
defer c.opLock.Unlock()
|
||||||
|
// adjust finished and started times
|
||||||
|
c.state.Finished, c.state.Started = c.state.Created, c.state.Created
|
||||||
|
c.state.Error = err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateStatus refreshes the status of the container.
|
// UpdateStatus refreshes the status of the container.
|
||||||
func (r *Runtime) UpdateStatus(c *Container) error {
|
func (r *Runtime) UpdateStatus(c *Container) error {
|
||||||
c.opLock.Lock()
|
c.opLock.Lock()
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/kubernetes-incubator/cri-o/oci"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||||
)
|
)
|
||||||
|
@ -15,12 +16,26 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
state := s.runtime.ContainerStatus(c)
|
||||||
if err = s.runtime.StartContainer(c); err != nil {
|
if state.Status != oci.ContainerStateCreated {
|
||||||
return nil, fmt.Errorf("failed to start container %s: %v", c.ID(), err)
|
return nil, fmt.Errorf("container %s is not in created state: %s", c.ID(), state.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.containerStateToDisk(c)
|
defer func() {
|
||||||
|
// if the call to StartContainer fails below we still want to fill
|
||||||
|
// some fields of a container status. In particular, we're going to
|
||||||
|
// adjust container started/finished time and set an error to be
|
||||||
|
// returned in the Reason field for container status call.
|
||||||
|
if err != nil {
|
||||||
|
s.runtime.SetStartFailed(c, err)
|
||||||
|
}
|
||||||
|
s.containerStateToDisk(c)
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = s.runtime.StartContainer(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to start container %s: %v", c.ID(), err)
|
||||||
|
}
|
||||||
|
|
||||||
resp := &pb.StartContainerResponse{}
|
resp := &pb.StartContainerResponse{}
|
||||||
logrus.Debugf("StartContainerResponse %+v", resp)
|
logrus.Debugf("StartContainerResponse %+v", resp)
|
||||||
|
|
|
@ -12,6 +12,12 @@ import (
|
||||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
oomKilledReason = "OOMKilled"
|
||||||
|
completedReason = "Completed"
|
||||||
|
errorReason = "Error"
|
||||||
|
)
|
||||||
|
|
||||||
// ContainerStatus returns status of the container.
|
// ContainerStatus returns status of the container.
|
||||||
func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) {
|
func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) {
|
||||||
logrus.Debugf("ContainerStatusRequest %+v", req)
|
logrus.Debugf("ContainerStatusRequest %+v", req)
|
||||||
|
@ -100,11 +106,12 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq
|
||||||
resp.Status.ExitCode = cState.ExitCode
|
resp.Status.ExitCode = cState.ExitCode
|
||||||
switch {
|
switch {
|
||||||
case cState.OOMKilled:
|
case cState.OOMKilled:
|
||||||
resp.Status.Reason = "OOMKilled"
|
resp.Status.Reason = oomKilledReason
|
||||||
case cState.ExitCode == 0:
|
case cState.ExitCode == 0:
|
||||||
resp.Status.Reason = "Completed"
|
resp.Status.Reason = completedReason
|
||||||
default:
|
default:
|
||||||
resp.Status.Reason = "Error"
|
resp.Status.Reason = errorReason
|
||||||
|
resp.Status.Message = cState.Error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue