Skip to content

Commit 746bcf2

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

6 files changed

Lines changed: 104 additions & 11 deletions

File tree

pkg/cri/cri.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
6565
ic.Meta.Exports = map[string]string{"CRIVersion": constants.CRIVersion, "CRIVersionAlpha": constants.CRIVersionAlpha}
6666
ctx := ic.Context
6767
pluginConfig := ic.Config.(*criconfig.PluginConfig)
68+
ws, err := ic.Get(plugin.WarningPlugin)
69+
if err != nil {
70+
return nil, err
71+
}
72+
warn := ws.(warning.Service)
73+
6874
if warnings, err := criconfig.ValidatePluginConfig(ctx, pluginConfig); err != nil {
6975
return nil, fmt.Errorf("invalid plugin config: %w", err)
7076
} else if len(warnings) > 0 {
71-
ws, err := ic.Get(plugin.WarningPlugin)
72-
if err != nil {
73-
return nil, err
74-
}
75-
warn := ws.(warning.Service)
7677
for _, w := range warnings {
7778
warn.Emit(ctx, w)
7879
}
@@ -107,7 +108,7 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
107108
return nil, fmt.Errorf("failed to create containerd client: %w", err)
108109
}
109110

110-
s, err := server.NewCRIService(c, client)
111+
s, err := server.NewCRIService(c, client, warn)
111112
if err != nil {
112113
return nil, fmt.Errorf("failed to create CRI service: %w", err)
113114
}

pkg/cri/server/instrumented_service.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ package server
1818

1919
import (
2020
"errors"
21+
"sync"
2122

2223
"github.com/containerd/containerd/errdefs"
2324
"github.com/containerd/containerd/log"
25+
"github.com/containerd/containerd/services/warning"
2426
"golang.org/x/net/context"
2527
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2628
runtime_alpha "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
2729

2830
ctrdutil "github.com/containerd/containerd/pkg/cri/util"
31+
"github.com/containerd/containerd/pkg/deprecation"
2932
)
3033

3134
// instrumentedService wraps service with containerd namespace and logs.
@@ -39,11 +42,13 @@ func newInstrumentedService(c *criService) grpcServices {
3942

4043
// instrumentedAlphaService wraps service with containerd namespace and logs.
4144
type instrumentedAlphaService struct {
42-
c *criService
45+
c *criService
46+
warn warning.Service
47+
emitWarning sync.Once
4348
}
4449

45-
func newInstrumentedAlphaService(c *criService) grpcAlphaServices {
46-
return &instrumentedAlphaService{c: c}
50+
func newInstrumentedAlphaService(c *criService, warn warning.Service) grpcAlphaServices {
51+
return &instrumentedAlphaService{c: c, warn: warn}
4752
}
4853

4954
// checkInitialized returns error if the server is not fully initialized.
@@ -1427,6 +1432,13 @@ func (in *instrumentedService) Status(ctx context.Context, r *runtime.StatusRequ
14271432
}
14281433

14291434
func (in *instrumentedAlphaService) Status(ctx context.Context, r *runtime_alpha.StatusRequest) (res *runtime_alpha.StatusResponse, err error) {
1435+
// Only emit the warning the first time an v1alpha2 api is called
1436+
in.emitWarning.Do(func() {
1437+
log.G(ctx).Warning("CRI API v1alpha2 is deprecated since containerd v1.7 and removed in containerd v2.0. Use CRI API v1 instead.")
1438+
if in.warn != nil {
1439+
in.warn.Emit(ctx, deprecation.CRIAPIV1Alpha2)
1440+
}
1441+
})
14301442
if err := in.checkInitialized(); err != nil {
14311443
return nil, err
14321444
}
@@ -1480,6 +1492,13 @@ func (in *instrumentedService) Version(ctx context.Context, r *runtime.VersionRe
14801492
}
14811493

14821494
func (in *instrumentedAlphaService) Version(ctx context.Context, r *runtime_alpha.VersionRequest) (res *runtime_alpha.VersionResponse, err error) {
1495+
// Only emit the warning the first time the v1alpha2 api is called
1496+
in.emitWarning.Do(func() {
1497+
log.G(ctx).Warning("CRI API v1alpha2 is deprecated since containerd v1.7 and removed in containerd v2.0. Use CRI API v1 instead.")
1498+
if in.warn != nil {
1499+
in.warn.Emit(ctx, deprecation.CRIAPIV1Alpha2)
1500+
}
1501+
})
14831502
if err := in.checkInitialized(); err != nil {
14841503
return nil, err
14851504
}

pkg/cri/server/service.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/containerd/containerd/pkg/cri/streaming"
3232
"github.com/containerd/containerd/pkg/kmutex"
3333
"github.com/containerd/containerd/plugin"
34+
"github.com/containerd/containerd/services/warning"
3435
cni "github.com/containerd/go-cni"
3536
"github.com/sirupsen/logrus"
3637
"google.golang.org/grpc"
@@ -118,10 +119,12 @@ type criService struct {
118119
// one in-flight fetch request or unpack handler for a given descriptor's
119120
// or chain ID.
120121
unpackDuplicationSuppressor kmutex.KeyedLocker
122+
// warn is used to emit warnings for cri-api v1alpha2 usage.
123+
warn warning.Service
121124
}
122125

123126
// NewCRIService returns a new instance of CRIService
124-
func NewCRIService(config criconfig.Config, client *containerd.Client) (CRIService, error) {
127+
func NewCRIService(config criconfig.Config, client *containerd.Client, warn warning.Service) (CRIService, error) {
125128
var err error
126129
labels := label.NewStore()
127130
c := &criService{
@@ -137,6 +140,7 @@ func NewCRIService(config criconfig.Config, client *containerd.Client) (CRIServi
137140
initialized: atomic.NewBool(false),
138141
netPlugin: make(map[string]cni.CNI),
139142
unpackDuplicationSuppressor: kmutex.New(),
143+
warn: warn,
140144
}
141145

142146
if client.SnapshotService(c.config.ContainerdConfig.Snapshotter) == nil {
@@ -319,7 +323,7 @@ func (c *criService) register(s *grpc.Server) error {
319323
instrumented := newInstrumentedService(c)
320324
runtime.RegisterRuntimeServiceServer(s, instrumented)
321325
runtime.RegisterImageServiceServer(s, instrumented)
322-
instrumentedAlpha := newInstrumentedAlphaService(c)
326+
instrumentedAlpha := newInstrumentedAlphaService(c, c.warn)
323327
runtime_alpha.RegisterRuntimeServiceServer(s, instrumentedAlpha)
324328
runtime_alpha.RegisterImageServiceServer(s, instrumentedAlpha)
325329
return nil

pkg/cri/server/service_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package server
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"os"
2223
"testing"
@@ -25,14 +26,17 @@ import (
2526
"github.com/containerd/go-cni"
2627
"github.com/stretchr/testify/assert"
2728
"github.com/stretchr/testify/require"
29+
"k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
2830

31+
"github.com/containerd/containerd/pkg/atomic"
2932
criconfig "github.com/containerd/containerd/pkg/cri/config"
3033
servertesting "github.com/containerd/containerd/pkg/cri/server/testing"
3134
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
3235
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
3336
"github.com/containerd/containerd/pkg/cri/store/label"
3437
sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
3538
snapshotstore "github.com/containerd/containerd/pkg/cri/store/snapshot"
39+
"github.com/containerd/containerd/pkg/deprecation"
3640
ostesting "github.com/containerd/containerd/pkg/os/testing"
3741
"github.com/containerd/containerd/pkg/registrar"
3842
)
@@ -70,6 +74,7 @@ func newTestCRIService() *criService {
7074
netPlugin: map[string]cni.CNI{
7175
defaultNetworkPlugin: servertesting.NewFakeCNIPlugin(),
7276
},
77+
initialized: atomic.NewBool(false),
7378
}
7479
}
7580

@@ -103,3 +108,18 @@ func TestLoadBaseOCISpec(t *testing.T) {
103108
assert.Equal(t, "1.0.2", out.Version)
104109
assert.Equal(t, "default", out.Hostname)
105110
}
111+
112+
func TestAlphaCRIWarning(t *testing.T) {
113+
ctx := context.Background()
114+
ws := servertesting.NewFakeWarningService()
115+
c := newInstrumentedAlphaService(newTestCRIService(), ws)
116+
117+
c.Version(ctx, &v1alpha2.VersionRequest{})
118+
c.Status(ctx, &v1alpha2.StatusRequest{})
119+
120+
// Only emit the warning the first time an v1alpha2 api is called.
121+
expectedWarnings := []deprecation.Warning{
122+
deprecation.CRIAPIV1Alpha2,
123+
}
124+
assert.Equal(t, expectedWarnings, ws.GetWarnings())
125+
}
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)