File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,30 @@ export async function patchResourceFinalizers(
144144 console . log ( `Patched resources finalizers for ${ kind } ${ name } ` ) ;
145145}
146146
147+ /**
148+ * https://github.com/kubernetes/kubernetes/issues/90438
149+ * Namespaces cannot be modified with `kubectl patch` mechanism, using proxy and API
150+ * @param name namespace to patch finalizers
151+ */
152+ export async function patchNamespaceFinalizers ( name : string ) {
153+ console . log ( `Patching resource finalizers for namespace ${ name } ` ) ;
154+
155+ // Run proxy in background
156+ await exec ( 'nohup ./oc proxy > /dev/null 2>&1 &' ) ;
157+ await exec (
158+ `./kubectl get ns ${ name } -o json | jq '.spec = {"finalizers":[]}' > ${ name } .json` ,
159+ ) ;
160+ await exec (
161+ `curl -k -H "Content-Type: application/json" -X PUT --data-binary @${ name } .json http://127.0.0.1:8001/api/v1/namespaces/${ name } /finalize` ,
162+ ) ;
163+
164+ // Clean up
165+ await exec ( "kill -9 $(ps aux | grep proxy | head -n 1 | awk '{ print $2 }')" ) ;
166+ await exec ( 'rm -rf ${name}.json' ) ;
167+
168+ console . log ( `Patched namespace finalizers for ${ name } ` ) ;
169+ }
170+
147171export async function deleteResource (
148172 kind : string ,
149173 name : string ,
Original file line number Diff line number Diff line change @@ -130,6 +130,7 @@ export async function clean(): Promise<void> {
130130 . catch ( ( ) => undefined ) ,
131131 ] ) ;
132132
133+ // Remove resources
133134 await Promise . all ( [
134135 kubectl
135136 . deleteResource (
@@ -147,6 +148,16 @@ export async function clean(): Promise<void> {
147148 kubectl
148149 . deleteResource ( 'clusterrole' , 'snyk-monitor' , 'default' )
149150 . catch ( ( ) => undefined ) ,
151+ ] ) ;
152+
153+ // Kubernetes will be stuck trying to delete these namespaces if we don't clear the finalizers.
154+ await Promise . all ( [
155+ kubectl . patchNamespaceFinalizers ( 'services' ) . catch ( ( ) => undefined ) ,
156+ kubectl . patchNamespaceFinalizers ( 'argo-rollouts' ) . catch ( ( ) => undefined ) ,
157+ kubectl . patchNamespaceFinalizers ( 'snyk-monitor' ) . catch ( ( ) => undefined ) ,
158+ ] ) ;
159+ // Remove namespaces
160+ await Promise . all ( [
150161 kubectl . deleteNamespace ( 'services' ) . catch ( ( ) => undefined ) ,
151162 kubectl . deleteNamespace ( 'argo-rollouts' ) . catch ( ( ) => undefined ) ,
152163 kubectl . deleteNamespace ( 'snyk-monitor' ) . catch ( ( ) => undefined ) ,
You can’t perform that action at this time.
0 commit comments