1- import { werft } from './util/shell' ;
1+ import { werft , exec } from './util/shell' ;
22import { wipePreviewEnvironment , listAllPreviewNamespaces } from './util/kubectl' ;
3+ import * as fs from 'fs' ;
4+ import { deleteExternalIp } from './util/gcloud' ;
35
46
5- async function wipeDevstaging ( ) {
7+ async function wipePreviewCluster ( pathToKubeConfig : string ) {
68 const namespace_raw = process . env . NAMESPACE ;
79 const namespaces : string [ ] = [ ] ;
810 if ( namespace_raw === "<no value>" || ! namespace_raw ) {
911 werft . log ( 'wipe' , "Going to wipe all namespaces" ) ;
10- listAllPreviewNamespaces ( "" )
12+ listAllPreviewNamespaces ( pathToKubeConfig )
1113 . map ( ns => namespaces . push ( ns ) ) ;
1214 } else {
1315 werft . log ( 'wipe' , `Going to wipe namespace ${ namespace_raw } ` ) ;
1416 namespaces . push ( namespace_raw ) ;
1517 }
1618
1719 for ( const namespace of namespaces ) {
18- await wipePreviewEnvironment ( "" , "gitpod" , namespace , { slice : 'wipe' } ) ;
20+ await wipePreviewEnvironment ( pathToKubeConfig , "gitpod" , namespace , { slice : 'wipe' } ) ;
1921 }
20- werft . done ( 'wipe' ) ;
2122}
2223
23- wipeDevstaging ( )
24+ // if we have "/workspace/k3s-external.yaml" present that means a k3s ws cluster
25+ // exists, therefore, delete corresponding preview deployment from that cluster too
26+ // NOTE: Even for a non k3s ws deployment we will attempt to clean the preview.
27+ // This saves us from writing complex logic of querying meta cluster for registered workspaces
28+ // Since we use the same namespace to deploy in both dev and k3s cluster, this is safe
29+ async function k3sCleanup ( ) {
30+ if ( fs . existsSync ( "/workspace/k3s-external.yaml" ) ) {
31+ werft . log ( "wipe" , "found /workspace/k3s-external.yaml, assuming k3s ws cluster deployment exists, will attempt to wipe it" )
32+ await wipePreviewCluster ( "/workspace/k3s-external.yaml" )
33+ const namespace_raw = process . env . NAMESPACE ;
34+
35+ // Since werft creates static external IP for ws-proxy of k3s using gcloud
36+ // we delete it here. We retry because the ws-proxy-service which binds to this IP might not be deleted immediately
37+ const k3sWsProxyIP =
38+ deleteExternalIp ( "wipe" , namespace_raw )
39+ } else {
40+ werft . log ( "wipe" , `file /workspace/k3s-external.yaml does not exist, no cleanup for k3s cluster` )
41+ }
42+ }
43+
44+ // clean up the dev cluster in gitpod-core-dev
45+ async function devCleanup ( ) {
46+ await wipePreviewCluster ( "" )
47+ }
48+
49+ // sweeper runs in the dev cluster so we need to delete the k3s cluster first and then delete self contained namespace
50+ k3sCleanup ( ) . then ( ( ) => {
51+ devCleanup ( )
52+ } )
53+
54+
55+ werft . done ( 'wipe' ) ;
0 commit comments