CreateContainer creates a new container in the given PodSandbox.
(ctx context.Context, r *runtime.CreateContainerRequest)
| 58 | |
| 59 | // CreateContainer creates a new container in the given PodSandbox. |
| 60 | func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateContainerRequest) (_ *runtime.CreateContainerResponse, retErr error) { |
| 61 | span := tracing.SpanFromContext(ctx) |
| 62 | config := r.GetConfig() |
| 63 | log.G(ctx).Debugf("Container config %+v", config) |
| 64 | sandboxConfig := r.GetSandboxConfig() |
| 65 | sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId()) |
| 66 | if err != nil { |
| 67 | return nil, fmt.Errorf("failed to find sandbox id %q: %w", r.GetPodSandboxId(), err) |
| 68 | } |
| 69 | |
| 70 | cstatus, err := c.sandboxService.SandboxStatus(ctx, sandbox.Sandboxer, sandbox.ID, false) |
| 71 | if err != nil { |
| 72 | return nil, fmt.Errorf("failed to get controller status: %w", err) |
| 73 | } |
| 74 | |
| 75 | var ( |
| 76 | sandboxID = cstatus.SandboxID |
| 77 | sandboxPid = cstatus.Pid |
| 78 | ) |
| 79 | span.SetAttributes( |
| 80 | tracing.Attribute("sandbox.id", sandboxID), |
| 81 | tracing.Attribute("sandbox.pid", sandboxPid), |
| 82 | ) |
| 83 | // Generate unique id and name for the container and reserve the name. |
| 84 | // Reserve the container name to avoid concurrent `CreateContainer` request creating |
| 85 | // the same container. |
| 86 | id := util.GenerateID() |
| 87 | metadata := config.GetMetadata() |
| 88 | if metadata == nil { |
| 89 | return nil, errors.New("container config must include metadata") |
| 90 | } |
| 91 | sandboxMetadata := sandboxConfig.GetMetadata() |
| 92 | if sandboxMetadata == nil { |
| 93 | return nil, errors.New("pod sandbox config must include metadata") |
| 94 | } |
| 95 | containerName := metadata.Name |
| 96 | name := makeContainerName(metadata, sandboxMetadata) |
| 97 | log.G(ctx).Debugf("Generated id %q for container %q", id, name) |
| 98 | if _, err := criSignalToOCIStopSignal(config.GetStopSignal()); err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | if err = c.containerNameIndex.Reserve(name, id); err != nil { |
| 102 | var resErr *registrar.ReservedErr |
| 103 | if errors.As(err, &resErr) { |
| 104 | log.G(ctx).WithError(err).Warn("possible concurrent CreateContainer request") |
| 105 | return nil, fmt.Errorf("failed to reserve container name %q; check if another CreateContainer request is in progress: %w", name, err) |
| 106 | } |
| 107 | return nil, fmt.Errorf("failed to reserve container name %q: %w", name, err) |
| 108 | } |
| 109 | span.SetAttributes( |
| 110 | tracing.Attribute("container.id", id), |
| 111 | tracing.Attribute("container.name", name), |
| 112 | ) |
| 113 | defer func() { |
| 114 | // Release the name if the function returns with an error. |
| 115 | if retErr != nil { |
| 116 | c.containerNameIndex.ReleaseByName(name) |
| 117 | } |
nothing calls this directly
no test coverage detected