pod_delete_task.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package guest
  15. import (
  16. "context"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. )
  25. type PodDeleteTask struct {
  26. SGuestBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(PodDeleteTask{})
  30. }
  31. func (t *PodDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  32. t.SetStage("OnWaitContainerDeleted", nil)
  33. t.OnWaitContainerDeleted(ctx, obj.(*models.SGuest), nil)
  34. }
  35. func (t *PodDeleteTask) OnWaitContainerDeleted(ctx context.Context, pod *models.SGuest, _ jsonutils.JSONObject) {
  36. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_DELETING_CONTAINER, "")
  37. ctrs, err := models.GetContainerManager().GetContainersByPod(pod.GetId())
  38. if err != nil {
  39. if strings.Contains(err.Error(), "NotFoundError") {
  40. // already deleted
  41. t.OnContainerDeleted(ctx, pod)
  42. return
  43. }
  44. t.OnWaitContainerDeletedFailed(ctx, pod, jsonutils.NewString(errors.Wrap(err, "GetContainersByPod").Error()))
  45. return
  46. }
  47. if len(ctrs) == 0 {
  48. t.OnContainerDeleted(ctx, pod)
  49. return
  50. }
  51. curCtr := ctrs[0]
  52. curCtr.StartDeleteTask(ctx, t.GetUserCred(), t.GetTaskId(), jsonutils.QueryBoolean(t.GetParams(), "purge", false))
  53. }
  54. func (t *PodDeleteTask) OnWaitContainerDeletedFailed(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  55. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_DELETE_CONTAINER_FAILED, data.String())
  56. t.SetStageFailed(ctx, data)
  57. }
  58. func (t *PodDeleteTask) OnContainerDeleted(ctx context.Context, pod *models.SGuest) {
  59. t.startDeletePod(ctx, pod)
  60. }
  61. func (t *PodDeleteTask) startDeletePod(ctx context.Context, pod *models.SGuest) {
  62. t.SetStage("OnPodDeleted", nil)
  63. err := pod.StartBaseDeleteTask(ctx, t)
  64. if err != nil {
  65. t.OnPodDeletedFailed(ctx, pod, jsonutils.NewString(err.Error()))
  66. return
  67. }
  68. }
  69. func (t *PodDeleteTask) OnPodDeleted(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  70. pod := obj.(*models.SGuest)
  71. pod.FinalizeDeleteTask(ctx, t.GetUserCred(), t, data)
  72. t.SetStageComplete(ctx, nil)
  73. }
  74. func (t *PodDeleteTask) OnPodDeletedFailed(ctx context.Context, obj db.IStandaloneModel, reason jsonutils.JSONObject) {
  75. t.SetStageFailed(ctx, reason)
  76. }