@@ -5,29 +5,53 @@ import { getGlobalWerftInstance } from './werft';
55
66export const IS_PREVIEW_APP_LABEL : string = "isPreviewApp" ;
77
8+ export const helmInstallName = "gitpod" ;
9+
810export function setKubectlContextNamespace ( namespace : string , shellOpts : ExecOptions ) {
911 [
1012 `kubectl config current-context` ,
1113 `kubectl config set-context --current --namespace=${ namespace } `
1214 ] . forEach ( cmd => exec ( cmd , shellOpts ) ) ;
1315}
1416
17+ export async function wipePreviewEnvironmentAndNamespace ( helmInstallName : string , namespace : string , shellOpts : ExecOptions ) {
18+ // wipe preview envs built with installer
19+ await wipePreviewEnvironmentInstaller ( namespace , shellOpts ) ;
20+
21+ // wipe preview envs previously built with helm
22+ await wipePreviewEnvironmentHelm ( helmInstallName , namespace , shellOpts )
23+
24+ deleteAllWorkspaces ( namespace , shellOpts ) ;
25+
26+ await deleteAllUnnamespacedObjects ( namespace , shellOpts ) ;
27+
28+ deleteNamespace ( true , namespace , shellOpts ) ;
29+ }
30+
1531export async function wipeAndRecreateNamespace ( helmInstallName : string , namespace : string , shellOpts : ExecOptions ) {
16- await wipePreviewEnvironment ( helmInstallName , namespace , shellOpts ) ;
32+ await wipePreviewEnvironmentAndNamespace ( helmInstallName , namespace , shellOpts ) ;
1733
1834 createNamespace ( namespace , shellOpts ) ;
1935}
2036
21- export async function wipePreviewEnvironment ( helmInstallName : string , namespace : string , shellOpts : ExecOptions ) {
37+ export async function wipePreviewEnvironmentHelm ( helmInstallName : string , namespace : string , shellOpts : ExecOptions ) {
2238 // uninstall helm first so that:
2339 // - ws-scaler can't create new ghosts in the meantime
2440 // - ws-manager can't start new probes/workspaces
2541 uninstallHelm ( helmInstallName , namespace , shellOpts )
42+ }
2643
27- deleteAllWorkspaces ( namespace , shellOpts ) ;
28- await deleteAllUnnamespacedObjects ( namespace , shellOpts ) ;
44+ async function wipePreviewEnvironmentInstaller ( namespace : string , shellOpts : ExecOptions ) {
45+ const slice = shellOpts . slice || "installer" ;
46+ const werft = getGlobalWerftInstance ( ) ;
2947
30- deleteNamespace ( true , namespace , shellOpts ) ;
48+ const hasGitpodConfigmap = ( exec ( `kubectl -n ${ namespace } get configmap gitpod-app` , { slice, dontCheckRc : true } ) ) . code === 0 ;
49+ if ( hasGitpodConfigmap ) {
50+ werft . log ( slice , `${ namespace } has Gitpod configmap, proceeding with removal` ) ;
51+ exec ( `./.werft/util/uninstall-gitpod.sh ${ namespace } ` , { slice } ) ;
52+ } else {
53+ werft . log ( slice , `There is no Gitpod configmap, moving on` ) ;
54+ }
3155}
3256
3357function uninstallHelm ( installationName : string , namespace : string , shellOpts : ExecOptions ) {
@@ -43,6 +67,7 @@ function uninstallHelm(installationName: string, namespace: string, shellOpts: E
4367 exec ( `helm --namespace ${ namespace } delete ${ installationName } --wait` , shellOpts ) ;
4468}
4569
70+ // Delete pods for running workspaces, even if they are stuck in terminating because of the finalizer decorator
4671function deleteAllWorkspaces ( namespace : string , shellOpts : ExecOptions ) {
4772 const objs = exec ( `kubectl get pod -l component=workspace --namespace ${ namespace } --no-headers -o=custom-columns=:metadata.name` , { ...shellOpts , async : false } )
4873 . split ( "\n" )
@@ -74,13 +99,14 @@ async function deleteAllUnnamespacedObjects(namespace: string, shellOpts: ExecOp
7499
75100 const promisedDeletes : Promise < any > [ ] = [ ] ;
76101 for ( const resType of [ "clusterrole" , "clusterrolebinding" , "podsecuritypolicy" ] ) {
77- werft . log ( slice , `Deleting old ${ resType } s...` ) ;
102+ werft . log ( slice , `Searching and filtering ${ resType } s...` ) ;
78103 const objs = exec ( `kubectl get ${ resType } --no-headers -o=custom-columns=:metadata.name` , { ...shellOpts , slice, async : false } )
79104 . split ( "\n" )
80105 . map ( o => o . trim ( ) )
81106 . filter ( o => o . length > 0 )
82107 . filter ( o => o . startsWith ( `${ namespace } -ns-` ) ) ; // "{{ .Release.Namespace }}-ns-" is the prefix-pattern we use throughout our helm resources for un-namespaced resources
83108
109+ werft . log ( slice , `Deleting old ${ resType } s...` ) ;
84110 for ( const obj of objs ) {
85111 promisedDeletes . push ( exec ( `kubectl delete ${ resType } ${ obj } ` , { ...shellOpts , slice, async : true } ) as Promise < any > ) ;
86112 }
@@ -127,7 +153,7 @@ export function deleteNamespace(wait: boolean, namespace: string, shellOpts: Exe
127153 }
128154}
129155
130- export function deleteNonNamespaceObjects ( namespace : string , destname : string , shellOpts : ExecOptions ) {
156+ export async function deleteNonNamespaceObjects ( namespace : string , destname : string , shellOpts : ExecOptions ) {
131157 exec ( `/usr/local/bin/helm3 delete gitpod-${ destname } || echo gitpod-${ destname } was not installed yet` , { ...shellOpts } ) ;
132158
133159 let objs = [ ] ;
@@ -141,9 +167,11 @@ export function deleteNonNamespaceObjects(namespace: string, destname: string, s
141167 )
142168 )
143169
170+ const promisedDeletes : Promise < any > [ ] = [ ] ;
144171 objs . forEach ( o => {
145- exec ( `kubectl delete ${ o . kind } ${ o . obj } ` , shellOpts ) ;
172+ promisedDeletes . push ( exec ( `kubectl delete ${ o . kind } ${ o . obj } ` , { ... shellOpts , async : true } ) as Promise < any > ) ;
146173 } ) ;
174+ await Promise . all ( promisedDeletes ) ;
147175}
148176
149177export interface PortRange {
0 commit comments