import { createNamespaceIfNotExists } from '../cluster' ; import { execCmd , execCmdWithExitOnFailure } from '../cmd-utils' ; import { installAndEnableMetricsDeps , installCertManagerAndNginx , isCelotoolHelmDryRun , } from '../helm_deploy' ; export enum CloudProvider { AWS , AZURE , GCP } export interface BaseClusterConfig { cloudProvider : CloudProvider ; clusterName : string ; } export abstract class BaseClusterManager { protected _clusterConfig : BaseClusterConfig ; private _celoEnv : string ; constructor ( clusterConfig , celoEnv ) { this . _clusterConfig = clusterConfig ; this . _celoEnv = celoEnv ; } async switchToClusterContext ( skipSetup , context ? ) { const exists = await this . switchToClusterContextIfExists ( ) ; if ( ! exists ) { await this . getAndSwitchToClusterContext ( ) ; } await execCmdWithExitOnFailure ( `kubectl config set-context --current --namespace default` ) ; if ( ! skipSetup ) { if ( ! isCelotoolHelmDryRun ( ) ) { await this . setupCluster ( context ) ; } else { console . info ( `Skipping cluster setup due to --helmdryrun` ) ; } } } param clusterConfig async switchToClusterContextIfExists ( ) { await this . switchToSubscription ( ) ; let currentContext = null ; try { ; [ currentContext ] = await execCmd ( 'kubectl config current-context' ) ; } catch ( error ) { console . info ( 'No context currently set' ) ; } if ( currentContext === null || currentContext . trim ( ) !== this . kubernetesContextName ) { const [ existingContextsStr ] = await execCmdWithExitOnFailure ( 'kubectl config get-contexts -o name' ) ; const existingContexts = existingContextsStr . trim ( ) . split ( '\n' ) ; if ( existingContexts . includes ( this . clusterConfig . clusterName ) ) { await execCmdWithExitOnFailure ( `kubectl config use-context ${ this . clusterConfig . clusterName }` ) ; } else { return false ; } } return true ; } async setupCluster ( context ? ) { await createNamespaceIfNotExists ( this . celoEnv ) ; if ( ! isCelotoolHelmDryRun ( ) ) { console . info ( 'Performing any cluster setup that needs to be done...' ) ; await installCertManagerAndNginx ( this . celoEnv , this . clusterConfig ) ; await installAndEnableMetricsDeps ( true , context , this . clusterConfig ) ; } } abstract switchToSubscription ( ) ; abstract getAndSwitchToClusterContext ( ) ; abstract get kubernetesContextName ( ) : string ; abstract get cloudProvider ( ) : CloudProvider ; get clusterConfig ( ) : BaseClusterConfig { return this . _clusterConfig ; } get celoEnv ( ) : string { return this . _celoEnv ; } }