Skip to content

Commit

Permalink
chore: use database runtime directly as resource output
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmakine committed Nov 26, 2024
1 parent e6b6694 commit 5a1f065
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 419 deletions.
353 changes: 116 additions & 237 deletions backend/protos/xyz/block/ftl/v1beta1/provisioner/resource.pb.go

Large diffs are not rendered by default.

12 changes: 2 additions & 10 deletions backend/protos/xyz/block/ftl/v1beta1/provisioner/resource.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@ message Resource {
// any output created by the provisioner is stored in a field called "output"

message PostgresResource {
message PostgresResourceOutput {
string read_dsn = 1;
string write_dsn = 2;
}
PostgresResourceOutput output = 1;
v1.schema.DatabaseRuntime output = 1;
}

message MysqlResource {
message MysqlResourceOutput {
string read_dsn = 1;
string write_dsn = 2;
}
MysqlResourceOutput output = 1;
v1.schema.DatabaseRuntime output = 1;
}

message SqlMigrationResource {
Expand Down
16 changes: 2 additions & 14 deletions backend/provisioner/controller_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ func NewControllerProvisioner(client ftlv1connect.ControllerServiceClient) *InMe
if !ok {
return nil, fmt.Errorf("failed to find database declaration for %s", dep.ResourceId)
}
decl.Database.Runtime = &schemapb.DatabaseRuntime{
Value: &schemapb.DatabaseRuntime_DsnDatabaseRuntime{
DsnDatabaseRuntime: &schemapb.DSNDatabaseRuntime{
Dsn: r.Postgres.Output.WriteDsn,
},
},
}
decl.Database.Runtime = r.Postgres.Output
case *provisioner.Resource_Mysql:
if r.Mysql == nil || r.Mysql.Output == nil {
return nil, fmt.Errorf("mysql resource has not been provisioned")
Expand All @@ -55,13 +49,7 @@ func NewControllerProvisioner(client ftlv1connect.ControllerServiceClient) *InMe
if !ok {
return nil, fmt.Errorf("failed to find database declaration for %s", dep.ResourceId)
}
decl.Database.Runtime = &schemapb.DatabaseRuntime{
Value: &schemapb.DatabaseRuntime_DsnDatabaseRuntime{
DsnDatabaseRuntime: &schemapb.DSNDatabaseRuntime{
Dsn: r.Mysql.Output.WriteDsn,
},
},
}
decl.Database.Runtime = r.Mysql.Output
case *provisioner.Resource_Topic:
if r.Topic == nil || r.Topic.Output == nil {
return nil, fmt.Errorf("topic resource has not been provisioned")
Expand Down
14 changes: 10 additions & 4 deletions backend/provisioner/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"connectrpc.com/connect"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
proto "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner/provisionerconnect"
"github.com/TBD54566975/ftl/backend/provisioner"
Expand Down Expand Up @@ -121,9 +122,14 @@ func TestDeployment_Progress(t *testing.T) {
psql.Postgres = &proto.PostgresResource{}
}
if psql.Postgres.Output == nil {
psql.Postgres.Output = &proto.PostgresResource_PostgresResourceOutput{}
psql.Postgres.Output = &schemapb.DatabaseRuntime{
Value: &schemapb.DatabaseRuntime_DsnDatabaseRuntime{
DsnDatabaseRuntime: &schemapb.DSNDatabaseRuntime{
Dsn: "postgres://localhost:5432/foo",
},
},
}
}
psql.Postgres.Output.ReadDsn = "postgres://localhost:5432/foo"
} else {
return nil, fmt.Errorf("expected postgres resource, got %T", req.DesiredResources[0].Resource)
}
Expand All @@ -143,8 +149,8 @@ func TestDeployment_Progress(t *testing.T) {
for _, res := range req.DesiredResources {
for _, dep := range res.Dependencies {
if psql, ok := dep.Resource.(*proto.Resource_Postgres); ok && psql.Postgres != nil {
if psql.Postgres.Output == nil || psql.Postgres.Output.ReadDsn == "" {
return nil, fmt.Errorf("read dsn is empty")
if psql.Postgres.Output == nil {
return nil, fmt.Errorf("output is nil")
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions backend/provisioner/dev_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
_ "github.com/go-sql-driver/mysql"

"github.com/TBD54566975/ftl/backend/controller/dsn"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner"
"github.com/TBD54566975/ftl/internal/dev"
"github.com/TBD54566975/ftl/internal/log"
Expand Down Expand Up @@ -96,9 +97,12 @@ func establishMySQLDB(ctx context.Context, rc *provisioner.ResourceContext, mysq
mysql.Mysql = &provisioner.MysqlResource{}
}
dsn := dsn.MySQLDSN(dbName, dsn.Port(mysqlPort))
mysql.Mysql.Output = &provisioner.MysqlResource_MysqlResourceOutput{
WriteDsn: dsn,
ReadDsn: dsn,
mysql.Mysql.Output = &schemapb.DatabaseRuntime{
Value: &schemapb.DatabaseRuntime_DsnDatabaseRuntime{
DsnDatabaseRuntime: &schemapb.DSNDatabaseRuntime{
Dsn: dsn,
},
},
}
return rc.Resource, nil
}
Expand All @@ -113,7 +117,7 @@ func ProvisionPostgresForTest(ctx context.Context, module string, id string) (st
return "", err
}

return res.GetPostgres().GetOutput().GetWriteDsn(), nil
return res.GetPostgres().GetOutput().GetDsnDatabaseRuntime().GetDsn(), nil
}

func ProvisionMySQLForTest(ctx context.Context, module string, id string) (string, error) {
Expand All @@ -125,7 +129,7 @@ func ProvisionMySQLForTest(ctx context.Context, module string, id string) (strin
if err != nil {
return "", err
}
return res.GetMysql().GetOutput().GetWriteDsn(), nil
return res.GetMysql().GetOutput().GetDsnDatabaseRuntime().GetDsn(), nil
}

func provisionPostgres(postgresPort int) func(ctx context.Context, rc *provisioner.ResourceContext, module string, id string) (*provisioner.Resource, error) {
Expand Down Expand Up @@ -179,9 +183,12 @@ func provisionPostgres(postgresPort int) func(ctx context.Context, rc *provision
pg.Postgres = &provisioner.PostgresResource{}
}
dsn := dsn.PostgresDSN(dbName, dsn.Port(postgresPort))
pg.Postgres.Output = &provisioner.PostgresResource_PostgresResourceOutput{
WriteDsn: dsn,
ReadDsn: dsn,
pg.Postgres.Output = &schemapb.DatabaseRuntime{
Value: &schemapb.DatabaseRuntime_DsnDatabaseRuntime{
DsnDatabaseRuntime: &schemapb.DSNDatabaseRuntime{
Dsn: dsn,
},
},
}
return rc.Resource, nil
}
Expand Down
3 changes: 1 addition & 2 deletions backend/provisioner/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ func resourceEqual(desired, existing *provisioner.Resource) bool {
return cmp.Equal(desired, existing,
protocmp.Transform(),
protocmp.IgnoreMessages(
&provisioner.MysqlResource_MysqlResourceOutput{},
&provisioner.PostgresResource_PostgresResourceOutput{},
&schemapb.DatabaseRuntime{},
&provisioner.ModuleResource_ModuleResourceOutput{},
&provisioner.SqlMigrationResource_SqlMigrationResourceOutput{},
&provisioner.TopicResource_TopicResourceOutput{},
Expand Down
9 changes: 7 additions & 2 deletions backend/provisioner/resource_equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provisioner
import (
"testing"

schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner"
)

Expand Down Expand Up @@ -52,8 +53,12 @@ func TestResourceEqual(t *testing.T) {
ResourceId: "test",
Resource: &provisioner.Resource_Postgres{
Postgres: &provisioner.PostgresResource{
Output: &provisioner.PostgresResource_PostgresResourceOutput{
ReadDsn: "foo",
Output: &schemapb.DatabaseRuntime{
Value: &schemapb.DatabaseRuntime_DsnDatabaseRuntime{
DsnDatabaseRuntime: &schemapb.DSNDatabaseRuntime{
Dsn: "foo",
},
},
},
},
},
Expand Down
11 changes: 9 additions & 2 deletions backend/provisioner/sql_migration_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/TBD54566975/ftl/backend/controller/artefacts"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner"
"github.com/TBD54566975/ftl/internal/errors"
"github.com/TBD54566975/ftl/internal/infra"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/sha256"
)
Expand Down Expand Up @@ -62,9 +63,15 @@ func provisionSQLMigration(registryConfig artefacts.RegistryConfig) func(ctx con
resource := rc.Dependencies[0].Resource
switch res := resource.(type) {
case *provisioner.Resource_Postgres:
dsn = res.Postgres.GetOutput().GetWriteDsn()
dsn, err = infra.ResolvePostgresDSN(ctx, res.Postgres.GetOutput())
if err != nil {
return nil, fmt.Errorf("failed to resolve postgres DSN: %w", err)
}
case *provisioner.Resource_Mysql:
dsn = "mysql://" + res.Mysql.GetOutput().GetWriteDsn()
dsn, err = infra.ResolveMySQLDSN(ctx, res.Mysql.GetOutput())
if err != nil {
return nil, fmt.Errorf("failed to resolve mysql DSN: %w", err)
}
// strip the tcp part
exp := regexp.MustCompile(`tcp\((.*?)\)`)
dsn = exp.ReplaceAllString(dsn, "$1")
Expand Down
12 changes: 8 additions & 4 deletions cmd/ftl-provisioner-cloudformation/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
_ "github.com/jackc/pgx/v5/stdlib" // SQL driver

schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner"
)

Expand Down Expand Up @@ -82,7 +83,7 @@ func (c *CloudformationProvisioner) updateResources(ctx context.Context, outputs
postgres.Postgres = &provisioner.PostgresResource{}
}
if postgres.Postgres.Output == nil {
postgres.Postgres.Output = &provisioner.PostgresResource_PostgresResourceOutput{}
postgres.Postgres.Output = &schemapb.DatabaseRuntime{}
}

if err := c.updatePostgresOutputs(ctx, postgres.Postgres.Output, resource.ResourceId, byResourceID[resource.ResourceId]); err != nil {
Expand All @@ -95,7 +96,7 @@ func (c *CloudformationProvisioner) updateResources(ctx context.Context, outputs
return nil
}

func (c *CloudformationProvisioner) updatePostgresOutputs(ctx context.Context, to *provisioner.PostgresResource_PostgresResourceOutput, resourceID string, outputs []types.Output) error {
func (c *CloudformationProvisioner) updatePostgresOutputs(ctx context.Context, to *schemapb.DatabaseRuntime, resourceID string, outputs []types.Output) error {
byName, err := outputsByPropertyName(outputs)
if err != nil {
return fmt.Errorf("failed to group outputs by property name: %w", err)
Expand All @@ -108,8 +109,11 @@ func (c *CloudformationProvisioner) updatePostgresOutputs(ctx context.Context, t
return fmt.Errorf("failed to get username and password from secret ARN: %w", err)
}

to.ReadDsn = endpointToDSN(byName[PropertyPsqlReadEndpoint].OutputValue, resourceID, 5432, username, password)
to.WriteDsn = endpointToDSN(byName[PropertyPsqlWriteEndpoint].OutputValue, resourceID, 5432, username, password)
to.Value = &schemapb.DatabaseRuntime_DsnDatabaseRuntime{
DsnDatabaseRuntime: &schemapb.DSNDatabaseRuntime{
Dsn: endpointToDSN(byName[PropertyMySQLWriteEndpoint].OutputValue, resourceID, 5432, username, password),
},
}

return nil
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5a1f065

Please sign in to comment.