Merge pull request #8763 from slonopotamus/GetTopic

Move GetTopic function out of runc shim
This commit is contained in:
Phil Estes 2023-07-03 09:39:00 -04:00 committed by GitHub
commit 330273d236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 34 deletions

View File

@ -16,6 +16,11 @@
package runtime package runtime
import (
"github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/log"
)
const ( const (
// TaskCreateEventTopic for task create // TaskCreateEventTopic for task create
TaskCreateEventTopic = "/tasks/create" TaskCreateEventTopic = "/tasks/create"
@ -40,3 +45,33 @@ const (
// TaskUnknownTopic for unknown task events // TaskUnknownTopic for unknown task events
TaskUnknownTopic = "/tasks/?" TaskUnknownTopic = "/tasks/?"
) )
// GetTopic converts an event from an interface type to the specific
// event topic id
func GetTopic(e interface{}) string {
switch e.(type) {
case *events.TaskCreate:
return TaskCreateEventTopic
case *events.TaskStart:
return TaskStartEventTopic
case *events.TaskOOM:
return TaskOOMEventTopic
case *events.TaskExit:
return TaskExitEventTopic
case *events.TaskDelete:
return TaskDeleteEventTopic
case *events.TaskExecAdded:
return TaskExecAddedEventTopic
case *events.TaskExecStarted:
return TaskExecStartedEventTopic
case *events.TaskPaused:
return TaskPausedEventTopic
case *events.TaskResumed:
return TaskResumedEventTopic
case *events.TaskCheckpointed:
return TaskCheckpointedEventTopic
default:
log.L.Warnf("no topic for type %#v", e)
}
return TaskUnknownTopic
}

View File

@ -42,6 +42,7 @@ import (
"github.com/containerd/containerd/pkg/userns" "github.com/containerd/containerd/pkg/userns"
"github.com/containerd/containerd/protobuf" "github.com/containerd/containerd/protobuf"
ptypes "github.com/containerd/containerd/protobuf/types" ptypes "github.com/containerd/containerd/protobuf/types"
"github.com/containerd/containerd/runtime"
"github.com/containerd/containerd/runtime/v2/runc" "github.com/containerd/containerd/runtime/v2/runc"
"github.com/containerd/containerd/runtime/v2/runc/options" "github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/containerd/runtime/v2/shim" "github.com/containerd/containerd/runtime/v2/shim"
@ -687,7 +688,7 @@ func (s *service) forward(ctx context.Context, publisher shim.Publisher) {
ns, _ := namespaces.Namespace(ctx) ns, _ := namespaces.Namespace(ctx)
ctx = namespaces.WithNamespace(context.Background(), ns) ctx = namespaces.WithNamespace(context.Background(), ns)
for e := range s.events { for e := range s.events {
err := publisher.Publish(ctx, runc.GetTopic(e), e) err := publisher.Publish(ctx, runtime.GetTopic(e), e)
if err != nil { if err != nil {
log.G(ctx).WithError(err).Error("post event") log.G(ctx).WithError(err).Error("post event")
} }

View File

@ -24,43 +24,10 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/runtime"
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
) )
// GetTopic converts an event from an interface type to the specific
// event topic id
func GetTopic(e interface{}) string {
switch e.(type) {
case *events.TaskCreate:
return runtime.TaskCreateEventTopic
case *events.TaskStart:
return runtime.TaskStartEventTopic
case *events.TaskOOM:
return runtime.TaskOOMEventTopic
case *events.TaskExit:
return runtime.TaskExitEventTopic
case *events.TaskDelete:
return runtime.TaskDeleteEventTopic
case *events.TaskExecAdded:
return runtime.TaskExecAddedEventTopic
case *events.TaskExecStarted:
return runtime.TaskExecStartedEventTopic
case *events.TaskPaused:
return runtime.TaskPausedEventTopic
case *events.TaskResumed:
return runtime.TaskResumedEventTopic
case *events.TaskCheckpointed:
return runtime.TaskCheckpointedEventTopic
default:
logrus.Warnf("no topic for type %#v", e)
}
return runtime.TaskUnknownTopic
}
// ShouldKillAllOnExit reads the bundle's OCI spec and returns true if // ShouldKillAllOnExit reads the bundle's OCI spec and returns true if
// there is an error reading the spec or if the container has a private PID namespace // there is an error reading the spec or if the container has a private PID namespace
func ShouldKillAllOnExit(ctx context.Context, bundlePath string) bool { func ShouldKillAllOnExit(ctx context.Context, bundlePath string) bool {