kube_cluster_delete_task.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 kube
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/apis"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type KubeClusterDeleteTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(KubeClusterDeleteTask{})
  32. }
  33. func (self *KubeClusterDeleteTask) taskFailed(ctx context.Context, cluster *models.SKubeCluster, err error) {
  34. cluster.SetStatus(ctx, self.UserCred, apis.STATUS_DELETE_FAILED, err.Error())
  35. db.OpsLog.LogEvent(cluster, db.ACT_DELOCATE_FAIL, err, self.UserCred)
  36. logclient.AddActionLogWithStartable(self, cluster, logclient.ACT_DELETE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *KubeClusterDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. cluster := obj.(*models.SKubeCluster)
  41. iCluster, err := cluster.GetIKubeCluster(ctx)
  42. if err != nil {
  43. if errors.Cause(err) == cloudprovider.ErrNotFound {
  44. self.taskComplete(ctx, cluster)
  45. return
  46. }
  47. self.taskFailed(ctx, cluster, errors.Wrapf(err, "GetIKubeCluster"))
  48. return
  49. }
  50. isRetain := jsonutils.QueryBoolean(self.GetParams(), "retain", false)
  51. err = iCluster.Delete(isRetain)
  52. if err != nil {
  53. self.taskFailed(ctx, cluster, errors.Wrapf(err, "Delete"))
  54. return
  55. }
  56. err = cloudprovider.WaitDeleted(iCluster, time.Second*10, time.Minute*10)
  57. if err != nil {
  58. self.taskFailed(ctx, cluster, errors.Wrapf(err, "WaitDeleted"))
  59. return
  60. }
  61. self.taskComplete(ctx, cluster)
  62. }
  63. func (self *KubeClusterDeleteTask) taskComplete(ctx context.Context, cluster *models.SKubeCluster) {
  64. logclient.AddActionLogWithStartable(self, cluster, logclient.ACT_DELETE, cluster.GetShortDesc(ctx), self.UserCred, true)
  65. cluster.RealDelete(ctx, self.GetUserCred())
  66. self.SetStageComplete(ctx, nil)
  67. }