Skip to content

Commit

Permalink
Run postupgrade job only when upgrading (#1760)
Browse files Browse the repository at this point in the history
* Run postupgrade job only when upgrading

* Init collections when syncing
  • Loading branch information
Andres Martinez Gotor committed Jun 3, 2020
1 parent 0307553 commit ea22723
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions chart/kubeapps/templates/apprepository-jobs-postupgrade.yaml
@@ -1,11 +1,11 @@
{{- if .Values.featureFlags.invalidateCache }}
# Ensure db indexes are set and invalidate the chart cache during both install and upgrade.
# Ensure db indexes are set and invalidate the chart when upgrading.
apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "kubeapps.apprepository-job-postupgrade.fullname" . }}
annotations:
helm.sh/hook: post-upgrade,post-install
helm.sh/hook: post-upgrade
helm.sh/hook-weight: "0"
helm.sh/hook-delete-policy: hook-succeeded
labels:
Expand Down
5 changes: 5 additions & 0 deletions cmd/asset-syncer/mongodb_utils.go
Expand Up @@ -47,6 +47,11 @@ func newMongoDBManager(config datastore.Config, kubeappsNamespace string) assetM
// imported into the database as fast as possible. E.g. we want all icons for
// charts before fetching readmes for each chart and version pair.
func (m *mongodbAssetManager) Sync(repo models.Repo, charts []models.Chart) error {
err := m.InitCollections()
if err != nil {
return err
}

return m.importCharts(charts, repo)
}

Expand Down
28 changes: 19 additions & 9 deletions pkg/dbutils/mongodb_utils.go
Expand Up @@ -56,17 +56,12 @@ func (m *MongodbAssetManager) Close() error {
return nil
}

func (m *MongodbAssetManager) InvalidateCache() error {
// InitCollections ensure indexes of the different collections
func (m *MongodbAssetManager) InitCollections() error {
db, closer := m.DBSession.DB()
defer closer()

err := db.C(ChartCollection).DropCollection()
// We ignore "ns not found" which relates to an operation on a non-existent collection.
if err != nil && err.Error() != "ns not found" {
return err
}

err = db.C(ChartCollection).EnsureIndex(mgo.Index{
err := db.C(ChartCollection).EnsureIndex(mgo.Index{
Key: []string{"chart_id", "repo.namespace", "repo.name"},
Unique: true,
DropDups: true,
Expand All @@ -75,12 +70,27 @@ func (m *MongodbAssetManager) InvalidateCache() error {
if err != nil {
return err
}
err = db.C(ChartFilesCollection).EnsureIndex(mgo.Index{
return db.C(ChartFilesCollection).EnsureIndex(mgo.Index{
Key: []string{"file_id", "repo.namespace", "repo.name"},
Background: false,
})
}

// InvalidateCache drops the different collections and initialize them again
func (m *MongodbAssetManager) InvalidateCache() error {
db, closer := m.DBSession.DB()
defer closer()

err := db.C(ChartCollection).DropCollection()
// We ignore "ns not found" which relates to an operation on a non-existent collection.
if err != nil && err.Error() != "ns not found" {
return err
}

err = m.InitCollections()
if err != nil {
return err
}

return m.DBSession.Fsync(false)
}

0 comments on commit ea22723

Please sign in to comment.