Skip to content

Commit d23ac11

Browse files
committed
Split CRI image service from GRPC handler
Prepares the CRI image service for splitting CRI into multiple plugins. Also prepares for config migration which will spread across multiple different plugins. Signed-off-by: Derek McGowan <[email protected]>
1 parent a2f167e commit d23ac11

18 files changed

+346
-198
lines changed

contrib/fuzz/cri_fuzzer.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ func printExecutions() {
8080
}
8181
}
8282

83-
func fuzzCRI(f *fuzz.ConsumeFuzzer, c server.CRIService) int {
83+
type fuzzCRIService interface {
84+
server.CRIService
85+
runtime.RuntimeServiceServer
86+
runtime.ImageServiceServer
87+
}
88+
89+
func fuzzCRI(f *fuzz.ConsumeFuzzer, c fuzzCRIService) int {
8490
executionOrder = make([]string, 0)
8591
defer printExecutions()
8692

@@ -157,7 +163,7 @@ func logExecution(apiName, request string) {
157163

158164
// createContainerFuzz creates a CreateContainerRequest and passes
159165
// it to c.CreateContainer
160-
func createContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
166+
func createContainerFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
161167
r := &runtime.CreateContainerRequest{}
162168
err := f.GenerateStruct(r)
163169
if err != nil {
@@ -171,7 +177,7 @@ func createContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
171177

172178
// removeContainerFuzz creates a RemoveContainerRequest and passes
173179
// it to c.RemoveContainer
174-
func removeContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
180+
func removeContainerFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
175181
r := &runtime.RemoveContainerRequest{}
176182
err := f.GenerateStruct(r)
177183
if err != nil {
@@ -183,7 +189,7 @@ func removeContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
183189
return nil
184190
}
185191

186-
func sandboxStore(cs server.CRIService) (*sandboxstore.Store, error) {
192+
func sandboxStore(cs fuzzCRIService) (*sandboxstore.Store, error) {
187193
var (
188194
ss *sandboxstore.Store
189195
err error
@@ -201,7 +207,7 @@ func sandboxStore(cs server.CRIService) (*sandboxstore.Store, error) {
201207
}
202208

203209
// addSandboxesFuzz creates a sandbox and adds it to the sandboxstore
204-
func addSandboxesFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
210+
func addSandboxesFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
205211
quantity, err := f.GetInt()
206212
if err != nil {
207213
return err
@@ -246,7 +252,7 @@ func getSandboxFuzz(f *fuzz.ConsumeFuzzer) (sandboxstore.Sandbox, error) {
246252

247253
// listContainersFuzz creates a ListContainersRequest and passes
248254
// it to c.ListContainers
249-
func listContainersFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
255+
func listContainersFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
250256
r := &runtime.ListContainersRequest{}
251257
err := f.GenerateStruct(r)
252258
if err != nil {
@@ -260,7 +266,7 @@ func listContainersFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
260266

261267
// startContainerFuzz creates a StartContainerRequest and passes
262268
// it to c.StartContainer
263-
func startContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
269+
func startContainerFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
264270
r := &runtime.StartContainerRequest{}
265271
err := f.GenerateStruct(r)
266272
if err != nil {
@@ -274,7 +280,7 @@ func startContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
274280

275281
// containerStatsFuzz creates a ContainerStatsRequest and passes
276282
// it to c.ContainerStats
277-
func containerStatsFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
283+
func containerStatsFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
278284
r := &runtime.ContainerStatsRequest{}
279285
err := f.GenerateStruct(r)
280286
if err != nil {
@@ -288,7 +294,7 @@ func containerStatsFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
288294

289295
// listContainerStatsFuzz creates a ListContainerStatsRequest and
290296
// passes it to c.ListContainerStats
291-
func listContainerStatsFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
297+
func listContainerStatsFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
292298
r := &runtime.ListContainerStatsRequest{}
293299
err := f.GenerateStruct(r)
294300
if err != nil {
@@ -302,7 +308,7 @@ func listContainerStatsFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
302308

303309
// containerStatusFuzz creates a ContainerStatusRequest and passes
304310
// it to c.ContainerStatus
305-
func containerStatusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
311+
func containerStatusFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
306312
r := &runtime.ContainerStatusRequest{}
307313
err := f.GenerateStruct(r)
308314
if err != nil {
@@ -316,7 +322,7 @@ func containerStatusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
316322

317323
// stopContainerFuzz creates a StopContainerRequest and passes
318324
// it to c.StopContainer
319-
func stopContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
325+
func stopContainerFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
320326
r := &runtime.StopContainerRequest{}
321327
err := f.GenerateStruct(r)
322328
if err != nil {
@@ -330,7 +336,7 @@ func stopContainerFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
330336

331337
// updateContainerResourcesFuzz creates a UpdateContainerResourcesRequest
332338
// and passes it to c.UpdateContainerResources
333-
func updateContainerResourcesFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
339+
func updateContainerResourcesFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
334340
r := &runtime.UpdateContainerResourcesRequest{}
335341
err := f.GenerateStruct(r)
336342
if err != nil {
@@ -344,7 +350,7 @@ func updateContainerResourcesFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) er
344350

345351
// listImagesFuzz creates a ListImagesRequest and passes it to
346352
// c.ListImages
347-
func listImagesFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
353+
func listImagesFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
348354
r := &runtime.ListImagesRequest{}
349355
err := f.GenerateStruct(r)
350356
if err != nil {
@@ -358,7 +364,7 @@ func listImagesFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
358364

359365
// removeImagesFuzz creates a RemoveImageRequest and passes it to
360366
// c.RemoveImage
361-
func removeImagesFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
367+
func removeImagesFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
362368
r := &runtime.RemoveImageRequest{}
363369
err := f.GenerateStruct(r)
364370
if err != nil {
@@ -372,7 +378,7 @@ func removeImagesFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
372378

373379
// imageStatusFuzz creates an ImageStatusRequest and passes it to
374380
// c.ImageStatus
375-
func imageStatusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
381+
func imageStatusFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
376382
r := &runtime.ImageStatusRequest{}
377383
err := f.GenerateStruct(r)
378384
if err != nil {
@@ -386,7 +392,7 @@ func imageStatusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
386392

387393
// imageFsInfoFuzz creates an ImageFsInfoRequest and passes it to
388394
// c.ImageFsInfo
389-
func imageFsInfoFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
395+
func imageFsInfoFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
390396
r := &runtime.ImageFsInfoRequest{}
391397
err := f.GenerateStruct(r)
392398
if err != nil {
@@ -400,7 +406,7 @@ func imageFsInfoFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
400406

401407
// listPodSandboxFuzz creates a ListPodSandboxRequest and passes
402408
// it to c.ListPodSandbox
403-
func listPodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
409+
func listPodSandboxFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
404410
r := &runtime.ListPodSandboxRequest{}
405411
err := f.GenerateStruct(r)
406412
if err != nil {
@@ -414,7 +420,7 @@ func listPodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
414420

415421
// portForwardFuzz creates a PortForwardRequest and passes it to
416422
// c.PortForward
417-
func portForwardFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
423+
func portForwardFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
418424
r := &runtime.PortForwardRequest{}
419425
err := f.GenerateStruct(r)
420426
if err != nil {
@@ -428,7 +434,7 @@ func portForwardFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
428434

429435
// removePodSandboxFuzz creates a RemovePodSandboxRequest and
430436
// passes it to c.RemovePodSandbox
431-
func removePodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
437+
func removePodSandboxFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
432438
r := &runtime.RemovePodSandboxRequest{}
433439
err := f.GenerateStruct(r)
434440
if err != nil {
@@ -442,7 +448,7 @@ func removePodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
442448

443449
// runPodSandboxFuzz creates a RunPodSandboxRequest and passes
444450
// it to c.RunPodSandbox
445-
func runPodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
451+
func runPodSandboxFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
446452
r := &runtime.RunPodSandboxRequest{}
447453
err := f.GenerateStruct(r)
448454
if err != nil {
@@ -456,7 +462,7 @@ func runPodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
456462

457463
// podSandboxStatusFuzz creates a PodSandboxStatusRequest and
458464
// passes it to
459-
func podSandboxStatusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
465+
func podSandboxStatusFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
460466
r := &runtime.PodSandboxStatusRequest{}
461467
err := f.GenerateStruct(r)
462468
if err != nil {
@@ -470,7 +476,7 @@ func podSandboxStatusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
470476

471477
// stopPodSandboxFuzz creates a StopPodSandboxRequest and passes
472478
// it to c.StopPodSandbox
473-
func stopPodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
479+
func stopPodSandboxFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
474480
r := &runtime.StopPodSandboxRequest{}
475481
err := f.GenerateStruct(r)
476482
if err != nil {
@@ -483,7 +489,7 @@ func stopPodSandboxFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
483489
}
484490

485491
// statusFuzz creates a StatusRequest and passes it to c.Status
486-
func statusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
492+
func statusFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
487493
r := &runtime.StatusRequest{}
488494
err := f.GenerateStruct(r)
489495
if err != nil {
@@ -495,7 +501,7 @@ func statusFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
495501
return nil
496502
}
497503

498-
func updateRuntimeConfigFuzz(c server.CRIService, f *fuzz.ConsumeFuzzer) error {
504+
func updateRuntimeConfigFuzz(c fuzzCRIService, f *fuzz.ConsumeFuzzer) error {
499505
r := &runtime.UpdateRuntimeConfigRequest{}
500506
err := f.GenerateStruct(r)
501507
if err != nil {

contrib/fuzz/cri_server_fuzzer.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ package fuzz
2020

2121
import (
2222
fuzz "github.com/AdaLogics/go-fuzz-headers"
23+
"google.golang.org/grpc"
24+
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2325

2426
containerd "github.com/containerd/containerd/v2/client"
2527
"github.com/containerd/containerd/v2/oci"
2628
criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
29+
"github.com/containerd/containerd/v2/pkg/cri/instrument"
2730
"github.com/containerd/containerd/v2/pkg/cri/server"
28-
"github.com/containerd/containerd/v2/pkg/cri/server/base"
2931
"github.com/containerd/containerd/v2/pkg/cri/server/images"
3032
)
3133

@@ -42,20 +44,38 @@ func FuzzCRIServer(data []byte) int {
4244

4345
config := criconfig.Config{}
4446

45-
criBase := &base.CRIBase{
46-
Config: config,
47-
BaseOCISpecs: map[string]*oci.Spec{},
48-
}
49-
50-
imageService, err := images.NewService(config, client)
47+
imageService, err := images.NewService(config, map[string]string{}, client)
5148
if err != nil {
5249
panic(err)
5350
}
5451

55-
c, err := server.NewCRIService(criBase, imageService, client, nil)
52+
is := images.NewGRPCService(imageService)
53+
54+
c, rs, err := server.NewCRIService(config, &server.CRIServiceOptions{
55+
ImageService: imageService,
56+
Client: client,
57+
BaseOCISpecs: map[string]*oci.Spec{},
58+
})
5659
if err != nil {
5760
panic(err)
5861
}
5962

60-
return fuzzCRI(f, c)
63+
return fuzzCRI(f, &service{
64+
CRIService: c,
65+
RuntimeServiceServer: rs,
66+
ImageServiceServer: is,
67+
})
68+
}
69+
70+
type service struct {
71+
server.CRIService
72+
runtime.RuntimeServiceServer
73+
runtime.ImageServiceServer
74+
}
75+
76+
func (c *service) Register(s *grpc.Server) error {
77+
instrumented := instrument.NewService(c)
78+
runtime.RegisterRuntimeServiceServer(s, instrumented)
79+
runtime.RegisterImageServiceServer(s, instrumented)
80+
return nil
6181
}

integration/image_pull_timeout_test.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,13 @@ import (
3737
"github.com/containerd/log/logtest"
3838
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3939
"github.com/stretchr/testify/assert"
40-
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
4140

4241
containerd "github.com/containerd/containerd/v2/client"
4342
"github.com/containerd/containerd/v2/content"
4443
"github.com/containerd/containerd/v2/leases"
4544
"github.com/containerd/containerd/v2/namespaces"
46-
"github.com/containerd/containerd/v2/oci"
4745
criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
4846
criserver "github.com/containerd/containerd/v2/pkg/cri/server"
49-
"github.com/containerd/containerd/v2/pkg/cri/server/base"
5047
"github.com/containerd/containerd/v2/pkg/cri/server/images"
5148
)
5249

@@ -94,11 +91,7 @@ func testCRIImagePullTimeoutBySlowCommitWriter(t *testing.T) {
9491

9592
ctx := namespaces.WithNamespace(logtest.WithT(context.Background(), t), k8sNamespace)
9693

97-
_, err = criService.PullImage(ctx, &runtimeapi.PullImageRequest{
98-
Image: &runtimeapi.ImageSpec{
99-
Image: pullProgressTestImageName,
100-
},
101-
})
94+
_, err = criService.PullImage(ctx, pullProgressTestImageName, nil, nil)
10295
assert.NoError(t, err)
10396
}
10497

@@ -217,11 +210,7 @@ func testCRIImagePullTimeoutByHoldingContentOpenWriter(t *testing.T) {
217210
go func() {
218211
defer close(errCh)
219212

220-
_, err := criService.PullImage(ctx, &runtimeapi.PullImageRequest{
221-
Image: &runtimeapi.ImageSpec{
222-
Image: pullProgressTestImageName,
223-
},
224-
})
213+
_, err := criService.PullImage(ctx, pullProgressTestImageName, nil, nil)
225214
errCh <- err
226215
}()
227216

@@ -304,11 +293,7 @@ func testCRIImagePullTimeoutByNoDataTransferred(t *testing.T) {
304293
dctx, _, err := cli.WithLease(ctx)
305294
assert.NoError(t, err)
306295

307-
_, err = criService.PullImage(dctx, &runtimeapi.PullImageRequest{
308-
Image: &runtimeapi.ImageSpec{
309-
Image: fmt.Sprintf("%s/%s", mirrorURL.Host, "containerd/volume-ownership:2.1"),
310-
},
311-
})
296+
_, err = criService.PullImage(dctx, fmt.Sprintf("%s/%s", mirrorURL.Host, "containerd/volume-ownership:2.1"), nil, nil)
312297

313298
assert.Equal(t, context.Canceled, errors.Unwrap(err), "[%v] expected canceled error, but got (%v)", idx, err)
314299
assert.True(t, mirrorSrv.limiter.clearHitCircuitBreaker(), "[%v] expected to hit circuit breaker", idx)
@@ -487,7 +472,7 @@ func (l *ioCopyLimiter) limitedCopy(ctx context.Context, dst io.Writer, src io.R
487472
//
488473
// NOTE: We don't need to start the CRI plugin here because we just need the
489474
// ImageService API.
490-
func initLocalCRIPlugin(client *containerd.Client, tmpDir string, registryCfg criconfig.Registry) (criserver.CRIService, error) {
475+
func initLocalCRIPlugin(client *containerd.Client, tmpDir string, registryCfg criconfig.Registry) (criserver.ImageService, error) {
491476
containerdRootDir := filepath.Join(tmpDir, "root")
492477
criWorkDir := filepath.Join(tmpDir, "cri-plugin")
493478

@@ -505,15 +490,5 @@ func initLocalCRIPlugin(client *containerd.Client, tmpDir string, registryCfg cr
505490
StateDir: filepath.Join(criWorkDir, "state"),
506491
}
507492

508-
criBase := &base.CRIBase{
509-
Config: cfg,
510-
BaseOCISpecs: map[string]*oci.Spec{},
511-
}
512-
513-
imageService, err := images.NewService(cfg, client)
514-
if err != nil {
515-
panic(err)
516-
}
517-
518-
return criserver.NewCRIService(criBase, imageService, client, nil)
493+
return images.NewService(cfg, map[string]string{}, client)
519494
}

0 commit comments

Comments
 (0)