@@ -24,31 +24,43 @@ import { type LeaseRenewalService } from './lease_renewal.ts'
24
24
import { sleep } from './helpers.ts'
25
25
26
26
export class Lease {
27
+ /** The default duration in seconds for which the lease is to be held before being considered expired. */
27
28
public static readonly DEFAULT_LEASE_DURATION = 20
28
29
29
- private readonly _client : K8
30
- private readonly _renewalService : LeaseRenewalService
30
+ /** The holder of the lease. */
31
31
private readonly _leaseHolder : LeaseHolder
32
+
33
+ /** The namespace which contains the lease. */
32
34
private readonly _namespace : string
33
35
36
+ /** The name of the lease. */
34
37
private readonly _leaseName : string
38
+
39
+ /** The duration in seconds for which the lease is to be held. */
35
40
private readonly _durationSeconds : number
36
41
42
+ /** The identifier of the scheduled lease renewal. */
37
43
private _scheduleId : number | null = null
38
44
39
- public constructor ( client : K8 ,
40
- renewalService : LeaseRenewalService ,
45
+ /**
46
+ * @param client - Injected kubernetes client need by the methods to create, renew, and delete leases.
47
+ * @param renewalService - Injected lease renewal service need to support automatic (background) lease renewals.
48
+ * @param leaseHolder - The holder of the lease.
49
+ * @param namespace - The namespace in which the lease is to be acquired.
50
+ * @param leaseName - The name of the lease to be acquired.
51
+ * @param durationSeconds - The duration in seconds for which the lease is to be held.
52
+ */
53
+ public constructor ( private readonly client : K8 ,
54
+ private readonly renewalService : LeaseRenewalService ,
41
55
leaseHolder : LeaseHolder ,
42
56
namespace : string ,
43
57
leaseName : string | null = null ,
44
58
durationSeconds : number | null = null ) {
45
- if ( ! client ) throw new MissingArgumentError ( '_client is required' )
46
- if ( ! renewalService ) throw new MissingArgumentError ( '_renewalService is required' )
59
+ if ( ! client ) throw new MissingArgumentError ( 'client is required' )
60
+ if ( ! renewalService ) throw new MissingArgumentError ( 'renewalService is required' )
47
61
if ( ! leaseHolder ) throw new MissingArgumentError ( '_leaseHolder is required' )
48
62
if ( ! namespace ) throw new MissingArgumentError ( '_namespace is required' )
49
63
50
- this . _client = client
51
- this . _renewalService = renewalService
52
64
this . _leaseHolder = leaseHolder
53
65
this . _namespace = namespace
54
66
@@ -140,10 +152,10 @@ export class Lease {
140
152
const lease = await this . retrieveLease ( )
141
153
142
154
if ( this . scheduleId ) {
143
- await this . _renewalService . cancel ( this . scheduleId )
155
+ await this . renewalService . cancel ( this . scheduleId )
144
156
// Needed to ensure any pending renewals are truly cancelled before proceeding to delete the Lease.
145
157
// This is required because clearInterval() is not guaranteed to abort any pending interval.
146
- await sleep ( this . _renewalService . calculateRenewalDelay ( this ) )
158
+ await sleep ( this . renewalService . calculateRenewalDelay ( this ) )
147
159
}
148
160
149
161
this . scheduleId = null
@@ -184,7 +196,7 @@ export class Lease {
184
196
185
197
private async retrieveLease ( ) : Promise < V1Lease > {
186
198
try {
187
- return await this . _client . readNamespacedLease ( this . leaseName , this . namespace )
199
+ return await this . client . readNamespacedLease ( this . leaseName , this . namespace )
188
200
} catch ( e : any ) {
189
201
if ( ! ( e instanceof SoloError ) ) {
190
202
throw new LeaseAcquisitionError ( `failed to read the lease named '${ this . leaseName } ' in the ` +
@@ -203,13 +215,13 @@ export class Lease {
203
215
private async createOrRenewLease ( lease : V1Lease ) : Promise < void > {
204
216
try {
205
217
if ( ! lease ) {
206
- await this . _client . createNamespacedLease ( this . leaseName , this . namespace , this . leaseHolder . toJson ( ) , this . durationSeconds )
218
+ await this . client . createNamespacedLease ( this . leaseName , this . namespace , this . leaseHolder . toJson ( ) , this . durationSeconds )
207
219
} else {
208
- await this . _client . renewNamespaceLease ( this . leaseName , this . namespace , lease )
220
+ await this . client . renewNamespaceLease ( this . leaseName , this . namespace , lease )
209
221
}
210
222
211
223
if ( ! this . scheduleId ) {
212
- this . scheduleId = await this . _renewalService . schedule ( this )
224
+ this . scheduleId = await this . renewalService . schedule ( this )
213
225
}
214
226
} catch ( e : any ) {
215
227
throw new LeaseAcquisitionError ( `failed to create or renew the lease named '${ this . leaseName } ' in the ` +
@@ -219,10 +231,10 @@ export class Lease {
219
231
220
232
private async transferLease ( lease : V1Lease ) : Promise < void > {
221
233
try {
222
- await this . _client . transferNamespaceLease ( lease , this . leaseHolder . toJson ( ) )
234
+ await this . client . transferNamespaceLease ( lease , this . leaseHolder . toJson ( ) )
223
235
224
- if ( ! this . _scheduleId ) {
225
- this . _scheduleId = await this . _renewalService . schedule ( this )
236
+ if ( ! this . scheduleId ) {
237
+ this . scheduleId = await this . renewalService . schedule ( this )
226
238
}
227
239
} catch ( e : any ) {
228
240
throw new LeaseAcquisitionError ( `failed to transfer the lease named '${ this . leaseName } ' in the ` +
@@ -232,7 +244,7 @@ export class Lease {
232
244
233
245
private async deleteLease ( ) : Promise < void > {
234
246
try {
235
- await this . _client . deleteNamespacedLease ( this . leaseName , this . namespace )
247
+ await this . client . deleteNamespacedLease ( this . leaseName , this . namespace )
236
248
} catch ( e : any ) {
237
249
throw new LeaseRelinquishmentError ( `failed to delete the lease named '${ this . leaseName } ' in the ` +
238
250
`'${ this . namespace } ' namespace` , e )
0 commit comments