Skip to content

Commit 97eb1cd

Browse files
committed
change criService.runtimeHandlers slice to a map
This patch changes `criService.runtimeHandlers` from a slice to a map, so we don't need to for-loop slice in `createContainer`. It also refactors `introspectRuntimeHandler` a bit so it can be called given a single `config.Runtime`. This will help implement extracting runtime config to separate files (containerd#9296) Signed-off-by: Jin Dong <djdongjin95@gmail.com>
1 parent 757c0bd commit 97eb1cd

3 files changed

Lines changed: 40 additions & 40 deletions

File tree

internal/cri/server/container_create.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,9 @@ func (c *criService) createContainer(r *createContainerRequest) (_ string, retEr
283283
log.G(r.ctx).Debugf("Ignoring volumes defined in image %v because IgnoreImageDefinedVolumes is set", r.imageID)
284284
}
285285

286-
var runtimeHandler *runtime.RuntimeHandler
287-
for _, f := range c.runtimeHandlers {
288-
if f.Name == r.sandboxRuntimeHandler {
289-
runtimeHandler = f
290-
break
291-
}
286+
runtimeHandler, ok := c.runtimeHandlers[r.sandboxRuntimeHandler]
287+
if !ok {
288+
return "", fmt.Errorf("failed to find runtime handler %q", r.sandboxRuntimeHandler)
292289
}
293290
log.G(r.ctx).Debugf("Use OCI runtime %+v for sandbox %q and container %q", ociRuntime, r.sandboxID, r.containerID)
294291

internal/cri/server/service.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ type criService struct {
155155
// sandboxService is the sandbox related service for CRI
156156
sandboxService sandboxService
157157
// runtimeHandlers contains runtime handler info
158-
runtimeHandlers []*runtime.RuntimeHandler
158+
runtimeHandlers map[string]*runtime.RuntimeHandler
159159
// runtimeFeatures container runtime features info
160160
runtimeFeatures *runtime.RuntimeFeatures
161161
}
@@ -198,6 +198,7 @@ func NewCRIService(options *CRIServiceOptions) (CRIService, runtime.RuntimeServi
198198
containerNameIndex: registrar.NewRegistrar(),
199199
netPlugin: make(map[string]cni.CNI),
200200
sandboxService: newCriSandboxService(&config, options.SandboxControllers),
201+
runtimeHandlers: make(map[string]*runtime.RuntimeHandler),
201202
}
202203

203204
// TODO: Make discard time configurable
@@ -241,9 +242,11 @@ func NewCRIService(options *CRIServiceOptions) (CRIService, runtime.RuntimeServi
241242

242243
c.nri = nri.NewAPI(options.NRI, &criImplementation{c})
243244

244-
c.runtimeHandlers, err = c.introspectRuntimeHandlers(ctx)
245-
if err != nil {
246-
return nil, nil, fmt.Errorf("failed to introspect runtime handlers: %w", err)
245+
intro := c.client.IntrospectionService()
246+
for name, r := range c.config.Runtimes {
247+
if err := c.introspectRuntimeHandler(ctx, intro, name, r); err != nil {
248+
return nil, nil, fmt.Errorf("failed to introspect runtime %s: %w", name, err)
249+
}
247250
}
248251

249252
c.runtimeFeatures = &runtime.RuntimeFeatures{
@@ -365,38 +368,36 @@ func (c *criService) IsInitialized() bool {
365368
return c.initialized.Load()
366369
}
367370

368-
func (c *criService) introspectRuntimeHandlers(ctx context.Context) ([]*runtime.RuntimeHandler, error) {
369-
var res []*runtime.RuntimeHandler
370-
intro := c.client.IntrospectionService()
371-
for name, r := range c.config.Runtimes {
372-
h := runtime.RuntimeHandler{
373-
Name: name,
374-
}
375-
rawFeatures, err := introspectRuntimeFeatures(ctx, intro, r)
376-
if err != nil {
377-
log.G(ctx).WithError(err).Debugf("failed to introspect features of runtime %q", name)
378-
} else {
379-
h.Features = &runtime.RuntimeHandlerFeatures{}
380-
if slices.Contains(rawFeatures.MountOptions, "rro") {
381-
if kernelSupportsRRO {
382-
log.G(ctx).Debugf("runtime %q supports recursive read-only mounts", name)
383-
h.Features.RecursiveReadOnlyMounts = true
384-
} else {
385-
log.G(ctx).Debugf("runtime %q supports recursive read-only mounts, but the kernel does not", name)
386-
}
371+
func (c *criService) introspectRuntimeHandler(ctx context.Context, intro introspection.Service, name string, r config.Runtime) error {
372+
h := &runtime.RuntimeHandler{
373+
Name: name,
374+
}
375+
rawFeatures, err := introspectRuntimeFeatures(ctx, intro, r)
376+
if err != nil {
377+
log.G(ctx).WithError(err).Debugf("failed to introspect features of runtime %q", name)
378+
} else {
379+
h.Features = &runtime.RuntimeHandlerFeatures{}
380+
if slices.Contains(rawFeatures.MountOptions, "rro") {
381+
if kernelSupportsRRO {
382+
log.G(ctx).Debugf("runtime %q supports recursive read-only mounts", name)
383+
h.Features.RecursiveReadOnlyMounts = true
384+
} else {
385+
log.G(ctx).Debugf("runtime %q supports recursive read-only mounts, but the kernel does not", name)
387386
}
388-
userns := supportsCRIUserns(rawFeatures)
389-
h.Features.UserNamespaces = userns
390-
log.G(ctx).Debugf("runtime %q supports CRI userns: %v", name, userns)
391-
}
392-
res = append(res, &h)
393-
if name == c.config.DefaultRuntimeName {
394-
defH := h
395-
defH.Name = "" // denotes default
396-
res = append(res, &defH)
397387
}
388+
userns := supportsCRIUserns(rawFeatures)
389+
h.Features.UserNamespaces = userns
390+
log.G(ctx).Debugf("runtime %q supports CRI userns: %v", name, userns)
398391
}
399-
return res, nil
392+
393+
c.runtimeHandlers[name] = h
394+
if name == c.config.DefaultRuntimeName {
395+
defH := *h
396+
defH.Name = "" // denotes default
397+
c.runtimeHandlers[""] = &defH
398+
}
399+
400+
return nil
400401
}
401402

402403
func introspectRuntimeFeatures(ctx context.Context, intro introspection.Service, r config.Runtime) (*features.Features, error) {

internal/cri/server/status.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"maps"
2324
goruntime "runtime"
25+
"slices"
2426

2527
"github.com/containerd/containerd/api/services/introspection/v1"
2628
"github.com/containerd/log"
@@ -57,7 +59,7 @@ func (c *criService) Status(ctx context.Context, r *runtime.StatusRequest) (*run
5759
runtimeCondition,
5860
networkCondition,
5961
}},
60-
RuntimeHandlers: c.runtimeHandlers,
62+
RuntimeHandlers: slices.Collect(maps.Values(c.runtimeHandlers)),
6163
Features: c.runtimeFeatures,
6264
}
6365
if r.Verbose {

0 commit comments

Comments
 (0)