Skip to content

Commit

Permalink
chore: add a call through ftl-provisioner service (#2747)
Browse files Browse the repository at this point in the history
In the spirit of keeping PRs small, this sets up a simple call through
service `ftl-provisioner` to call create deployments in
`ftl-controller`.

The idea is to extend this later to do the infra provisioning before
calling the controller, and then at some point point deployments to this
service instead.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
jvmakine and github-actions[bot] authored Sep 22, 2024
1 parent e4b9646 commit 9003ae3
Show file tree
Hide file tree
Showing 8 changed files with 534 additions and 0 deletions.

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

95 changes: 95 additions & 0 deletions backend/protos/xyz/block/ftl/v1beta1/provisioner/service.pb.go

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

17 changes: 17 additions & 0 deletions backend/protos/xyz/block/ftl/v1beta1/provisioner/service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package xyz.block.ftl.v1beta1.provisioner;

import "xyz/block/ftl/v1/ftl.proto";

option go_package = "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner;provisioner";
option java_multiple_files = true;

service ProvisionerService {
rpc Ping(xyz.block.ftl.v1.PingRequest) returns (xyz.block.ftl.v1.PingResponse) {
option idempotency_level = NO_SIDE_EFFECTS;
}

// Create a deployment.
rpc CreateDeployment(xyz.block.ftl.v1.CreateDeploymentRequest) returns (xyz.block.ftl.v1.CreateDeploymentResponse);
}
6 changes: 6 additions & 0 deletions backend/provisioner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# FTL Provisioner

FTL Provisioner will be responsible for creating any infrastructure required by deployments, as well as
orchestrating deployments, and keeping track of the state of the underlying infrastructure.

This is still a WIP and should not be used yet.
88 changes: 88 additions & 0 deletions backend/provisioner/provisioner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package provisioner

import (
"context"
"fmt"
"net/url"

"connectrpc.com/connect"
"github.com/alecthomas/kong"
"golang.org/x/sync/errgroup"

ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner/provisionerconnect"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/rpc"
)

type Config struct {
Bind *url.URL `help:"Socket to bind to." default:"http://127.0.0.1:8894" env:"FTL_PROVISIONER_BIND"`
IngressBind *url.URL `help:"Socket to bind to for ingress." default:"http://127.0.0.1:8893" env:"FTL_PROVISIONER_INGRESS_BIND"`
Advertise *url.URL `help:"Endpoint the Provisioner should advertise (must be unique across the cluster, defaults to --bind if omitted)." env:"FTL_PROVISIONER_ADVERTISE"`
ControllerEndpoint *url.URL `name:"ftl-endpoint" help:"Controller endpoint." env:"FTL_ENDPOINT" default:"http://127.0.0.1:8892"`
}

func (c *Config) SetDefaults() {
if err := kong.ApplyDefaults(c); err != nil {
panic(err)
}
if c.Advertise == nil {
c.Advertise = c.Bind
}
}

type Service struct {
controllerClient ftlv1connect.ControllerServiceClient
}

var _ provisionerconnect.ProvisionerServiceHandler = (*Service)(nil)

func New(ctx context.Context, config Config, controllerClient ftlv1connect.ControllerServiceClient, devel bool) (*Service, error) {
return &Service{
controllerClient: controllerClient,
}, nil
}

func (s *Service) CreateDeployment(ctx context.Context, req *connect.Request[ftlv1.CreateDeploymentRequest]) (*connect.Response[ftlv1.CreateDeploymentResponse], error) {
// TODO: provision infrastructure
response, err := s.controllerClient.CreateDeployment(ctx, req)
if err != nil {
return nil, fmt.Errorf("call to ftl-controller failed: %w", err)
}
return response, nil
}

func (s *Service) Ping(context.Context, *connect.Request[ftlv1.PingRequest]) (*connect.Response[ftlv1.PingResponse], error) {
return &connect.Response[ftlv1.PingResponse]{}, nil
}

// Start the Provisioner. Blocks until the context is cancelled.
func Start(ctx context.Context, config Config, devel bool) error {
config.SetDefaults()

logger := log.FromContext(ctx)
logger.Debugf("Starting FTL provisioner")

controllerClient := rpc.Dial(ftlv1connect.NewControllerServiceClient, config.ControllerEndpoint.String(), log.Error)

svc, err := New(ctx, config, controllerClient, devel)
if err != nil {
return err
}
logger.Debugf("Listening on %s", config.Bind)
logger.Debugf("Advertising as %s", config.Advertise)
logger.Debugf("Using FTL endpoint: %s", config.ControllerEndpoint)

g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return rpc.Serve(ctx, config.Bind,
rpc.GRPC(provisionerconnect.NewProvisionerServiceHandler, svc),
rpc.PProf(),
)
})
if err := g.Wait(); err != nil {
return fmt.Errorf("error waiting for rpc.Serve: %w", err)
}
return nil
}
45 changes: 45 additions & 0 deletions cmd/ftl-provisioner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"fmt"
"os"
"strconv"
"time"

"github.com/alecthomas/kong"

"github.com/TBD54566975/ftl"
"github.com/TBD54566975/ftl/backend/provisioner"
_ "github.com/TBD54566975/ftl/internal/automaxprocs" // Set GOMAXPROCS to match Linux container CPU quota.
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/observability"
)

var cli struct {
Version kong.VersionFlag `help:"Show version."`
ObservabilityConfig observability.Config `embed:"" prefix:"o11y-"`
LogConfig log.Config `embed:"" prefix:"log-"`
ProvisionerConfig provisioner.Config `embed:""`
ConfigFlag string `name:"config" short:"C" help:"Path to FTL project cf file." env:"FTL_CONFIG" placeholder:"FILE"`
}

func main() {
t, err := strconv.ParseInt(ftl.Timestamp, 10, 64)
if err != nil {
panic(fmt.Sprintf("invalid timestamp %q: %s", ftl.Timestamp, err))
}
kctx := kong.Parse(&cli,
kong.Description(`FTL - Towards a 𝝺-calculus for large-scale systems`),
kong.UsageOnError(),
kong.Vars{"version": ftl.Version, "timestamp": time.Unix(t, 0).Format(time.RFC3339)},
)
cli.ProvisionerConfig.SetDefaults()

ctx := log.ContextWithLogger(context.Background(), log.Configure(os.Stderr, cli.LogConfig))
err = observability.Init(ctx, false, "", "ftl-provisioner", ftl.Version, cli.ObservabilityConfig)
kctx.FatalIfErrorf(err, "failed to initialize observability")

err = provisioner.Start(ctx, cli.ProvisionerConfig, false)
kctx.FatalIfErrorf(err)
}
Loading

0 comments on commit 9003ae3

Please sign in to comment.