-
Notifications
You must be signed in to change notification settings - Fork 787
/
step_e2e_gc.go
342 lines (315 loc) · 10.4 KB
/
step_e2e_gc.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package e2e
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/cluster"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/cluster/factory"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cloud"
"github.com/jenkins-x/jx/v2/pkg/cloud/gke"
"github.com/jenkins-x/jx/v2/pkg/cmd/deletecmd"
"github.com/jenkins-x/jx/v2/pkg/cmd/gc"
"github.com/jenkins-x/jx/v2/pkg/cmd/get"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/spf13/cobra"
)
// StepE2EGCOptions contains the command line flags
type StepE2EGCOptions struct {
step.StepOptions
ProjectID string
Providers []string
Region string
Duration int
}
var (
stepE2EGCLong = templates.LongDesc(`
This pipeline step removes stale E2E test clusters
`)
stepE2EGCExample = templates.Examples(`
# delete stale E2E test clusters
jx step e2e gc
`)
)
// NewCmdStepE2EGC creates the CLI command
func NewCmdStepE2EGC(commonOpts *opts.CommonOptions) *cobra.Command {
options := StepE2EGCOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "gc",
Short: "Removes unused e2e clusters",
Aliases: []string{},
Long: stepE2EGCLong,
Example: stepE2EGCExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Region, "region", "", "europe-west1-c", "GKE region to use. Default: europe-west1-c")
cmd.Flags().StringVarP(&options.ProjectID, "project-id", "p", "", "Google Project ID to delete cluster from")
cmd.Flags().IntVarP(&options.Duration, "duration", "d", 2, "How many hours old a cluster should be before it is deleted if it does not have a --delete tag")
cmd.Flags().StringArrayVarP(&options.Providers, "providers", "", []string{"gke"}, "The providers to run the cleanup for")
return cmd
}
// Run runs the command
func (o *StepE2EGCOptions) Run() error {
// Until https://github.com/jenkins-x/jx/issues/6206 is done, we are going to be using different approaches to run this for different providers
for _, pr := range o.Providers {
switch strings.ToLower(pr) {
case cloud.GKE:
return o.gcpGarbageCollection()
case cloud.AWS:
fallthrough
case cloud.EKS:
return o.eksGarbageCollection()
default:
return fmt.Errorf("provider %s doesn't have an E2E GC implementation defined", pr)
}
}
return nil
}
func (o *StepE2EGCOptions) eksGarbageCollection() error {
eksClient, err := factory.NewClientForProvider(cloud.EKS)
if err != nil {
return errors.Wrap(err, "could not obtain an EKS cluster client to ")
}
eksClusters, err := eksClient.List()
if err != nil {
return errors.Wrap(err, "there was a problem obtaining every eksClient in the current account")
}
for _, eksCluster := range eksClusters {
if eksCluster.Status == "ACTIVE" {
if !o.ShouldDeleteMarkedEKSCluster(eksCluster) {
if !o.ShouldDeleteOlderThanDurationEKS(eksCluster) {
if o.ShouldDeleteDueToNewerRunEKS(eksCluster, eksClusters) {
err = o.deleteEksCluster(eksCluster, eksClient)
}
} else {
err = o.deleteEksCluster(eksCluster, eksClient)
}
} else {
err = o.deleteEksCluster(eksCluster, eksClient)
}
}
if err != nil {
log.Logger().Errorf("error deleting cluster %s: %s", eksCluster.Name, err.Error())
}
}
return nil
}
func (o *StepE2EGCOptions) gcpGarbageCollection() error {
err := o.InstallRequirements(cloud.GKE)
if err != nil {
return err
}
gkeSa := os.Getenv("GKE_SA_KEY_FILE")
if gkeSa != "" {
err = o.GCloud().Login(gkeSa, true)
if err != nil {
return err
}
}
clusters, err := o.GCloud().ListClusters(o.Region, o.ProjectID)
if err != nil {
return err
}
for _, cluster := range clusters {
c := cluster
if c.Status == "RUNNING" {
// Marked for deletion
if !o.ShouldDeleteMarkedCluster(&c) {
// Older than duration in hours
if !o.ShouldDeleteOlderThanDuration(&c) {
// Delete build that has been replaced by a newer version
if o.ShouldDeleteDueToNewerRun(&c, clusters) {
o.deleteGkeCluster(&c)
}
} else {
o.deleteGkeCluster(&c)
}
} else {
o.deleteGkeCluster(&c)
}
}
}
gkeGCOpts := gc.GCGKEOptions{
CommonOptions: &opts.CommonOptions{},
}
gkeGCOpts.Err = o.Err
gkeGCOpts.Out = o.Out
gkeGCOpts.Flags.ProjectID = o.ProjectID
gkeGCOpts.Flags.RunNow = true
return gkeGCOpts.Run()
}
// GetBuildNumberFromClusterEKS gets the build number from the cluster labels
func (o *StepE2EGCOptions) GetBuildNumberFromClusterEKS(cluster *cluster.Cluster) (int, error) {
if branch, ok := cluster.Labels["branch"]; ok {
if clusterType, ok := cluster.Labels["cluster"]; ok {
buildNumStr := strings.Replace(strings.Replace(cluster.Name, branch+"-", "", -1), "-"+clusterType, "", -1)
return strconv.Atoi(buildNumStr)
}
}
return 0, fmt.Errorf("finding build number for cluster " + cluster.Name)
}
// GetBuildNumberFromCluster gets the build number from the cluster labels
func (o *StepE2EGCOptions) GetBuildNumberFromCluster(cluster *gke.Cluster) (int, error) {
if branch, ok := cluster.ResourceLabels["branch"]; ok {
if clusterType, ok := cluster.ResourceLabels["cluster"]; ok {
buildNumStr := strings.Replace(strings.Replace(cluster.Name, branch+"-", "", -1), "-"+clusterType, "", -1)
return strconv.Atoi(buildNumStr)
}
}
return 0, fmt.Errorf("finding build number for cluster " + cluster.Name)
}
// ShouldDeleteMarkedCluster returns true if the cluster has a delete label
func (o *StepE2EGCOptions) ShouldDeleteMarkedCluster(cluster *gke.Cluster) bool {
if deleteLabel, ok := cluster.ResourceLabels["delete-me"]; ok {
if deleteLabel == "true" {
return true
}
}
return false
}
// ShouldDeleteMarkedEKSCluster returns true if the cluster has a delete label
func (o *StepE2EGCOptions) ShouldDeleteMarkedEKSCluster(cluster *cluster.Cluster) bool {
if deleteLabel, ok := cluster.Labels["delete-me"]; ok {
if deleteLabel == "true" {
return true
}
}
return false
}
// ShouldDeleteOlderThanDurationEKS returns true if the cluster is older than the delete duration and does not have a keep label
func (o *StepE2EGCOptions) ShouldDeleteOlderThanDurationEKS(cluster *cluster.Cluster) bool {
if createdTime, ok := cluster.Labels["create-time"]; ok {
createdDate, err := time.Parse("Mon-Jan-2-2006-15-04-05", createdTime)
if err != nil {
log.Logger().Errorf("Error parsing date for cluster %s", createdTime)
log.Logger().Error(err)
} else {
ttlExceededDate := createdDate.Add(time.Duration(o.Duration) * time.Hour)
now := time.Now().UTC()
if now.After(ttlExceededDate) {
if _, ok := cluster.Labels["keep-me"]; !ok {
return true
}
}
}
}
return false
}
// ShouldDeleteOlderThanDuration returns true if the cluster is older than the delete duration and does not have a keep label
func (o *StepE2EGCOptions) ShouldDeleteOlderThanDuration(cluster *gke.Cluster) bool {
if createdTime, ok := cluster.ResourceLabels["create-time"]; ok {
createdDate, err := time.Parse("Mon-Jan-2-2006-15-04-05", createdTime)
if err != nil {
log.Logger().Errorf("Error parsing date for cluster %s", createdTime)
log.Logger().Error(err)
} else {
ttlExceededDate := createdDate.Add(time.Duration(o.Duration) * time.Hour)
now := time.Now().UTC()
if now.After(ttlExceededDate) {
if _, ok := cluster.ResourceLabels["keep-me"]; !ok {
return true
}
}
}
}
return false
}
// ShouldDeleteDueToNewerRunEKS returns true if a cluster with a higher build number exists
func (o *StepE2EGCOptions) ShouldDeleteDueToNewerRunEKS(cluster *cluster.Cluster, clusters []*cluster.Cluster) bool {
if branchLabel, ok := cluster.Labels["branch"]; ok {
if strings.Contains(branchLabel, "pr-") {
currentBuildNumber, err := o.GetBuildNumberFromClusterEKS(cluster)
if err == nil {
if clusterType, ok := cluster.Labels["cluster"]; ok {
for _, existingCluster := range clusters {
// Check for same PR & Cluster type
if existingClusterType, ok := existingCluster.Labels["cluster"]; ok {
if strings.Contains(existingCluster.Name, branchLabel) && existingClusterType == clusterType {
existingBuildNumber, err := o.GetBuildNumberFromClusterEKS(existingCluster)
if err == nil {
// Delete the older build
if currentBuildNumber < existingBuildNumber {
if _, ok := cluster.Labels["keep-me"]; !ok {
return true
}
break
}
}
}
}
}
}
}
}
}
return false
}
// ShouldDeleteDueToNewerRun returns true if a cluster with a higher build number exists
func (o *StepE2EGCOptions) ShouldDeleteDueToNewerRun(cluster *gke.Cluster, clusters []gke.Cluster) bool {
if branchLabel, ok := cluster.ResourceLabels["branch"]; ok {
if strings.Contains(branchLabel, "pr-") {
currentBuildNumber, err := o.GetBuildNumberFromCluster(cluster)
if err == nil {
if clusterType, ok := cluster.ResourceLabels["cluster"]; ok {
for _, ec := range clusters {
existingCluster := ec
// Check for same PR & Cluster type
if existingClusterType, ok := existingCluster.ResourceLabels["cluster"]; ok {
if strings.Contains(existingCluster.Name, branchLabel) && existingClusterType == clusterType {
existingBuildNumber, err := o.GetBuildNumberFromCluster(&existingCluster)
if err == nil {
// Delete the older build
if currentBuildNumber < existingBuildNumber {
if _, ok := cluster.ResourceLabels["keep-me"]; !ok {
return true
}
break
}
}
}
}
}
}
}
}
}
return false
}
func (o *StepE2EGCOptions) deleteEksCluster(cluster *cluster.Cluster, client cluster.Client) error {
err := client.Delete(cluster)
if err != nil {
return errors.Wrapf(err, "error deleting EKS cluster %s", cluster.Name)
}
return nil
}
func (o *StepE2EGCOptions) deleteGkeCluster(cluster *gke.Cluster) {
deleteOptions := &deletecmd.DeleteGkeOptions{
Options: get.Options{
CommonOptions: &opts.CommonOptions{},
},
}
deleteOptions.Args = []string{cluster.Name}
deleteOptions.ProjectID = o.ProjectID
deleteOptions.Region = o.Region
err := deleteOptions.Run()
if err != nil {
log.Logger().Error(err)
} else {
log.Logger().Infof("Deleted cluster %s", cluster.Name)
}
}