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

Fixed how to specify the namespace in Reset function for resources with a namespace #162

Merged
merged 5 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 10 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,23 @@ 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)
for _, ns := range nsList.Items {
ns := ns
196Ikuchil marked this conversation as resolved.
Show resolved Hide resolved
// This method deletes all pods on specified namespace scheduled to the specified node.
196Ikuchil marked this conversation as resolved.
Show resolved Hide resolved
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