Repository: containerd/containerd
File: plugins/cri/cri.go (Go)

Code before:
func initCRIService(ic *plugin.InitContext) (interface{}, error) {
	ctx := ic.Context
	config := ic.Config.(*criconfig.ServerConfig)

	// Get runtime service.
	criRuntimePlugin, err := ic.GetByID(plugins.CRIServicePlugin, "runtime")
	if err != nil {
		return nil, fmt.Errorf("unable to load CRI runtime service plugin dependency: %w", err)
	}

	// Get image service.
	criImagePlugin, err := ic.GetByID(plugins.CRIServicePlugin, "images")
	if err != nil {
		return nil, fmt.Errorf("unable to load CRI image service plugin dependency: %w", err)
	}

	// Propagate runtime-specific snapshotters from runtime config to image service.
	// This is needed because users may configure snapshotters in the runtime config
	// (containerd.runtimes.<name>.snapshotter) which the image service needs for pulling.
	type runtimeConfigProvider interface {
		Config() criconfig.Config
	}
	type runtimeSnapshotterUpdater interface {
		UpdateRuntimeSnapshotter(runtimeName string, imagePlatform images.ImagePlatform)
	}
	if rcp, ok := criRuntimePlugin.(runtimeConfigProvider); ok {
		if rsu, ok := criImagePlugin.(runtimeSnapshotterUpdater); ok {
			runtimeConfig := rcp.Config()
			for runtimeName, rt := range runtimeConfig.Runtimes {
				if rt.Snapshotter != "" {
					imagePlatform := images.ImagePlatform{
						Snapshotter: rt.Snapshotter,
						Platform:    platforms.DefaultSpec(),
					}
					rsu.UpdateRuntimeSnapshotter(runtimeName, imagePlatform)
				}
			}
		}
	}

	if warnings, err := criconfig.ValidateServerConfig(ic.Context, config); err != nil {
		return nil, fmt.Errorf("invalid cri image config: %w", err)
	} else if len(warnings) > 0 {
		ws, err := ic.GetSingle(plugins.WarningPlugin)
		if err != nil {
			return nil, err
		}
		warn := ws.(warning.Service)
		for _, w := range warnings {
			warn.Emit(ic.Context, w)
		}

Review comment (suggestion):
```suggestion
	if rcp, ok := criRuntimePlugin.(server.RuntimeService); ok {
```


We already cast these below:
```go
		RuntimeService:     criRuntimePlugin.(server.RuntimeService),
		ImageService:       criImagePlugin.(server.ImageService),
```

No need to define another interface (RuntimeService has Config)
Probably can update `server.ImageService` interface to include `UpdateRuntimeSnapshotter`.

Code after:
func initCRIService(ic *plugin.InitContext) (interface{}, error) {
	ctx := ic.Context
	config := ic.Config.(*criconfig.ServerConfig)

	// Get runtime service.
	criRuntimePlugin, err := ic.GetByID(plugins.CRIServicePlugin, "runtime")
	if err != nil {
		return nil, fmt.Errorf("unable to load CRI runtime service plugin dependency: %w", err)
	}

	// Get image service.
	criImagePlugin, err := ic.GetByID(plugins.CRIServicePlugin, "images")
	if err != nil {
		return nil, fmt.Errorf("unable to load CRI image service plugin dependency: %w", err)
	}

	// Propagate runtime-specific snapshotters from runtime config to image service.
	// This is needed because users may configure snapshotters in the runtime config
	// (containerd.runtimes.<name>.snapshotter) which the image service needs for pulling.
	runtimeSvc := criRuntimePlugin.(server.RuntimeService)
	imageSvc := criImagePlugin.(server.ImageService)
	runtimeConfig := runtimeSvc.Config()
	for runtimeName, rt := range runtimeConfig.Runtimes {
		if rt.Snapshotter != "" {
			imagePlatform := images.ImagePlatform{
				Snapshotter: rt.Snapshotter,
				Platform:    platforms.DefaultSpec(),
			}
			imageSvc.UpdateRuntimeSnapshotter(runtimeName, imagePlatform)
		}
	}

	if warnings, err := criconfig.ValidateServerConfig(ic.Context, config); err != nil {
		return nil, fmt.Errorf("invalid cri image config: %w", err)
	} else if len(warnings) > 0 {
		ws, err := ic.GetSingle(plugins.WarningPlugin)
		if err != nil {
			return nil, err
		}
		warn := ws.(warning.Service)
		for _, w := range warnings {
			warn.Emit(ic.Context, w)
		}
	}

	log.G(ctx).Info("Connect containerd service")
	client, err := containerd.New(
		"",
		containerd.WithDefaultNamespace(constants.K8sContainerdNamespace),
		containerd.WithDefaultPlatform(platforms.Default()),
		containerd.WithInMemoryServices(ic),