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

fix: move db migrations outside of configmap changes #872

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Changes from all 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
46 changes: 27 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ func main() {
log.Fatalf("Failed to connect to Kubernetes cluster: %v", err)
}

client.ClearSystemConfigCache()
sysConfig, err := client.GetSystemConfig()
if err != nil {
log.Fatalf("Failed to get system config: %v", err)
}

dbDriverName, databaseDataSourceName := sysConfig.DatabaseConnection()
// sqlx.MustConnect will panic when it can't connect to DB. In that case, this whole application will crash.
// This is okay, as the pod will restart and try connecting to DB again.
// dbDriverName may be nil, but sqlx will then panic.
db := sqlx.MustConnect(dbDriverName, databaseDataSourceName)
goose.SetTableName("goose_db_version")
if err := goose.Run("up", db.DB, filepath.Join("db", "sql")); err != nil {
log.Fatalf("Failed to run database sql migrations: %v", err)
db.Close()
}

goose.SetTableName("goose_db_go_version")
migrations.Initialize()
if err := goose.Run("up", db.DB, filepath.Join("db", "go")); err != nil {
log.Fatalf("Failed to run database go migrations: %v", err)
db.Close()
}

go watchConfigmapChanges("onepanel", stopCh, func(configMap *corev1.ConfigMap) error {
log.Printf("Configmap changed")
stopCh <- struct{}{}
Expand All @@ -65,36 +89,20 @@ func main() {

for {
client.ClearSystemConfigCache()
sysConfig, err := client.GetSystemConfig()
sysConfig, err = client.GetSystemConfig()
if err != nil {
log.Fatalf("Failed to get system config: %v", err)
}

dbDriverName, databaseDataSourceName := sysConfig.DatabaseConnection()
// sqlx.MustConnect will panic when it can't connect to DB. In that case, this whole application will crash.
// This is okay, as the pod will restart and try connecting to DB again.
// dbDriverName may be nil, but sqlx will then panic.
db := sqlx.MustConnect(dbDriverName, databaseDataSourceName)
goose.SetTableName("goose_db_version")
if err := goose.Run("up", db.DB, filepath.Join("db", "sql")); err != nil {
log.Fatalf("Failed to run database sql migrations: %v", err)
db.Close()
}

goose.SetTableName("goose_db_go_version")
migrations.Initialize()
if err := goose.Run("up", db.DB, filepath.Join("db", "go")); err != nil {
log.Fatalf("Failed to run database go migrations: %v", err)
db.Close()
}
dbDriverName, databaseDataSourceName = sysConfig.DatabaseConnection()

s := startRPCServer(v1.NewDB(db), kubeConfig, sysConfig, stopCh)

<-stopCh

s.Stop()
if err := db.Close(); err != nil {
log.Printf("[error] closing db connection")
log.Printf("[error] closing db connection %v", err.Error())
}
}
}()
Expand Down