@@ -24,6 +24,7 @@ import (
2424 util3 "github.com/devtron-labs/common-lib/utils/k8s"
2525 "io"
2626 "io/ioutil"
27+ errors2 "k8s.io/apimachinery/pkg/api/errors"
2728 "net/http"
2829 "net/url"
2930 "strconv"
@@ -70,7 +71,6 @@ type ChartRepositoryService interface {
7071 GetChartRepoByName (name string ) (* ChartRepoDto , error )
7172 GetChartRepoList () ([]* ChartRepoWithIsEditableDto , error )
7273 GetChartRepoListMin () ([]* ChartRepoDto , error )
73- ValidateDeploymentCount (request * ChartRepoDto ) error
7474 ValidateChartRepo (request * ChartRepoDto ) * DetailedErrorHelmRepoValidation
7575 ValidateAndCreateChartRepo (request * ChartRepoDto ) (* chartRepoRepository.ChartRepo , error , * DetailedErrorHelmRepoValidation )
7676 ValidateAndUpdateChartRepo (request * ChartRepoDto ) (* chartRepoRepository.ChartRepo , error , * DetailedErrorHelmRepoValidation )
@@ -111,12 +111,12 @@ func (impl *ChartRepositoryServiceImpl) CreateSecretDataForHelmChart(request *Ch
111111 if isPrivateChart {
112112 secretData [USERNAME ] = request .UserName
113113 secretData [PASSWORD ] = request .Password
114- isInsecureConnection := "true"
115- if ! request .AllowInsecureConnection {
116- isInsecureConnection = "false"
117- }
118- secretData [INSECRUE ] = isInsecureConnection
119114 }
115+ isInsecureConnection := "true"
116+ if ! request .AllowInsecureConnection {
117+ isInsecureConnection = "false"
118+ }
119+ secretData [INSECRUE ] = isInsecureConnection
120120
121121 return secretData
122122}
@@ -225,17 +225,13 @@ func (impl *ChartRepositoryServiceImpl) CreateChartRepo(request *ChartRepoDto) (
225225 return chartRepo , nil
226226}
227227
228- func (impl * ChartRepositoryServiceImpl ) ValidateDeploymentCount ( request * ChartRepoDto ) error {
229- activeDeploymentCount , err := impl .repoRepository .FindDeploymentCountByChartRepoId (request . Id )
228+ func (impl * ChartRepositoryServiceImpl ) getCountOfDeployedCharts ( chartRepoId int ) ( int , error ) {
229+ activeDeploymentCount , err := impl .repoRepository .FindDeploymentCountByChartRepoId (chartRepoId )
230230 if err != nil {
231- impl .logger .Errorw ("error in getting deployment count, CheckDeploymentCount" , "err " , err , "payload " , request )
232- return err
231+ impl .logger .Errorw ("error in getting deployment count, CheckDeploymentCount" , "chartRepoId " , chartRepoId , "err " , err )
232+ return 0 , err
233233 }
234- if activeDeploymentCount > 0 {
235- err = & util.ApiError {Code : "400" , HttpStatusCode : 400 , UserMessage : "cannot update, found charts deployed using this repo" }
236- return err
237- }
238- return err
234+ return activeDeploymentCount , nil
239235}
240236
241237func (impl * ChartRepositoryServiceImpl ) UpdateData (request * ChartRepoDto ) (* chartRepoRepository.ChartRepo , error ) {
@@ -256,6 +252,18 @@ func (impl *ChartRepositoryServiceImpl) UpdateData(request *ChartRepoDto) (*char
256252 if request .Name != previousName && strings .ToLower (request .Name ) != request .Name {
257253 return nil , errors .New ("invalid repo name: please use lowercase" )
258254 }
255+
256+ deployedChartCount , err := impl .getCountOfDeployedCharts (request .Id )
257+ if err != nil {
258+ impl .logger .Errorw ("error in getting charts deployed via chart repo" , "chartRepoId" , request .Id , "err" , err )
259+ return nil , err
260+ }
261+
262+ if deployedChartCount > 0 && (request .Name != previousName || request .Url != previousUrl ) {
263+ err = & util.ApiError {Code : "400" , HttpStatusCode : 400 , UserMessage : "cannot update, found charts deployed using this repo" }
264+ return nil , err
265+ }
266+
259267 chartRepo .Url = request .Url
260268 chartRepo .Name = request .Name
261269 chartRepo .AuthMode = request .AuthMode
@@ -347,11 +355,24 @@ func (impl *ChartRepositoryServiceImpl) UpdateData(request *ChartRepoDto) (*char
347355 } else {
348356 secretData := impl .CreateSecretDataForHelmChart (request , isPrivateChart )
349357 secret , err := impl .K8sUtil .GetSecret (impl .aCDAuthConfig .ACDConfigMapNamespace , previousName , client )
350- if err != nil {
358+ statusError , ok := err .(* errors2.StatusError )
359+ if err != nil && (ok && statusError != nil && statusError .Status ().Code != http .StatusNotFound ) {
351360 impl .logger .Errorw ("error in fetching secret" , "err" , err )
352361 continue
353362 }
354- secret .StringData = secretData
363+
364+ if ok && statusError != nil && statusError .Status ().Code == http .StatusNotFound {
365+ secretLabel := make (map [string ]string )
366+ secretLabel [LABEL ] = REPOSITORY
367+ _ , err = impl .K8sUtil .CreateSecret (impl .aCDAuthConfig .ACDConfigMapNamespace , nil , chartRepo .Name , "" , client , secretLabel , secretData )
368+ if err != nil {
369+ impl .logger .Errorw ("Error in creating secret for chart repo" , "Chart Name" , chartRepo .Name , "err" , err )
370+ continue
371+ }
372+ updateSuccess = true
373+ break
374+ }
375+
355376 if previousName != request .Name {
356377 err = impl .DeleteChartSecret (previousName )
357378 if err != nil {
@@ -365,6 +386,7 @@ func (impl *ChartRepositoryServiceImpl) UpdateData(request *ChartRepoDto) (*char
365386 impl .logger .Errorw ("Error in creating secret for chart repo" , "Chart Name" , chartRepo .Name , "err" , err )
366387 }
367388 } else {
389+ secret .StringData = secretData
368390 _ , err = impl .K8sUtil .UpdateSecret (impl .aCDAuthConfig .ACDConfigMapNamespace , secret , client )
369391 if err != nil {
370392 impl .logger .Errorw ("Error in creating secret for chart repo" , "Chart Name" , chartRepo .Name , "err" , err )
0 commit comments