Skip to content

Commit 7ef50ac

Browse files
committed
Reduce ImageService interface dependencies
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent e9dc15b commit 7ef50ac

7 files changed

Lines changed: 21 additions & 51 deletions

File tree

internal/cri/server/images/service.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,9 @@ func (c *CRIImageService) ImageFSPaths() map[string]string {
199199
return c.imageFSPaths
200200
}
201201

202-
// PinnedImage is used to lookup a pinned image by name.
203-
// Most often used to get the "sandbox" image.
204-
func (c *CRIImageService) PinnedImage(name string) string {
205-
return c.config.PinnedImages[name]
202+
// Config returns the image configuration.
203+
func (c *CRIImageService) Config() criconfig.ImageConfig {
204+
return c.config
206205
}
207206

208207
// GRPCService returns a new CRI Image Service grpc server.

internal/cri/server/podsandbox/controller.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"github.com/containerd/containerd/v2/internal/cri/server/podsandbox/types"
3737
imagestore "github.com/containerd/containerd/v2/internal/cri/store/image"
3838
ctrdutil "github.com/containerd/containerd/v2/internal/cri/util"
39-
"github.com/containerd/containerd/v2/pkg/oci"
4039
osinterface "github.com/containerd/containerd/v2/pkg/os"
4140
"github.com/containerd/containerd/v2/pkg/protobuf"
4241
"github.com/containerd/containerd/v2/plugins"
@@ -74,7 +73,6 @@ func init() {
7473
if err != nil {
7574
return nil, fmt.Errorf("unable to load CRI runtime service plugin dependency: %w", err)
7675
}
77-
runtimeService := criRuntimePlugin.(RuntimeService)
7876

7977
// Get image service.
8078
criImagePlugin, err := ic.GetByID(plugins.CRIServicePlugin, "images")
@@ -89,9 +87,9 @@ func init() {
8987

9088
c := Controller{
9189
client: client,
92-
config: runtimeService.Config(),
90+
config: criRuntimePlugin.(interface{ Config() criconfig.Config }).Config(),
91+
imageConfig: criImagePlugin.(interface{ Config() criconfig.ImageConfig }).Config(),
9392
os: osinterface.RealOS{},
94-
runtimeService: runtimeService,
9593
imageService: criImagePlugin.(ImageService),
9694
warningService: warningPlugin.(warning.Service),
9795
store: NewStore(),
@@ -111,28 +109,20 @@ func init() {
111109
})
112110
}
113111

114-
// RuntimeService specifies dependencies to CRI runtime service.
115-
type RuntimeService interface {
116-
Config() criconfig.Config
117-
LoadOCISpec(string) (*oci.Spec, error)
118-
}
119-
120112
// ImageService specifies dependencies to CRI image service.
121113
type ImageService interface {
122114
LocalResolve(refOrID string) (imagestore.Image, error)
123115
GetImage(id string) (imagestore.Image, error)
124116
PullImage(ctx context.Context, name string, creds func(string) (string, string, error), sc *runtime.PodSandboxConfig, runtimeHandler string) (string, error)
125-
RuntimeSnapshotter(ctx context.Context, ociRuntime criconfig.Runtime) string
126-
PinnedImage(string) string
127117
}
128118

129119
type Controller struct {
130120
// config contains all configurations.
131121
config criconfig.Config
122+
// imageConfig contains CRI image configuration.
123+
imageConfig criconfig.ImageConfig
132124
// client is an instance of the containerd client
133125
client *containerd.Client
134-
// runtimeService is a dependency to CRI runtime service.
135-
runtimeService RuntimeService
136126
// imageService is a dependency to CRI image service.
137127
imageService ImageService
138128
// warningService is used to emit deprecation warnings.

internal/cri/server/podsandbox/helpers.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,11 @@ func (c *Controller) toContainerdImage(ctx context.Context, image imagestore.Ima
8383
}
8484

8585
// runtimeSpec returns a default runtime spec used in cri-containerd.
86-
func (c *Controller) runtimeSpec(id string, baseSpecFile string, opts ...oci.SpecOpts) (*runtimespec.Spec, error) {
86+
func (c *Controller) runtimeSpec(id string, opts ...oci.SpecOpts) (*runtimespec.Spec, error) {
8787
// GenerateSpec needs namespace.
8888
ctx := ctrdutil.NamespacedContext()
8989
container := &containers.Container{ID: id}
9090

91-
if baseSpecFile != "" {
92-
baseSpec, err := c.runtimeService.LoadOCISpec(baseSpecFile)
93-
if err != nil {
94-
return nil, fmt.Errorf("can't load base OCI spec %q: %w", baseSpecFile, err)
95-
}
96-
97-
spec := oci.Spec{}
98-
if err := ctrdutil.DeepCopy(&spec, &baseSpec); err != nil {
99-
return nil, fmt.Errorf("failed to clone OCI spec: %w", err)
100-
}
101-
102-
// Fix up cgroups path
103-
applyOpts := append([]oci.SpecOpts{oci.WithNamespacedCgroup()}, opts...)
104-
105-
if err := oci.ApplyOpts(ctx, nil, container, &spec, applyOpts...); err != nil {
106-
return nil, fmt.Errorf("failed to apply OCI options: %w", err)
107-
}
108-
109-
return &spec, nil
110-
}
111-
11291
spec, err := oci.GenerateSpec(ctx, nil, container, opts...)
11392
if err != nil {
11493
return nil, fmt.Errorf("failed to generate spec: %w", err)

internal/cri/server/podsandbox/sandbox_run.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,13 @@ func (c *Controller) Start(ctx context.Context, id string) (cin sandbox.Controll
188188
}
189189
snapshotterOpt = append(snapshotterOpt, extraSOpts...)
190190

191+
sandboxSnapshotter := c.imageConfig.Snapshotter
192+
if ociRuntime.Snapshotter != "" {
193+
sandboxSnapshotter = ociRuntime.Snapshotter
194+
}
195+
191196
opts := []containerd.NewContainerOpts{
192-
containerd.WithSnapshotter(c.imageService.RuntimeSnapshotter(ctx, ociRuntime)),
197+
containerd.WithSnapshotter(sandboxSnapshotter),
193198
customopts.WithNewSnapshot(id, containerdImage, snapshotterOpt...),
194199
containerd.WithSpec(spec, specOpts...),
195200
containerd.WithContainerLabels(sandboxLabels),
@@ -346,11 +351,9 @@ func (c *Controller) ensureImageExists(ctx context.Context, ref string, config *
346351
func (c *Controller) getSandboxImageName() string {
347352
// returns the name of the sandbox image used to scope pod shared resources used by the pod's containers,
348353
// if empty return the default sandbox image.
349-
if c.imageService != nil {
350-
sandboxImage := c.imageService.PinnedImage("sandbox")
351-
if sandboxImage != "" {
352-
return sandboxImage
353-
}
354+
if image, ok := c.imageConfig.PinnedImages["sandbox"]; ok && image != "" {
355+
return image
354356
}
357+
355358
return criconfig.DefaultSandboxImage
356359
}

internal/cri/server/podsandbox/sandbox_run_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (c *Controller) sandboxContainerSpec(id string, config *runtime.PodSandboxC
196196

197197
specOpts = append(specOpts, annotations.DefaultCRIAnnotations(id, "", c.getSandboxImageName(), config, true)...)
198198

199-
return c.runtimeSpec(id, "", specOpts...)
199+
return c.runtimeSpec(id, specOpts...)
200200
}
201201

202202
// sandboxContainerSpecOpts generates OCI spec options for

internal/cri/server/podsandbox/sandbox_run_other.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import (
2727
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2828
)
2929

30-
func (c *Controller) sandboxContainerSpec(id string, config *runtime.PodSandboxConfig,
31-
imageConfig *imagespec.ImageConfig, nsPath string, runtimePodAnnotations []string) (_ *runtimespec.Spec, retErr error) {
32-
return c.runtimeSpec(id, "", annotations.DefaultCRIAnnotations(id, "", c.getSandboxImageName(), config, true)...)
30+
func (c *Controller) sandboxContainerSpec(id string, config *runtime.PodSandboxConfig, _ *imagespec.ImageConfig, _ string, _ []string) (_ *runtimespec.Spec, _ error) {
31+
return c.runtimeSpec(id, annotations.DefaultCRIAnnotations(id, "", c.getSandboxImageName(), config, true)...)
3332
}
3433

3534
// sandboxContainerSpecOpts generates OCI spec options for

internal/cri/server/podsandbox/sandbox_run_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (c *Controller) sandboxContainerSpec(id string, config *runtime.PodSandboxC
8787
annotations.DefaultCRIAnnotations(id, "", c.getSandboxImageName(), config, true)...,
8888
)
8989

90-
return c.runtimeSpec(id, "", specOpts...)
90+
return c.runtimeSpec(id, specOpts...)
9191
}
9292

9393
// No sandbox container spec options for windows yet.

0 commit comments

Comments
 (0)