@@ -8,9 +8,28 @@ const ProgressEvents = require('../../helpers/progressEvents');
8
8
const cliProgress = require ( 'cli-progress' ) ;
9
9
const createClusterCmd = require ( '../cluster/create.cmd' ) ;
10
10
const colors = require ( 'colors' ) ;
11
+ const _ = require ( 'lodash' ) ;
11
12
12
13
const defaultNamespace = 'codefresh' ;
13
14
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
+ }
14
33
const installRuntimeCmd = new Command ( {
15
34
root : false ,
16
35
parent : installRoot ,
@@ -143,7 +162,7 @@ const installRuntimeCmd = new Command({
143
162
kubeContextName = getKubeContext ( kubeConfigPath ) ;
144
163
}
145
164
const clusterName = kubeContextName || getKubeContext ( kubeConfigPath ) ;
146
- const runtimeName = ` ${ clusterName } / ${ kubeNamespace } ` ;
165
+ const runtimeName = await newRuntimeName ( kubeContextName , kubeNamespace ) ;
147
166
148
167
if ( ! token ) {
149
168
// eslint-disable-next-line prefer-destructuring
@@ -158,6 +177,7 @@ const installRuntimeCmd = new Command({
158
177
nodeSelector : kubeNodeSelectorObj ,
159
178
annotations : buildAnnotations ,
160
179
clusterName,
180
+ runtimeName,
161
181
agent : true ,
162
182
} ) ;
163
183
console . log ( `Runtime environment "${ colors . cyan ( runtimeName ) } " has been created` ) ;
0 commit comments