MCPcopy Index your code
hub / github.com/containerd/containerd / CreateContainer

Method CreateContainer

internal/cri/server/container_create.go:60–213  ·  view source on GitHub ↗

CreateContainer creates a new container in the given PodSandbox.

(ctx context.Context, r *runtime.CreateContainerRequest)

Source from the content-addressed store, hash-verified

58
59// CreateContainer creates a new container in the given PodSandbox.
60func (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 }

Callers

nothing calls this directly

Implementers 1

criServiceinternal/cri/server/service.go

Calls 15

CRImportCheckpointMethod · 0.95
toContainerdImageMethod · 0.95
createContainerMethod · 0.95
SpanFromContextFunction · 0.92
AttributeFunction · 0.92
GenerateIDFunction · 0.92
makeContainerNameFunction · 0.85
criSignalToOCIStopSignalFunction · 0.85
GetConfigMethod · 0.80
SetAttributesMethod · 0.80
GetMetadataMethod · 0.80

Tested by

no test coverage detected