@@ -18,6 +18,7 @@ package chartRef
18
18
19
19
import (
20
20
"bytes"
21
+ "context"
21
22
"encoding/json"
22
23
"errors"
23
24
"fmt"
@@ -29,8 +30,10 @@ import (
29
30
util2 "github.com/devtron-labs/devtron/util"
30
31
dirCopy "github.com/otiai10/copy"
31
32
"go.uber.org/zap"
33
+ "golang.org/x/exp/maps"
32
34
"io/ioutil"
33
35
"k8s.io/helm/pkg/chartutil"
36
+ chart2 "k8s.io/helm/pkg/proto/hapi/chart"
34
37
"os"
35
38
"path"
36
39
"path/filepath"
@@ -70,19 +73,21 @@ type ChartRefFileOpService interface {
70
73
GetRefChart (chartRefId int ) (string , string , string , string , error )
71
74
ExtractChartIfMissing (chartData []byte , refChartDir string , location string ) (* bean.ChartDataInfo , error )
72
75
GetChartInBytes (chartRefId int , deleteChart bool ) ([]byte , error )
73
- GetChartBytesInBulk ( chartRefIds [] int , deleteChart bool ) (map [int ][]byte , error )
76
+ GetChartBytesForApps ( ctx context. Context , appIdToAppName map [ int ] string ) (map [int ][]byte , error )
74
77
}
75
78
76
79
type ChartRefServiceImpl struct {
77
80
logger * zap.SugaredLogger
78
81
chartRefRepository chartRepoRepository.ChartRefRepository
79
82
chartTemplateService util.ChartTemplateService
80
83
mergeUtil util.MergeUtil
84
+ chartRepository chartRepoRepository.ChartRepository
81
85
}
82
86
83
87
func NewChartRefServiceImpl (logger * zap.SugaredLogger ,
84
88
chartRefRepository chartRepoRepository.ChartRefRepository ,
85
89
chartTemplateService util.ChartTemplateService ,
90
+ chartRepository chartRepoRepository.ChartRepository ,
86
91
mergeUtil util.MergeUtil ) * ChartRefServiceImpl {
87
92
// cache devtron reference charts list
88
93
devtronChartList , _ := chartRefRepository .FetchAllNonUserUploadedChartInfo ()
@@ -92,6 +97,7 @@ func NewChartRefServiceImpl(logger *zap.SugaredLogger,
92
97
chartRefRepository : chartRefRepository ,
93
98
chartTemplateService : chartTemplateService ,
94
99
mergeUtil : mergeUtil ,
100
+ chartRepository : chartRepository ,
95
101
}
96
102
}
97
103
@@ -309,23 +315,83 @@ func (impl *ChartRefServiceImpl) extractChartInBytes(chartRef *chartRepoReposito
309
315
}
310
316
return manifestByteArr , nil
311
317
}
312
- func (impl * ChartRefServiceImpl ) GetChartBytesInBulk (chartRefIds []int , performCleanup bool ) (map [int ][]byte , error ) {
318
+
319
+ func (impl * ChartRefServiceImpl ) getChartPath (chartRef * chartRepoRepository.ChartRef ) (string , error ) {
320
+ refChartPath := filepath .Join (bean .RefChartDirPath , chartRef .Location )
321
+ // For user uploaded charts ChartData will be retrieved from DB
322
+ if chartRef .ChartData != nil {
323
+ chartInfo , err := impl .ExtractChartIfMissing (chartRef .ChartData , bean .RefChartDirPath , chartRef .Location )
324
+ if chartInfo != nil && chartInfo .TemporaryFolder != "" {
325
+ err1 := os .RemoveAll (chartInfo .TemporaryFolder )
326
+ if err1 != nil {
327
+ impl .logger .Errorw ("error in deleting temp dir " , "err" , err )
328
+ }
329
+ }
330
+ } else {
331
+ // For Devtron reference charts the chart will be load from the directory location
332
+ }
333
+ return refChartPath , nil
334
+ }
335
+
336
+ func (impl * ChartRefServiceImpl ) GetChartBytesForApps (ctx context.Context , appIdToAppName map [int ]string ) (map [int ][]byte , error ) {
337
+
338
+ appIds := maps .Keys (appIdToAppName )
339
+ charts , err := impl .chartRepository .FindLatestChartByAppIds (appIds )
340
+ if err != nil {
341
+ impl .logger .Errorw ("error in fetching chart" , "err" , err , "appIds" , appIds )
342
+ return nil , err
343
+ }
344
+
345
+ chartRefIdTOAppIds := make (map [int ][]int )
346
+ var chartRefIds []int
347
+ chartRefToChartVersion := make (map [int ]string )
348
+
349
+ for _ , chart := range charts {
350
+ chartRefIds = append (chartRefIds , chart .ChartRefId )
351
+ chartRefToChartVersion [chart .ChartRefId ] = chart .ChartVersion
352
+ refAppIds , ok := chartRefIdTOAppIds [chart .ChartRefId ]
353
+ if ! ok {
354
+ refAppIds = make ([]int , 0 )
355
+ }
356
+ refAppIds = append (refAppIds , chart .AppId )
357
+ chartRefIdTOAppIds [chart .ChartRefId ] = refAppIds
358
+ }
359
+
313
360
chartRefs , err := impl .chartRefRepository .FindByIds (chartRefIds )
314
361
if err != nil {
315
362
impl .logger .Errorw ("error getting chart data" , "chartRefIds" , chartRefIds , "err" , err )
316
363
return nil , err
317
364
}
318
- chartRefIdToBytes := make (map [int ][]byte )
365
+
366
+ appIdToBytes := make (map [int ][]byte )
367
+
368
+ // this loops run with O(len(apps)) T.C
319
369
for _ , chartRef := range chartRefs {
320
- chartInBytes , err := impl .extractChartInBytes (chartRef , performCleanup )
370
+ refChartPath , err := impl .getChartPath (chartRef )
321
371
if err != nil {
322
372
impl .logger .Errorw ("error in converting chart to bytes" , "chartRefId" , chartRef .Id , "err" , err )
323
373
return nil , err
324
374
}
325
- chartRefIdToBytes [chartRef .Id ] = chartInBytes
375
+
376
+ refAppIds := chartRefIdTOAppIds [chartRef .Id ]
377
+ for _ , appId := range refAppIds {
378
+ chartMetaData := & chart2.Metadata {
379
+ Name : appIdToAppName [appId ],
380
+ Version : chartRefToChartVersion [chartRef .Id ],
381
+ }
382
+ tempReferenceTemplateDir , err := impl .chartTemplateService .BuildChart (ctx , chartMetaData , refChartPath )
383
+ if err != nil {
384
+ impl .logger .Errorw ("error in building chart" , "chartMetaData" , chartMetaData , "refChartPath" , refChartPath )
385
+ return nil , err
386
+ }
387
+ chartInBytes , err := impl .chartTemplateService .LoadChartInBytes (tempReferenceTemplateDir , true )
388
+ appIdToBytes [appId ] = chartInBytes
389
+ }
390
+
326
391
}
327
- return chartRefIdToBytes , nil
392
+ return appIdToBytes , nil
328
393
}
394
+
329
395
func (impl * ChartRefServiceImpl ) FetchCustomChartsInfo () ([]* bean.ChartDto , error ) {
330
396
resultsMetadata , err := impl .chartRefRepository .GetAllChartMetadata ()
331
397
if err != nil {
0 commit comments