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 resources "delete all" method in each service #68

Merged

Conversation

sivchari
Copy link
Member

What type of PR is this?

/kind feature

What this PR does / why we need it:

I added delete all resources of each objects.

Which issue(s) this PR fixes:

Fixes #67

Special notes for your reviewer:

/label tide/merge-method-squash

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 19, 2021
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 19, 2021
@@ -18,6 +18,7 @@ type PodService interface {
List(ctx context.Context) (*corev1.PodList, error)
Apply(ctx context.Context, pod *configv1.PodApplyConfiguration) (*corev1.Pod, error)
Delete(ctx context.Context, name string) error
Deletes(ctx context.Context) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot guess what Deletes does.
Maybe DeleteAll or Reset is better.

node/node.go Outdated
return xerrors.Errorf("list pods: %w", err)
}

// delete all pods
Copy link
Member

@sanposhiho sanposhiho Dec 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use the ”delete all method” on Pod service?

node/node.go Outdated

// delete all nodes
if err := s.client.CoreV1().Nodes().DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete nodes: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return xerrors.Errorf("delete nodes: %w", err)
return xerrors.Errorf("delete all nodes: %w", err)

// Deletes deletes all persistentVolumes.
func (s *Service) Deletes(ctx context.Context) error {
if err := s.client.CoreV1().PersistentVolumes().DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete persistentVolumes: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return xerrors.Errorf("delete persistentVolumes: %w", err)
return xerrors.Errorf("delete all persistentVolumes: %w", err)

// Deletes deletes all persistentVolumeClaims.
func (s *Service) Deletes(ctx context.Context) error {
if err := s.client.CoreV1().PersistentVolumeClaims(defaultNamespaceName).DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete persistentVolumeClaims: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return xerrors.Errorf("delete persistentVolumeClaims: %w", err)
return xerrors.Errorf("delete all persistentVolumeClaims: %w", err)

pod/pod.go Outdated
// Deltes deletes all pods.
func (s *Service) Deletes(ctx context.Context) error {
if err := s.client.CoreV1().Pods(defaultNamespaceName).DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return fmt.Errorf("delete pods: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("delete pods: %w", err)
return fmt.Errorf("delete all pods: %w", err)

// Deletes deletes all priority classes.
func (s *Service) Deletes(ctx context.Context) error {
if err := s.client.SchedulingV1().PriorityClasses().DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete priorityClasses: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return xerrors.Errorf("delete priorityClasses: %w", err)
return xerrors.Errorf("delete all priorityClasses: %w", err)

// Deletes deletes all storageClasss.
func (s *Service) Deletes(ctx context.Context) error {
if err := s.client.StorageV1().StorageClasses().DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete storageClasss: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return xerrors.Errorf("delete storageClasss: %w", err)
return xerrors.Errorf("delete all storageClasses: %w", err)

@sanposhiho
Copy link
Member

@sivchari Could you tag #26 in the issue description instead of #67?

@sanposhiho
Copy link
Member

sanposhiho commented Dec 20, 2021

Let me retitle this to be more appropriate.

/retitle add resources "delete all" method in each service.

@k8s-ci-robot k8s-ci-robot changed the title Feature add resources delete method add resources "delete all" method in each service. Dec 20, 2021
@sanposhiho
Copy link
Member

/retitle add resources "delete all" method in each service

@k8s-ci-robot k8s-ci-robot changed the title add resources "delete all" method in each service. add resources "delete all" method in each service Dec 20, 2021
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 20, 2021
@sivchari sivchari force-pushed the feature-add-resources-delete-method branch from 4c70044 to ab2aa35 Compare December 20, 2021 17:15
// DeleteAll deletes all storageClassses.
func (s *Service) DeleteAll(ctx context.Context) error {
if err := s.client.StorageV1().StorageClasses().DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return xerrors.Errorf("delete all storageClassses: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo (the same typo on the DeleteAll comment.)

Suggested change
return xerrors.Errorf("delete all storageClassses: %w", err)
return xerrors.Errorf("delete all storageClasses: %w", err)

node/node.go Outdated
}
if len(pl.Items) == 0 {
klog.Info("delete all nodes: no pods to delete")
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can we return here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I mistook.

@@ -146,3 +146,122 @@ func TestService_Delete(t *testing.T) {
})
}
}

func TestService_DeleteAll(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

node/node.go Outdated
return xerrors.Errorf("list pods: %w", err)
}
if len(pl.Items) == 0 {
klog.Info("delete all nodes: no pods to delete")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to list pod only to log it.

node/node.go Outdated

// delete all pods
if err := s.podService.DeleteAll(ctx); err != nil {
return xerrors.Errorf("delete all nodes: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return xerrors.Errorf("delete all nodes: %w", err)
return xerrors.Errorf("delete all pods: %w", err)

wantErr bool
}{
{
name: "delete all nodes and pods on nodes",
Copy link
Member

@sanposhiho sanposhiho Dec 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, Do you want to delete only Pods on node? (= do you want not to delete unscheduled pods?)
Yeah, I think it is better than deleting all nodes and all pods (include unscheduled pods.).

Currently, DeleteAll deletes all nodes and all pods, so, should be fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fixed it.

wantErr: false,
},
{
name: "fail to delete no nodes",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you double-check? I think this name is incorrect.

Suggested change
name: "fail to delete no nodes",
name: "fail if deleting all pods returns error",

@sivchari
Copy link
Member Author

sivchari commented Jan 7, 2022

I fixed DeleteAll method of pod.go.
I update it to delete pod if pod's node name is empty in concurrency.
If someone has better idea than implementation now, please tell me.

pod/pod.go Outdated
@@ -74,3 +74,39 @@ func (s *Service) Delete(ctx context.Context, name string) error {

return nil
}

// DelteAll deletes all pods.
func (s *Service) DeleteAll(ctx context.Context) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this DeleteAll method, you should delete all Pods.
But, in DeleteAll of node service, you shouldn't delete all Pods but delete all scheduled Pods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So,

  • rename this method to DeleteAllScheduledPod
  • use DeleteAllScheduledPod from DeleteAll in node service
  • create new DeleteAll which deletes all pods(= scheduled pods + unscheduled pods.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review.

create new DeleteAll which deletes all pods(= scheduled pods + unscheduled pods.)

This method will be not used now, so I think it's better not to create it.
What do you think ?

Copy link
Member

@sanposhiho sanposhiho Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method will be not used now,

No. DeleteAll method is needed to delete all Pods.

You'd like to create "delete all" method in each service now, right?

Deleting all Nodes means "delete all Nodes and delete all scheduled Pods". So, you need to create "DeleteAllScheduledPod" to delete all scheduled Pods in "DeleteAll" in node service.

Deleting all Pods means "delete all pods whether they are scheduled or not." So, you also need to create "DeleteAll" to delete all Pods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this PR is scoped to create delete all method in each service, so we should create DeleteAllScheduledPod and DeleteAll, right ?

Deleting all Nodes means "delete all Nodes and delete all scheduled Pods". So, you need to create
"DeleteAllScheduledPod" to delete all scheduled Pods in "DeleteAll" in node service.

Deleting all Pods means "delete all pods whether they are scheduled or not." So, you also need to create
"DeleteAll" to delete all Pods.

I know, thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sanposhiho

I added DeleteAll method, too. pod service test isn't existed, now.
Shall I add a test? Or do you hope to create different PR because the scope is larger than this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I've just commented about the test. Yes, please add test for DeleteAllScheduledPod.
#68 (comment)

node/node.go Outdated
func (s *Service) DeleteAll(ctx context.Context) error {
// delete all pods
if err := s.podService.DeleteAllScheduledPod(ctx); err != nil {
return xerrors.Errorf("delete all pods: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits:

Suggested change
return xerrors.Errorf("delete all pods: %w", err)
return xerrors.Errorf("delete all scheduled pods: %w", err)

wantErr: false,
},
{
name: "fail if delteing all pods returns error",
Copy link
Member

@sanposhiho sanposhiho Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: "fail if delteing all pods returns error",
name: "fail if deleting all pods returns error",

{
name: "delete all nodes and pods scheduled on them",
preparePodServiceMockFn: func(m *mock_node.MockPodService) {
m.EXPECT().List(gomock.Any()).Return(&corev1.PodList{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List method on Pod service is not called.

pod/pod.go Outdated
@@ -74,3 +73,21 @@ func (s *Service) Delete(ctx context.Context, name string) error {

return nil
}

// DeleteAllPod deletes all pods.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// DeleteAllPod deletes all pods.
// DeleteAll deletes all pods.

@@ -62,3 +62,12 @@ func (s *Service) Delete(ctx context.Context, name string) error {

return nil
}

// DeleteAll deletes all storageClassses.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// DeleteAll deletes all storageClassses.
// DeleteAll deletes all storageClasses.

pod/pod.go Outdated
}

// DelteAllScheduledPod deletes all scheduled pods.
func (s *Service) DeleteAllScheduledPod(ctx context.Context) error {
Copy link
Member

@sanposhiho sanposhiho Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you 念のため add a brief test for this method? :)

@sivchari
Copy link
Member Author

sivchari commented Jan 8, 2022

I'm checking, now. so I'll notify you I ready to review 👍

@@ -18,6 +18,8 @@ type PodService interface {
List(ctx context.Context) (*corev1.PodList, error)
Apply(ctx context.Context, pod *configv1.PodApplyConfiguration) (*corev1.Pod, error)
Delete(ctx context.Context, name string) error
DeleteAll(ctx context.Context) error
DeleteAllScheduledPod(ctx context.Context) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeleteAllScheduledPod is not expected to call from handler layer for now, so I think it is not needed to add this.

pod/pod.go Outdated
Comment on lines 85 to 86
// DelteAllScheduledPod deletes all scheduled pods.
func (s *Service) DeleteAllScheduledPod(ctx context.Context) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: DeleteAllScheduledPod → DeleteAllScheduledPods

Suggested change
// DelteAllScheduledPod deletes all scheduled pods.
func (s *Service) DeleteAllScheduledPod(ctx context.Context) error {
// DeleteAllScheduledPods deletes all scheduled pods.
func (s *Service) DeleteAllScheduledPods(ctx context.Context) error {

@sanposhiho
Copy link
Member

sanposhiho commented Jan 8, 2022

@sivchari

On my second thought, it is better to implement DeleteCollection in pod service and use it from node service.
DeleteCollection method should be like this:

func (s *Service) DeleteCollection(ctx context.Context, listOpts metav1.ListOptions) error {

Yes, we can delete all Pods with this DeleteCollection method, but I think DeleteAll on pod service is still needed because there is no uniformity in using the DeleteAll method to delete the resources of other services, but using this DeleteCollection for Pod.
Of course, DeleteAll should be changed to use DeleteCollection internally to make it dry.

What do you think about this? Could you re-implement like the above if no objections? 🙇

@sanposhiho
Copy link
Member

/assign

@sivchari
Copy link
Member Author

sivchari commented Jan 20, 2022

@sanposhiho
I refactored this PR and added pod test.

Copy link
Member

@sanposhiho sanposhiho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!! One comment.

@@ -62,3 +62,12 @@ func (s *Service) Delete(ctx context.Context, name string) error {

return nil
}

// DeleteCollection deletes all persistentVolumes.
Copy link
Member

@sanposhiho sanposhiho Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to try to make interfaces of all services same.
Currently, the pod service's DeleteCollection only accepts metav1.ListOptions arg. So, let's add ListOptions to other service's DeleteCollection arg.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I fixed it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this commit is failed.
I fixed it later.

@sanposhiho
Copy link
Member

/test pull-kube-scheduler-simulator-backend-unit-test
/test pull-kube-scheduler-simulator-backend-lint

@sivchari
Copy link
Member Author

@sanposhiho

I fixed. Please review 🙏

Copy link
Member

@sanposhiho sanposhiho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ❤️
One comment from me.

node/node.go Outdated
Comment on lines 104 to 114
lopts := metav1.ListOptions{
FieldSelector: "spec.nodeName=" + n.Name,
}
if err := s.podService.DeleteCollection(ctx, lopts); err != nil {
return xerrors.Errorf("failed to delete pods on node %s: %w", n.Name, err)
}

// delete specific node
if err := s.client.CoreV1().Nodes().Delete(ctx, n.Name, metav1.DeleteOptions{}); err != nil {
return xerrors.Errorf("delete node: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process these part in parallel with errgroup or waitgroup

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! I agree with you.

Copy link
Member

@sanposhiho sanposhiho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

node/node.go Outdated
@@ -90,3 +92,33 @@ func (s *Service) Delete(ctx context.Context, name string) error {
}
return nil
}

// DeleteCollection deletes all nodes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nits:
Please change other services doc as well

Suggested change
// DeleteCollection deletes all nodes.
// DeleteCollection deletes Nodes according to the list options.
// And it also deletes Pods scheduled on those Nodes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I fixed it.

Copy link
Member

@sanposhiho sanposhiho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 29, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sanposhiho, sivchari

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 29, 2022
@k8s-ci-robot k8s-ci-robot merged commit 6021a87 into kubernetes-sigs:master Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reset scheduler and all resources
3 participants