StartContainer starts the container.
(ctx context.Context, r *runtime.StartContainerRequest)
| 48 | |
| 49 | // StartContainer starts the container. |
| 50 | func (c *criService) StartContainer(ctx context.Context, r *runtime.StartContainerRequest) (retRes *runtime.StartContainerResponse, retErr error) { |
| 51 | span := tracing.SpanFromContext(ctx) |
| 52 | start := time.Now() |
| 53 | cntr, err := c.containerStore.Get(r.GetContainerId()) |
| 54 | if err != nil { |
| 55 | return nil, fmt.Errorf("an error occurred when try to find container %q: %w", r.GetContainerId(), err) |
| 56 | } |
| 57 | span.SetAttributes(tracing.Attribute("container.id", cntr.ID)) |
| 58 | info, err := cntr.Container.Info(ctx) |
| 59 | if err != nil { |
| 60 | return nil, fmt.Errorf("get container info: %w", err) |
| 61 | } |
| 62 | |
| 63 | id := cntr.ID |
| 64 | meta := cntr.Metadata |
| 65 | container := cntr.Container |
| 66 | config := meta.Config |
| 67 | |
| 68 | // Set starting state to prevent other start/remove operations against this container |
| 69 | // while it's being started. |
| 70 | if err := setContainerStarting(cntr); err != nil { |
| 71 | return nil, fmt.Errorf("failed to set starting state for container %q: %w", id, err) |
| 72 | } |
| 73 | defer func() { |
| 74 | if retErr != nil { |
| 75 | // Set container to exited if fail to start. |
| 76 | if err := cntr.Status.UpdateSync(func(status containerstore.Status) (containerstore.Status, error) { |
| 77 | status.Pid = 0 |
| 78 | status.FinishedAt = time.Now().UnixNano() |
| 79 | status.ExitCode = errorStartExitCode |
| 80 | status.Reason = errorStartReason |
| 81 | status.Message = retErr.Error() |
| 82 | return status, nil |
| 83 | }); err != nil { |
| 84 | log.G(ctx).WithError(err).Errorf("failed to set start failure state for container %q", id) |
| 85 | } |
| 86 | } |
| 87 | if err := resetContainerStarting(cntr); err != nil { |
| 88 | log.G(ctx).WithError(err).Errorf("failed to reset starting state for container %q", id) |
| 89 | } |
| 90 | }() |
| 91 | |
| 92 | // Get sandbox config from sandbox store. |
| 93 | sandbox, err := c.sandboxStore.Get(meta.SandboxID) |
| 94 | if err != nil { |
| 95 | return nil, fmt.Errorf("sandbox %q not found: %w", meta.SandboxID, err) |
| 96 | } |
| 97 | sandboxID := meta.SandboxID |
| 98 | if sandbox.Status.Get().State != sandboxstore.StateReady { |
| 99 | return nil, fmt.Errorf("sandbox container %q is not running", sandboxID) |
| 100 | } |
| 101 | span.SetAttributes(tracing.Attribute("sandbox.id", sandboxID)) |
| 102 | |
| 103 | ioCreation := func(id string) (_ containerdio.IO, err error) { |
| 104 | stdoutWC, stderrWC, err := c.createContainerLoggers(meta.LogPath, config.GetTty()) |
| 105 | if err != nil { |
| 106 | return nil, fmt.Errorf("failed to create container loggers: %w", err) |
| 107 | } |
nothing calls this directly
no test coverage detected