-
Notifications
You must be signed in to change notification settings - Fork 34
/
loadbalancer.go
136 lines (108 loc) · 3.73 KB
/
loadbalancer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package loadbalancer
import (
"errors"
"fmt"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
cluster_builder "github.com/berops/claudie/services/terraformer/server/domain/utils/cluster-builder"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
)
var (
// ErrCreateDNSRecord is returned when an error occurs during the creation of the DNS records
ErrCreateDNSRecord = errors.New("failed to create DNS record")
)
type LBcluster struct {
ProjectName string
DesiredState *pb.LBcluster
CurrentState *pb.LBcluster
// SpawnProcessLimit represents a synchronization channel which limits the number of spawned terraform
// processes. This values should always be non-nil and be buffered, where the capacity indicates
// the limit.
SpawnProcessLimit chan struct{}
}
func (l *LBcluster) Id() string {
state := l.DesiredState
if state == nil {
state = l.CurrentState
}
return utils.GetClusterID(state.ClusterInfo)
}
func (l *LBcluster) Build(logger zerolog.Logger) error {
logger.Info().Msgf("Building LB Cluster %s and DNS", l.DesiredState.ClusterInfo.Name)
var currentClusterInfo *pb.ClusterInfo
var currentDNS *pb.DNS
var currentNodeIPs []string
// Check if current cluster was defined, to avoid access of unrefferenced memory
if l.CurrentState != nil {
currentClusterInfo = l.CurrentState.ClusterInfo
currentDNS = l.CurrentState.Dns
currentNodeIPs = getNodeIPs(l.CurrentState.ClusterInfo.NodePools)
}
clusterBuilder := cluster_builder.ClusterBuilder{
DesiredClusterInfo: l.DesiredState.ClusterInfo,
CurrentClusterInfo: currentClusterInfo,
ProjectName: l.ProjectName,
ClusterType: pb.ClusterType_LB,
Metadata: map[string]any{
"roles": l.DesiredState.Roles,
},
SpawnProcessLimit: l.SpawnProcessLimit,
}
if err := clusterBuilder.CreateNodepools(); err != nil {
return fmt.Errorf("error while creating the LB cluster %s : %w", l.DesiredState.ClusterInfo.Name, err)
}
nodeIPs := getNodeIPs(l.DesiredState.ClusterInfo.NodePools)
dns := DNS{
ClusterName: l.DesiredState.ClusterInfo.Name,
ClusterHash: l.DesiredState.ClusterInfo.Hash,
CurrentNodeIPs: currentNodeIPs,
DesiredNodeIPs: nodeIPs,
CurrentDNS: currentDNS,
DesiredDNS: l.DesiredState.Dns,
ProjectName: l.ProjectName,
SpawnProcessLimit: l.SpawnProcessLimit,
}
endpoint, err := dns.CreateDNSRecords(logger)
if err != nil {
return fmt.Errorf("%w for %s: %w", ErrCreateDNSRecord, l.DesiredState.ClusterInfo.Name, err)
}
l.DesiredState.Dns.Endpoint = endpoint
return nil
}
func (l *LBcluster) Destroy(logger zerolog.Logger) error {
group := errgroup.Group{}
logger.Info().Msgf("Destroying LB Cluster %s and DNS", l.CurrentState.ClusterInfo.Name)
group.Go(func() error {
cluster := cluster_builder.ClusterBuilder{
CurrentClusterInfo: l.CurrentState.ClusterInfo,
ProjectName: l.ProjectName,
ClusterType: pb.ClusterType_LB,
SpawnProcessLimit: l.SpawnProcessLimit,
}
return cluster.DestroyNodepools()
})
group.Go(func() error {
dns := DNS{
ClusterName: l.CurrentState.ClusterInfo.Name,
ClusterHash: l.CurrentState.ClusterInfo.Hash,
CurrentNodeIPs: getNodeIPs(l.CurrentState.ClusterInfo.NodePools),
CurrentDNS: l.CurrentState.Dns,
ProjectName: l.ProjectName,
SpawnProcessLimit: l.SpawnProcessLimit,
}
return dns.DestroyDNSRecords(logger)
})
return group.Wait()
}
func (l *LBcluster) UpdateCurrentState() { l.CurrentState = l.DesiredState }
// getNodeIPs returns slice of public IPs used in the node pool.
func getNodeIPs(nodepools []*pb.NodePool) []string {
var ips []string
for _, nodepool := range nodepools {
for _, node := range nodepool.Nodes {
ips = append(ips, node.Public)
}
}
return ips
}