Skip to content

Commit

Permalink
upgrade: run upgrade job when dburi changes (PROJQUAY-3776)
Browse files Browse the repository at this point in the history
We also need to run the upgrade job when the dburi changes, not only
when there is an operator version upgrade.
  • Loading branch information
ricardomaraschini authored and HammerMeetNail committed May 9, 2022
1 parent c01e1b0 commit b6839c6
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 4 deletions.
8 changes: 7 additions & 1 deletion pkg/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,12 @@ func Inflate(
}
parsedUserConfig["SECRET_KEY"] = ctx.SecretKey

var dbCfgHasChanged bool
if dbURI, ok := parsedUserConfig["DB_URI"].(string); ok && len(dbURI) > 0 {
dbCfgHasChanged = parsedUserConfig["DB_URI"] != ctx.DbUri
ctx.DbUri = dbURI
} else if v1.ComponentIsManaged(quay.Spec.Components, v1.ComponentPostgres) && len(ctx.DbUri) == 0 {
dbCfgHasChanged = true
log.Info("managed `DB_URI` not found in config, generating new one")
user := quay.GetName() + "-quay-database"
name := quay.GetName() + "-quay-database"
Expand Down Expand Up @@ -585,7 +588,10 @@ func Inflate(
var overlay string
if rolloutBlocked(quay) {
overlay = configEditorOnlyOverlay()
} else if quay.Status.CurrentVersion != v1.QuayVersionCurrent {
} else if quay.Status.CurrentVersion != v1.QuayVersionCurrent || dbCfgHasChanged {
// we render the upgrade overlay directory only if the operator version or the
// database configuration has changed. this scales down quay and runs a job to
// migrate the database.
overlay = upgradeOverlayDir()
} else if !v1.ComponentIsManaged(quay.Spec.Components, v1.ComponentTLS) {
overlay = unmanagedTLSOverlayDir()
Expand Down
124 changes: 121 additions & 3 deletions pkg/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ var inflateTests = []struct {
"config.yaml": encode(map[string]interface{}{"SERVER_HOSTNAME": "quay.io"}),
},
},
expected: withComponents([]string{"quay", "clair", "postgres", "redis", "objectstorage", "mirror", "horizontalpodautoscaler", "clairpostgres"}),
expected: withComponents([]string{"job", "quay", "clair", "postgres", "redis", "objectstorage", "mirror", "horizontalpodautoscaler", "clairpostgres"}),
expectedErr: nil,
},
{
Expand Down Expand Up @@ -386,7 +386,7 @@ var inflateTests = []struct {
"config.yaml": encode(map[string]interface{}{"SERVER_HOSTNAME": "quay.io"}),
},
},
expected: withComponents([]string{"quay", "postgres", "clair", "mirror", "clairpostgres"}),
expected: withComponents([]string{"job", "quay", "postgres", "clair", "mirror", "clairpostgres"}),
expectedErr: nil,
},
{
Expand All @@ -408,6 +408,7 @@ var inflateTests = []struct {
},
ctx: quaycontext.QuayRegistryContext{
SupportsObjectStorage: true,
DbUri: "postgresql://user:pass@db:5432/db",
},
configBundle: &corev1.Secret{
Data: map[string][]byte{
Expand Down Expand Up @@ -436,6 +437,7 @@ var inflateTests = []struct {
},
ctx: quaycontext.QuayRegistryContext{
SupportsObjectStorage: true,
DbUri: "postgresql://user:pass@db:5432/db",
},
configBundle: &corev1.Secret{
Data: map[string][]byte{
Expand Down Expand Up @@ -483,7 +485,123 @@ var inflateTests = []struct {
}),
},
},
expected: withComponents([]string{"quay"}),
expected: withComponents([]string{"job", "quay"}),
expectedErr: nil,
},
{
name: "PostgresConfigurationManuallyUpdated",
quayRegistry: &v1.QuayRegistry{
Spec: v1.QuayRegistrySpec{
Components: []v1.Component{
{Kind: "postgres", Managed: false},
},
},
Status: v1.QuayRegistryStatus{
CurrentVersion: v1.QuayVersionCurrent,
},
},
ctx: quaycontext.QuayRegistryContext{
DbUri: "postgresql://olduser:oldpass@olddb:5432/olddatabase",
},
configBundle: &corev1.Secret{
Data: map[string][]byte{
"config.yaml": encode(map[string]interface{}{
"SERVER_HOSTNAME": "quay.io",
"DB_URI": "postgresql://test-quay-database:postgres@test-quay-database:5432/test-quay-database",
}),
},
},
expected: withComponents([]string{"job", "quay"}),
expectedErr: nil,
},
{
name: "NoChangeInDatabaseButUpgradedVersion",
quayRegistry: &v1.QuayRegistry{
Spec: v1.QuayRegistrySpec{
Components: []v1.Component{
{Kind: "postgres", Managed: false},
},
},
Status: v1.QuayRegistryStatus{
CurrentVersion: "v0.0.0",
},
},
ctx: quaycontext.QuayRegistryContext{
DbUri: "postgresql://olduser:oldpass@olddb:5432/olddatabase",
},
configBundle: &corev1.Secret{
Data: map[string][]byte{
"config.yaml": encode(map[string]interface{}{
"SERVER_HOSTNAME": "quay.io",
"DB_URI": "postgresql://olduser:oldpass@olddb:5432/olddatabase",
}),
},
},
expected: withComponents([]string{"job", "quay"}),
expectedErr: nil,
},
{
name: "RerenderWithoutChanges",
quayRegistry: &v1.QuayRegistry{
Spec: v1.QuayRegistrySpec{
Components: []v1.Component{
{Kind: "postgres", Managed: true},
{Kind: "clair", Managed: true},
{Kind: "clairpostgres", Managed: true},
{Kind: "redis", Managed: true},
{Kind: "objectstorage", Managed: true},
{Kind: "mirror", Managed: true},
{Kind: "horizontalpodautoscaler", Managed: true},
},
},
},
ctx: quaycontext.QuayRegistryContext{
SupportsObjectStorage: true,
DbUri: "postgresql://user:pass@db:5432/db",
},
configBundle: &corev1.Secret{
Data: map[string][]byte{
"config.yaml": encode(
map[string]interface{}{
"SERVER_HOSTNAME": "quay.io",
"DB_URI": "postgresql://user:pass@db:5432/db",
},
),
},
},
expected: withComponents([]string{"quay", "clair", "postgres", "redis", "objectstorage", "mirror", "horizontalpodautoscaler", "clairpostgres"}),
expectedErr: nil,
},
{
name: "RerenderWithDatabaseChanges",
quayRegistry: &v1.QuayRegistry{
Spec: v1.QuayRegistrySpec{
Components: []v1.Component{
{Kind: "postgres", Managed: true},
{Kind: "clair", Managed: true},
{Kind: "clairpostgres", Managed: true},
{Kind: "redis", Managed: true},
{Kind: "objectstorage", Managed: true},
{Kind: "mirror", Managed: true},
{Kind: "horizontalpodautoscaler", Managed: true},
},
},
},
ctx: quaycontext.QuayRegistryContext{
SupportsObjectStorage: true,
DbUri: "postgresql://user:pass@db:5432/db",
},
configBundle: &corev1.Secret{
Data: map[string][]byte{
"config.yaml": encode(
map[string]interface{}{
"SERVER_HOSTNAME": "quay.io",
"DB_URI": "postgresql://new:new@new:5432/db",
},
),
},
},
expected: withComponents([]string{"job", "quay", "clair", "postgres", "redis", "objectstorage", "mirror", "horizontalpodautoscaler", "clairpostgres"}),
expectedErr: nil,
},
}
Expand Down

0 comments on commit b6839c6

Please sign in to comment.