@@ -93,7 +93,7 @@ class HarvesterPreviewEnvironment {
93
93
*/
94
94
static expectedNamespaceFromBranch ( branch : string ) : string {
95
95
const previewName = previewNameFromBranchName ( branch )
96
- return `${ HarvesterPreviewEnvironment . namespacePrefix } - ${ previewName } `
96
+ return `${ HarvesterPreviewEnvironment . namespacePrefix } ${ previewName } `
97
97
}
98
98
}
99
99
@@ -113,7 +113,7 @@ class CoreDevPreviewEnvironment {
113
113
}
114
114
115
115
async delete ( sliceID : string ) : Promise < void > {
116
- await wipePreviewEnvironmentAndNamespace ( helmInstallName , this . name , CORE_DEV_KUBECONFIG_PATH , { slice : sliceID } )
116
+ await wipePreviewEnvironmentAndNamespace ( helmInstallName , this . namespace , CORE_DEV_KUBECONFIG_PATH , { slice : sliceID } )
117
117
}
118
118
119
119
async removeDNSRecords ( sliceID : string ) {
@@ -191,7 +191,7 @@ class CoreDevPreviewEnvironment {
191
191
*/
192
192
static expectedNamespaceFromBranch ( branch : string ) : string {
193
193
const previewName = previewNameFromBranchName ( branch )
194
- return `${ CoreDevPreviewEnvironment . namespacePrefix } - ${ previewName } `
194
+ return `${ CoreDevPreviewEnvironment . namespacePrefix } ${ previewName } `
195
195
}
196
196
197
197
}
@@ -251,6 +251,53 @@ async function deletePreviewEnvironments() {
251
251
252
252
werft . phase ( "Determining which preview environments are stale" ) ;
253
253
254
+ const previewsToDelete = await determineStalePreviewEnvironments ( {
255
+ branches : branches ,
256
+ previews : previews
257
+ } )
258
+
259
+ if ( previewsToDelete . length == 0 ) {
260
+ werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , "No stale preview environments." )
261
+ werft . done ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS )
262
+ return
263
+ } else {
264
+ werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Found ${ previewsToDelete . length } stale preview environments` )
265
+ werft . done ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS )
266
+ }
267
+
268
+ werft . phase ( "Deleting stale preview environments" )
269
+ if ( DRY_RUN ) {
270
+ previewsToDelete . forEach ( preview => {
271
+ werft . log ( SLICES . DELETING_PREVIEW_ENVIRONMNETS , `Would have deleted preview environment ${ preview . name } (${ preview . namespace } )` )
272
+ } )
273
+ werft . done ( SLICES . DELETING_PREVIEW_ENVIRONMNETS )
274
+ return
275
+ }
276
+
277
+ try {
278
+ const promises : Promise < any > [ ] = [ ] ;
279
+ previewsToDelete . forEach ( preview => promises . push ( removePreviewEnvironment ( preview ) ) )
280
+ await Promise . all ( promises )
281
+ werft . done ( SLICES . DELETING_PREVIEW_ENVIRONMNETS )
282
+ } catch ( err ) {
283
+ werft . fail ( SLICES . DELETING_PREVIEW_ENVIRONMNETS , err )
284
+ }
285
+ }
286
+
287
+ /**
288
+ * Determines if preview environemnts are stale and should be deleted
289
+ *
290
+ * As we don't have a mapping from preview environnment -> Git branch we have to
291
+ * go about this in a bit of a backwards way.
292
+ *
293
+ * Based on the active git branches we compute a set of "expected" preview environments
294
+ * and then use that to compare with the "live" preview environemnts to decide which
295
+ * ones to keep
296
+ */
297
+ async function determineStalePreviewEnvironments ( options : { previews : PreviewEnvironment [ ] , branches : string [ ] } ) : Promise < PreviewEnvironment [ ] > {
298
+
299
+ const { branches, previews} = options
300
+
254
301
// The set of namespaces that we would expect based on the open branches.
255
302
// This contains both the core-dev and the harvester namespaces as we only use this set for
256
303
// testing membership in situations where we don't care if the preview environment is based on
@@ -276,48 +323,27 @@ async function deletePreviewEnvironments() {
276
323
HarvesterPreviewEnvironment . expectedNamespaceFromBranch ( branch )
277
324
] ) )
278
325
279
- const deleteDueToMissingBranch = previews . filter ( preview => {
280
- werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Considering ${ preview . name } (${ preview . namespace } ) stale due to missing branch` )
281
- return ! previewNamespaceBasedOnBranches . has ( preview . namespace )
282
- } )
283
-
284
- const deleteDueToNoCommitActivity = previews . filter ( preview => {
285
- werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Considering ${ preview . name } (${ preview . namespace } ) stale due to no recent commit activity` )
286
- return previewNamespaceBasedOnStaleBranches . has ( preview . namespace )
287
- } )
288
-
289
- const deleteDueToNoDBActivity = previews . filter ( ( preview : PreviewEnvironment ) => {
290
- werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Considering ${ preview . name } (${ preview . namespace } ) stale due to no recent DB activity` )
291
- return preview . isInactive ( )
292
- } )
326
+ const previewsToDelete = previews . filter ( ( preview : PreviewEnvironment ) => {
327
+ if ( ! previewNamespaceBasedOnBranches . has ( preview . namespace ) ) {
328
+ werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Considering ${ preview . name } (${ preview . namespace } ) stale due to missing branch` )
329
+ return true
330
+ }
293
331
294
- const previewsToDelete = new Set ( [ ...deleteDueToMissingBranch , ...deleteDueToNoCommitActivity , ...deleteDueToNoDBActivity ] )
332
+ if ( previewNamespaceBasedOnStaleBranches . has ( preview . namespace ) ) {
333
+ werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Considering ${ preview . name } (${ preview . namespace } ) stale due to no recent commit activity` )
334
+ return true
335
+ }
295
336
296
- if ( previewsToDelete . size == 0 ) {
297
- werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , "No stale preview environments." )
298
- werft . done ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS )
299
- return
300
- } else {
301
- werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Found ${ previewsToDelete . size } stale preview environments` )
302
- }
337
+ if ( preview . isInactive ( ) ) {
338
+ werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Considering ${ preview . name } (${ preview . namespace } ) stale due to no recent DB activity` )
339
+ return true
340
+ }
303
341
304
- werft . phase ( "Deleting stale preview environments" )
305
- if ( DRY_RUN ) {
306
- previewsToDelete . forEach ( preview => {
307
- werft . log ( SLICES . DELETING_PREVIEW_ENVIRONMNETS , `Would have deleted preview environment ${ preview . name } (${ preview . namespace } )` )
308
- } )
309
- werft . done ( SLICES . DELETING_PREVIEW_ENVIRONMNETS )
310
- return
311
- }
342
+ werft . log ( SLICES . DETERMINING_STALE_PREVIEW_ENVIRONMENTS , `Considering ${ preview . name } (${ preview . namespace } ) active` )
343
+ return false
344
+ } )
312
345
313
- try {
314
- const promises : Promise < any > [ ] = [ ] ;
315
- previewsToDelete . forEach ( preview => promises . push ( removePreviewEnvironment ( preview ) ) )
316
- await Promise . all ( promises )
317
- werft . done ( SLICES . DELETING_PREVIEW_ENVIRONMNETS )
318
- } catch ( err ) {
319
- werft . fail ( SLICES . DELETING_PREVIEW_ENVIRONMNETS , err )
320
- }
346
+ return previewsToDelete
321
347
}
322
348
323
349
async function removePreviewEnvironment ( previewEnvironment : PreviewEnvironment ) {
@@ -330,12 +356,12 @@ async function removePreviewEnvironment(previewEnvironment: PreviewEnvironment)
330
356
await previewEnvironment . delete ( sliceID )
331
357
werft . done ( sliceID )
332
358
} catch ( e ) {
333
- werft . fail ( sliceID , e )
359
+ werft . failSlice ( sliceID , e )
334
360
}
335
361
}
336
362
337
363
async function removeCertificate ( preview : string , kubectlConfig : string , slice : string ) {
338
- return exec ( `kubectl --kubeconfig ${ kubectlConfig } -n certs delete cert ${ preview } ` , { slice : slice , async : true } )
364
+ return exec ( `kubectl --kubeconfig ${ kubectlConfig } -n certs delete --ignore-not-found=true cert ${ preview } ` , { slice : slice , async : true } )
339
365
}
340
366
341
367
async function cleanLoadbalancer ( ) {
0 commit comments