Skip to content

Commit

Permalink
refactor: integrated lease methods & moved leases to resources/lease (#…
Browse files Browse the repository at this point in the history
…1328)

Signed-off-by: Jeromy Cannon <[email protected]>
  • Loading branch information
jeromy-cannon authored Feb 9, 2025
1 parent 547ed19 commit d990add
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 65 deletions.
17 changes: 1 addition & 16 deletions src/core/kube/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {type Contexts} from './contexts.js';
import {type Pvcs} from './resources/pvc/pvcs.js';
import {type Services} from './services.js';
import {type Pods} from './resources/pod/pods.js';
import {type Leases} from './leases.js';
import {type Leases} from './resources/lease/leases.js';
import {type IngressClasses} from './ingress_classes.js';
import {type Secrets} from './secrets.js';

Expand Down Expand Up @@ -176,21 +176,6 @@ export interface K8 {
*/
deletePvc(name: string, namespace: NamespaceName): Promise<boolean>;

createNamespacedLease(
namespace: NamespaceName,
leaseName: string,
holderName: string,
durationSeconds,
): Promise<k8s.V1Lease>;

readNamespacedLease(leaseName: string, namespace: NamespaceName, timesCalled?): Promise<any>;

renewNamespaceLease(leaseName: string, namespace: NamespaceName, lease: k8s.V1Lease): Promise<k8s.V1Lease>;

transferNamespaceLease(lease: k8s.V1Lease, newHolderName: string): Promise<V1Lease>;

deleteNamespacedLease(name: string, namespace: NamespaceName): Promise<k8s.V1Status>;

patchIngress(namespace: NamespaceName, ingressName: string, patch: object): Promise<void>;

patchConfigMap(namespace: NamespaceName, configMapName: string, data: Record<string, string>): Promise<void>;
Expand Down
33 changes: 2 additions & 31 deletions src/core/kube/k8_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import * as k8s from '@kubernetes/client-node';
import {type V1Lease} from '@kubernetes/client-node';
import {Flags as flags} from '../../commands/flags.js';
import {MissingArgumentError, SoloError} from './../errors.js';
import * as constants from './../constants.js';
import {ConfigManager} from './../config_manager.js';
import {SoloLogger} from './../logging.js';
import {type TarCreateFilter} from '../../types/aliases.js';
import {type ExtendedNetServer} from '../../types/index.js';
import {inject, injectable} from 'tsyringe-neo';
import {patchInject} from './../container_helper.js';
import {type K8} from './k8.js';
Expand All @@ -31,8 +29,8 @@ import {type Services} from './services.js';
import {K8ClientServices} from './k8_client/k8_client_services.js';
import {type Pvcs} from './resources/pvc/pvcs.js';
import {K8ClientPvcs} from './k8_client/resources/pvc/k8_client_pvcs.js';
import {type Leases} from './leases.js';
import {K8ClientLeases} from './k8_client/k8_client_leases.js';
import {type Leases} from './resources/lease/leases.js';
import {K8ClientLeases} from './k8_client/resources/lease/k8_client_leases.js';
import {K8ClientNamespaces} from './k8_client/resources/namespace/k8_client_namespaces.js';
import {K8ClientIngressClasses} from './k8_client/k8_client_ingress_classes.js';
import {type IngressClasses} from './ingress_classes.js';
Expand Down Expand Up @@ -199,33 +197,6 @@ export class K8Client extends K8ClientBase implements K8 {

// --------------------------------------- Utility Methods --------------------------------------- //

// --------------------------------------- LEASES --------------------------------------- //

public async createNamespacedLease(
namespace: NamespaceName,
leaseName: string,
holderName: string,
durationSeconds = 20,
) {
return this.leases().create(namespace, leaseName, holderName, durationSeconds);
}

public async readNamespacedLease(leaseName: string, namespace: NamespaceName, timesCalled = 0) {
return this.leases().read(namespace, leaseName, timesCalled);
}

public async renewNamespaceLease(leaseName: string, namespace: NamespaceName, lease: k8s.V1Lease) {
return this.leases().renew(namespace, leaseName, lease);
}

public async transferNamespaceLease(lease: k8s.V1Lease, newHolderName: string): Promise<V1Lease> {
return this.leases().transfer(lease, newHolderName);
}

public async deleteNamespacedLease(name: string, namespace: NamespaceName) {
return this.leases().delete(namespace, name);
}

/* ------------- Utilities ------------- */

private getNamespace(): NamespaceName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
V1ObjectMeta,
type V1Status,
} from '@kubernetes/client-node';
import {type Leases} from '../leases.js';
import {type NamespaceName} from '../resources/namespace/namespace_name.js';
import {type Leases} from '../../../resources/lease/leases.js';
import {type NamespaceName} from '../../../resources/namespace/namespace_name.js';
import type http from 'node:http';
import {SoloError} from '../../errors.js';
import {SoloError} from '../../../../errors.js';
import {getReasonPhrase, StatusCodes} from 'http-status-codes';
import {SoloLogger} from '../../logging.js';
import {SoloLogger} from '../../../../logging.js';
import {container} from 'tsyringe-neo';
import {sleep} from '../../helpers.js';
import {Duration} from '../../time/duration.js';
import {sleep} from '../../../../helpers.js';
import {Duration} from '../../../../time/duration.js';

export class K8ClientLeases implements Leases {
private readonly logger: SoloLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import {type V1Lease, type V1Status} from '@kubernetes/client-node';
import {type NamespaceName} from './resources/namespace/namespace_name.js';
import {type NamespaceName} from '../namespace/namespace_name.js';

export interface Leases {
/**
Expand Down
17 changes: 7 additions & 10 deletions src/core/lease/interval_lease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class IntervalLease implements Lease {
*/
private async retrieveLease(): Promise<V1Lease> {
try {
return await this.client.readNamespacedLease(this.leaseName, this.namespace);
return await this.client.leases().read(this.namespace, this.leaseName);
} catch (e: any) {
if (!(e instanceof SoloError)) {
throw new LeaseAcquisitionError(
Expand Down Expand Up @@ -311,14 +311,11 @@ export class IntervalLease implements Lease {
private async createOrRenewLease(lease: V1Lease): Promise<void> {
try {
if (!lease) {
await this.client.createNamespacedLease(
this.namespace,
this.leaseName,
this.leaseHolder.toJson(),
this.durationSeconds,
);
await this.client
.leases()
.create(this.namespace, this.leaseName, this.leaseHolder.toJson(), this.durationSeconds);
} else {
await this.client.renewNamespaceLease(this.leaseName, this.namespace, lease);
await this.client.leases().renew(this.namespace, this.leaseName, lease);
}

if (!this.scheduleId) {
Expand All @@ -339,7 +336,7 @@ export class IntervalLease implements Lease {
*/
private async transferLease(lease: V1Lease): Promise<void> {
try {
await this.client.transferNamespaceLease(lease, this.leaseHolder.toJson());
await this.client.leases().transfer(lease, this.leaseHolder.toJson());

if (!this.scheduleId) {
this.scheduleId = await this.renewalService.schedule(this);
Expand All @@ -357,7 +354,7 @@ export class IntervalLease implements Lease {
*/
private async deleteLease(): Promise<void> {
try {
await this.client.deleteNamespacedLease(this.leaseName, this.namespace);
await this.client.leases().delete(this.namespace, this.leaseName);
} catch (e: any) {
throw new LeaseRelinquishmentError(
`failed to delete the lease named '${this.leaseName}' in the ` + `'${this.namespace}' namespace`,
Expand Down
4 changes: 3 additions & 1 deletion test/unit/commands/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ describe('NetworkCommand unit tests', () => {
waitForRunningPhase: sinon.stub(),
waitForReadyStatus: sinon.stub(),
});
opts.k8.readNamespacedLease = sinon.stub();
opts.k8.leases = sinon.stub().returns({
read: sinon.stub(),
});
const clusterChecksStub = sinon.stub() as unknown as ClusterChecks;
clusterChecksStub.isMinioInstalled = sinon.stub();
clusterChecksStub.isPrometheusInstalled = sinon.stub();
Expand Down

0 comments on commit d990add

Please sign in to comment.