Skip to content

Commit

Permalink
Fixed how to specify the namespace in Reset function for resources …
Browse files Browse the repository at this point in the history
…with a namespace (#162)

* fix:namespace

* fix:lint

* fix:test

* test:new testcase of multiple namespaces

* fix:remove line
  • Loading branch information
196Ikuchil committed May 10, 2022
1 parent a7c02e4 commit 85c8ac2
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
12 changes: 9 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,22 @@ func (s *Service) DeleteCollection(ctx context.Context, lopts metav1.ListOptions
return xerrors.Errorf("list nodes: %w", err)
}
eg, ctx := errgroup.WithContext(ctx)
nsList, err := s.client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return xerrors.Errorf("list namespaces: %w", err)
}
for _, n := range ns.Items {
n := n
eg.Go(func() error {
// delete pods on specific node
lopts := metav1.ListOptions{
FieldSelector: "spec.nodeName=" + n.Name,
}
// This method deletes all pods on all namespaces scheduled to the specified node.
if err := s.podService.DeleteCollection(ctx, metav1.NamespaceAll, lopts); err != nil {
return xerrors.Errorf("failed to delete pods on node %s: %w\n", n.Name, err)
// This method deletes all pods on specified namespace scheduled to the specified node.
for _, ns := range nsList.Items {
if err := s.podService.DeleteCollection(ctx, ns.GetName(), lopts); err != nil {
return xerrors.Errorf("failed to delete pods on node %s: %w\n", n.Name, err)
}
}

// delete specific node
Expand Down
76 changes: 76 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func TestService_DeleteCollection(t *testing.T) {
},
prepareFakeClientSetFn: func() *fake.Clientset {
c := fake.NewSimpleClientset()
c.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
}, metav1.CreateOptions{})
c.CoreV1().Nodes().Create(context.Background(), &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Expand All @@ -204,6 +209,11 @@ func TestService_DeleteCollection(t *testing.T) {
},
prepareFakeClientSetFn: func() *fake.Clientset {
c := fake.NewSimpleClientset()
c.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
}, metav1.CreateOptions{})
c.CoreV1().Nodes().Create(context.Background(), &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Expand All @@ -225,6 +235,19 @@ func TestService_DeleteCollection(t *testing.T) {
},
prepareFakeClientSetFn: func() *fake.Clientset {
c := fake.NewSimpleClientset()
c.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
}, metav1.CreateOptions{})
c.CoreV1().Pods("default").Create(context.Background(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
},
Spec: corev1.PodSpec{
NodeName: "node1",
},
}, metav1.CreateOptions{})
c.CoreV1().Nodes().Create(context.Background(), &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Expand All @@ -237,6 +260,59 @@ func TestService_DeleteCollection(t *testing.T) {
},
wantErr: true,
},
{
name: "delete nodes with multiple existing namespaced pods",
preparePodServiceMockFn: func(m *mock_node.MockPodService) {
m.EXPECT().DeleteCollection(gomock.Any(), "default1", metav1.ListOptions{
FieldSelector: "spec.nodeName=node1",
}).Return(nil)
m.EXPECT().DeleteCollection(gomock.Any(), "default2", metav1.ListOptions{
FieldSelector: "spec.nodeName=node1",
}).Return(nil)
m.EXPECT().DeleteCollection(gomock.Any(), "default3", metav1.ListOptions{
FieldSelector: "spec.nodeName=node1",
}).Return(errors.New("error"))
},
prepareFakeClientSetFn: func() *fake.Clientset {
c := fake.NewSimpleClientset()
c.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default1",
},
}, metav1.CreateOptions{})
c.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default2",
},
}, metav1.CreateOptions{})
c.CoreV1().Pods("default1").Create(context.Background(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
},
Spec: corev1.PodSpec{
NodeName: "node1",
},
}, metav1.CreateOptions{})
c.CoreV1().Pods("default2").Create(context.Background(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod2",
},
Spec: corev1.PodSpec{
NodeName: "node1",
},
}, metav1.CreateOptions{})
c.CoreV1().Nodes().Create(context.Background(), &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
}, metav1.CreateOptions{})
return c
},
lopts: metav1.ListOptions{
FieldSelector: "spec.nodeName!=",
},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
Expand Down
21 changes: 14 additions & 7 deletions reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,23 @@ func NewResetService(
// Reset cleans up all resources and scheduler configuration.
func (s *Service) Reset(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
nsList, err := s.client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return xerrors.Errorf("list namespaces: %w", err)
}
for k, ds := range s.deleteServicesForNamespacedResources {
ds := ds
k := k
eg.Go(func() error {
// this method deletes all resources on all namespaces.
if err := ds.DeleteCollection(ctx, metav1.NamespaceAll, metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete collecton of %s service: %w", k, err)
}
return nil
})
for _, ns := range nsList.Items {
ns := ns
eg.Go(func() error {
// this method deletes all resources on specified namespace.
if err := ds.DeleteCollection(ctx, ns.GetName(), metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete collecton of %s service: %w", k, err)
}
return nil
})
}
}
for k, ds := range s.deleteServices {
ds := ds
Expand Down

0 comments on commit 85c8ac2

Please sign in to comment.