Skip to content

Commit 44d8d1b

Browse files
authored
koordlet: add nri remove (#2046)
Signed-off-by: Zhang Kang <[email protected]>
1 parent 09ec01d commit 44d8d1b

File tree

6 files changed

+165
-1
lines changed

6 files changed

+165
-1
lines changed

pkg/koordlet/runtimehooks/hooks/hooks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func init() {
106106
rmconfig.PostStopContainer: make([]*Hook, 0),
107107
rmconfig.PostStopPodSandbox: make([]*Hook, 0),
108108
rmconfig.PreUpdateContainerResources: make([]*Hook, 0),
109+
rmconfig.PreRemoveRunPodSandbox: make([]*Hook, 0),
109110
}
110111
}
111112

pkg/koordlet/runtimehooks/nri/server.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type NriServer struct {
6969
}
7070

7171
const (
72-
events = "RunPodSandbox,CreateContainer,UpdateContainer"
72+
events = "RunPodSandbox,RemovePodSandbox,CreateContainer,UpdateContainer"
7373
pluginName = "koordlet_nri"
7474
pluginIdx = "00"
7575
)
@@ -78,6 +78,7 @@ var (
7878
_ = stub.ConfigureInterface(&NriServer{})
7979
_ = stub.SynchronizeInterface(&NriServer{})
8080
_ = stub.RunPodInterface(&NriServer{})
81+
_ = stub.RemovePodInterface(&NriServer{})
8182
_ = stub.CreateContainerInterface(&NriServer{})
8283
_ = stub.UpdateContainerInterface(&NriServer{})
8384
opts []stub.Option
@@ -227,6 +228,23 @@ func (p *NriServer) UpdateContainer(pod *api.PodSandbox, container *api.Containe
227228
return []*api.ContainerUpdate{update}, nil
228229
}
229230

231+
func (p *NriServer) RemovePodSandbox(pod *api.PodSandbox) error {
232+
podCtx := &protocol.PodContext{}
233+
podCtx.FromNri(pod)
234+
// todo: return error or bypass error based on PluginFailurePolicy
235+
err := hooks.RunHooks(p.options.PluginFailurePolicy, rmconfig.PreRemoveRunPodSandbox, podCtx)
236+
if err != nil {
237+
klog.Errorf("nri hooks run error: %v", err)
238+
if p.options.PluginFailurePolicy == rmconfig.PolicyFail {
239+
return err
240+
}
241+
}
242+
podCtx.NriRemoveDone(p.options.Executor)
243+
244+
klog.V(6).Infof("handle NRI RemovePodSandbox successfully, pod %s/%s", pod.GetNamespace(), pod.GetName())
245+
return nil
246+
}
247+
230248
func (p *NriServer) onClose() {
231249
//TODO: consider the pod status during restart
232250
retryFunc := func() (bool, error) {

pkg/koordlet/runtimehooks/nri/server_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package nri
1818

1919
import (
20+
"fmt"
2021
"reflect"
2122
"testing"
2223
"time"
@@ -25,10 +26,22 @@ import (
2526
"github.com/containerd/nri/pkg/stub"
2627

2728
"github.com/koordinator-sh/koordinator/pkg/koordlet/resourceexecutor"
29+
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks"
30+
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/protocol"
2831
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system"
2932
"github.com/koordinator-sh/koordinator/pkg/runtimeproxy/config"
3033
)
3134

35+
type mockPlugin struct{}
36+
37+
func (p *mockPlugin) Register(op hooks.Options) {
38+
hooks.Register(config.PreRemoveRunPodSandbox, "mockPlugin", "mockPlugin remove", p.Remove)
39+
}
40+
41+
func (p *mockPlugin) Remove(proto protocol.HooksProtocol) error {
42+
return fmt.Errorf("mock error")
43+
}
44+
3245
func getDisableStagesMap(stagesSlice []string) map[string]struct{} {
3346
stagesMap := map[string]struct{}{}
3447
for _, item := range stagesSlice {
@@ -508,3 +521,88 @@ func TestNriServer_UpdateContainer(t *testing.T) {
508521
})
509522
}
510523
}
524+
525+
func TestNriServer_RemovePodSandbox(t *testing.T) {
526+
type fields struct {
527+
stub stub.Stub
528+
mask stub.EventMask
529+
options Options
530+
runPodSandbox func(*NriServer, *api.PodSandbox, *api.Container) error
531+
createContainer func(*NriServer, *api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error)
532+
updateContainer func(*NriServer, *api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
533+
plugin *mockPlugin
534+
}
535+
pod := &api.PodSandbox{
536+
Id: "test",
537+
Name: "test",
538+
Uid: "test",
539+
Namespace: "test",
540+
Labels: nil,
541+
Annotations: nil,
542+
Linux: &api.LinuxPodSandbox{
543+
PodOverhead: nil,
544+
PodResources: nil,
545+
CgroupParent: "",
546+
CgroupsPath: "",
547+
Namespaces: nil,
548+
Resources: nil,
549+
},
550+
Pid: 0,
551+
}
552+
type args struct {
553+
pod *api.PodSandbox
554+
}
555+
tests := []struct {
556+
name string
557+
fields fields
558+
args args
559+
wantErr bool
560+
}{
561+
{
562+
name: "RemovePodSandbox success",
563+
fields: fields{
564+
stub: nil,
565+
options: Options{
566+
PluginFailurePolicy: config.PolicyIgnore,
567+
DisableStages: getDisableStagesMap([]string{"PreRemovePodSandbox"}),
568+
Executor: resourceexecutor.NewTestResourceExecutor(),
569+
},
570+
},
571+
args: args{
572+
pod: pod,
573+
},
574+
wantErr: false,
575+
},
576+
{
577+
name: "RemovePodSandbox fail",
578+
fields: fields{
579+
stub: nil,
580+
options: Options{
581+
PluginFailurePolicy: config.PolicyFail,
582+
DisableStages: getDisableStagesMap([]string{"PreRemovePodSandbox"}),
583+
Executor: resourceexecutor.NewTestResourceExecutor(),
584+
},
585+
plugin: &mockPlugin{},
586+
},
587+
args: args{
588+
pod: pod,
589+
},
590+
wantErr: true,
591+
},
592+
}
593+
for _, tt := range tests {
594+
t.Run(tt.name, func(t *testing.T) {
595+
p := &NriServer{
596+
stub: tt.fields.stub,
597+
mask: tt.fields.mask,
598+
options: tt.fields.options,
599+
}
600+
if tt.fields.plugin != nil {
601+
tt.fields.plugin.Register(hooks.Options{})
602+
}
603+
if err := p.RemovePodSandbox(tt.args.pod); (err != nil) != tt.wantErr {
604+
t.Errorf("RemovePodSandbox() error = %v, wantErr %v", err, tt.wantErr)
605+
}
606+
})
607+
}
608+
}

pkg/koordlet/runtimehooks/protocol/pod_context.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ func (p *PodContext) NriDone(executor resourceexecutor.ResourceUpdateExecutor) {
177177
p.Update()
178178
}
179179

180+
func (p *PodContext) NriRemoveDone(executor resourceexecutor.ResourceUpdateExecutor) {
181+
if p.executor == nil {
182+
p.executor = executor
183+
}
184+
p.removeForExt()
185+
p.Update()
186+
}
187+
180188
func (p *PodContext) FromReconciler(podMeta *statesinformer.PodMeta) {
181189
p.Request.FromReconciler(podMeta)
182190
}
@@ -278,3 +286,7 @@ func (p *PodContext) injectForExt() {
278286
}
279287
}
280288
}
289+
290+
func (p *PodContext) removeForExt() {
291+
// TODO: cleanup
292+
}

pkg/koordlet/runtimehooks/protocol/pod_context_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,37 @@ func TestPodContext_NriDone(t *testing.T) {
140140
})
141141
}
142142
}
143+
144+
func TestPodContext_NriRemoveDone(t *testing.T) {
145+
type fields struct {
146+
Request PodRequest
147+
Response PodResponse
148+
executor resourceexecutor.ResourceUpdateExecutor
149+
}
150+
type args struct {
151+
executor resourceexecutor.ResourceUpdateExecutor
152+
}
153+
tests := []struct {
154+
name string
155+
fields fields
156+
args args
157+
}{
158+
{
159+
name: "nri remove done",
160+
fields: fields{
161+
executor: resourceexecutor.NewTestResourceExecutor(),
162+
},
163+
args: args{},
164+
},
165+
}
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
p := &PodContext{
169+
Request: tt.fields.Request,
170+
Response: tt.fields.Response,
171+
executor: tt.fields.executor,
172+
}
173+
p.NriRemoveDone(tt.args.executor)
174+
})
175+
}
176+
}

pkg/runtimeproxy/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const (
5757
PostStartContainer RuntimeHookType = "PostStartContainer"
5858
PreUpdateContainerResources RuntimeHookType = "PreUpdateContainerResources"
5959
PostStopContainer RuntimeHookType = "PostStopContainer"
60+
PreRemoveRunPodSandbox RuntimeHookType = "PreRemoveRunPodSandbox"
6061
NoneRuntimeHookType RuntimeHookType = "NoneRuntimeHookType"
6162
)
6263

0 commit comments

Comments
 (0)