Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1521 delete tables on upgrade #1532

Merged
merged 3 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions chart/kubeapps/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ Create name for the apprepository-controller based on the fullname
{{- end -}}

{{/*
Create name for the apprepository bootstrap job
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is an old job which no longer exists, as I couldn't see the name being used anywhere.

Create name for the apprepository pre-upgrade job
*/}}
{{- define "kubeapps.apprepository-jobs-bootstrap.fullname" -}}
{{ template "kubeapps.fullname" . }}-internal-apprepository-jobs-bootstrap
{{- define "kubeapps.apprepository-jobs-preupgrade.fullname" -}}
{{ template "kubeapps.fullname" . }}-internal-apprepository-jobs-preupgrade
{{- end -}}

{{/*
Expand Down
67 changes: 67 additions & 0 deletions chart/kubeapps/templates/apprepository-jobs-preupgrade.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Invalidate the chart cache during upgrade.
apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "kubeapps.apprepository-jobs-preupgrade.fullname" . }}
annotations:
helm.sh/hook: pre-upgrade
helm.sh/hook-delete-policy: hook-succeeded
labels:
app: {{ template "kubeapps.apprepository-jobs-preupgrade.fullname" . }}
chart: {{ template "kubeapps.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
template:
metadata:
labels:
app: {{ template "kubeapps.apprepository-jobs-preupgrade.fullname" . }}
release: {{ .Release.Name }}
spec:
{{- include "kubeapps.imagePullSecrets" . | indent 6 }}
{{- if .Values.hooks.affinity }}
affinity: {{- include "kubeapps.tplValue" (dict "value" .Values.hooks.affinity "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.hooks.nodeSelector }}
nodeSelector: {{- include "kubeapps.tplValue" (dict "value" .Values.hooks.nodeSelector "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.hooks.tolerations }}
tolerations: {{- include "kubeapps.tplValue" (dict "value" .Values.hooks.tolerations "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.securityContext.enabled }}
securityContext:
fsGroup: {{ .Values.securityContext.fsGroup }}
runAsUser: {{ .Values.securityContext.runAsUser }}
{{- end }}
restartPolicy: OnFailure
containers:
- name: invalidate-cache
image: {{ template "kubeapps.image" (list .Values.apprepository.syncImage .Values.global) }}
command:
- /asset-syncer
args:
- invalidate-cache
{{- if .Values.mongodb.enabled }}
- --database-type=mongodb
- --database-url={{ template "kubeapps.mongodb.fullname" . }}
- --database-user=root
- --database-name=charts
{{- end }}
{{- if .Values.postgresql.enabled }}
- --database-type=postgresql
- --database-url={{ template "kubeapps.postgresql.fullname" . }}:5432
- --database-user=postgres
- --database-name=assets
{{- end }}
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
{{- if .Values.mongodb.enabled }}
key: mongodb-root-password
name: {{ .Values.mongodb.existingSecret }}
{{- end }}
{{- if .Values.postgresql.enabled }}
key: postgresql-password
name: {{ .Values.postgresql.existingSecret }}
{{- end }}
56 changes: 56 additions & 0 deletions cmd/asset-syncer/invalidate-cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright (c) 2020 Bitnami

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"github.com/kubeapps/common/datastore"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var invalidateCacheCmd = &cobra.Command{
Use: "invalidate-cache",
Short: "removes all data so the cache can be rebuilt",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
logrus.Info("This command does not take any arguments")
cmd.Help()
return
}

if debug {
logrus.SetLevel(logrus.DebugLevel)
}

dbConfig := datastore.Config{URL: databaseURL, Database: databaseName, Username: databaseUser, Password: databasePassword}
manager, err := newManager(databaseType, dbConfig)
if err != nil {
logrus.Fatal(err)
}
err = manager.Init()
if err != nil {
logrus.Fatal(err)
}
defer manager.Close()

err = manager.InvalidateCache()
if err != nil {
logrus.Fatal(err)
}
logrus.Infof("Successfully invalidated cache")
},
}
2 changes: 1 addition & 1 deletion cmd/asset-syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func init() {

databasePassword = os.Getenv("DB_PASSWORD")

cmds := []*cobra.Command{syncCmd, deleteCmd}
cmds := []*cobra.Command{syncCmd, deleteCmd, invalidateCacheCmd}
for _, cmd := range cmds {
rootCmd.AddCommand(cmd)
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/asset-syncer/mongodb_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ func (m *mongodbAssetManager) insertFiles(chartFilesID string, files models.Char
_, err := db.C(chartFilesCollection).UpsertId(chartFilesID, files)
return err
}

// InvalidateCache for mongodb currently is a noop to fulfil the interface.
func (m *mongodbAssetManager) InvalidateCache() error {
// TODO: implement a cache invalidation
return nil
}
9 changes: 1 addition & 8 deletions cmd/asset-syncer/postgresql_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"fmt"
"os"
"strconv"
"strings"
"testing"

"github.com/kubeapps/common/datastore"
Expand Down Expand Up @@ -72,13 +71,7 @@ func getInitializedManager(t *testing.T) (*postgresAssetManager, func()) {
pam := openTestManager(t)
cleanup := func() { pam.Close() }

tables := strings.Join([]string{dbutils.RepositoryTable, dbutils.ChartTable, dbutils.ChartFilesTable}, ",")
_, err := pam.DB.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s CASCADE", tables))
if err != nil {
t.Fatalf("%+v", err)
}

err = pam.initTables()
err := pam.InvalidateCache()
if err != nil {
t.Fatalf("%+v", err)
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/asset-syncer/postgresql_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,14 @@ func (m *postgresAssetManager) insertFiles(chartFilesID string, files models.Cha
}
return err
}

// InvalidateCache for postgresql deletes and re-writes the schema
func (m *postgresAssetManager) InvalidateCache() error {
tables := strings.Join([]string{dbutils.RepositoryTable, dbutils.ChartTable, dbutils.ChartFilesTable}, ",")
_, err := m.DB.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s CASCADE", tables))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the CASCADE necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here it's not, thanks. I'd originally only deleted the one table and relied on the cascades, but I had to change that as the files table doesn't have a FK in this PR. I've removed the CASCADE and will re-add it when the FK's are in the next PR (I think)>

if err != nil {
return err
}

return m.initTables()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not really necessary since it will be executed with syncing but I am okay if you want to leave it to leave the tables ready

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the reason for having it here is so that it is sync'd with the correct code (the new, rather than from the still running older pods). We will probably need a transaction here to avoid race conditions with the existing pods too.

}
1 change: 1 addition & 0 deletions cmd/asset-syncer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type assetManager interface {
UpdateLastCheck(repoNamespace, repoName, checksum string, now time.Time) error
Init() error
Close() error
InvalidateCache() error
updateIcon(data []byte, contentType, ID string) error
filesExist(chartFilesID, digest string) bool
insertFiles(chartFilesID string, files models.ChartFiles) error
Expand Down