|
| 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