shalb / cluster.dev

Cloud-native infrastructure templating. Creating cloud installers for SaaS. Replication of complex cloud-native infrastructures.
https://docs.cluster.dev/
GNU Affero General Public License v3.0
404 stars 36 forks source link

Multiple backends #276

Open kinseii opened 3 months ago

kinseii commented 3 months ago

We have multiple subscriptions in a cloud provider and we would like to store the state files for each StackTemplate in separate storages (backends). The documentation says (https://docs.cluster.dev/structure-project):

backend-name of the backend that will be used to store the cluster.dev state of the current project. Optional.

That is, it seems to be the way it was intended, since this parameter is optional in the Project. However, the validate and plan commands produce an error:

01:16:46 [FATAL] Project configuration check: fail
load base configuration: loading project: error in project config: backend is not defined. To use default local backend, set 'backend: default' option

Thus I can't specify multiple backends. If this is not a bug, I would like to request such a feature, thank you very much!

romanprog commented 2 months ago

Hello. You can use multiple backends for terraform states (configured in kind: stack ). The backend is tied to the stack. But cdev's own state (configured in project.yaml, kind: project ) can be stored only in one backend. Here is a conditional example: project.yaml

name: example
kind: Project
backend: aws-backend # save cdev state in aws s3
variables:
  organization: cluster-dev
  region: eu-central-1
  state_bucket_name: cluster-dev-gha-tests

stack.yaml

name: aws-backend
kind: Backend
provider: s3
spec:
  bucket: {{ .project.variables.state_bucket_name }}
  region: {{ .project.variables.region }}
---
name: azurerm-backend
kind: backend
provider: azurerm
spec:
  resource_group_name: "StorageAccount-ResourceGroup"
  storage_account_name: "example"
  container_name: "cdev-states"
---
name: aws-stack
template: https://...
kind: Stack
backend: aws-backend # save tf states in aws s3
---
name: azure-stack
template: https://...
kind: Stack
backend: azurerm-backend # save tf states in azure 

Does this answer the question?

kinseii commented 2 months ago

I've suspended PoC for cdev for now, but I'd like to report the following.

We are using Azure Blob Storage (ABS), we have multiple stacks in our project that use separate Subscriptions. I can manually switch Subscriptions with the command az account set -n $SUBSCRIPTION-NAME.

If I am in Subscription1 and if we have a Stack using Subscription1 in the backend. And Cdev itself is using Subscription2, then plan/apply gives an error (just the string “Error”), if you enable debug, you will see that it can't access Blob Storage for Cdev itself. So we have several subscriptions in our configuration and we need to define where to save the states of cdev itself and if I specify one of them (in the kind: Project), I will get an error when running for a subscription other than cdev backend. I have so far used backend: default so that the cdev states are saved locally, but of course we have to do something about it.

romanprog commented 2 months ago

Understood, i changed label of this issue to bug. Will check this problem.