Skip to content

Commit 0603780

Browse files
committed
WIP: Reword later
1 parent e4bb3aa commit 0603780

File tree

3 files changed

+89
-43
lines changed

3 files changed

+89
-43
lines changed

.werft/platform-delete-preview-environments-cron.ts

+69-43
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class HarvesterPreviewEnvironment {
9393
*/
9494
static expectedNamespaceFromBranch(branch: string): string {
9595
const previewName = previewNameFromBranchName(branch)
96-
return `${HarvesterPreviewEnvironment.namespacePrefix}-${previewName}`
96+
return `${HarvesterPreviewEnvironment.namespacePrefix}${previewName}`
9797
}
9898
}
9999

@@ -113,7 +113,7 @@ class CoreDevPreviewEnvironment {
113113
}
114114

115115
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 })
117117
}
118118

119119
async removeDNSRecords(sliceID: string) {
@@ -191,7 +191,7 @@ class CoreDevPreviewEnvironment {
191191
*/
192192
static expectedNamespaceFromBranch(branch: string): string {
193193
const previewName = previewNameFromBranchName(branch)
194-
return `${CoreDevPreviewEnvironment.namespacePrefix}-${previewName}`
194+
return `${CoreDevPreviewEnvironment.namespacePrefix}${previewName}`
195195
}
196196

197197
}
@@ -251,6 +251,53 @@ async function deletePreviewEnvironments() {
251251

252252
werft.phase("Determining which preview environments are stale");
253253

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+
254301
// The set of namespaces that we would expect based on the open branches.
255302
// This contains both the core-dev and the harvester namespaces as we only use this set for
256303
// testing membership in situations where we don't care if the preview environment is based on
@@ -276,48 +323,27 @@ async function deletePreviewEnvironments() {
276323
HarvesterPreviewEnvironment.expectedNamespaceFromBranch(branch)
277324
]))
278325

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+
}
293331

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+
}
295336

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+
}
303341

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+
})
312345

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
321347
}
322348

323349
async function removePreviewEnvironment(previewEnvironment: PreviewEnvironment) {
@@ -330,12 +356,12 @@ async function removePreviewEnvironment(previewEnvironment: PreviewEnvironment)
330356
await previewEnvironment.delete(sliceID)
331357
werft.done(sliceID)
332358
} catch (e) {
333-
werft.fail(sliceID, e)
359+
werft.failSlice(sliceID, e)
334360
}
335361
}
336362

337363
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})
339365
}
340366

341367
async function cleanLoadbalancer() {

.werft/platform-delete-preview-environments-cron.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pod:
1919
- name: harvester-kubeconfig
2020
secret:
2121
secretName: harvester-kubeconfig
22+
- name: harvester-k3s-dockerhub-pull-account
23+
secret:
24+
secretName: harvester-k3s-dockerhub-pull-account
2225
containers:
2326
- name: build
2427
image: eu.gcr.io/gitpod-core-dev/dev/dev-environment:aledbf-go.4
@@ -33,6 +36,8 @@ pod:
3336
readOnly: true
3437
- name: harvester-kubeconfig
3538
mountPath: /mnt/secrets/harvester-kubeconfig
39+
- name: harvester-k3s-dockerhub-pull-account
40+
mountPath: /mnt/secrets/harvester-k3s-dockerhub-pull-account
3641
env:
3742
- name: WERFT_HOST
3843
value: "werft.werft.svc.cluster.local:7777"

.werft/util/werft.ts

+15
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export class Werft {
6666
cmd.toString().split("\n").forEach((line: string) => this.log(slice, line))
6767
}
6868

69+
/**
70+
* Use this when you intend to fail the werft job
71+
*/
6972
public fail(slice, err) {
7073
// Set the status on the span for the slice and also propagate the status to the phase and root span
7174
// as well so we can query on all phases that had an error regardless of which slice produced the error.
@@ -85,6 +88,18 @@ export class Werft {
8588
throw err;
8689
}
8790

91+
/**
92+
* Use this when you intend to fail a single slice, but not the entire Werft job.
93+
*/
94+
public failSlice(slice: string, error: Error) {
95+
const span = this.sliceSpans[slice]
96+
if (span) {
97+
span.end()
98+
delete this.sliceSpans[slice]
99+
}
100+
console.log(`[${slice}|FAIL] ${error}`)
101+
}
102+
88103
public done(slice: string) {
89104
const span = this.sliceSpans[slice]
90105
if (span) {

0 commit comments

Comments
 (0)