Skip to content

Commit d62cba4

Browse files
committed
Expose usage of cri-api v1alpha2
Signed-off-by: ruiwen-zhao <[email protected]>
1 parent ebe25d0 commit d62cba4

10 files changed

Lines changed: 117 additions & 16 deletions

File tree

contrib/fuzz/cri_sbserver_fuzzer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/containerd/containerd"
2525
criconfig "github.com/containerd/containerd/pkg/cri/config"
2626
"github.com/containerd/containerd/pkg/cri/sbserver"
27+
"github.com/containerd/containerd/pkg/cri/server/testing"
2728
)
2829

2930
func FuzzCRISandboxServer(data []byte) int {
@@ -37,7 +38,7 @@ func FuzzCRISandboxServer(data []byte) int {
3738
}
3839
defer client.Close()
3940

40-
c, err := sbserver.NewCRIService(criconfig.Config{}, client, nil)
41+
c, err := sbserver.NewCRIService(criconfig.Config{}, client, nil, testing.NewFakeWarningService())
4142
if err != nil {
4243
panic(err)
4344
}

contrib/fuzz/cri_server_fuzzer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/containerd/containerd"
2525
criconfig "github.com/containerd/containerd/pkg/cri/config"
2626
"github.com/containerd/containerd/pkg/cri/server"
27+
"github.com/containerd/containerd/pkg/cri/server/testing"
2728
)
2829

2930
func FuzzCRIServer(data []byte) int {
@@ -37,7 +38,7 @@ func FuzzCRIServer(data []byte) int {
3738
}
3839
defer client.Close()
3940

40-
c, err := server.NewCRIService(criconfig.Config{}, client, nil)
41+
c, err := server.NewCRIService(criconfig.Config{}, client, nil, testing.NewFakeWarningService())
4142
if err != nil {
4243
panic(err)
4344
}

integration/image_pull_timeout_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/containerd/containerd/namespaces"
4242
criconfig "github.com/containerd/containerd/pkg/cri/config"
4343
criserver "github.com/containerd/containerd/pkg/cri/server"
44+
servertesting "github.com/containerd/containerd/pkg/cri/server/testing"
4445
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
4546
"github.com/stretchr/testify/assert"
4647
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
@@ -471,5 +472,5 @@ func initLocalCRIPlugin(client *containerd.Client, tmpDir string, registryCfg cr
471472
RootDir: filepath.Join(criWorkDir, "root"),
472473
StateDir: filepath.Join(criWorkDir, "state"),
473474
}
474-
return criserver.NewCRIService(cfg, client, nil)
475+
return criserver.NewCRIService(cfg, client, nil, servertesting.NewFakeWarningService())
475476
}

pkg/cri/cri.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
6060
ic.Meta.Exports = map[string]string{"CRIVersion": constants.CRIVersion, "CRIVersionAlpha": constants.CRIVersionAlpha}
6161
ctx := ic.Context
6262
pluginConfig := ic.Config.(*criconfig.PluginConfig)
63+
ws, err := ic.Get(plugin.WarningPlugin)
64+
if err != nil {
65+
return nil, err
66+
}
67+
warn := ws.(warning.Service)
68+
6369
if warnings, err := criconfig.ValidatePluginConfig(ctx, pluginConfig); err != nil {
6470
return nil, fmt.Errorf("invalid plugin config: %w", err)
6571
} else if len(warnings) > 0 {
66-
ws, err := ic.Get(plugin.WarningPlugin)
67-
if err != nil {
68-
return nil, err
69-
}
70-
warn := ws.(warning.Service)
7172
for _, w := range warnings {
7273
warn.Emit(ctx, w)
7374
}
@@ -100,10 +101,10 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
100101
var s server.CRIService
101102
if os.Getenv("ENABLE_CRI_SANDBOXES") != "" {
102103
log.G(ctx).Info("using experimental CRI Sandbox server - unset ENABLE_CRI_SANDBOXES to disable")
103-
s, err = sbserver.NewCRIService(c, client, getNRIAPI(ic))
104+
s, err = sbserver.NewCRIService(c, client, getNRIAPI(ic), warn)
104105
} else {
105106
log.G(ctx).Info("using legacy CRI server")
106-
s, err = server.NewCRIService(c, client, getNRIAPI(ic))
107+
s, err = server.NewCRIService(c, client, getNRIAPI(ic), warn)
107108
}
108109
if err != nil {
109110
return nil, fmt.Errorf("failed to create CRI service: %w", err)

pkg/cri/instrument/instrumented_service.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ package instrument
1919
import (
2020
"context"
2121
"errors"
22+
"sync"
2223

2324
"github.com/containerd/containerd/errdefs"
2425
"github.com/containerd/containerd/log"
26+
"github.com/containerd/containerd/services/warning"
2527
runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
2628
"github.com/containerd/containerd/tracing"
2729
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2830

2931
ctrdutil "github.com/containerd/containerd/pkg/cri/util"
32+
"github.com/containerd/containerd/pkg/deprecation"
3033
)
3134

3235
const (
@@ -69,10 +72,12 @@ type instrumentedAlphaService struct {
6972
c criService
7073
runtime_alpha.UnimplementedRuntimeServiceServer
7174
runtime_alpha.UnimplementedImageServiceServer
75+
warn warning.Service
76+
emitWarning sync.Once
7277
}
7378

74-
func NewAlphaService(c criService) GRPCAlphaServices {
75-
return &instrumentedAlphaService{c: c}
79+
func NewAlphaService(c criService, warn warning.Service) GRPCAlphaServices {
80+
return &instrumentedAlphaService{c: c, warn: warn}
7681
}
7782

7883
// checkInitialized returns error if the server is not fully initialized.
@@ -1486,6 +1491,13 @@ func (in *instrumentedService) Status(ctx context.Context, r *runtime.StatusRequ
14861491
}
14871492

14881493
func (in *instrumentedAlphaService) Status(ctx context.Context, r *runtime_alpha.StatusRequest) (res *runtime_alpha.StatusResponse, err error) {
1494+
// Only emit the warning the first time an v1alpha2 api is called
1495+
in.emitWarning.Do(func() {
1496+
log.G(ctx).Warning("CRI API v1alpha2 is deprecated since containerd v1.7 and removed in containerd v2.0. Use CRI API v1 instead.")
1497+
if in.warn != nil {
1498+
in.warn.Emit(ctx, deprecation.CRIAPIV1Alpha2)
1499+
}
1500+
})
14891501
if err := in.checkInitialized(); err != nil {
14901502
return nil, err
14911503
}
@@ -1539,6 +1551,13 @@ func (in *instrumentedService) Version(ctx context.Context, r *runtime.VersionRe
15391551
}
15401552

15411553
func (in *instrumentedAlphaService) Version(ctx context.Context, r *runtime_alpha.VersionRequest) (res *runtime_alpha.VersionResponse, err error) {
1554+
// Only emit the warning the first time the v1alpha2 api is called
1555+
in.emitWarning.Do(func() {
1556+
log.G(ctx).Warning("CRI API v1alpha2 is deprecated since containerd v1.7 and removed in containerd v2.0. Use CRI API v1 instead.")
1557+
if in.warn != nil {
1558+
in.warn.Emit(ctx, deprecation.CRIAPIV1Alpha2)
1559+
}
1560+
})
15421561
if err := in.checkInitialized(); err != nil {
15431562
return nil, err
15441563
}

pkg/cri/sbserver/service.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/containerd/containerd/pkg/kmutex"
3636
"github.com/containerd/containerd/plugin"
3737
"github.com/containerd/containerd/sandbox"
38+
"github.com/containerd/containerd/services/warning"
3839
runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
3940
"github.com/containerd/go-cni"
4041
"github.com/sirupsen/logrus"
@@ -122,10 +123,12 @@ type criService struct {
122123
containerEventsChan chan runtime.ContainerEventResponse
123124
// nri is used to hook NRI into CRI request processing.
124125
nri *nri.API
126+
// warn is used to emit warnings for cri-api v1alpha2 usage.
127+
warn warning.Service
125128
}
126129

127130
// NewCRIService returns a new instance of CRIService
128-
func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.API) (CRIService, error) {
131+
func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.API, warn warning.Service) (CRIService, error) {
129132
var err error
130133
labels := label.NewStore()
131134
c := &criService{
@@ -142,6 +145,7 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.
142145
netPlugin: make(map[string]cni.CNI),
143146
unpackDuplicationSuppressor: kmutex.New(),
144147
sandboxControllers: make(map[criconfig.SandboxControllerMode]sandbox.Controller),
148+
warn: warn,
145149
}
146150

147151
// TODO: figure out a proper channel size.
@@ -343,7 +347,7 @@ func (c *criService) register(s *grpc.Server) error {
343347
runtime.RegisterRuntimeServiceServer(s, instrumented)
344348
runtime.RegisterImageServiceServer(s, instrumented)
345349

346-
instrumentedAlpha := instrument.NewAlphaService(c)
350+
instrumentedAlpha := instrument.NewAlphaService(c, c.warn)
347351
runtime_alpha.RegisterRuntimeServiceServer(s, instrumentedAlpha)
348352
runtime_alpha.RegisterImageServiceServer(s, instrumentedAlpha)
349353

pkg/cri/server/service.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/containerd/containerd/pkg/cri/streaming"
3434
"github.com/containerd/containerd/pkg/kmutex"
3535
"github.com/containerd/containerd/plugin"
36+
"github.com/containerd/containerd/services/warning"
3637
runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
3738
cni "github.com/containerd/go-cni"
3839
"github.com/sirupsen/logrus"
@@ -117,10 +118,12 @@ type criService struct {
117118
// containerEventsChan is used to capture container events and send them
118119
// to the caller of GetContainerEvents.
119120
containerEventsChan chan runtime.ContainerEventResponse
121+
// warn is used to emit warnings for cri-api v1alpha2 usage.
122+
warn warning.Service
120123
}
121124

122125
// NewCRIService returns a new instance of CRIService
123-
func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.API) (CRIService, error) {
126+
func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.API, warn warning.Service) (CRIService, error) {
124127
var err error
125128
labels := label.NewStore()
126129
c := &criService{
@@ -136,6 +139,7 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.
136139
initialized: atomic.NewBool(false),
137140
netPlugin: make(map[string]cni.CNI),
138141
unpackDuplicationSuppressor: kmutex.New(),
142+
warn: warn,
139143
}
140144

141145
// TODO: figure out a proper channel size.
@@ -328,7 +332,7 @@ func (c *criService) register(s *grpc.Server) error {
328332
runtime.RegisterRuntimeServiceServer(s, instrumented)
329333
runtime.RegisterImageServiceServer(s, instrumented)
330334

331-
instrumentedAlpha := instrument.NewAlphaService(c)
335+
instrumentedAlpha := instrument.NewAlphaService(c, c.warn)
332336
runtime_alpha.RegisterRuntimeServiceServer(s, instrumentedAlpha)
333337
runtime_alpha.RegisterImageServiceServer(s, instrumentedAlpha)
334338

pkg/cri/server/service_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,27 @@
1717
package server
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"os"
2223
"testing"
2324

2425
"github.com/containerd/containerd/oci"
26+
"github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
2527
"github.com/containerd/go-cni"
2628
"github.com/stretchr/testify/assert"
2729
"github.com/stretchr/testify/require"
2830

31+
"github.com/containerd/containerd/pkg/atomic"
2932
criconfig "github.com/containerd/containerd/pkg/cri/config"
33+
"github.com/containerd/containerd/pkg/cri/instrument"
3034
servertesting "github.com/containerd/containerd/pkg/cri/server/testing"
3135
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
3236
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
3337
"github.com/containerd/containerd/pkg/cri/store/label"
3438
sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
3539
snapshotstore "github.com/containerd/containerd/pkg/cri/store/snapshot"
40+
"github.com/containerd/containerd/pkg/deprecation"
3641
ostesting "github.com/containerd/containerd/pkg/os/testing"
3742
"github.com/containerd/containerd/pkg/registrar"
3843
)
@@ -53,6 +58,7 @@ func newTestCRIService() *criService {
5358
netPlugin: map[string]cni.CNI{
5459
defaultNetworkPlugin: servertesting.NewFakeCNIPlugin(),
5560
},
61+
initialized: atomic.NewBool(false),
5662
}
5763
}
5864

@@ -86,3 +92,18 @@ func TestLoadBaseOCISpec(t *testing.T) {
8692
assert.Equal(t, "1.0.2", out.Version)
8793
assert.Equal(t, "default", out.Hostname)
8894
}
95+
96+
func TestAlphaCRIWarning(t *testing.T) {
97+
ctx := context.Background()
98+
ws := servertesting.NewFakeWarningService()
99+
c := instrument.NewAlphaService(newTestCRIService(), ws)
100+
101+
c.Version(ctx, &v1alpha2.VersionRequest{})
102+
c.Status(ctx, &v1alpha2.StatusRequest{})
103+
104+
// Only emit the warning the first time an v1alpha2 api is called.
105+
expectedWarnings := []deprecation.Warning{
106+
deprecation.CRIAPIV1Alpha2,
107+
}
108+
assert.Equal(t, expectedWarnings, ws.GetWarnings())
109+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testing
18+
19+
import (
20+
"context"
21+
22+
"github.com/containerd/containerd/pkg/deprecation"
23+
"github.com/containerd/containerd/services/warning"
24+
)
25+
26+
// FakeWarningService is a fake service used for test.
27+
type FakeWarningService struct {
28+
warnings []deprecation.Warning
29+
}
30+
31+
// NewFakeWarningService create a FakeWarningService.
32+
func NewFakeWarningService() *FakeWarningService {
33+
return &FakeWarningService{warnings: []deprecation.Warning{}}
34+
}
35+
36+
func (ws *FakeWarningService) Emit(ctx context.Context, w deprecation.Warning) {
37+
ws.warnings = append(ws.warnings, w)
38+
}
39+
40+
func (ws *FakeWarningService) Warnings() []warning.Warning {
41+
return nil
42+
}
43+
44+
func (ws *FakeWarningService) GetWarnings() []deprecation.Warning {
45+
return ws.warnings
46+
}

pkg/deprecation/deprecation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131
CRIRegistryAuths Warning = Prefix + "cri-registry-auths"
3232
// CRIRegistryConfigs is a warning for the use of the `configs` property
3333
CRIRegistryConfigs Warning = Prefix + "cri-registry-configs"
34+
// CRIAPIV1Alpha2 is a warning for the use of CRI-API v1alpha2
35+
CRIAPIV1Alpha2 Warning = Prefix + "cri-api-v1alpha2"
3436
)
3537

3638
var messages = map[Warning]string{
@@ -43,6 +45,7 @@ var messages = map[Warning]string{
4345
"Use `ImagePullSecrets` instead.",
4446
CRIRegistryConfigs: "The `configs` property of `[plugins.\"io.containerd.grpc.v1.cri\".registry]` is deprecated since containerd v1.5 and will be removed in containerd v2.0." +
4547
"Use `config_path` instead.",
48+
CRIAPIV1Alpha2: "CRI API v1alpha2 is deprecated since containerd v1.7 and removed in containerd v2.0. Use CRI API v1 instead.",
4649
}
4750

4851
// Valid checks whether a given Warning is valid

0 commit comments

Comments
 (0)