Skip to content
Closed
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
39 changes: 38 additions & 1 deletion internal/cri/server/container_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"io"
"os"
"time"

"github.com/containerd/errdefs"
Expand Down Expand Up @@ -99,6 +100,39 @@ func (c *criService) StartContainer(ctx context.Context, r *runtime.StartContain
}
}

volatileContainerRootDir := c.getVolatileContainerRootDir(id)
if err = c.os.MkdirAll(volatileContainerRootDir, 0o755); err != nil {
return nil, fmt.Errorf("failed to create volatile container root directory %q: %w",
volatileContainerRootDir, err)
}
containerIO, err := cio.NewContainerIO(id,
cio.WithNewFIFOs(volatileContainerRootDir, config.GetTty(), config.GetStdin()))
if err != nil {
return nil, fmt.Errorf("failed to create container io: %w", err)
}
defer func() {
if retErr != nil {
if err := containerIO.Close(); err != nil {
log.G(ctx).WithError(err).Errorf("Failed to close container io %q", id)
}
}
}()
if cntr.IO != nil {
if _, err := os.Stat(cntr.IO.Config().Stdin); err != nil && os.IsNotExist(err) {
// cleanup old fifos tempdir
cntr.IO.Close()
cntr.IO = nil
}
}
if cntr.IO == nil {
cntr.IO = containerIO
// update container IO into container store
// OR, tempdir for fifos will be remained when handleContainerExit
if err := c.containerStore.UpdateContainerIO(cntr.ID, containerIO); err != nil {
return nil, fmt.Errorf("failed to update container io %q into store: %w", id, err)
}
}

ioCreation := func(id string) (_ containerdio.IO, err error) {
stdoutWC, stderrWC, err := c.createContainerLoggers(meta.LogPath, config.GetTty())
if err != nil {
Expand Down Expand Up @@ -172,6 +206,7 @@ func (c *criService) StartContainer(ctx context.Context, r *runtime.StartContain
// Update container start timestamp.
if err := cntr.Status.UpdateSync(func(status containerstore.Status) (containerstore.Status, error) {
status.Pid = task.Pid()
status.FinishedAt = 0
status.StartedAt = time.Now().UnixNano()
return status, nil
}); err != nil {
Expand All @@ -198,7 +233,9 @@ func (c *criService) StartContainer(ctx context.Context, r *runtime.StartContain
func setContainerStarting(container containerstore.Container) error {
return container.Status.Update(func(status containerstore.Status) (containerstore.Status, error) {
// Return error if container is not in created state.
if status.State() != runtime.ContainerState_CONTAINER_CREATED {
st := status.State()
if st != runtime.ContainerState_CONTAINER_CREATED &&
st != runtime.ContainerState_CONTAINER_EXITED {
return status, fmt.Errorf("container is in %s state", criContainerStateToString(status.State()))
}
// Do not start the container when there is a removal in progress.
Expand Down
2 changes: 1 addition & 1 deletion internal/cri/server/container_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestSetContainerStarting(t *testing.T) {
StartedAt: time.Now().UnixNano(),
FinishedAt: time.Now().UnixNano(),
},
expectErr: true,
expectErr: false,
},
{
desc: "should return error when container is in unknown state",
Expand Down
23 changes: 23 additions & 0 deletions internal/cri/store/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,26 @@ func (s *Store) Delete(id string) {
s.idIndex.Delete(id)
delete(s.containers, id)
}

// UpdateContainerIO update container IO
// useful when using StartContainer
func (s *Store) UpdateContainerIO(id string, io *cio.ContainerIO) error {
s.lock.Lock()
defer s.lock.Unlock()
id, err := s.idIndex.Get(id)
if err != nil {
if err == truncindex.ErrNotExist {
err = errdefs.ErrNotFound
}
return err
}

if _, ok := s.containers[id]; !ok {
return errdefs.ErrNotFound
}

c := s.containers[id]
c.IO = io
s.containers[id] = c
return nil
}