@@ -42,6 +42,7 @@ import (
4242 clientErrors "github.com/devtron-labs/devtron/pkg/errors"
4343 "github.com/devtron-labs/devtron/pkg/eventProcessor/out"
4444 "github.com/devtron-labs/devtron/pkg/imageDigestPolicy"
45+ k8s2 "github.com/devtron-labs/devtron/pkg/k8s"
4546 "github.com/devtron-labs/devtron/pkg/pipeline"
4647 bean8 "github.com/devtron-labs/devtron/pkg/pipeline/bean"
4748 "github.com/devtron-labs/devtron/pkg/pipeline/history"
@@ -64,6 +65,7 @@ import (
6465 "k8s.io/helm/pkg/proto/hapi/chart"
6566 "net/http"
6667 "path"
68+ "regexp"
6769 "strconv"
6870 "strings"
6971 "time"
@@ -137,6 +139,7 @@ type TriggerServiceImpl struct {
137139 ciPipelineRepository pipelineConfig.CiPipelineRepository
138140 appWorkflowRepository appWorkflow.AppWorkflowRepository
139141 dockerArtifactStoreRepository repository4.DockerArtifactStoreRepository
142+ K8sUtil * util5.K8sServiceImpl
140143}
141144
142145func NewTriggerServiceImpl (logger * zap.SugaredLogger , cdWorkflowCommonService cd.CdWorkflowCommonService ,
@@ -186,7 +189,8 @@ func NewTriggerServiceImpl(logger *zap.SugaredLogger, cdWorkflowCommonService cd
186189 ciPipelineRepository pipelineConfig.CiPipelineRepository ,
187190 appWorkflowRepository appWorkflow.AppWorkflowRepository ,
188191 dockerArtifactStoreRepository repository4.DockerArtifactStoreRepository ,
189- imageScanService security2.ImageScanService ) (* TriggerServiceImpl , error ) {
192+ imageScanService security2.ImageScanService ,
193+ K8sUtil * util5.K8sServiceImpl ) (* TriggerServiceImpl , error ) {
190194 impl := & TriggerServiceImpl {
191195 logger : logger ,
192196 cdWorkflowCommonService : cdWorkflowCommonService ,
@@ -237,6 +241,7 @@ func NewTriggerServiceImpl(logger *zap.SugaredLogger, cdWorkflowCommonService cd
237241 appWorkflowRepository : appWorkflowRepository ,
238242 dockerArtifactStoreRepository : dockerArtifactStoreRepository ,
239243 imageScanService : imageScanService ,
244+ K8sUtil : K8sUtil ,
240245 }
241246 config , err := types .GetCdConfig ()
242247 if err != nil {
@@ -877,9 +882,23 @@ func (impl *TriggerServiceImpl) createHelmAppForCdPipeline(overrideRequest *bean
877882 Name : pipeline .App .AppName ,
878883 Version : envOverride .Chart .ChartVersion ,
879884 }
880- referenceTemplatePath := path .Join (bean5 .RefChartDirPath , envOverride .Chart .ReferenceTemplate )
885+ referenceTemplate := envOverride .Chart .ReferenceTemplate
886+ referenceTemplatePath := path .Join (bean5 .RefChartDirPath , referenceTemplate )
881887
882888 if util .IsHelmApp (pipeline .DeploymentAppType ) {
889+ var sanitizedK8sVersion string
890+ //handle specific case for all cronjob charts from cronjob-chart_1-2-0 to cronjob-chart_1-5-0 where semverCompare
891+ //comparison func has wrong api version mentioned, so for already installed charts via these charts that comparison
892+ //is always false, handles the gh issue:- https://github.com/devtron-labs/devtron/issues/4860
893+ cronJobChartRegex := regexp .MustCompile (bean .CronJobChartRegexExpression )
894+ if cronJobChartRegex .MatchString (referenceTemplate ) {
895+ k8sServerVersion , err := impl .K8sUtil .GetKubeVersion ()
896+ if err != nil {
897+ impl .logger .Errorw ("exception caught in getting k8sServerVersion" , "err" , err )
898+ return false , err
899+ }
900+ sanitizedK8sVersion = k8s2 .StripPrereleaseFromK8sVersion (k8sServerVersion .String ())
901+ }
883902 referenceChartByte := envOverride .Chart .ReferenceChart
884903 // here updating reference chart into database.
885904 if len (envOverride .Chart .ReferenceChart ) == 0 {
@@ -927,6 +946,9 @@ func (impl *TriggerServiceImpl) createHelmAppForCdPipeline(overrideRequest *bean
927946 HistoryMax : impl .helmAppService .GetRevisionHistoryMaxValue (bean6 .SOURCE_DEVTRON_APP ),
928947 ChartContent : & gRPC.ChartContent {Content : referenceChartByte },
929948 }
949+ if len (sanitizedK8sVersion ) > 0 {
950+ req .K8SVersion = sanitizedK8sVersion
951+ }
930952 if impl .isDevtronAsyncInstallModeEnabled (bean .Helm ) {
931953 req .RunInCtx = true
932954 }
@@ -948,7 +970,7 @@ func (impl *TriggerServiceImpl) createHelmAppForCdPipeline(overrideRequest *bean
948970
949971 } else {
950972
951- helmResponse , err := impl .helmInstallReleaseWithCustomChart (ctx , releaseIdentifier , referenceChartByte , mergeAndSave )
973+ helmResponse , err := impl .helmInstallReleaseWithCustomChart (ctx , releaseIdentifier , referenceChartByte , mergeAndSave , sanitizedK8sVersion )
952974
953975 // For connection related errors, no need to update the db
954976 if err != nil && strings .Contains (err .Error (), "connection error" ) {
@@ -1154,13 +1176,16 @@ func (impl *TriggerServiceImpl) updatePipeline(pipeline *pipelineConfig.Pipeline
11541176}
11551177
11561178// helmInstallReleaseWithCustomChart performs helm install with custom chart
1157- func (impl * TriggerServiceImpl ) helmInstallReleaseWithCustomChart (ctx context.Context , releaseIdentifier * gRPC.ReleaseIdentifier , referenceChartByte []byte , valuesYaml string ) (* gRPC.HelmInstallCustomResponse , error ) {
1179+ func (impl * TriggerServiceImpl ) helmInstallReleaseWithCustomChart (ctx context.Context , releaseIdentifier * gRPC.ReleaseIdentifier , referenceChartByte []byte , valuesYaml string , k8sServerVersion string ) (* gRPC.HelmInstallCustomResponse , error ) {
11581180
11591181 helmInstallRequest := gRPC.HelmInstallCustomRequest {
11601182 ValuesYaml : valuesYaml ,
11611183 ChartContent : & gRPC.ChartContent {Content : referenceChartByte },
11621184 ReleaseIdentifier : releaseIdentifier ,
11631185 }
1186+ if len (k8sServerVersion ) > 0 {
1187+ helmInstallRequest .K8SVersion = k8sServerVersion
1188+ }
11641189 if impl .isDevtronAsyncInstallModeEnabled (bean .Helm ) {
11651190 helmInstallRequest .RunInCtx = true
11661191 }
0 commit comments