-
Notifications
You must be signed in to change notification settings - Fork 113
/
wait_for_cluster_deletion.go
93 lines (74 loc) · 2.75 KB
/
wait_for_cluster_deletion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package deprovisioning
import (
"context"
"time"
"github.com/kyma-project/control-plane/components/provisioner/internal/apperrors"
"github.com/kyma-project/control-plane/components/provisioner/internal/util"
"github.com/kyma-project/control-plane/components/provisioner/internal/provisioning/persistence/dbsession"
"github.com/kyma-project/control-plane/components/provisioner/internal/model"
"github.com/kyma-project/control-plane/components/provisioner/internal/operations"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type WaitForClusterDeletionStep struct {
gardenerClient GardenerClient
dbsFactory dbsession.Factory
nextStep model.OperationStage
timeLimit time.Duration
}
func NewWaitForClusterDeletionStep(gardenerClient GardenerClient, dbsFactory dbsession.Factory, nextStep model.OperationStage, timeLimit time.Duration) *WaitForClusterDeletionStep {
return &WaitForClusterDeletionStep{
gardenerClient: gardenerClient,
dbsFactory: dbsFactory,
nextStep: nextStep,
timeLimit: timeLimit,
}
}
func (s *WaitForClusterDeletionStep) Name() model.OperationStage {
return model.WaitForClusterDeletion
}
func (s *WaitForClusterDeletionStep) TimeLimit() time.Duration {
return s.timeLimit
}
func (s *WaitForClusterDeletionStep) Run(cluster model.Cluster, _ model.Operation, _ logrus.FieldLogger) (operations.StageResult, error) {
shootExists, err := s.shootExists(cluster.ClusterConfig.Name)
if err != nil {
return operations.StageResult{}, err
}
if shootExists {
return operations.StageResult{Stage: s.Name(), Delay: 20 * time.Second}, nil
}
err = s.setDeprovisioningFinished(cluster)
if err != nil {
return operations.StageResult{}, err
}
return operations.StageResult{Stage: s.nextStep, Delay: 0}, nil
}
func (s *WaitForClusterDeletionStep) shootExists(gardenerClusterName string) (bool, error) {
_, err := s.gardenerClient.Get(context.Background(), gardenerClusterName, v1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return false, nil
}
return false, util.K8SErrorToAppError(err).SetComponent(apperrors.ErrGardenerClient)
}
return true, nil
}
func (s *WaitForClusterDeletionStep) setDeprovisioningFinished(cluster model.Cluster) error {
session, dberr := s.dbsFactory.NewSessionWithinTransaction()
if dberr != nil {
return errors.Wrap(dberr, "error starting db session with transaction")
}
defer session.RollbackUnlessCommitted()
dberr = session.MarkClusterAsDeleted(cluster.ID)
if dberr != nil {
return errors.Wrap(dberr, "error marking cluster for deletion")
}
dberr = session.Commit()
if dberr != nil {
return errors.Wrap(dberr, "error commiting transaction")
}
return nil
}