Skip to content

Commit

Permalink
fix: config/secrets weren't propagating to runners (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored Mar 14, 2024
1 parent 456d2fe commit 47818c8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
8 changes: 5 additions & 3 deletions cmd/ftl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ func main() {

// Set some envars for child processes.
os.Setenv("LOG_LEVEL", cli.LogConfig.Level.String())
if len(cli.ConfigFlag) > 0 {
os.Setenv("FTL_CONFIG", strings.Join(cli.ConfigFlag, ","))
}

ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -86,6 +83,11 @@ func main() {
kctx.BindTo(sr, (*cf.Resolver[cf.Secrets])(nil))
kctx.BindTo(cr, (*cf.Resolver[cf.Configuration])(nil))

// Propagate to runner processes.
// TODO: This is a bit of a hack until we get proper configuration
// management through the Controller.
os.Setenv("FTL_CONFIG", strings.Join(cr.ConfigPaths(), ","))

// Handle signals.
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
Expand Down
14 changes: 6 additions & 8 deletions common/configuration/projectconfig_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
//
// See the [projectconfig] package for details on the configuration file format.
type ProjectConfigResolver[R Role] struct {
Config []string `help:"Path to project configuration file." placeholder:"FILE" type:"existingfile" env:"FTL_CONFIG"`
Config []string `name:"config" short:"C" help:"Paths to FTL project configuration files." env:"FTL_CONFIG" placeholder:"FILE[,FILE,...]" type:"existingfile"`
}

var _ Resolver[Configuration] = ProjectConfigResolver[Configuration]{}
Expand Down Expand Up @@ -101,7 +101,8 @@ func (p ProjectConfigResolver[From]) Unset(ctx context.Context, ref Ref) error {
return p.setMapping(config, ref.Module, mapping)
}

func (p ProjectConfigResolver[R]) configPaths() []string {
// ConfigPaths returns the computed list of configuration paths to load.
func (p ProjectConfigResolver[R]) ConfigPaths() []string {
if len(p.Config) > 0 {
return p.Config
}
Expand All @@ -114,7 +115,7 @@ func (p ProjectConfigResolver[R]) configPaths() []string {
}

func (p ProjectConfigResolver[R]) loadWritableConfig(ctx context.Context) (pc.Config, error) {
configPaths := p.configPaths()
configPaths := p.ConfigPaths()
if len(configPaths) == 0 {
return pc.Config{}, nil
}
Expand All @@ -125,7 +126,7 @@ func (p ProjectConfigResolver[R]) loadWritableConfig(ctx context.Context) (pc.Co

func (p ProjectConfigResolver[R]) loadConfig(ctx context.Context) (pc.Config, error) {
logger := log.FromContext(ctx)
configPaths := p.configPaths()
configPaths := p.ConfigPaths()
logger.Tracef("Loading config from %s", strings.Join(configPaths, " "))
config, err := pc.Merge(configPaths...)
if err != nil {
Expand Down Expand Up @@ -156,9 +157,6 @@ func (p ProjectConfigResolver[R]) getMapping(config pc.Config, module optional.O
} else {
mapping = get(config.Global)
}
if mapping == nil {
return map[string]*pc.URL{}, nil
}
return mapping, nil
}

Expand All @@ -183,6 +181,6 @@ func (p ProjectConfigResolver[R]) setMapping(config pc.Config, module optional.O
} else {
set(&config.Global, mapping)
}
configPaths := p.configPaths()
configPaths := p.ConfigPaths()
return pc.Save(configPaths[len(configPaths)-1], config)
}
5 changes: 4 additions & 1 deletion go-runtime/ftl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type ConfigValue[T ConfigType] struct {
name string
}

func (c ConfigValue[T]) String() string { return fmt.Sprintf("config \"%s.%s\"", c.module, c.name) }

func (c ConfigValue[T]) GoString() string {
var t T
return fmt.Sprintf("ftl.ConfigValue[%T](\"%s.%s\")", t, c.module, c.name)
Expand All @@ -32,7 +34,8 @@ func (c ConfigValue[T]) GoString() string {
// Get returns the value of the configuration key from FTL.
func (c ConfigValue[T]) Get(ctx context.Context) (out T) {
cm := configuration.ConfigFromContext(ctx)
err := cm.Get(ctx, configuration.NewRef(c.module, c.name), &out)
ref := configuration.NewRef(c.module, c.name)
err := cm.Get(ctx, ref, &out)
if err != nil {
panic(fmt.Errorf("failed to get %s: %w", c, err))
}
Expand Down
1 change: 1 addition & 0 deletions go-runtime/ftl/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func TestConfig(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())
cr := configuration.ProjectConfigResolver[configuration.Configuration]{Config: []string{"testdata/ftl-project.toml"}}
assert.Equal(t, []string{"testdata/ftl-project.toml"}, cr.ConfigPaths())
cm, err := configuration.NewConfigurationManager(ctx, cr)
assert.NoError(t, err)
ctx = configuration.ContextWithConfig(ctx, cm)
Expand Down
2 changes: 2 additions & 0 deletions go-runtime/ftl/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type SecretValue[T SecretType] struct {
name string
}

func (s SecretValue[T]) String() string { return fmt.Sprintf("secret \"%s.%s\"", s.module, s.name) }

func (s SecretValue[T]) GoString() string {
var t T
return fmt.Sprintf("ftl.SecretValue[%T](\"%s.%s\")", t, s.module, s.name)
Expand Down

0 comments on commit 47818c8

Please sign in to comment.