/
utils.go
66 lines (56 loc) · 2.13 KB
/
utils.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
package main
import (
"fmt"
"github.com/Berops/claudie/internal/utils"
"github.com/Berops/claudie/proto/pb"
cbox "github.com/Berops/claudie/services/context-box/client"
)
// destroyConfig destroys all the current state of the config.
func destroyConfig(config *pb.Config, clusterView *ClusterView, c pb.ContextBoxServiceClient) error {
if err := utils.ConcurrentExec(config.CurrentState.Clusters, func(cluster *pb.K8Scluster) error {
return destroyCluster(&BuilderContext{
projectName: config.Name,
cluster: cluster,
loadbalancers: clusterView.Loadbalancers[cluster.ClusterInfo.Name],
})
}); err != nil {
return err
}
return cbox.DeleteConfigFromDB(c, config.Id, pb.IdType_HASH)
}
// destroy destroys any Loadbalancers or the cluster itself.
func destroy(projectName, clusterName string, clusterView *ClusterView) (bool, error) {
deleteCtx := &BuilderContext{
projectName: projectName,
}
if clusterView.Clusters[clusterName] != nil && clusterView.DesiredClusters[clusterName] == nil {
deleteCtx.cluster = clusterView.Clusters[clusterName]
}
if len(clusterView.DeletedLoadbalancers[clusterName]) != 0 {
deleteCtx.loadbalancers = clusterView.DeletedLoadbalancers[clusterName]
}
if deleteCtx.cluster != nil || len(deleteCtx.loadbalancers) > 0 {
if err := destroyCluster(deleteCtx); err != nil {
return false, err
}
// if there is no desired state for the cluster there is no more work to be done.
if deleteCtx.cluster != nil {
return true, nil
}
}
return false, nil
}
// saveErrorMessage saves error message to config
func saveErrorMessage(config *pb.Config, c pb.ContextBoxServiceClient, err error) error {
if config.DesiredState != nil {
// Update currentState preemptively, so we can use it for terraform destroy
// id DesiredState is null, we are already in deletion process, thus CurrentState should stay as is when error occurs
config.CurrentState = config.DesiredState
}
config.ErrorMessage = err.Error()
errSave := cbox.SaveConfigBuilder(c, &pb.SaveConfigRequest{Config: config})
if errSave != nil {
return fmt.Errorf("error while saving the config in Builder: %w", err)
}
return nil
}