Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 106 additions & 36 deletions pkg/adaptation/adaptation.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,19 @@ func (r *Adaptation) Stop() {
}

// RunPodSandbox relays the corresponding CRI event to plugins.
func (r *Adaptation) RunPodSandbox(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_RUN_POD_SANDBOX
return r.StateChange(ctx, evt)
func (r *Adaptation) RunPodSandbox(ctx context.Context, req *RunPodSandboxRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.runPodSandbox(ctx, req)
if err != nil {
return err
}
}

return nil
}

// UpdatePodSandbox relays the corresponding CRI request to plugins.
Expand All @@ -239,21 +249,51 @@ func (r *Adaptation) UpdatePodSandbox(ctx context.Context, req *UpdatePodSandbox
}

// PostUpdatePodSandbox relays the corresponding CRI event to plugins.
func (r *Adaptation) PostUpdatePodSandbox(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_POST_UPDATE_POD_SANDBOX
return r.StateChange(ctx, evt)
func (r *Adaptation) PostUpdatePodSandbox(ctx context.Context, req *PostUpdatePodSandboxRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.postUpdatePodSandbox(ctx, req)
if err != nil {
return err
}
}

return nil
}

// StopPodSandbox relays the corresponding CRI event to plugins.
func (r *Adaptation) StopPodSandbox(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_STOP_POD_SANDBOX
return r.StateChange(ctx, evt)
func (r *Adaptation) StopPodSandbox(ctx context.Context, req *StopPodSandboxRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.stopPodSandbox(ctx, req)
if err != nil {
return err
}
}

return nil
}

// RemovePodSandbox relays the corresponding CRI event to plugins.
func (r *Adaptation) RemovePodSandbox(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_REMOVE_POD_SANDBOX
return r.StateChange(ctx, evt)
func (r *Adaptation) RemovePodSandbox(ctx context.Context, req *RemovePodSandboxRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.removePodSandbox(ctx, req)
if err != nil {
return err
}
}

return nil
}

// CreateContainer relays the corresponding CRI request to plugins.
Expand Down Expand Up @@ -292,21 +332,51 @@ func (r *Adaptation) CreateContainer(ctx context.Context, req *CreateContainerRe
}

// PostCreateContainer relays the corresponding CRI event to plugins.
func (r *Adaptation) PostCreateContainer(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_POST_CREATE_CONTAINER
return r.StateChange(ctx, evt)
func (r *Adaptation) PostCreateContainer(ctx context.Context, req *PostCreateContainerRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.postCreateContainer(ctx, req)
if err != nil {
return err
}
}

return nil
}

// StartContainer relays the corresponding CRI event to plugins.
func (r *Adaptation) StartContainer(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_START_CONTAINER
return r.StateChange(ctx, evt)
func (r *Adaptation) StartContainer(ctx context.Context, req *StartContainerRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.startContainer(ctx, req)
if err != nil {
return err
}
}

return nil
}

// PostStartContainer relays the corresponding CRI event to plugins.
func (r *Adaptation) PostStartContainer(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_POST_START_CONTAINER
return r.StateChange(ctx, evt)
func (r *Adaptation) PostStartContainer(ctx context.Context, req *PostStartContainerRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.postStartContainer(ctx, req)
if err != nil {
return err
}
}

return nil
}

// UpdateContainer relays the corresponding CRI request to plugins.
Expand All @@ -331,9 +401,19 @@ func (r *Adaptation) UpdateContainer(ctx context.Context, req *UpdateContainerRe
}

// PostUpdateContainer relays the corresponding CRI event to plugins.
func (r *Adaptation) PostUpdateContainer(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_POST_UPDATE_CONTAINER
return r.StateChange(ctx, evt)
func (r *Adaptation) PostUpdateContainer(ctx context.Context, req *PostUpdateContainerRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
_, err := plugin.postUpdateContainer(ctx, req)
if err != nil {
return err
}
}

return nil
}

// StopContainer relays the corresponding CRI request to plugins.
Expand All @@ -358,23 +438,13 @@ func (r *Adaptation) StopContainer(ctx context.Context, req *StopContainerReques
}

// RemoveContainer relays the corresponding CRI event to plugins.
func (r *Adaptation) RemoveContainer(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_REMOVE_CONTAINER
return r.StateChange(ctx, evt)
}

// StateChange relays pod- or container events to plugins.
func (r *Adaptation) StateChange(ctx context.Context, evt *StateChangeEvent) error {
if evt.Event == Event_UNKNOWN {
return errors.New("invalid (unset) event in state change notification")
}

func (r *Adaptation) RemoveContainer(ctx context.Context, req *RemoveContainerRequest) error {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

for _, plugin := range r.plugins {
err := plugin.StateChange(ctx, evt)
_, err := plugin.removeContainer(ctx, req)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/adaptation/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ type (
StateChangeEvent = api.StateChangeEvent
StateChangeResponse = api.StateChangeResponse
RunPodSandboxRequest = api.RunPodSandboxRequest
RunPodSandboxResponse = api.RunPodSandboxResponse
UpdatePodSandboxRequest = api.UpdatePodSandboxRequest
UpdatePodSandboxResponse = api.UpdatePodSandboxResponse
StopPodSandboxRequest = api.StopPodSandboxRequest
StopPodSandboxResponse = api.StopPodSandboxResponse
RemovePodSandboxRequest = api.RemovePodSandboxRequest
RemovePodSandboxResponse = api.RemovePodSandboxResponse
PostUpdatePodSandboxRequest = api.PostUpdatePodSandboxRequest
PostUpdatePodSandboxResponse = api.PostUpdatePodSandboxResponse
StartContainerRequest = api.StartContainerRequest
Expand Down
133 changes: 81 additions & 52 deletions pkg/adaptation/builtin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,46 @@ func (b *BuiltinPlugin) Shutdown(context.Context, *api.ShutdownRequest) (*api.Sh
return &api.ShutdownResponse{}, nil
}

// RunPodSandbox implements PluginService of the NRI API.
func (b *BuiltinPlugin) RunPodSandbox(ctx context.Context, req *api.RunPodSandboxRequest) (*api.RunPodSandboxResponse, error) {
if b.Handlers.RunPodSandbox != nil {
return &api.RunPodSandboxResponse{}, b.Handlers.RunPodSandbox(ctx, req)
}
return &api.RunPodSandboxResponse{}, nil
}

// UpdatePodSandbox implements PluginService of the NRI API.
func (b *BuiltinPlugin) UpdatePodSandbox(ctx context.Context, req *api.UpdatePodSandboxRequest) (*api.UpdatePodSandboxResponse, error) {
if b.Handlers.UpdatePodSandbox != nil {
return b.Handlers.UpdatePodSandbox(ctx, req)
}
return &api.UpdatePodSandboxResponse{}, nil
}

// PostUpdatePodSandbox is a handler for the PostUpdatePodSandbox event.
func (b *BuiltinPlugin) PostUpdatePodSandbox(ctx context.Context, req *api.PostUpdatePodSandboxRequest) (*api.PostUpdatePodSandboxResponse, error) {
if b.Handlers.PostUpdatePodSandbox != nil {
return &api.PostUpdatePodSandboxResponse{}, b.Handlers.PostUpdatePodSandbox(ctx, req)
}
return &api.PostUpdatePodSandboxResponse{}, nil
}

// StopPodSandbox implements PluginService of the NRI API.
func (b *BuiltinPlugin) StopPodSandbox(ctx context.Context, req *api.StopPodSandboxRequest) (*api.StopPodSandboxResponse, error) {
if b.Handlers.StopPodSandbox != nil {
return &api.StopPodSandboxResponse{}, b.Handlers.StopPodSandbox(ctx, req)
}
return &api.StopPodSandboxResponse{}, nil
}

// RemovePodSandbox implements PluginService of the NRI API.
func (b *BuiltinPlugin) RemovePodSandbox(ctx context.Context, req *api.RemovePodSandboxRequest) (*api.RemovePodSandboxResponse, error) {
if b.Handlers.RemovePodSandbox != nil {
return &api.RemovePodSandboxResponse{}, b.Handlers.RemovePodSandbox(ctx, req)
}
return &api.RemovePodSandboxResponse{}, nil
}

// CreateContainer implements PluginService of the NRI API.
func (b *BuiltinPlugin) CreateContainer(ctx context.Context, req *api.CreateContainerRequest) (*api.CreateContainerResponse, error) {
if b.Handlers.CreateContainer != nil {
Expand All @@ -139,6 +179,30 @@ func (b *BuiltinPlugin) CreateContainer(ctx context.Context, req *api.CreateCont
return &api.CreateContainerResponse{}, nil
}

// PostCreateContainer implements PluginsService of the NRI API.
func (b *BuiltinPlugin) PostCreateContainer(ctx context.Context, req *api.PostCreateContainerRequest) (*api.PostCreateContainerResponse, error) {
if b.Handlers.PostCreateContainer != nil {
return &api.PostCreateContainerResponse{}, b.Handlers.PostCreateContainer(ctx, req)
}
return &api.PostCreateContainerResponse{}, nil
}

// StartContainer implements PluginService of the NRI API.
func (b *BuiltinPlugin) StartContainer(ctx context.Context, req *api.StartContainerRequest) (*api.StartContainerResponse, error) {
if b.Handlers.StartContainer != nil {
return &api.StartContainerResponse{}, b.Handlers.StartContainer(ctx, req)
}
return &api.StartContainerResponse{}, nil
}

// PostStartContainer implements PluginService of the NRI API.
func (b *BuiltinPlugin) PostStartContainer(ctx context.Context, req *api.PostStartContainerRequest) (*api.PostStartContainerResponse, error) {
if b.Handlers.PostStartContainer != nil {
return &api.PostStartContainerResponse{}, b.Handlers.PostStartContainer(ctx, req)
}
return &api.PostStartContainerResponse{}, nil
}

// UpdateContainer implements PluginService of the NRI API.
func (b *BuiltinPlugin) UpdateContainer(ctx context.Context, req *api.UpdateContainerRequest) (*api.UpdateContainerResponse, error) {
if b.Handlers.UpdateContainer != nil {
Expand All @@ -147,6 +211,14 @@ func (b *BuiltinPlugin) UpdateContainer(ctx context.Context, req *api.UpdateCont
return &api.UpdateContainerResponse{}, nil
}

// PostUpdateContainer implements PluginService of the NRI API.
func (b *BuiltinPlugin) PostUpdateContainer(ctx context.Context, req *api.PostUpdateContainerRequest) (*api.PostUpdateContainerResponse, error) {
if b.Handlers.PostUpdateContainer != nil {
return &api.PostUpdateContainerResponse{}, b.Handlers.PostUpdateContainer(ctx, req)
}
return &api.PostUpdateContainerResponse{}, nil
}

// StopContainer implements PluginService of the NRI API.
func (b *BuiltinPlugin) StopContainer(ctx context.Context, req *api.StopContainerRequest) (*api.StopContainerResponse, error) {
if b.Handlers.StopContainer != nil {
Expand All @@ -155,61 +227,18 @@ func (b *BuiltinPlugin) StopContainer(ctx context.Context, req *api.StopContaine
return &api.StopContainerResponse{}, nil
}

// StateChange implements PluginService of the NRI API.
func (b *BuiltinPlugin) StateChange(ctx context.Context, evt *api.StateChangeEvent) (*api.StateChangeResponse, error) {
var err error
switch evt.Event {
case api.Event_RUN_POD_SANDBOX:
if b.Handlers.RunPodSandbox != nil {
err = b.Handlers.RunPodSandbox(ctx, evt)
}
case api.Event_STOP_POD_SANDBOX:
if b.Handlers.StopPodSandbox != nil {
err = b.Handlers.StopPodSandbox(ctx, evt)
}
case api.Event_REMOVE_POD_SANDBOX:
if b.Handlers.RemovePodSandbox != nil {
err = b.Handlers.RemovePodSandbox(ctx, evt)
}
case api.Event_POST_CREATE_CONTAINER:
if b.Handlers.PostCreateContainer != nil {
err = b.Handlers.PostCreateContainer(ctx, evt)
}
case api.Event_START_CONTAINER:
if b.Handlers.StartContainer != nil {
err = b.Handlers.StartContainer(ctx, evt)
}
case api.Event_POST_START_CONTAINER:
if b.Handlers.PostStartContainer != nil {
err = b.Handlers.PostStartContainer(ctx, evt)
}
case api.Event_POST_UPDATE_CONTAINER:
if b.Handlers.PostUpdateContainer != nil {
err = b.Handlers.PostUpdateContainer(ctx, evt)
}
case api.Event_REMOVE_CONTAINER:
if b.Handlers.RemoveContainer != nil {
err = b.Handlers.RemoveContainer(ctx, evt)
}
}

return &api.StateChangeResponse{}, err
}

// UpdatePodSandbox implements PluginService of the NRI API.
func (b *BuiltinPlugin) UpdatePodSandbox(ctx context.Context, req *api.UpdatePodSandboxRequest) (*api.UpdatePodSandboxResponse, error) {
if b.Handlers.UpdatePodSandbox != nil {
return b.Handlers.UpdatePodSandbox(ctx, req)
// RemoveContainer implements PluginService of the NRI API.
func (b *BuiltinPlugin) RemoveContainer(ctx context.Context, req *api.RemoveContainerRequest) (*api.RemoveContainerResponse, error) {
if b.Handlers.RemoveContainer != nil {
return &api.RemoveContainerResponse{}, b.Handlers.RemoveContainer(ctx, req)
}
return &api.UpdatePodSandboxResponse{}, nil
return &api.RemoveContainerResponse{}, nil
}

// PostUpdatePodSandbox is a handler for the PostUpdatePodSandbox event.
func (b *BuiltinPlugin) PostUpdatePodSandbox(ctx context.Context, req *api.PostUpdatePodSandboxRequest) error {
if b.Handlers.PostUpdatePodSandbox != nil {
return b.Handlers.PostUpdatePodSandbox(ctx, req)
}
return nil
// StateChange implements PluginService of the NRI API.
func (b *BuiltinPlugin) StateChange(_ context.Context, _ *api.StateChangeEvent) (*api.StateChangeResponse, error) {
// TODO: remove eventually once StateChange is removed from the wire protocol.
return &api.StateChangeResponse{}, nil
}

// ValidateContainerAdjustment implements PluginService of the NRI API.
Expand Down
Loading
Loading