-
Notifications
You must be signed in to change notification settings - Fork 34
/
workflow_helpers.go
302 lines (251 loc) · 11.8 KB
/
workflow_helpers.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package usecases
import (
"errors"
"fmt"
"github.com/berops/claudie/services/builder/domain/usecases/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog/log"
"strings"
cutils "github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
"github.com/berops/claudie/services/builder/domain/usecases/utils"
)
const (
// maxDeleteRetry defines how many times the config should try to be deleted before returning an error, if encountered.
maxDeleteRetry = 3
)
// buildCluster performs whole Claudie workflow on the given cluster.
func (u *Usecases) buildCluster(ctx *utils.BuilderContext, cboxClient pb.ContextBoxServiceClient) (*utils.BuilderContext, error) {
// LB add nodes prometheus metrics.
for _, lb := range ctx.DesiredLoadbalancers {
var currNodes int
if idx := cutils.GetLBClusterByName(lb.ClusterInfo.Name, ctx.CurrentLoadbalancers); idx >= 0 {
currNodes = cutils.CountLbNodes(ctx.CurrentLoadbalancers[idx])
}
adding := max(0, cutils.CountLbNodes(lb)-currNodes)
metrics.LbAddingNodesInProgress.With(prometheus.Labels{
metrics.LBClusterLabel: lb.ClusterInfo.Name,
metrics.K8sClusterLabel: lb.TargetedK8S,
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(float64(adding))
defer func(k8s, lb string, c int) {
metrics.LbAddingNodesInProgress.With(prometheus.Labels{
metrics.LBClusterLabel: lb,
metrics.K8sClusterLabel: k8s,
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(-float64(c))
}(lb.TargetedK8S, lb.ClusterInfo.Name, adding)
deleting := -min(cutils.CountLbNodes(lb)-currNodes, 0)
metrics.LbDeletingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: lb.TargetedK8S,
metrics.LBClusterLabel: lb.ClusterInfo.Name,
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(float64(deleting))
defer func(k8s, lb string, c int) {
metrics.LbDeletingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: k8s,
metrics.LBClusterLabel: lb,
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(-float64(c))
}(lb.TargetedK8S, lb.ClusterInfo.Name, deleting)
}
metrics.K8sAddingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: ctx.GetClusterName(),
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(float64(
max(0, cutils.CountNodes(ctx.DesiredCluster)-cutils.CountNodes(ctx.CurrentCluster)),
))
defer func(c int) {
metrics.K8sAddingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: ctx.GetClusterName(),
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(-float64(c))
}(max(0, cutils.CountNodes(ctx.DesiredCluster)-cutils.CountNodes(ctx.CurrentCluster)))
// Reconcile infrastructure via terraformer.
if err := u.reconcileInfrastructure(ctx, cboxClient); err != nil {
return ctx, fmt.Errorf("error in Terraformer for cluster %s project %s : %w", ctx.GetClusterName(), ctx.ProjectName, err)
}
// Configure infrastructure via Ansibler.
if err := u.configureInfrastructure(ctx, cboxClient); err != nil {
return ctx, fmt.Errorf("error in Ansibler for cluster %s project %s : %w", ctx.GetClusterName(), ctx.ProjectName, err)
}
// Build k8s cluster via Kube-eleven.
if err := u.reconcileK8sCluster(ctx, cboxClient); err != nil {
return ctx, fmt.Errorf("error in Kube-eleven for cluster %s project %s : %w", ctx.GetClusterName(), ctx.ProjectName, err)
}
// Reconcile k8s configuration via Kuber.
if err := u.reconcileK8sConfiguration(ctx, cboxClient); err != nil {
return ctx, fmt.Errorf("error in Kuber for cluster %s project %s : %w", ctx.GetClusterName(), ctx.ProjectName, err)
}
return ctx, nil
}
// destroyCluster destroys existing clusters infrastructure for a config and cleans up management cluster from any of the cluster data.
func (u *Usecases) destroyCluster(ctx *utils.BuilderContext, cboxClient pb.ContextBoxServiceClient) error {
// K8s delete nodes prometheus metric.
metrics.K8sDeletingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: ctx.GetClusterName(),
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(float64(cutils.CountNodes(ctx.CurrentCluster)))
defer func(c int) {
metrics.K8sDeletingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: ctx.GetClusterName(),
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(-float64(c))
}(cutils.CountNodes(ctx.CurrentCluster))
// LB delete nodes prometheus metrics.
for _, lb := range ctx.CurrentLoadbalancers {
metrics.LbDeletingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: lb.TargetedK8S,
metrics.LBClusterLabel: lb.ClusterInfo.Name,
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(float64(cutils.CountLbNodes(lb)))
defer func(k8s, lb string, c int) {
metrics.LbDeletingNodesInProgress.With(prometheus.Labels{
metrics.K8sClusterLabel: k8s,
metrics.LBClusterLabel: lb,
metrics.InputManifestLabel: ctx.ProjectName,
}).Add(-float64(c))
}(lb.TargetedK8S, lb.ClusterInfo.Name, cutils.CountLbNodes(lb))
}
metrics.LBClustersInDeletion.Add(float64(len(ctx.CurrentLoadbalancers)))
defer func(c int) { metrics.LBClustersInDeletion.Add(-float64(c)) }(len(ctx.CurrentLoadbalancers))
if s := cutils.GetCommonStaticNodePools(ctx.CurrentCluster.GetClusterInfo().GetNodePools()); len(s) > 0 {
if err := u.destroyK8sCluster(ctx, cboxClient); err != nil {
log.Error().Msgf("error in destroy Kube-Eleven for config %s project %s : %v", ctx.GetClusterName(), ctx.ProjectName, err)
}
if err := u.removeClaudieUtilities(ctx, cboxClient); err != nil {
log.Error().Msgf("error while removing claudie installed utilities for config %s project %s: %v", ctx.GetClusterName(), ctx.ProjectName, err)
}
}
// Destroy infrastructure for the given cluster.
if err := u.destroyInfrastructure(ctx, cboxClient); err != nil {
return fmt.Errorf("error in destroy config Terraformer for config %s project %s : %w", ctx.GetClusterName(), ctx.ProjectName, err)
}
// Delete Cluster data from management cluster.
if err := u.deleteClusterData(ctx, cboxClient); err != nil {
return fmt.Errorf("error in delete kubeconfig for config %s project %s : %w", ctx.GetClusterName(), ctx.ProjectName, err)
}
metrics.LBClustersDeleted.Add(float64(len(ctx.CurrentLoadbalancers)))
return nil
}
// destroyConfig destroys all the current state of the config.
func (u *Usecases) destroyConfig(config *pb.Config, clusterView *cutils.ClusterView, c pb.ContextBoxServiceClient) error {
// Destroy all cluster concurrently.
if err := cutils.ConcurrentExec(config.CurrentState.Clusters, func(_ int, cluster *pb.K8Scluster) error {
err := u.destroyCluster(&utils.BuilderContext{
ProjectName: config.Name,
CurrentCluster: cluster,
CurrentLoadbalancers: clusterView.Loadbalancers[cluster.ClusterInfo.Name],
Workflow: clusterView.ClusterWorkflows[cluster.ClusterInfo.Name],
}, c)
if err != nil {
clusterView.SetWorkflowError(cluster.ClusterInfo.Name, err)
return err
}
clusterView.SetWorkflowDone(cluster.ClusterInfo.Name)
return nil
}); err != nil {
return err
}
return u.ContextBox.DeleteConfig(config, c)
}
// saveWorkflowDescription sets description for a given builder context and saves it to Claudie database.
func (u *Usecases) saveWorkflowDescription(ctx *utils.BuilderContext, description string, cboxClient pb.ContextBoxServiceClient) {
log := cutils.CreateLoggerWithProjectName(ctx.ProjectName)
ctx.Workflow.Description = strings.TrimSpace(description)
if err := u.ContextBox.SaveWorkflowState(ctx.ProjectName, ctx.GetClusterName(), ctx.Workflow, cboxClient); err != nil {
log.Err(err).Msgf("failed to update workflow description")
}
}
// deleteConfig calls destroy config to remove all traces of infrastructure from given config.
func (u *Usecases) deleteConfig(config *pb.Config, clusterView *cutils.ClusterView, cboxClient pb.ContextBoxServiceClient) error {
log := cutils.CreateLoggerWithProjectName(config.Name)
var err error
for i := 0; i < maxDeleteRetry; i++ {
if err = u.destroyConfig(config, clusterView, cboxClient); err == nil {
break
}
log.Err(err).Msg("failed to destroy config")
}
return err
}
// deleteCluster calls destroy cluster to remove all traces of infrastructure from cluster.
func (u *Usecases) deleteCluster(configName, clusterName string, clusterView *cutils.ClusterView, cboxClient pb.ContextBoxServiceClient) error {
log := cutils.CreateLoggerWithProjectAndClusterName(configName, clusterName)
deleteCtx := &utils.BuilderContext{
ProjectName: configName,
CurrentCluster: clusterView.CurrentClusters[clusterName],
CurrentLoadbalancers: clusterView.DeletedLoadbalancers[clusterName],
Workflow: clusterView.ClusterWorkflows[clusterName],
}
var err error
for i := 0; i < maxDeleteRetry; i++ {
if err = u.destroyCluster(deleteCtx, cboxClient); err == nil {
break
}
log.Err(err).Msg("failed to destroy cluster")
}
return err
}
// deleteNodes deletes nodes from the cluster based on the node map specified.
func (u *Usecases) deleteNodes(currentCluster, desiredCluster *pb.K8Scluster, nodes map[string]int32) (*pb.K8Scluster, error) {
master, worker := utils.SeparateNodepools(nodes, currentCluster.ClusterInfo, desiredCluster.ClusterInfo)
newCluster, err := u.callDeleteNodes(master, worker, currentCluster)
if err != nil {
return nil, fmt.Errorf("error while deleting nodes for %s : %w", currentCluster.ClusterInfo.Name, err)
}
return newCluster, nil
}
// applyIR applies intermediate representation of the infrastructure to the Claudie workflow.
func (u *Usecases) applyIR(configName, clusterName string, clusterView *cutils.ClusterView, cboxClient pb.ContextBoxServiceClient, diff *utils.IntermediateRepresentation) (*utils.BuilderContext, error) {
ctx := &utils.BuilderContext{
ProjectName: configName,
CurrentCluster: clusterView.CurrentClusters[clusterName],
DesiredCluster: diff.IR,
CurrentLoadbalancers: clusterView.Loadbalancers[clusterName],
DesiredLoadbalancers: diff.IRLbs,
DeletedLoadBalancers: nil,
Workflow: clusterView.ClusterWorkflows[clusterName],
}
if ctx, err := u.buildCluster(ctx, cboxClient); err != nil {
clusterView.CurrentClusters[clusterName] = ctx.DesiredCluster
clusterView.Loadbalancers[clusterName] = ctx.DesiredLoadbalancers
if errors.Is(err, ErrFailedToBuildInfrastructure) {
clusterView.CurrentClusters[clusterName] = ctx.CurrentCluster
clusterView.Loadbalancers[clusterName] = ctx.CurrentLoadbalancers
}
return ctx, err
}
return ctx, nil
}
// applyAPIEndpointReplacement applies workflow for kube API Endpoint replacement.
func (u *Usecases) applyAPIEndpointReplacement(configName, clusterName string, clusterView *cutils.ClusterView, cboxClient pb.ContextBoxServiceClient) error {
ctx := &utils.BuilderContext{
ProjectName: configName,
CurrentCluster: clusterView.CurrentClusters[clusterName],
DesiredCluster: clusterView.DesiredClusters[clusterName],
Workflow: clusterView.ClusterWorkflows[clusterName],
}
if err := u.callUpdateAPIEndpoint(ctx, cboxClient); err != nil {
return err
}
clusterView.CurrentClusters[clusterName] = ctx.CurrentCluster
clusterView.DesiredClusters[clusterName] = ctx.DesiredCluster
ctx = &utils.BuilderContext{
ProjectName: configName,
DesiredCluster: clusterView.CurrentClusters[clusterName],
DesiredLoadbalancers: clusterView.Loadbalancers[clusterName],
Workflow: clusterView.ClusterWorkflows[clusterName],
}
// Reconcile k8s cluster to assure new API endpoint has correct certificates.
if err := u.reconcileK8sCluster(ctx, cboxClient); err != nil {
return err
}
clusterView.CurrentClusters[clusterName] = ctx.DesiredCluster
clusterView.Loadbalancers[clusterName] = ctx.DesiredLoadbalancers
// Patch cluster-info config map to update certificates.
if err := u.callPatchClusterInfoConfigMap(ctx, cboxClient); err != nil {
return err
}
return nil
}