@@ -8,9 +8,28 @@ const ProgressEvents = require('../../helpers/progressEvents');
88const cliProgress = require ( 'cli-progress' ) ;
99const createClusterCmd = require ( '../cluster/create.cmd' ) ;
1010const colors = require ( 'colors' ) ;
11+ const _ = require ( 'lodash' ) ;
1112
1213const defaultNamespace = 'codefresh' ;
1314
15+ async function newRuntimeName ( kubeContextName , kubeNamespace ) {
16+ const defaultName = `${ kubeContextName } /${ kubeNamespace } ` ;
17+ const runtimes = await sdk . runtimeEnvs . list ( { } ) ;
18+ let name ;
19+
20+ if ( ! _ . isArray ( runtimes ) || ! _ . find ( runtimes , re => _ . get ( re , 'metadata.name' ) === defaultName ) ) {
21+ name = defaultName ; // use the default name if there are no collisions
22+ } else {
23+ const reNames = new Set ( _ . map ( runtimes , re => _ . get ( re , 'metadata.name' ) ) ) ; // for fast lookup
24+ let i = 1 ;
25+ while ( reNames . has ( `${ defaultName } _${ i } ` ) ) {
26+ i += 1 ;
27+ }
28+ name = `${ defaultName } _${ i } ` ;
29+ }
30+
31+ return name ;
32+ }
1433const installRuntimeCmd = new Command ( {
1534 root : false ,
1635 parent : installRoot ,
@@ -143,7 +162,7 @@ const installRuntimeCmd = new Command({
143162 kubeContextName = getKubeContext ( kubeConfigPath ) ;
144163 }
145164 const clusterName = kubeContextName || getKubeContext ( kubeConfigPath ) ;
146- const runtimeName = ` ${ clusterName } / ${ kubeNamespace } ` ;
165+ const runtimeName = await newRuntimeName ( kubeContextName , kubeNamespace ) ;
147166
148167 if ( ! token ) {
149168 // eslint-disable-next-line prefer-destructuring
@@ -158,6 +177,7 @@ const installRuntimeCmd = new Command({
158177 nodeSelector : kubeNodeSelectorObj ,
159178 annotations : buildAnnotations ,
160179 clusterName,
180+ runtimeName,
161181 agent : true ,
162182 } ) ;
163183 console . log ( `Runtime environment "${ colors . cyan ( runtimeName ) } " has been created` ) ;
0 commit comments