From e7ae568bf121c01466e7d57ad27fa8c624358d83 Mon Sep 17 00:00:00 2001 From: Maxence Maireaux Date: Thu, 23 May 2024 14:22:31 +0200 Subject: [PATCH] feat(operator): Add Payments Connector Statefull & Replicas use for ledger (#1525) Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .github/workflows/main.yml | 4 +-- .github/workflows/releases.yml | 4 +-- components/operator/README.md | 4 ++- .../api/formance.com/v1beta1/ledger_types.go | 2 ++ .../crd/bases/formance.com_ledgers.yaml | 1 + components/operator/docs/crd.md | 2 +- ...sourcedefinition_ledgers.formance.com.yaml | 1 + .../internal/resources/ledgers/deployments.go | 26 ++++++++++++++----- .../resources/payments/deployments.go | 1 + 9 files changed, 32 insertions(+), 13 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fd8d6fd6b7..0bb79e8453 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -52,7 +52,7 @@ jobs: uses: ./.github/actions/env - run: > earthly - --no-output + --no-output --auto-skip --allow-privileged --secret SPEAKEASY_API_KEY=$SPEAKEASY_API_KEY ${{ contains(github.event.pull_request.labels.*.name, 'no-cache') && '--no-cache' || '' }} @@ -72,7 +72,7 @@ jobs: uses: ./.github/actions/env - run: > earthly - --no-output + --no-output --auto-skip --allow-privileged --secret SPEAKEASY_API_KEY=$SPEAKEASY_API_KEY ${{ contains(github.event.pull_request.labels.*.name, 'no-cache') && '--no-cache' || '' }} diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml index d75f1b77e7..5ea6f60337 100644 --- a/.github/workflows/releases.yml +++ b/.github/workflows/releases.yml @@ -17,7 +17,7 @@ jobs: uses: ./.github/actions/env - run: > earthly - --allow-privileged --auto-skip + --allow-privileged --secret SPEAKEASY_API_KEY=$SPEAKEASY_API_KEY ${{ contains(github.event.pull_request.labels.*.name, 'no-cache') && '--no-cache' || '' }} +pre-commit @@ -129,7 +129,7 @@ jobs: - name: Publish Helm run: > earthly - --allow-privileged --auto-skip + --allow-privileged --secret GITHUB_TOKEN=$GITHUB_TOKEN +helm-publish env: diff --git a/components/operator/README.md b/components/operator/README.md index f0946f13ef..827237e81d 100644 --- a/components/operator/README.md +++ b/components/operator/README.md @@ -178,12 +178,14 @@ Available settings: | temporal.tls.key | string | | Temporal certificate key | | broker.dsn | URI | | Broker URI | | opentelemetry.traces.dsn | URI | | OpenTelemetry collector URI | -| clear-database | bool | true | Whether or not remove databases on stack deletion | +| clear-database | bool | true | Whether to remove databases on stack deletion | +| ledger.deployment-strategy | string | single | Ledger deployment type | | payments.encryption-key | string | | Payments data encryption key | | deployments.``.init-containers.``.resource-requirements | Map | cpu=X, mem=X | | | deployments.``.containers.``.resource-requirements | Map | cpu=X, mem=X | | | deployments.``.init-containers.``.run-as | Map | user=X, group=X | | | deployments.``.containers.``.run-as | Map | user=X, group=X | | +| deployments.``.replicas | string | 2 | | | caddy.image | string | | Caddy image | | registries.``.endpoint | string | | Specify a custom endpoint for a specific docker repository | | registries.``.images.``.rewrite | string | formancehq/example | Allow to rewrite the image path | diff --git a/components/operator/api/formance.com/v1beta1/ledger_types.go b/components/operator/api/formance.com/v1beta1/ledger_types.go index dc09cc6495..e994628a24 100644 --- a/components/operator/api/formance.com/v1beta1/ledger_types.go +++ b/components/operator/api/formance.com/v1beta1/ledger_types.go @@ -56,6 +56,8 @@ type LedgerSpec struct { StackDependency `json:",inline"` // +optional Auth *AuthConfig `json:"auth,omitempty"` + //+kubebuilder:Enum:={single, single-writer} + //+kubebuilder:default:=single //+optional DeploymentStrategy DeploymentStrategy `json:"deploymentStrategy,omitempty"` // Locking is intended for ledger v1 only diff --git a/components/operator/config/crd/bases/formance.com_ledgers.yaml b/components/operator/config/crd/bases/formance.com_ledgers.yaml index 3bb2be20e8..10c5de789a 100644 --- a/components/operator/config/crd/bases/formance.com_ledgers.yaml +++ b/components/operator/config/crd/bases/formance.com_ledgers.yaml @@ -64,6 +64,7 @@ spec: debug: type: boolean deploymentStrategy: + default: single type: string dev: type: boolean diff --git a/components/operator/docs/crd.md b/components/operator/docs/crd.md index 54cb8ade67..2c9aa72906 100644 --- a/components/operator/docs/crd.md +++ b/components/operator/docs/crd.md @@ -700,7 +700,7 @@ _Appears in:_ | `version` _string_ | | | | | `stack` _string_ | | | | | `auth` _[AuthConfig](#authconfig)_ | | | | -| `deploymentStrategy` _[DeploymentStrategy](#deploymentstrategy)_ | | | | +| `deploymentStrategy` _[DeploymentStrategy](#deploymentstrategy)_ | | single | | | `locking` _[LockingStrategy](#lockingstrategy)_ | Locking is intended for ledger v1 only | | | diff --git a/components/operator/helm/crds/templates/crds/apiextensions.k8s.io_v1_customresourcedefinition_ledgers.formance.com.yaml b/components/operator/helm/crds/templates/crds/apiextensions.k8s.io_v1_customresourcedefinition_ledgers.formance.com.yaml index 15ac058347..aebdb61020 100644 --- a/components/operator/helm/crds/templates/crds/apiextensions.k8s.io_v1_customresourcedefinition_ledgers.formance.com.yaml +++ b/components/operator/helm/crds/templates/crds/apiextensions.k8s.io_v1_customresourcedefinition_ledgers.formance.com.yaml @@ -64,6 +64,7 @@ spec: debug: type: boolean deploymentStrategy: + default: single type: string dev: type: boolean diff --git a/components/operator/internal/resources/ledgers/deployments.go b/components/operator/internal/resources/ledgers/deployments.go index 1bc9f35938..fa21d5f116 100644 --- a/components/operator/internal/resources/ledgers/deployments.go +++ b/components/operator/internal/resources/ledgers/deployments.go @@ -27,18 +27,30 @@ import ( func installLedger(ctx core.Context, stack *v1beta1.Stack, ledger *v1beta1.Ledger, database *v1beta1.Database, image string, isV2 bool) error { - switch ledger.Spec.DeploymentStrategy { - case v1beta1.DeploymentStrategyMonoWriterMultipleReader: - if err := core.DeleteIfExists[*appsv1.Deployment](ctx, core.GetNamespacedResourceName(stack.Name, "ledger")); err != nil { - return err - } - return installLedgerMonoWriterMultipleReader(ctx, stack, ledger, database, image, isV2) - default: + deploymentStrategySettings, err := settings.GetStringOrDefault(ctx, stack.Name, v1beta1.DeploymentStrategySingle, "ledger", "deployment-strategy") + if err != nil { + return err + } + + isSingle := true + if deploymentStrategySettings == v1beta1.DeploymentStrategyMonoWriterMultipleReader { + isSingle = false + } + if ledger.Spec.DeploymentStrategy == v1beta1.DeploymentStrategyMonoWriterMultipleReader { + isSingle = false + } + + if isSingle { if err := uninstallLedgerMonoWriterMultipleReader(ctx, stack); err != nil { return err } return installLedgerSingleInstance(ctx, stack, ledger, database, image, isV2) } + + if err := core.DeleteIfExists[*appsv1.Deployment](ctx, core.GetNamespacedResourceName(stack.Name, "ledger")); err != nil { + return err + } + return installLedgerMonoWriterMultipleReader(ctx, stack, ledger, database, image, isV2) } func installLedgerSingleInstance(ctx core.Context, stack *v1beta1.Stack, diff --git a/components/operator/internal/resources/payments/deployments.go b/components/operator/internal/resources/payments/deployments.go index 1ff049f134..c86f7a7ae0 100644 --- a/components/operator/internal/resources/payments/deployments.go +++ b/components/operator/internal/resources/payments/deployments.go @@ -253,6 +253,7 @@ func createConnectorsDeployment(ctx core.Context, stack *v1beta1.Stack, payments }, }, }). + WithStateful(true). Install(ctx) if err != nil { return err