Skip to content

Commit

Permalink
warning: new service for deprecations
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Karp <samuelkarp@google.com>
(cherry picked from commit 240733c)
Signed-off-by: Samuel Karp <samuelkarp@google.com>
  • Loading branch information
samuelkarp committed Nov 4, 2023
1 parent da1b441 commit de3cb4c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/containerd/builtins.go
Expand Up @@ -36,5 +36,6 @@ import (
_ "github.com/containerd/containerd/services/snapshots"
_ "github.com/containerd/containerd/services/tasks"
_ "github.com/containerd/containerd/services/version"
_ "github.com/containerd/containerd/services/warning"
_ "github.com/containerd/containerd/tracing/plugin"
)
5 changes: 4 additions & 1 deletion plugin/plugin.go
Expand Up @@ -78,6 +78,8 @@ const (
EventPlugin Type = "io.containerd.event.v1"
// TracingProcessorPlugin implements a open telemetry span processor
TracingProcessorPlugin Type = "io.containerd.tracing.processor.v1"
// WarningPlugin implements a warning service
WarningPlugin Type = "io.containerd.warning.v1"
)

const (
Expand All @@ -86,7 +88,8 @@ const (
// RuntimeRuncV1 is the runc runtime that supports a single container
RuntimeRuncV1 = "io.containerd.runc.v1"
// RuntimeRuncV2 is the runc runtime that supports multiple containers per shim
RuntimeRuncV2 = "io.containerd.runc.v2"
RuntimeRuncV2 = "io.containerd.runc.v2"
DeprecationsPlugin = "deprecations"
)

// Registration contains information for registering a plugin
Expand Down
83 changes: 83 additions & 0 deletions services/warning/service.go
@@ -0,0 +1,83 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package warning

import (
"context"
"sync"
"time"

"github.com/containerd/log"

deprecation "github.com/containerd/containerd/pkg/deprecation"
"github.com/containerd/containerd/plugin"
)

type Service interface {
Emit(context.Context, deprecation.Warning)
Warnings() []Warning
}

func init() {
plugin.Register(&plugin.Registration{
Type: plugin.WarningPlugin,
ID: plugin.DeprecationsPlugin,
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
return &service{warnings: make(map[deprecation.Warning]time.Time)}, nil
},
})
}

type Warning struct {
ID deprecation.Warning
LastOccurrence time.Time
Message string
}

var _ Service = (*service)(nil)

type service struct {
warnings map[deprecation.Warning]time.Time
m sync.RWMutex
}

func (s *service) Emit(ctx context.Context, warning deprecation.Warning) {
if !deprecation.Valid(warning) {
log.G(ctx).WithField("warningID", string(warning)).Warn("invalid deprecation warning")
return
}
s.m.Lock()
defer s.m.Unlock()
s.warnings[warning] = time.Now()
}
func (s *service) Warnings() []Warning {
s.m.RLock()
defer s.m.RUnlock()
var warnings []Warning
for k, v := range s.warnings {
msg, ok := deprecation.Message(k)
if !ok {
continue
}
warnings = append(warnings, Warning{
ID: k,
LastOccurrence: v,
Message: msg,
})
}
return warnings
}

0 comments on commit de3cb4c

Please sign in to comment.