@@ -15,6 +15,7 @@ export class PodCleaner {
15
15
private logger = new SimpleLogger ( "[PodCleaner]" ) ;
16
16
private k8sClient : {
17
17
core : k8s . CoreV1Api ;
18
+ apps : k8s . AppsV1Api ;
18
19
kubeConfig : k8s . KubeConfig ;
19
20
} ;
20
21
@@ -43,6 +44,7 @@ export class PodCleaner {
43
44
44
45
return {
45
46
core : kubeConfig . makeApiClient ( k8s . CoreV1Api ) ,
47
+ apps : kubeConfig . makeApiClient ( k8s . AppsV1Api ) ,
46
48
kubeConfig : kubeConfig ,
47
49
} ;
48
50
}
@@ -98,6 +100,25 @@ export class PodCleaner {
98
100
. catch ( this . #handleK8sError. bind ( this ) ) ;
99
101
}
100
102
103
+ async #deleteDaemonSets( opts : {
104
+ namespace : string ;
105
+ dryRun ?: boolean ;
106
+ fieldSelector ?: string ;
107
+ labelSelector ?: string ;
108
+ } ) {
109
+ return await this . k8sClient . apps
110
+ . deleteCollectionNamespacedDaemonSet (
111
+ opts . namespace ,
112
+ undefined , // pretty
113
+ undefined , // continue
114
+ opts . dryRun ? "All" : undefined ,
115
+ opts . fieldSelector ,
116
+ undefined , // gracePeriodSeconds
117
+ opts . labelSelector
118
+ )
119
+ . catch ( this . #handleK8sError. bind ( this ) ) ;
120
+ }
121
+
101
122
async #deleteCompletedRuns( ) {
102
123
this . logger . log ( "Deleting completed runs" ) ;
103
124
@@ -152,6 +173,28 @@ export class PodCleaner {
152
173
} ) ;
153
174
}
154
175
176
+ async #deleteCompletedPrePulls( ) {
177
+ this . logger . log ( "Deleting completed pre-pulls" ) ;
178
+
179
+ const start = Date . now ( ) ;
180
+
181
+ const result = await this . #deleteDaemonSets( {
182
+ namespace : this . namespace ,
183
+ labelSelector : "app=task-prepull" ,
184
+ } ) ;
185
+
186
+ const elapsedMs = Date . now ( ) - start ;
187
+
188
+ if ( ! result ) {
189
+ this . logger . log ( "Deleting completed pre-pulls: No delete result" , { elapsedMs } ) ;
190
+ return ;
191
+ }
192
+
193
+ const total = ( result . response as any ) ?. body ?. items ?. length ?? 0 ;
194
+
195
+ this . logger . log ( "Deleting completed pre-pulls: Done" , { total, elapsedMs } ) ;
196
+ }
197
+
155
198
async start ( ) {
156
199
this . enabled = true ;
157
200
this . logger . log ( "Starting" ) ;
@@ -186,6 +229,22 @@ export class PodCleaner {
186
229
2 * this . intervalInSeconds * 1000
187
230
) ;
188
231
232
+ const completedPrePullInterval = setInterval (
233
+ async ( ) => {
234
+ if ( ! this . enabled ) {
235
+ clearInterval ( completedPrePullInterval ) ;
236
+ return ;
237
+ }
238
+
239
+ try {
240
+ await this . #deleteCompletedPrePulls( ) ;
241
+ } catch ( error ) {
242
+ this . logger . error ( "Error deleting completed pre-pulls" , error ) ;
243
+ }
244
+ } ,
245
+ 2 * this . intervalInSeconds * 1000
246
+ ) ;
247
+
189
248
// this.#launchTests();
190
249
}
191
250
0 commit comments