Skip to content

Commit b331bc9

Browse files
authored
fix: latest version in default cluster and on UI (#5259)
* fix for latest version in default cluster and on UI * wire gen * defining variable for isOCIRepo * adding app store repository in provider * wip: adding latest check in chart search query * updating indentation and comment
1 parent 01ad655 commit b331bc9

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

pkg/appStore/chartGroup/ChartGroupService.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type ChartGroupServiceImpl struct {
6363
chartGroupDeploymentRepository repository2.ChartGroupDeploymentRepository
6464
installedAppRepository repository.InstalledAppRepository
6565
appStoreVersionValuesRepository appStoreValuesRepository.AppStoreVersionValuesRepository
66+
appStoreRepository appStoreDiscoverRepository.AppStoreRepository
6667
userAuthService user.UserAuthService
6768
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository
6869
environmentService cluster2.EnvironmentService
@@ -86,6 +87,7 @@ func NewChartGroupServiceImpl(logger *zap.SugaredLogger,
8687
chartGroupDeploymentRepository repository2.ChartGroupDeploymentRepository,
8788
installedAppRepository repository.InstalledAppRepository,
8889
appStoreVersionValuesRepository appStoreValuesRepository.AppStoreVersionValuesRepository,
90+
appStoreRepository appStoreDiscoverRepository.AppStoreRepository,
8991
userAuthService user.UserAuthService,
9092
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository,
9193
environmentService cluster2.EnvironmentService,
@@ -123,6 +125,7 @@ func NewChartGroupServiceImpl(logger *zap.SugaredLogger,
123125
gitOperationService: gitOperationService,
124126
installAppService: installAppService,
125127
appStoreAppsEventPublishService: appStoreAppsEventPublishService,
128+
appStoreRepository: appStoreRepository,
126129
}
127130
return impl, nil
128131
}
@@ -773,11 +776,26 @@ func (impl *ChartGroupServiceImpl) DeployDefaultChartOnCluster(bean *cluster2.Cl
773776
chartGroupInstallRequest.UserId = userId
774777
var chartGroupInstallChartRequests []*ChartGroupInstallChartRequest
775778
for _, item := range charts.ChartComponent {
776-
appStoreApplicationVersionId, err := impl.appStoreApplicationVersionRepository.FindLatestAppStoreVersionIdByAppStoreName(item.Name)
779+
appStore, err := impl.appStoreRepository.FindAppStoreByName(item.Name)
777780
if err != nil {
778-
impl.logger.Errorw("DeployDefaultChartOnCluster, error in getting app store", "data", t, "err", err)
781+
impl.logger.Errorw("error in getting app store by name", "appStoreName", item.Name, "err", err)
779782
return false, err
780783
}
784+
isOCIRepo := len(appStore.DockerArtifactStoreId) > 0
785+
var appStoreApplicationVersionId int
786+
if isOCIRepo {
787+
appStoreApplicationVersionId, err = impl.appStoreApplicationVersionRepository.FindLatestVersionByAppStoreIdForOCIRepo(appStore.Id)
788+
if err != nil {
789+
impl.logger.Errorw("DeployDefaultChartOnCluster, error in getting app store", "data", t, "err", err)
790+
return false, err
791+
}
792+
} else {
793+
appStoreApplicationVersionId, err = impl.appStoreApplicationVersionRepository.FindLatestVersionByAppStoreIdForChartRepo(appStore.Id)
794+
if err != nil {
795+
impl.logger.Errorw("DeployDefaultChartOnCluster, error in getting app store", "data", t, "err", err)
796+
return false, err
797+
}
798+
}
781799
chartGroupInstallChartRequest := &ChartGroupInstallChartRequest{
782800
AppName: fmt.Sprintf("%d-%d-%s", bean.Id, env.Id, item.Name),
783801
EnvironmentId: env.Id,

pkg/appStore/discover/repository/AppStoreApplicationVersionRepository.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ type AppStoreApplicationVersionRepository interface {
3434
FindChartVersionByAppStoreId(id int) ([]*AppStoreApplicationVersion, error)
3535
FindByIds(ids []int) ([]*AppStoreApplicationVersion, error)
3636
GetChartInfoById(id int) (*AppStoreApplicationVersion, error)
37-
FindLatestAppStoreVersionIdByAppStoreName(name string) (int, error)
37+
FindLatestVersionByAppStoreIdForChartRepo(id int) (int, error)
38+
FindLatestVersionByAppStoreIdForOCIRepo(id int) (int, error)
3839
SearchAppStoreChartByName(chartName string) ([]*appStoreBean.ChartRepoSearch, error)
3940
}
4041

@@ -98,33 +99,40 @@ func updateFindWithFilterQuery(filter *appStoreBean.AppStoreFilter, updateAction
9899
query = " ch.name as chart_name, das.id as docker_artifact_store_id"
99100
}
100101
}
102+
//for chart repos, created (derived through index.yaml) column of app_store_application_version is used for finding latest version and for oci repo id is used (because created is null)
103+
latestAppStoreVersionQueryForChartRepo := " SELECT MAX(created) as created " +
104+
" FROM app_store_application_version asv " +
105+
" INNER JOIN app_store aps ON (asv.app_store_id = aps.id and aps.active = true and aps.chart_repo_id is NOT NULL) " +
106+
" GROUP BY asv.app_store_id "
101107

102-
latestAppStoreVersionQuery := " SELECT MAX(asv.id) as id " +
108+
latestAppStoreVersionQueryForOCIRepo := " SELECT MAX(asv.id) as id " +
103109
" FROM app_store_application_version asv " +
104-
" INNER JOIN app_store aps ON (asv.app_store_id = aps.id and aps.active = true) " +
110+
" INNER JOIN app_store aps ON (asv.app_store_id = aps.id and aps.active = true and aps.docker_artifact_store_id is NOT NULL) " +
105111
" GROUP BY asv.app_store_id "
106112

113+
combinedWhereClause := fmt.Sprintf("( (asv.created IN (%s) and aps.chart_repo_id is not null ) or (asv.id IN (%s) and aps.docker_artifact_store_id is not null) )", latestAppStoreVersionQueryForChartRepo, latestAppStoreVersionQueryForOCIRepo)
114+
107115
if updateAction == QUERY_JOIN_UPDTAE {
108116
if len(filter.ChartRepoId) > 0 && len(filter.RegistryId) > 0 {
109117
query = " LEFT JOIN chart_repo ch ON (aps.chart_repo_id = ch.id and ch.deleted IS FALSE)" +
110118
" LEFT JOIN docker_artifact_store das ON aps.docker_artifact_store_id = das.id" +
111119
" LEFT JOIN oci_registry_config oci ON oci.docker_artifact_store_id = das.id" +
112-
fmt.Sprintf(" WHERE (asv.id IN (%s) AND (ch.active IS TRUE OR (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)))", latestAppStoreVersionQuery) +
120+
fmt.Sprintf(" WHERE ( (%s) AND (ch.active IS TRUE OR (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)))", combinedWhereClause) +
113121
" AND (ch.id IN (?) OR das.id IN (?))"
114122
} else if len(filter.RegistryId) > 0 {
115123
query = " LEFT JOIN docker_artifact_store das ON aps.docker_artifact_store_id = das.id" +
116124
" LEFT JOIN oci_registry_config oci ON oci.docker_artifact_store_id = das.id" +
117-
fmt.Sprintf(" WHERE asv.id IN (%s) AND (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)", latestAppStoreVersionQuery) +
125+
fmt.Sprintf(" WHERE asv.id IN (%s) AND (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)", latestAppStoreVersionQueryForOCIRepo) +
118126
" AND das.id IN (?)"
119127
} else if len(filter.ChartRepoId) > 0 {
120128
query = " LEFT JOIN chart_repo ch ON (aps.chart_repo_id = ch.id and ch.deleted IS FALSE)" +
121-
fmt.Sprintf(" WHERE asv.id IN (%s) AND ch.active IS TRUE", latestAppStoreVersionQuery) +
129+
fmt.Sprintf(" WHERE asv.created IN (%s) AND ch.active IS TRUE", latestAppStoreVersionQueryForChartRepo) +
122130
" AND ch.id IN (?)"
123131
} else {
124132
query = " LEFT JOIN chart_repo ch ON (aps.chart_repo_id = ch.id and ch.deleted IS FALSE)" +
125133
" LEFT JOIN docker_artifact_store das ON aps.docker_artifact_store_id = das.id" +
126134
" LEFT JOIN oci_registry_config oci ON oci.docker_artifact_store_id = das.id" +
127-
fmt.Sprintf(" WHERE (asv.id IN (%s) AND (ch.active IS TRUE OR (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)))", latestAppStoreVersionQuery)
135+
fmt.Sprintf(" WHERE (%s AND (ch.active IS TRUE OR (das.active IS TRUE AND oci.deleted IS FALSE AND oci.is_chart_pull_active IS TRUE)))", combinedWhereClause)
128136
}
129137
}
130138
return query
@@ -227,22 +235,36 @@ func (impl AppStoreApplicationVersionRepositoryImpl) FindVersionsByAppStoreId(id
227235
return appStoreApplicationVersions, err
228236
}
229237

230-
func (impl *AppStoreApplicationVersionRepositoryImpl) FindLatestAppStoreVersionIdByAppStoreName(name string) (int, error) {
238+
func (impl *AppStoreApplicationVersionRepositoryImpl) FindLatestVersionByAppStoreIdForChartRepo(id int) (int, error) {
239+
var appStoreApplicationVersionId int
240+
queryTemp := "SELECT asv.id AS app_store_application_version_id FROM app_store_application_version AS asv JOIN app_store AS ap ON asv.app_store_id = ap.id WHERE ap.id = ? order by created desc limit 1;"
241+
_, err := impl.dbConnection.Query(&appStoreApplicationVersionId, queryTemp, id)
242+
return appStoreApplicationVersionId, err
243+
}
244+
245+
func (impl *AppStoreApplicationVersionRepositoryImpl) FindLatestVersionByAppStoreIdForOCIRepo(id int) (int, error) {
231246
var appStoreApplicationVersionId int
232-
queryTemp := "SELECT MAX(asv.id) AS app_store_application_version_id FROM app_store_application_version AS asv JOIN app_store AS ap ON asv.app_store_id = ap.id WHERE ap.name = ?;"
233-
_, err := impl.dbConnection.Query(&appStoreApplicationVersionId, queryTemp, name)
247+
queryTemp := "SELECT MAX(asv.id) AS app_store_application_version_id FROM app_store_application_version AS asv JOIN app_store AS ap ON asv.app_store_id = ap.id WHERE ap.id = ?;"
248+
_, err := impl.dbConnection.Query(&appStoreApplicationVersionId, queryTemp, id)
234249
return appStoreApplicationVersionId, err
235250
}
236251

237252
func (impl *AppStoreApplicationVersionRepositoryImpl) SearchAppStoreChartByName(chartName string) ([]*appStoreBean.ChartRepoSearch, error) {
238253
var chartRepos []*appStoreBean.ChartRepoSearch
254+
//for chart repos, created (derived through index.yaml) column of app_store_application_version is used for finding latest version and for oci repo id is used (because created is null)
239255
queryTemp := "select asv.id as app_store_application_version_id, asv.version, asv.deprecated, aps.id as chart_id," +
240256
" aps.name as chart_name, chr.id as chart_repo_id, chr.name as chart_repo_name" +
241257
" from app_store_application_version asv" +
242258
" inner join app_store aps on asv.app_store_id = aps.id" +
243259
" left join chart_repo chr on aps.chart_repo_id = chr.id" +
244260
" left join docker_artifact_store das on aps.docker_artifact_store_id = das.id" +
245-
" where aps.name like '%" + chartName + "%' and asv.id = (SELECT MAX(id) FROM app_store_application_version WHERE app_store_id = asv.app_store_id) and aps.active=true order by aps.name asc;"
261+
" where aps.name like '%" + chartName + "%' and" +
262+
"( " +
263+
"( aps.docker_artifact_store_id is NOT NULL and asv.id = (SELECT MAX(id) FROM app_store_application_version WHERE app_store_id = asv.app_store_id))" +
264+
" or " +
265+
"(aps.chart_repo_id is NOT NULL and asv.created = (SELECT MAX(created) FROM app_store_application_version WHERE app_store_id = asv.app_store_id)) " +
266+
") " +
267+
"and aps.active=true order by aps.name asc;"
246268
_, err := impl.dbConnection.Query(&chartRepos, queryTemp)
247269
if err != nil {
248270
return nil, err

pkg/appStore/discover/repository/AppStoreRepository.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"time"
2525
)
2626

27-
type AppStoreRepository interface{}
27+
type AppStoreRepository interface {
28+
FindAppStoreByName(name string) (*AppStore, error)
29+
}
2830

2931
type AppStoreRepositoryImpl struct {
3032
dbConnection *pg.DB
@@ -48,3 +50,9 @@ type AppStore struct {
4850
ChartRepo *chartRepoRepository.ChartRepo
4951
DockerArtifactStore *dockerArtifactStoreRegistry.DockerArtifactStore
5052
}
53+
54+
func (impl *AppStoreRepositoryImpl) FindAppStoreByName(name string) (*AppStore, error) {
55+
var AppStore AppStore
56+
err := impl.dbConnection.Model(&AppStore).Where("name = ? ", name).Limit(1).Select()
57+
return &AppStore, err
58+
}

wire_gen.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)