Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
deniseli committed Jun 7, 2024
1 parent 50aecf8 commit 4403d1b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
)

type CmdClient interface {
// Client standardizes an common interface between the AdminService as accessed via gRPC
// and a purely-local variant that doesn't require a running controller to access.
type Client interface {
Ping(ctx context.Context, req *connect.Request[ftlv1.PingRequest]) (*connect.Response[ftlv1.PingResponse], error)

// List configuration.
Expand Down Expand Up @@ -39,16 +41,20 @@ type CmdClient interface {
SecretUnset(ctx context.Context, req *connect.Request[ftlv1.UnsetSecretRequest]) (*connect.Response[ftlv1.UnsetSecretResponse], error)
}

// NewCmdClient takes the service client and endpoint flag received by the cmd interface
// NewClient takes the service client and endpoint flag received by the cmd interface
// and returns an appropriate interface for the cmd library to use.
//
// If the controller is not present AND endpoint is local, then inject a purely-local
// implementation of the interface so that the user does not need to spin up a controller
// just to run the `ftl config/secret` commands. Otherwise, return back the gRPC client.
func NewCmdClient(ctx context.Context, adminClient ftlv1connect.AdminServiceClient, endpoint *url.URL) (CmdClient, error) {
_, err := adminClient.Ping(ctx, connect.NewRequest(&ftlv1.PingRequest{}))
if isConnectUnavailableError(err) && isEndpointLocal(endpoint) {
return newLocalCmdClient(ctx), nil
func NewClient(ctx context.Context, adminClient ftlv1connect.AdminServiceClient, endpoint *url.URL) (Client, error) {
isLocal, err := isEndpointLocal(endpoint)
if err != nil {
return adminClient, err
}
_, err = adminClient.Ping(ctx, connect.NewRequest(&ftlv1.PingRequest{}))
if isConnectUnavailableError(err) && isLocal {
return newLocalClient(ctx), nil
}
return adminClient, nil
}
Expand All @@ -61,16 +67,16 @@ func isConnectUnavailableError(err error) bool {
return false
}

func isEndpointLocal(endpoint *url.URL) bool {
func isEndpointLocal(endpoint *url.URL) (bool, error) {
h := endpoint.Hostname()
ips, err := net.LookupIP(h)
if err != nil {
panic(err.Error())
return false, err
}
for _, netip := range ips {
if netip.IsLoopback() {
return true
return true, nil
}
}
return false
return false, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func TestIsEndpointLocal(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
u, err := url.Parse(test.Endpoint)
assert.NoError(t, err)
assert.Equal(t, isEndpointLocal(u), test.Want)
got, err := isEndpointLocal(u)
assert.NoError(t, err)
assert.Equal(t, got, test.Want)
})
}
}
20 changes: 20 additions & 0 deletions backend/controller/admin/local_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package admin

import (
"context"

"github.com/TBD54566975/ftl/common/configuration"
)

// localClient reads and writes to local projectconfig files without making any network
// calls. It allows us to interface with local ftl-project.toml files without needing to
// start a controller.
type localClient struct {
*AdminService
}

func newLocalClient(ctx context.Context) *localClient {
cm := configuration.ConfigFromContext(ctx)
sm := configuration.SecretsFromContext(ctx)
return &localClient{NewAdminService(cm, sm)}
}
59 changes: 0 additions & 59 deletions backend/controller/admin/local_cmd_client.go

This file was deleted.

8 changes: 4 additions & 4 deletions cmd/ftl/cmd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type configListCmd struct {
Module string `optional:"" arg:"" placeholder:"MODULE" help:"List configuration only in this module."`
}

func (s *configListCmd) Run(ctx context.Context, adminClient admin.CmdClient) error {
func (s *configListCmd) Run(ctx context.Context, adminClient admin.Client) error {
resp, err := adminClient.ConfigList(ctx, connect.NewRequest(&ftlv1.ListConfigRequest{
Module: &s.Module,
IncludeValues: &s.Values,
Expand Down Expand Up @@ -84,7 +84,7 @@ Returns a JSON-encoded configuration value.
`
}

func (s *configGetCmd) Run(ctx context.Context, adminClient admin.CmdClient) error {
func (s *configGetCmd) Run(ctx context.Context, adminClient admin.Client) error {
resp, err := adminClient.ConfigGet(ctx, connect.NewRequest(&ftlv1.GetConfigRequest{
Ref: configRefFromRef(s.Ref),
}))
Expand All @@ -107,7 +107,7 @@ type configSetCmd struct {
Value *string `arg:"" placeholder:"VALUE" help:"Configuration value (read from stdin if omitted)." optional:""`
}

func (s *configSetCmd) Run(ctx context.Context, scmd *configCmd, adminClient admin.CmdClient) error {
func (s *configSetCmd) Run(ctx context.Context, scmd *configCmd, adminClient admin.Client) error {
var err error
var config []byte
if s.Value != nil {
Expand Down Expand Up @@ -146,7 +146,7 @@ type configUnsetCmd struct {
Ref cf.Ref `arg:"" help:"Configuration reference in the form [<module>.]<name>."`
}

func (s *configUnsetCmd) Run(ctx context.Context, scmd *configCmd, adminClient admin.CmdClient) error {
func (s *configUnsetCmd) Run(ctx context.Context, scmd *configCmd, adminClient admin.Client) error {
req := &ftlv1.UnsetConfigRequest{
Ref: configRefFromRef(s.Ref),
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/ftl/cmd_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type secretListCmd struct {
Module string `optional:"" arg:"" placeholder:"MODULE" help:"List secrets only in this module."`
}

func (s *secretListCmd) Run(ctx context.Context, adminClient admin.CmdClient) error {
func (s *secretListCmd) Run(ctx context.Context, adminClient admin.Client) error {
resp, err := adminClient.SecretsList(ctx, connect.NewRequest(&ftlv1.ListSecretsRequest{
Module: &s.Module,
IncludeValues: &s.Values,
Expand Down Expand Up @@ -87,7 +87,7 @@ Returns a JSON-encoded secret value.
`
}

func (s *secretGetCmd) Run(ctx context.Context, adminClient admin.CmdClient) error {
func (s *secretGetCmd) Run(ctx context.Context, adminClient admin.Client) error {
resp, err := adminClient.SecretGet(ctx, connect.NewRequest(&ftlv1.GetSecretRequest{
Ref: configRefFromRef(s.Ref),
}))
Expand All @@ -108,7 +108,7 @@ type secretSetCmd struct {
Ref cf.Ref `arg:"" help:"Secret reference in the form [<module>.]<name>."`
}

func (s *secretSetCmd) Run(ctx context.Context, scmd *secretCmd, adminClient admin.CmdClient) error {
func (s *secretSetCmd) Run(ctx context.Context, scmd *secretCmd, adminClient admin.Client) error {
// Prompt for a secret if stdin is a terminal, otherwise read from stdin.
var err error
var secret []byte
Expand Down Expand Up @@ -153,7 +153,7 @@ type secretUnsetCmd struct {
Ref cf.Ref `arg:"" help:"Secret reference in the form [<module>.]<name>."`
}

func (s *secretUnsetCmd) Run(ctx context.Context, scmd *secretCmd, adminClient admin.CmdClient) error {
func (s *secretUnsetCmd) Run(ctx context.Context, scmd *secretCmd, adminClient admin.Client) error {
req := &ftlv1.UnsetSecretRequest{
Ref: configRefFromRef(s.Ref),
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/ftl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ func main() {

adminServiceClient := rpc.Dial(ftlv1connect.NewAdminServiceClient, cli.Endpoint.String(), log.Error)
ctx = rpc.ContextWithClient(ctx, adminServiceClient)
adminClient, err := admin.NewCmdClient(ctx, adminServiceClient, cli.Endpoint)
kctx.BindTo(adminClient, (*admin.CmdClient)(nil))
adminClient, err := admin.NewClient(ctx, adminServiceClient, cli.Endpoint)
kctx.FatalIfErrorf(err)
kctx.BindTo(adminClient, (*admin.Client)(nil))

controllerServiceClient := rpc.Dial(ftlv1connect.NewControllerServiceClient, cli.Endpoint.String(), log.Error)
ctx = rpc.ContextWithClient(ctx, controllerServiceClient)
Expand Down

0 comments on commit 4403d1b

Please sign in to comment.