Skip to content

Commit de3cb4c

Browse files
committed
warning: new service for deprecations
Signed-off-by: Samuel Karp <[email protected]> (cherry picked from commit 240733c) Signed-off-by: Samuel Karp <[email protected]>
1 parent da1b441 commit de3cb4c

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

cmd/containerd/builtins.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ import (
3636
_ "github.com/containerd/containerd/services/snapshots"
3737
_ "github.com/containerd/containerd/services/tasks"
3838
_ "github.com/containerd/containerd/services/version"
39+
_ "github.com/containerd/containerd/services/warning"
3940
_ "github.com/containerd/containerd/tracing/plugin"
4041
)

plugin/plugin.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ const (
7878
EventPlugin Type = "io.containerd.event.v1"
7979
// TracingProcessorPlugin implements a open telemetry span processor
8080
TracingProcessorPlugin Type = "io.containerd.tracing.processor.v1"
81+
// WarningPlugin implements a warning service
82+
WarningPlugin Type = "io.containerd.warning.v1"
8183
)
8284

8385
const (
@@ -86,7 +88,8 @@ const (
8688
// RuntimeRuncV1 is the runc runtime that supports a single container
8789
RuntimeRuncV1 = "io.containerd.runc.v1"
8890
// RuntimeRuncV2 is the runc runtime that supports multiple containers per shim
89-
RuntimeRuncV2 = "io.containerd.runc.v2"
91+
RuntimeRuncV2 = "io.containerd.runc.v2"
92+
DeprecationsPlugin = "deprecations"
9093
)
9194

9295
// Registration contains information for registering a plugin

services/warning/service.go

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

0 commit comments

Comments
 (0)