Skip to content

Commit 240733c

Browse files
committed
warning: new service for deprecations
Signed-off-by: Samuel Karp <[email protected]>
1 parent aff5b80 commit 240733c

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

cmd/containerd/builtins/builtins.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ import (
4646
_ "github.com/containerd/containerd/services/tasks"
4747
_ "github.com/containerd/containerd/services/transfer"
4848
_ "github.com/containerd/containerd/services/version"
49+
_ "github.com/containerd/containerd/services/warning"
4950
)

plugins/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ const (
6565
SandboxControllerPlugin plugin.Type = "io.containerd.sandbox.controller.v1"
6666
// ImageVerifierPlugin implements an image verifier service
6767
ImageVerifierPlugin plugin.Type = "io.containerd.image-verifier.v1"
68+
// WarningPlugin implements a warning service
69+
WarningPlugin plugin.Type = "io.containerd.warning.v1"
6870
)
6971

7072
const (
7173
// RuntimeRuncV2 is the runc runtime that supports multiple containers per shim
7274
RuntimeRuncV2 = "io.containerd.runc.v2"
75+
76+
DeprecationsPlugin = "deprecations"
7377
)
7478

7579
const (

services/warning/service.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 warning
18+
19+
import (
20+
"context"
21+
"sync"
22+
"time"
23+
24+
"github.com/containerd/log"
25+
26+
deprecation "github.com/containerd/containerd/pkg/deprecation"
27+
"github.com/containerd/containerd/plugin"
28+
"github.com/containerd/containerd/plugin/registry"
29+
"github.com/containerd/containerd/plugins"
30+
)
31+
32+
type Service interface {
33+
Emit(context.Context, deprecation.Warning)
34+
Warnings() []Warning
35+
}
36+
37+
func init() {
38+
registry.Register(&plugin.Registration{
39+
Type: plugins.WarningPlugin,
40+
ID: plugins.DeprecationsPlugin,
41+
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
42+
return &service{warnings: make(map[deprecation.Warning]time.Time)}, nil
43+
},
44+
})
45+
}
46+
47+
type Warning struct {
48+
ID deprecation.Warning
49+
LastOccurrence time.Time
50+
Message string
51+
}
52+
53+
var _ Service = (*service)(nil)
54+
55+
func init() {
56+
registry.Register(&plugin.Registration{
57+
Type: plugins.InternalPlugin,
58+
ID: "warning",
59+
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
60+
return &service{warnings: make(map[deprecation.Warning]time.Time)}, nil
61+
},
62+
})
63+
}
64+
65+
type service struct {
66+
warnings map[deprecation.Warning]time.Time
67+
m sync.RWMutex
68+
}
69+
70+
func (s *service) Emit(ctx context.Context, warning deprecation.Warning) {
71+
if !deprecation.Valid(warning) {
72+
log.G(ctx).WithField("warningID", string(warning)).Warn("invalid deprecation warning")
73+
return
74+
}
75+
s.m.Lock()
76+
defer s.m.Unlock()
77+
s.warnings[warning] = time.Now()
78+
}
79+
func (s *service) Warnings() []Warning {
80+
s.m.RLock()
81+
defer s.m.RUnlock()
82+
var warnings []Warning
83+
for k, v := range s.warnings {
84+
msg, ok := deprecation.Message(k)
85+
if !ok {
86+
continue
87+
}
88+
warnings = append(warnings, Warning{
89+
ID: k,
90+
LastOccurrence: v,
91+
Message: msg,
92+
})
93+
}
94+
return warnings
95+
}

0 commit comments

Comments
 (0)