Skip to content

Commit

Permalink
feat: move versions in database object annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Feb 8, 2024
1 parent 0cc3fca commit 2351295
Show file tree
Hide file tree
Showing 20 changed files with 47 additions and 38 deletions.
2 changes: 0 additions & 2 deletions components/operator/api/formance.com/v1beta1/ledger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ type LedgerSpec struct {
// LedgerStatus defines the observed state of Ledger
type LedgerStatus struct {
ModuleStatus `json:",inline"`
//+optional
Version string `json:"version,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type PaymentsSpec struct {
// PaymentsStatus defines the observed state of Payments
type PaymentsStatus struct {
ModuleStatus `json:",inline"`
//+optional
Version string `json:"version,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
2 changes: 0 additions & 2 deletions components/operator/api/formance.com/v1beta1/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ func (c *StatusWithConditions) SetCondition(condition Condition) {

type ModuleStatus struct {
StatusWithConditions `json:",inline"`
//+optional
Version string `json:"version,omitempty"`
}

type AuthConfig struct {
Expand Down
2 changes: 0 additions & 2 deletions components/operator/config/crd/bases/formance.com_auths.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ spec:
items:
type: string
type: array
version:
type: string
required:
- authEnabled
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ spec:
type: boolean
temporalURI:
type: string
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ spec:
type: string
ready:
type: boolean
version:
type: string
type: object
type: object
served: true
Expand Down
7 changes: 5 additions & 2 deletions components/operator/internal/resources/auths/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ func Reconcile(ctx Context, stack *v1beta1.Stack, auth *v1beta1.Auth, version st
return errors.Wrap(err, "resolving image")
}

if IsGreaterOrEqual(version, "v2.0.0-rc.5") && auth.Status.Version != version {
if IsGreaterOrEqual(version, "v2.0.0-rc.5") && databases.GetSavedModuleVersion(database) != version {
if err := jobs.Handle(ctx, auth, "migrate", databases.MigrateDatabaseContainer(image, database)); err != nil {
return err
}
auth.Status.Version = version

if err := databases.SaveModuleVersion(ctx, database, version); err != nil {
return errors.Wrap(err, "saving module version in database object")
}
}

_, err = createDeployment(ctx, stack, auth, database, configMap, image)
Expand Down
16 changes: 16 additions & 0 deletions components/operator/internal/resources/databases/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package databases

import (
"github.com/pkg/errors"
"strings"

v1beta1 "github.com/formancehq/operator/api/formance.com/v1beta1"
Expand Down Expand Up @@ -51,3 +52,18 @@ func Create(ctx core.Context, owner interface {

return database, err
}

const ServiceVersion = "formance.com/module-version"

func SaveModuleVersion(ctx core.Context, database *v1beta1.Database, version string) error {
patch := client.MergeFrom(database.DeepCopy())
if database.Annotations == nil {
database.Annotations = make(map[string]string)
}
database.Annotations[ServiceVersion] = version
return errors.Wrap(ctx.GetClient().Patch(ctx, database, patch), "patching database")
}

func GetSavedModuleVersion(database *v1beta1.Database) string {
return database.Annotations[ServiceVersion]
}
8 changes: 6 additions & 2 deletions components/operator/internal/resources/ledgers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ledgers
import (
_ "embed"
"fmt"
"github.com/pkg/errors"

"github.com/formancehq/operator/api/formance.com/v1beta1"
. "github.com/formancehq/operator/internal/core"
Expand Down Expand Up @@ -92,11 +93,14 @@ func Reconcile(ctx Context, stack *v1beta1.Stack, ledger *v1beta1.Ledger, versio
}

if database.Status.Ready {
if isV2 && ledger.Status.Version != version {
if isV2 && databases.GetSavedModuleVersion(database) != version {
if err := migrate(ctx, stack, ledger, database, image); err != nil {
return err
}
ledger.Status.Version = version

if err := databases.SaveModuleVersion(ctx, database, version); err != nil {
return errors.Wrap(err, "saving module version in database object")
}
}

err = installLedger(ctx, stack, ledger, database, image, isV2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ func Reconcile(ctx Context, stack *v1beta1.Stack, o *v1beta1.Orchestration, vers
return errors.Wrap(err, "resolving image")
}

if IsGreaterOrEqual(version, "v2.0.0-rc.5") && o.Status.Version != version {
if IsGreaterOrEqual(version, "v2.0.0-rc.5") && databases.GetSavedModuleVersion(database) != version {
if err := jobs.Handle(ctx, o, "migrate", databases.MigrateDatabaseContainer(image, database)); err != nil {
return err
}
o.Status.Version = version

if err := databases.SaveModuleVersion(ctx, database, version); err != nil {
return errors.Wrap(err, "saving module version in database object")
}
}

if consumers.Ready() {
Expand Down
8 changes: 6 additions & 2 deletions components/operator/internal/resources/payments/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "embed"
"github.com/formancehq/operator/internal/resources/jobs"
"github.com/formancehq/operator/internal/resources/registries"
"github.com/pkg/errors"
batchv1 "k8s.io/api/batch/v1"
"net/http"

Expand Down Expand Up @@ -54,7 +55,7 @@ func Reconcile(ctx Context, stack *v1beta1.Stack, p *v1beta1.Payments, version s
return err
}

if p.Status.Version != version {
if databases.GetSavedModuleVersion(database) != version {
if err := jobs.Handle(ctx, p, "migrate", databases.MigrateDatabaseContainer(image, database,
func(m *databases.MigrationConfiguration) {
m.AdditionalEnv = []corev1.EnvVar{
Expand All @@ -64,7 +65,10 @@ func Reconcile(ctx Context, stack *v1beta1.Stack, p *v1beta1.Payments, version s
)); err != nil {
return err
}
p.Status.Version = version

if err := databases.SaveModuleVersion(ctx, database, version); err != nil {
return errors.Wrap(err, "saving module version in database object")
}
}

if semver.IsValid(version) && semver.Compare(version, "v1.0.0-alpha") < 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ func Reconcile(ctx Context, stack *v1beta1.Stack, reconciliation *v1beta1.Reconc
return errors.Wrap(err, "resolving image")
}

if IsGreaterOrEqual(version, "v2.0.0-rc.5") && reconciliation.Status.Version != version {
if IsGreaterOrEqual(version, "v2.0.0-rc.5") && databases.GetSavedModuleVersion(database) != version {
if err := jobs.Handle(ctx, reconciliation, "migrate", databases.MigrateDatabaseContainer(image, database)); err != nil {
return err
}
reconciliation.Status.Version = version

if err := databases.SaveModuleVersion(ctx, database, version); err != nil {
return errors.Wrap(err, "saving module version in database object")
}
}

if err := createDeployment(ctx, stack, reconciliation, database, authClient, image); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions components/operator/internal/resources/webhooks/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ func Reconcile(ctx Context, stack *v1beta1.Stack, webhooks *v1beta1.Webhooks, ve
return errors.Wrap(err, "resolving image")
}

if IsGreaterOrEqual(version, "v2.0.0-rc.5") && webhooks.Status.Version != version {
if IsGreaterOrEqual(version, "v2.0.0-rc.5") && databases.GetSavedModuleVersion(database) != version {
if err := jobs.Handle(ctx, webhooks, "migrate", databases.MigrateDatabaseContainer(image, database)); err != nil {
return err
}
webhooks.Status.Version = version
if err := databases.SaveModuleVersion(ctx, database, version); err != nil {
return errors.Wrap(err, "saving module version in database object")
}
}

if consumers.Ready() {
Expand Down

0 comments on commit 2351295

Please sign in to comment.