Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UpdatePodSandbox to NRI plugin API #141

Merged
merged 1 commit into from
Feb 19, 2025
Merged
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
22 changes: 22 additions & 0 deletions pkg/adaptation/adaptation.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ func (r *Adaptation) RunPodSandbox(ctx context.Context, evt *StateChangeEvent) e
return r.StateChange(ctx, evt)
}

// UpdatePodSandbox relays the corresponding CRI request to plugins.
func (r *Adaptation) UpdatePodSandbox(ctx context.Context, req *UpdatePodSandboxRequest) (*UpdatePodSandboxResponse, error) {
r.Lock()
defer r.Unlock()
defer r.removeClosedPlugins()

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

return &UpdatePodSandboxResponse{}, nil
}

// 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)
}

// StopPodSandbox relays the corresponding CRI event to plugins.
func (r *Adaptation) StopPodSandbox(ctx context.Context, evt *StateChangeEvent) error {
evt.Event = Event_STOP_POD_SANDBOX
Expand Down
6 changes: 4 additions & 2 deletions pkg/adaptation/adaptation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ var _ = Describe("Pod and container requests and events", func() {
}
},
Entry("with RunPodSandbox", "RunPodSandbox"),
Entry("with UpdatePodSandbox", "UpdatePodSandbox"),
Entry("with PostUpdatePodSandbox", "PostUpdatePodSandbox"),
Entry("with StopPodSandbox", "StopPodSandbox"),
Entry("with RemovePodSandbox", "RemovePodSandbox"),

Expand All @@ -343,7 +345,7 @@ var _ = Describe("Pod and container requests and events", func() {
"RemoveContainer",
),
Entry("with all pod and container requests and events",
"RunPodSandbox,StopPodSandbox,RemovePodSandbox",
"RunPodSandbox,UpdatePodSandbox,PostUpdatePodSandbox,StopPodSandbox,RemovePodSandbox",
"CreateContainer,PostCreateContainer",
"StartContainer,PostStartContainer",
"UpdateContainer,PostUpdateContainer",
Expand Down Expand Up @@ -419,7 +421,7 @@ var _ = Describe("Pod and container requests and events", func() {
"RemoveContainer",
),
Entry("with all pod and container requests and events",
"RunPodSandbox,StopPodSandbox,RemovePodSandbox",
"RunPodSandbox,UpdatePodSandbox,PostUpdatePodSandbox,StopPodSandbox,RemovePodSandbox",
"CreateContainer,PostCreateContainer",
"StartContainer,PostStartContainer",
"UpdateContainer,PostUpdateContainer",
Expand Down
62 changes: 34 additions & 28 deletions pkg/adaptation/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,25 @@ type (
StopContainerRequest = api.StopContainerRequest
StopContainerResponse = api.StopContainerResponse

StateChangeEvent = api.StateChangeEvent
StateChangeResponse = api.StateChangeResponse
RunPodSandboxRequest = api.RunPodSandboxRequest
StopPodSandboxRequest = api.StopPodSandboxRequest
RemovePodSandboxRequest = api.RemovePodSandboxRequest
StartContainerRequest = api.StartContainerRequest
StartContainerResponse = api.StartContainerResponse
RemoveContainerRequest = api.RemoveContainerRequest
RemoveContainerResponse = api.RemoveContainerResponse
PostCreateContainerRequest = api.PostCreateContainerRequest
PostCreateContainerResponse = api.PostCreateContainerResponse
PostStartContainerRequest = api.PostStartContainerRequest
PostStartContainerResponse = api.PostStartContainerResponse
PostUpdateContainerRequest = api.PostUpdateContainerRequest
PostUpdateContainerResponse = api.PostUpdateContainerResponse
StateChangeEvent = api.StateChangeEvent
StateChangeResponse = api.StateChangeResponse
RunPodSandboxRequest = api.RunPodSandboxRequest
UpdatePodSandboxRequest = api.UpdatePodSandboxRequest
UpdatePodSandboxResponse = api.UpdatePodSandboxResponse
StopPodSandboxRequest = api.StopPodSandboxRequest
RemovePodSandboxRequest = api.RemovePodSandboxRequest
PostUpdatePodSandboxRequest = api.PostUpdatePodSandboxRequest
PostUpdatePodSandboxResponse = api.PostUpdatePodSandboxResponse
StartContainerRequest = api.StartContainerRequest
StartContainerResponse = api.StartContainerResponse
RemoveContainerRequest = api.RemoveContainerRequest
RemoveContainerResponse = api.RemoveContainerResponse
PostCreateContainerRequest = api.PostCreateContainerRequest
PostCreateContainerResponse = api.PostCreateContainerResponse
PostStartContainerRequest = api.PostStartContainerRequest
PostStartContainerResponse = api.PostStartContainerResponse
PostUpdateContainerRequest = api.PostUpdateContainerRequest
PostUpdateContainerResponse = api.PostUpdateContainerResponse

PodSandbox = api.PodSandbox
LinuxPodSandbox = api.LinuxPodSandbox
Expand Down Expand Up @@ -90,19 +94,21 @@ type (
// Aliased consts for api/api.proto.
// nolint
const (
Event_UNKNOWN = api.Event_UNKNOWN
Event_RUN_POD_SANDBOX = api.Event_RUN_POD_SANDBOX
Event_STOP_POD_SANDBOX = api.Event_STOP_POD_SANDBOX
Event_REMOVE_POD_SANDBOX = api.Event_REMOVE_POD_SANDBOX
Event_CREATE_CONTAINER = api.Event_CREATE_CONTAINER
Event_POST_CREATE_CONTAINER = api.Event_POST_CREATE_CONTAINER
Event_START_CONTAINER = api.Event_START_CONTAINER
Event_POST_START_CONTAINER = api.Event_POST_START_CONTAINER
Event_UPDATE_CONTAINER = api.Event_UPDATE_CONTAINER
Event_POST_UPDATE_CONTAINER = api.Event_POST_UPDATE_CONTAINER
Event_STOP_CONTAINER = api.Event_STOP_CONTAINER
Event_REMOVE_CONTAINER = api.Event_REMOVE_CONTAINER
ValidEvents = api.ValidEvents
Event_UNKNOWN = api.Event_UNKNOWN
Event_RUN_POD_SANDBOX = api.Event_RUN_POD_SANDBOX
Event_UPDATE_POD_SANDBOX = api.Event_UPDATE_POD_SANDBOX
Event_POST_UPDATE_POD_SANDBOX = api.Event_POST_UPDATE_POD_SANDBOX
Event_STOP_POD_SANDBOX = api.Event_STOP_POD_SANDBOX
Event_REMOVE_POD_SANDBOX = api.Event_REMOVE_POD_SANDBOX
Event_CREATE_CONTAINER = api.Event_CREATE_CONTAINER
Event_POST_CREATE_CONTAINER = api.Event_POST_CREATE_CONTAINER
Event_START_CONTAINER = api.Event_START_CONTAINER
Event_POST_START_CONTAINER = api.Event_POST_START_CONTAINER
Event_UPDATE_CONTAINER = api.Event_UPDATE_CONTAINER
Event_POST_UPDATE_CONTAINER = api.Event_POST_UPDATE_CONTAINER
Event_STOP_CONTAINER = api.Event_STOP_CONTAINER
Event_REMOVE_CONTAINER = api.Event_REMOVE_CONTAINER
ValidEvents = api.ValidEvents

ContainerState_CONTAINER_UNKNOWN = api.ContainerState_CONTAINER_UNKNOWN
ContainerState_CONTAINER_CREATED = api.ContainerState_CONTAINER_CREATED
Expand Down
21 changes: 21 additions & 0 deletions pkg/adaptation/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,27 @@ func (p *plugin) stopContainer(ctx context.Context, req *StopContainerRequest) (
return rpl, nil
}

func (p *plugin) updatePodSandbox(ctx context.Context, req *UpdatePodSandboxRequest) (*UpdatePodSandboxResponse, error) {
if !p.events.IsSet(Event_UPDATE_POD_SANDBOX) {
return nil, nil
}

ctx, cancel := context.WithTimeout(ctx, getPluginRequestTimeout())
defer cancel()

if _, err := p.impl.UpdatePodSandbox(ctx, req); err != nil {
if isFatalError(err) {
log.Errorf(ctx, "closing plugin %s, failed to handle event %d: %v",
p.name(), Event_UPDATE_POD_SANDBOX, err)
p.close()
return nil, nil
}
return nil, err
}

return &UpdatePodSandboxResponse{}, nil
}

// Relay other pod or container state change events to the plugin.
func (p *plugin) StateChange(ctx context.Context, evt *StateChangeEvent) (err error) {
if !p.events.IsSet(evt.Event) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/adaptation/plugin_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func (p *pluginType) StopContainer(ctx context.Context, req *StopContainerReques
return p.ttrpcImpl.StopContainer(ctx, req)
}

func (p *pluginType) UpdatePodSandbox(ctx context.Context, req *UpdatePodSandboxRequest) (*UpdatePodSandboxResponse, error) {
if p.wasmImpl != nil {
return p.wasmImpl.UpdatePodSandbox(ctx, req)
}
return p.ttrpcImpl.UpdatePodSandbox(ctx, req)
}

func (p *pluginType) StateChange(ctx context.Context, req *StateChangeEvent) (err error) {
if p.wasmImpl != nil {
_, err = p.wasmImpl.StateChange(ctx, req)
Expand Down
96 changes: 74 additions & 22 deletions pkg/adaptation/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ func (m *mockRuntime) RunPodSandbox(ctx context.Context, evt *api.StateChangeEve
return m.runtime.RunPodSandbox(ctx, evt)
}

func (m *mockRuntime) UpdatePodSandbox(ctx context.Context, req *api.UpdatePodSandboxRequest) (*api.UpdatePodSandboxResponse, error) {
b := m.runtime.BlockPluginSync()
defer b.Unblock()
return m.runtime.UpdatePodSandbox(ctx, req)
}

func (m *mockRuntime) CreateContainer(ctx context.Context, req *api.CreateContainerRequest) (*api.CreateContainerResponse, error) {
b := m.runtime.BlockPluginSync()
defer b.Unblock()
Expand All @@ -242,6 +248,22 @@ func (m *mockRuntime) startStopPodAndContainer(ctx context.Context, pod *api.Pod
return err
}

_, err = m.UpdatePodSandbox(ctx, &api.UpdatePodSandboxRequest{
Pod: pod,
OverheadLinuxResources: &api.LinuxResources{},
LinuxResources: &api.LinuxResources{},
})
if err != nil {
return err
}

err = m.runtime.PostUpdatePodSandbox(ctx, &api.StateChangeEvent{
Pod: pod,
})
if err != nil {
return err
}

_, err = m.CreateContainer(ctx, &api.CreateContainerRequest{
Pod: pod,
Container: ctr,
Expand Down Expand Up @@ -341,25 +363,29 @@ type mockPlugin struct {
pods map[string]*api.PodSandbox
ctrs map[string]*api.Container

runPodSandbox func(*mockPlugin, *api.PodSandbox, *api.Container) error
stopPodSandbox func(*mockPlugin, *api.PodSandbox, *api.Container) error
removePodSandbox func(*mockPlugin, *api.PodSandbox, *api.Container) error
createContainer func(*mockPlugin, *api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error)
postCreateContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
startContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
postStartContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
updateContainer func(*mockPlugin, *api.PodSandbox, *api.Container, *api.LinuxResources) ([]*api.ContainerUpdate, error)
postUpdateContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
stopContainer func(*mockPlugin, *api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
removeContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
runPodSandbox func(*mockPlugin, *api.PodSandbox, *api.Container) error
updatePodSandbox func(*mockPlugin, *api.PodSandbox, *api.LinuxResources, *api.LinuxResources) error
postUpdatePodSandbox func(*mockPlugin, *api.PodSandbox, *api.Container) error
stopPodSandbox func(*mockPlugin, *api.PodSandbox, *api.Container) error
removePodSandbox func(*mockPlugin, *api.PodSandbox, *api.Container) error
createContainer func(*mockPlugin, *api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error)
postCreateContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
startContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
postStartContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
updateContainer func(*mockPlugin, *api.PodSandbox, *api.Container, *api.LinuxResources) ([]*api.ContainerUpdate, error)
postUpdateContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
stopContainer func(*mockPlugin, *api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
removeContainer func(*mockPlugin, *api.PodSandbox, *api.Container) error
}

var (
_ = stub.ConfigureInterface(&mockPlugin{})
_ = stub.SynchronizeInterface(&mockPlugin{})
_ = stub.RunPodInterface(&mockPlugin{})
_ = stub.UpdatePodInterface(&mockPlugin{})
_ = stub.StopPodInterface(&mockPlugin{})
_ = stub.RemovePodInterface(&mockPlugin{})
_ = stub.PostUpdatePodInterface(&mockPlugin{})
_ = stub.CreateContainerInterface(&mockPlugin{})
_ = stub.StartContainerInterface(&mockPlugin{})
_ = stub.UpdateContainerInterface(&mockPlugin{})
Expand Down Expand Up @@ -434,6 +460,12 @@ func (m *mockPlugin) Init(dir string) error {
if m.runPodSandbox == nil {
m.runPodSandbox = nopEvent
}
if m.updatePodSandbox == nil {
m.updatePodSandbox = nopUpdatePodSandbox
}
if m.postUpdatePodSandbox == nil {
m.postUpdatePodSandbox = nopEvent
}
if m.stopPodSandbox == nil {
m.stopPodSandbox = nopEvent
}
Expand Down Expand Up @@ -543,6 +575,20 @@ func (m *mockPlugin) RunPodSandbox(_ context.Context, pod *api.PodSandbox) error
return err
}

func (m *mockPlugin) UpdatePodSandbox(_ context.Context, pod *api.PodSandbox, overhead *api.LinuxResources, res *api.LinuxResources) error {
m.pods[pod.Id] = pod
m.q.Add(PodSandboxEvent(pod, UpdatePodSandbox))

return m.updatePodSandbox(m, pod, overhead, res)
}

func (m *mockPlugin) PostUpdatePodSandbox(_ context.Context, pod *api.PodSandbox) error {
m.pods[pod.Id] = pod
err := m.postUpdatePodSandbox(m, pod, nil)
m.q.Add(PodSandboxEvent(pod, PostUpdatePodSandbox))
return err
}

func (m *mockPlugin) StopPodSandbox(_ context.Context, pod *api.PodSandbox) error {
m.pods[pod.Id] = pod
err := m.stopPodSandbox(m, pod, nil)
Expand Down Expand Up @@ -624,6 +670,10 @@ func nopEvent(*mockPlugin, *api.PodSandbox, *api.Container) error {
return nil
}

func nopUpdatePodSandbox(*mockPlugin, *api.PodSandbox, *api.LinuxResources, *api.LinuxResources) error {
return nil
}

func nopCreateContainer(*mockPlugin, *api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error) {
return nil, nil, nil
}
Expand All @@ -648,17 +698,19 @@ const (
Disconnected = "closed"
Stopped = "stopped"

RunPodSandbox = "RunPodSandbox"
StopPodSandbox = "StopPodSandbox"
RemovePodSandbox = "RemovePodSandbox"
CreateContainer = "CreateContainer"
StartContainer = "StartContainer"
UpdateContainer = "UpdateContainer"
StopContainer = "StopContainer"
RemoveContainer = "RemoveContainer"
PostCreateContainer = "PostCreateContainer"
PostStartContainer = "PostStartContainer"
PostUpdateContainer = "PostUpdateContainer"
RunPodSandbox = "RunPodSandbox"
UpdatePodSandbox = "UpdatePodSandbox"
StopPodSandbox = "StopPodSandbox"
RemovePodSandbox = "RemovePodSandbox"
PostUpdatePodSandbox = "PostUpdatePodSandbox"
CreateContainer = "CreateContainer"
StartContainer = "StartContainer"
UpdateContainer = "UpdateContainer"
StopContainer = "StopContainer"
RemoveContainer = "RemoveContainer"
PostCreateContainer = "PostCreateContainer"
PostStartContainer = "PostStartContainer"
PostUpdateContainer = "PostUpdateContainer"

Error = "Error"
Timeout = ""
Expand Down
Loading
Loading