@@ -400,28 +400,36 @@ export async function saveValues(
400
400
401
401
export function renderManifest ( fileMap : FileMap , jsonPath : jsonpath . PathComponent [ ] , data : Record < string , any > ) {
402
402
//TODO remove this custom workaround for workloadValues
403
- const manifest =
404
- fileMap . kind === 'AplTeamWorkloadValues'
405
- ? omit ( data , [ 'id' , 'name' , 'teamId' ] )
406
- : {
407
- kind : fileMap . kind ,
408
- metadata : {
409
- name : getResourceName ( fileMap , jsonPath , data ) ,
410
- labels : { } ,
411
- } ,
412
- spec : data ,
413
- }
414
- if ( fileMap . resourceGroup === 'team' && fileMap . kind !== 'AplTeamWorkloadValues' ) {
403
+ let spec = data
404
+ if ( fileMap . resourceGroup === 'team' ) {
405
+ spec = omit ( data , [ 'id' , 'name' , 'teamId' ] )
406
+ }
407
+ const manifest = {
408
+ kind : fileMap . kind ,
409
+ metadata : {
410
+ name : getResourceName ( fileMap , jsonPath , data ) ,
411
+ labels : { } ,
412
+ } ,
413
+ spec,
414
+ }
415
+ if ( fileMap . resourceGroup === 'team' ) {
415
416
manifest . metadata . labels [ 'apl.io/teamId' ] = getTeamNameFromJsonPath ( jsonPath )
416
417
}
417
418
418
419
return manifest
419
420
}
420
421
421
- export function renderManifestForSecrets ( fileMap : FileMap , data : Record < string , any > ) {
422
+ export function renderManifestForSecrets ( fileMap : FileMap , resourceName : string , data : Record < string , any > ) {
423
+ let spec = data
424
+ if ( fileMap . resourceGroup === 'users' ) {
425
+ spec = omit ( data , [ 'id' , 'name' ] )
426
+ }
422
427
return {
423
428
kind : fileMap . kind ,
424
- spec : data ,
429
+ metadata : {
430
+ name : resourceName ,
431
+ } ,
432
+ spec,
425
433
}
426
434
}
427
435
@@ -461,7 +469,8 @@ export async function saveResourceGroupToFiles(
461
469
const nodeValue = node . value
462
470
try {
463
471
const filePath = getFilePath ( fileMap , nodePath , nodeValue , 'secrets.' )
464
- const manifest = renderManifestForSecrets ( fileMap , nodeValue )
472
+ const resourceName = getResourceName ( fileMap , nodePath , nodeValue )
473
+ const manifest = renderManifestForSecrets ( fileMap , resourceName , nodeValue )
465
474
await deps . writeValuesToFile ( filePath , manifest )
466
475
} catch ( e ) {
467
476
console . log ( nodePath )
@@ -472,6 +481,14 @@ export async function saveResourceGroupToFiles(
472
481
)
473
482
}
474
483
484
+ export function getUniqueIdentifierFromFilePath ( filePath : string ) : string {
485
+ return path
486
+ . basename ( filePath )
487
+ . replace ( / ^ s e c r e t s \. / , '' )
488
+ . replace ( / \. y a m l \. d e c $ / , '' )
489
+ . replace ( / \. y a m l $ / , '' )
490
+ }
491
+
475
492
export async function setValuesFile ( envDir : string , deps = { pathExists, loadValues, writeFile } ) : Promise < string > {
476
493
const valuesPath = path . join ( envDir , 'values-repo.yaml' )
477
494
// if (await deps.pathExists(valuesPath)) return valuesPath
@@ -554,7 +571,6 @@ export async function loadToSpec(
554
571
if ( hasCorrespondingDecryptedFile ( filePath , files ) ) return
555
572
promises . push ( deps . loadFileToSpec ( filePath , fileMap , spec ) )
556
573
} )
557
-
558
574
await Promise . all ( promises )
559
575
}
560
576
@@ -565,18 +581,32 @@ export async function loadFileToSpec(
565
581
deps = { loadYaml } ,
566
582
) : Promise < void > {
567
583
const jsonPath = getJsonPath ( fileMap , filePath )
568
- const data = await deps . loadYaml ( filePath )
569
- if ( fileMap . processAs === 'arrayItem' ) {
570
- const ref : Record < string , any > [ ] = get ( spec , jsonPath )
571
- ref . push ( data ?. spec )
572
- } else {
573
- const ref : Record < string , any > = get ( spec , jsonPath )
574
- // Decrypted secrets may need to be merged with plain text specs
575
- const newRef = merge ( cloneDeep ( ref ) , data ?. spec )
576
- set ( spec , jsonPath , newRef )
584
+ try {
585
+ const data = ( await deps . loadYaml ( filePath ) ) || { }
586
+
587
+ if ( ! filePath . includes ( 'secrets.' ) ) {
588
+ if ( fileMap . resourceGroup === 'team' && fileMap . processAs === 'arrayItem' ) {
589
+ data . spec . name = data . metadata . name
590
+ }
591
+ }
592
+ if ( fileMap . resourceGroup === 'users' ) {
593
+ data . spec . name = getUniqueIdentifierFromFilePath ( filePath )
594
+ }
595
+ if ( fileMap . processAs === 'arrayItem' ) {
596
+ const ref : Record < string , any > [ ] = get ( spec , jsonPath )
597
+ ref . push ( data ?. spec )
598
+ } else {
599
+ const ref : Record < string , any > = get ( spec , jsonPath )
600
+ // Decrypted secrets may need to be merged with plain text specs
601
+ const newRef = merge ( cloneDeep ( ref ) , data ?. spec )
602
+ set ( spec , jsonPath , newRef )
603
+ }
604
+ } catch ( e ) {
605
+ console . log ( filePath )
606
+ console . log ( fileMap )
607
+ throw e
577
608
}
578
609
}
579
-
580
610
export async function getKmsSettings ( envDir : string , deps = { loadToSpec } ) : Promise < Record < string , any > > {
581
611
const kmsFiles = getFileMap ( 'AplKms' , envDir )
582
612
const spec = { }
0 commit comments