Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(k8s): introduce Kubernetes lease lock mechanism #707

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
82dfdc7
added the new roles class that handles registering the cluster role &…
instamenta Sep 26, 2024
ff4a619
add initial method wrappers for the kubernetes client to handle clust…
instamenta Sep 26, 2024
263b6be
added login, register and delete for cluster roles
instamenta Sep 27, 2024
4c12d28
improved implementation, refactored added better error handling and m…
instamenta Sep 30, 2024
8c58428
refactoring, tying-up into over-all implementation and adding tests
instamenta Sep 30, 2024
c0d03f8
Merge remote-tracking branch 'origin/main' into 00043-introduce-clust…
instamenta Sep 30, 2024
743dee3
merge with main
instamenta Oct 18, 2024
5755d0a
added manager, transformed to ts existing code
instamenta Oct 18, 2024
3f8fda1
add lease quire, release and renew logic to all write commands
instamenta Oct 18, 2024
06bae40
add mechanism to enable tests not to crash
instamenta Oct 18, 2024
f489a13
bug fixes
instamenta Oct 18, 2024
223d560
fix integration test
instamenta Oct 18, 2024
3a1dd86
Merge branch 'main' into 00043-introduce-cluster-lock-so-that-solo-do…
instamenta Oct 18, 2024
e494bd3
fix 403 error
instamenta Oct 18, 2024
328ff8d
merge with main
instamenta Oct 18, 2024
17f0b55
merge with main
instamenta Oct 18, 2024
e9bcdfb
Merge remote-tracking branch 'origin/00043-introduce-cluster-lock-so-…
instamenta Oct 18, 2024
2262ff0
add retrying with limit and better encapsulated logic
instamenta Oct 18, 2024
13e7702
fix login and improve handling
instamenta Oct 21, 2024
8ec2b1c
fix
instamenta Oct 21, 2024
ba19592
introduce lease wrapper class to simplify the lease implementation
instamenta Oct 21, 2024
1d71020
fixing cli messages, and failing tests
instamenta Oct 21, 2024
a28245a
remove lease aquire from methods that are read only
instamenta Oct 21, 2024
f8ff765
encapsulate the lease wrapper logic inside the lease manager class
instamenta Oct 21, 2024
926a72d
formatting, fixing import's file extensions and removing obsolete --n…
instamenta Oct 21, 2024
9e6bc0f
merge with main
instamenta Oct 22, 2024
3793c8e
merge with main
instamenta Oct 22, 2024
7b29944
Merge branch 'main' into 00043-introduce-cluster-lock-so-that-solo-do…
instamenta Oct 22, 2024
f958697
merge with main
instamenta Oct 23, 2024
6eb1ac1
Merge remote-tracking branch 'origin/00043-introduce-cluster-lock-so-…
instamenta Oct 23, 2024
2799bbf
Merge branch 'main' into 00043-introduce-cluster-lock-so-that-solo-do…
instamenta Oct 23, 2024
237a4c1
merge with main and fix node commands to support lease
instamenta Oct 24, 2024
e28b116
add tests and support for lease
instamenta Oct 24, 2024
3e6e1b5
fix tests and code
instamenta Oct 24, 2024
de6968c
remove use of process.pwd()
instamenta Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/commands/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
* limitations under the License.
*
*/

import chalk from 'chalk'
import { BaseCommand } from './base.ts'
import { SoloError, IllegalArgumentError } from '../core/errors.ts'
import { flags } from './index.ts'
import { Listr } from 'listr2'
import * as prompts from './prompts.ts'
import { constants } from '../core/index.ts'
import { constants, type AccountManager } from '../core/index.ts'
import { type AccountId, AccountInfo, HbarUnit, PrivateKey } from '@hashgraph/sdk'
import { FREEZE_ADMIN_ACCOUNT } from '../core/constants.ts'
import { type AccountManager } from '../core/account_manager.ts'
import { type Opts } from '../types/index.ts'
import type { Opts } from '../types/index.ts'

export class AccountCommand extends BaseCommand {
private readonly accountManager: AccountManager
Expand Down Expand Up @@ -249,6 +247,7 @@ export class AccountCommand extends BaseCommand {

async create (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()

interface Context {
config: {
Expand Down Expand Up @@ -292,6 +291,8 @@ export class AccountCommand extends BaseCommand {
self.logger.debug('Initialized config', { config })

await self.accountManager.loadNodeClient(ctx.config.namespace)

return lease.buildAcquireTask(task)
}
},
{
Expand All @@ -314,6 +315,7 @@ export class AccountCommand extends BaseCommand {
} catch (e: Error | any) {
throw new SoloError(`Error in creating account: ${e.message}`, e)
} finally {
await lease.release()
await this.closeConnections()
}

Expand Down
10 changes: 5 additions & 5 deletions src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
import paths from 'path'
import { MissingArgumentError } from '../core/errors.ts'
import { ShellRunner } from '../core/shell_runner.ts'
import { type ChartManager, type ConfigManager, type Helm, type K8 } from '../core/index.ts'
import { type DependencyManager } from '../core/dependency_managers/index.ts'
import { type CommandFlag, type Opts } from '../types/index.ts'
import type { ChartManager, ConfigManager, Helm, K8, DependencyManager, LeaseManager } from '../core/index.ts'
import type { CommandFlag, Opts } from '../types/index.ts'

export class BaseCommand extends ShellRunner {
protected readonly helm: Helm
protected readonly k8: K8
protected readonly chartManager: ChartManager
protected readonly configManager: ConfigManager
protected readonly depManager: DependencyManager
protected readonly _configMaps: Map<string, any>
protected readonly leaseManager: LeaseManager
protected readonly _configMaps = new Map<string, any>()

constructor (opts: Opts) {
if (!opts || !opts.logger) throw new Error('An instance of core/SoloLogger is required')
Expand All @@ -45,7 +45,7 @@ export class BaseCommand extends ShellRunner {
this.chartManager = opts.chartManager
this.configManager = opts.configManager
this.depManager = opts.depManager
this._configMaps = new Map()
this.leaseManager = opts.leaseManager
}

async prepareChartPath (chartDir: string, chartRepo: string, chartReleaseName: string) {
Expand Down
6 changes: 5 additions & 1 deletion src/commands/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export class ClusterCommand extends BaseCommand {
flags.deployPrometheusStack
])

// prepare config
ctx.config = {
chartDir: self.configManager.getFlag<string>(flags.chartDirectory) as string,
clusterSetupNamespace: self.configManager.getFlag<string>(flags.clusterSetupNamespace) as string,
Expand Down Expand Up @@ -160,6 +159,7 @@ export class ClusterCommand extends BaseCommand {

async reset (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()

interface Context {
config: {
Expand Down Expand Up @@ -195,6 +195,8 @@ export class ClusterCommand extends BaseCommand {
if (!ctx.isChartInstalled) {
throw new SoloError('No chart found for the cluster')
}

return lease.buildAcquireTask(task)
}
},
{
Expand All @@ -217,6 +219,8 @@ export class ClusterCommand extends BaseCommand {
await tasks.run()
} catch (e: Error | any) {
throw new SoloError('Error on cluster reset', e)
} finally {
await lease.release()
}

return true
Expand Down
2 changes: 1 addition & 1 deletion src/commands/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { constants } from '../core/index.ts'
import * as core from '../core/index.ts'
import * as version from '../../version.ts'
import path from 'path'
import { type CommandFlag } from '../types/index.ts'
import type { CommandFlag } from '../types/index.ts'

/**
* Set flag from the flag option
Expand Down
18 changes: 9 additions & 9 deletions src/commands/mirror_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
import { Listr } from 'listr2'
import { SoloError, IllegalArgumentError, MissingArgumentError } from '../core/errors.ts'
import { constants, type ProfileManager } from '../core/index.ts'
import { constants, type ProfileManager, type AccountManager } from '../core/index.ts'
import { BaseCommand } from './base.ts'
import * as flags from './flags.ts'
import * as prompts from './prompts.ts'
import { getFileContents, getEnvValue } from '../core/helpers.ts'
import { type AccountManager } from '../core/account_manager.ts'
import { type PodName } from '../types/aliases.ts'
import { type Opts } from '../types/index.ts'

Expand Down Expand Up @@ -61,13 +60,6 @@ export class MirrorNodeCommand extends BaseCommand {
]
}

/**
* @param tlsClusterIssuerType
* @param enableHederaExplorerTls
* @param namespace
* @param hederaExplorerTlsLoadBalancerIp
* @param hederaExplorerTlsHostName
*/
getTlsValueArguments (tlsClusterIssuerType: string, enableHederaExplorerTls: boolean, namespace: string,
hederaExplorerTlsLoadBalancerIp: string, hederaExplorerTlsHostName: string) {
let valuesArg = ''
Expand Down Expand Up @@ -126,6 +118,7 @@ export class MirrorNodeCommand extends BaseCommand {

async deploy (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()

interface MirrorNodeDeployConfigClass {
chartDirectory: string
Expand Down Expand Up @@ -184,6 +177,8 @@ export class MirrorNodeCommand extends BaseCommand {
}

await self.accountManager.loadNodeClient(ctx.config.namespace)

return lease.buildAcquireTask(task)
}
},
{
Expand Down Expand Up @@ -327,6 +322,7 @@ export class MirrorNodeCommand extends BaseCommand {
} catch (e: Error | any) {
throw new SoloError(`Error starting node: ${e.message}`, e)
} finally {
await lease.release()
await self.accountManager.close()
}

Expand All @@ -335,6 +331,7 @@ export class MirrorNodeCommand extends BaseCommand {

async destroy (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()

interface Context {
config: {
Expand Down Expand Up @@ -384,6 +381,8 @@ export class MirrorNodeCommand extends BaseCommand {
}

await self.accountManager.loadNodeClient(ctx.config.namespace)

return lease.buildAcquireTask(task)
}
},
{
Expand Down Expand Up @@ -425,6 +424,7 @@ export class MirrorNodeCommand extends BaseCommand {
} catch (e: Error | any) {
throw new SoloError(`Error starting node: ${e.message}`, e)
} finally {
await lease.release()
await self.accountManager.close()
}

Expand Down
16 changes: 15 additions & 1 deletion src/commands/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@

async prepareValuesArg (config: {chartDirectory?: string; app?: string; nodeAliases?: string[]; debugNodeAlias?: NodeAlias;
enablePrometheusSvcMonitor?: boolean; releaseTag?: string; persistentVolumeClaims?: string;
valuesFile?: string; } = {}) {
valuesFile?: string; } = {}
) {
let valuesArg = config.chartDirectory ? `-f ${path.join(config.chartDirectory, 'solo-deployment', 'values.yaml')}` : ''

if (config.app !== constants.HEDERA_APP_NAME) {
Expand Down Expand Up @@ -216,6 +217,7 @@
/** Run helm install and deploy network components */
async deploy (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()

interface Context {
config: NetworkDeployConfigClass
Expand All @@ -226,6 +228,7 @@
title: 'Initialize',
task: async (ctx, task) => {
ctx.config = await self.prepareConfig(task, argv)
return lease.buildAcquireTask(task)
}
},
{
Expand Down Expand Up @@ -383,13 +386,16 @@
await tasks.run()
} catch (e: Error | any) {
throw new SoloError(`Error installing chart ${constants.SOLO_DEPLOYMENT_CHART}`, e)
} finally {
await lease.release()
}

return true
}

async destroy (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()

interface Context {
config: {
Expand Down Expand Up @@ -427,6 +433,8 @@
deleteSecrets: self.configManager.getFlag<boolean>(flags.deleteSecrets) as boolean,
namespace: self.configManager.getFlag<string>(flags.namespace) as string
}

return lease.buildAcquireTask(task)
}
},
{
Expand Down Expand Up @@ -470,6 +478,8 @@
await tasks.run()
} catch (e: Error | any) {
throw new SoloError('Error destroying network', e)
} finally {
await lease.release()
}

return true
Expand All @@ -478,6 +488,7 @@
/** Run helm upgrade to refresh network components with new settings */
async refresh (argv: any) {
const self = this
const lease = self.leaseManager.instantiateLease()

Check warning on line 491 in src/commands/network.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/network.ts#L491

Added line #L491 was not covered by tests

interface Context {
config: NetworkDeployConfigClass
Expand All @@ -488,6 +499,7 @@
title: 'Initialize',
task: async (ctx, task) => {
ctx.config = await self.prepareConfig(task, argv)
return lease.buildAcquireTask(task)

Check warning on line 502 in src/commands/network.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/network.ts#L502

Added line #L502 was not covered by tests
}
},
{
Expand Down Expand Up @@ -520,6 +532,8 @@
await tasks.run()
} catch (e: Error | any) {
throw new SoloError(`Error upgrading chart ${constants.SOLO_DEPLOYMENT_CHART}`, e)
} finally {
await lease.release()

Check warning on line 536 in src/commands/network.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/network.ts#L535-L536

Added lines #L535 - L536 were not covered by tests
}

return true
Expand Down
4 changes: 2 additions & 2 deletions src/commands/node/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import path from 'path'
import fs from 'fs'
import { validatePath } from '../../core/helpers.ts'
import * as flags from '../flags.ts'
import { type NodeAlias, type NodeAliases, type PodName } from '../../types/aliases.js'
import { type NetworkNodeServices } from '../../core/network_node_services.js'
import { type NodeAlias, type NodeAliases, type PodName } from '../../types/aliases.ts'
import { type NetworkNodeServices } from '../../core/network_node_services.ts'

export const PREPARE_UPGRADE_CONFIGS_NAME = 'prepareUpgradeConfig'
export const DOWNLOAD_GENERATED_FILES_CONFIGS_NAME = 'downloadGeneratedFilesConfig'
Expand Down
Loading
Loading