Skip to content

Commit

Permalink
(choria-io#1990) Add context to provtarget.Configure() and clarify pl…
Browse files Browse the repository at this point in the history
…ugin design

Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Feb 20, 2023
1 parent 1d1c140 commit ae96bf3
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 43 deletions.
4 changes: 2 additions & 2 deletions choria/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ func (fw *Framework) SupportsProvisioning() bool {
}

// ConfigureProvisioning adjusts the active configuration to match the provisioning profile
func (fw *Framework) ConfigureProvisioning() {
provtarget.Configure(fw.Config, fw.Logger("provtarget"))
func (fw *Framework) ConfigureProvisioning(ctx context.Context) {
provtarget.Configure(ctx, fw.Config, fw.Logger("provtarget"))

if !fw.ProvisionMode() {
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (b *buildinfoCommand) Configure() (err error) {
func (b *buildinfoCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

c.ConfigureProvisioning()
c.ConfigureProvisioning(ctx)

fmt.Println("Choria build settings:")
fmt.Println()
Expand Down
6 changes: 3 additions & 3 deletions cmd/server_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *serverRunCommand) Configure() error {
return fmt.Errorf("could not parse configuration: %s", err)
}

provtarget.Configure(cfg, log.WithField("component", "provtarget"))
provtarget.Configure(ctx, cfg, log.WithField("component", "provtarget"))

if r.shouldProvision(cfg) {
if cfg.Choria.ServerTokenSeedFile != "" {
Expand Down Expand Up @@ -95,7 +95,7 @@ func (r *serverRunCommand) Configure() error {
return fmt.Errorf("could not create default server configuration")
}

provtarget.Configure(cfg, log.WithField("component", "provtarget"))
provtarget.Configure(ctx, cfg, log.WithField("component", "provtarget"))

// if a config file didn't exist and prov is disabled we cant start
if !r.shouldProvision(cfg) {
Expand Down Expand Up @@ -227,7 +227,7 @@ func (r *serverRunCommand) prepareInstance() (i *server.Instance, err error) {
log.Warn("Running with TLS Verification disabled, not compatible with production use.")
}

c.ConfigureProvisioning()
c.ConfigureProvisioning(ctx)

instance, err := server.NewInstance(c)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tool_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *tProvisionerCommand) Configure() error {
func (p *tProvisionerCommand) Run(wg *sync.WaitGroup) (err error) {
defer wg.Done()

c.ConfigureProvisioning()
c.ConfigureProvisioning(ctx)

if !c.ProvisionMode() {
return fmt.Errorf("not a server compiled for auto provisioning or the provisioning target is not functional")
Expand Down
2 changes: 1 addition & 1 deletion inter/choria.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Framework interface {
Certname() string
ClientTLSConfig() (*tls.Config, error)
Colorize(c string, format string, a ...any) string
ConfigureProvisioning()
ConfigureProvisioning(ctx context.Context)
DDLResolvers() ([]DDLResolver, error)
DisableTLSVerify() bool
Enroll(ctx context.Context, wait time.Duration, cb func(digest string, try int)) error
Expand Down
8 changes: 4 additions & 4 deletions inter/imocks/framework.go

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

2 changes: 1 addition & 1 deletion providers/provtarget/builddefaults/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (b *Resolver) Name() string {
}

// Configure overrides build settings using the contents of the JWT
func (b *Resolver) Configure(cfg *config.Config, log *logrus.Entry) {
func (b *Resolver) Configure(ctx context.Context, cfg *config.Config, log *logrus.Entry) {
jwtf := b.bi.ProvisionJWTFile()
if jwtf == "" {
return
Expand Down
2 changes: 1 addition & 1 deletion providers/provtarget/builddefaults/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestDefault(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Provtarget/Default")
RunSpecs(t, "Providers/Provtarget/Default")
}

var _ = Describe("Default", func() {
Expand Down
16 changes: 12 additions & 4 deletions providers/provtarget/provtarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ type TargetResolver interface {
Targets(context.Context, *logrus.Entry) []string

// Configure will be called during server configuration and can be used to configure the target or adjust build settings or configuration
// this will always be called even when not in provisioning mode, one can use this to programatically set a provisioner token for example
Configure(*config.Config, *logrus.Entry)
// this will always be called even when not in provisioning mode, one can use this to programmatically set a provisioner token for example
//
// The intention of this function is that all the settings needed by provisioning (all the things in build) should be set during configure
// stage. Later when Targets() are called the intention is that either the configured targets are returned verbatim or if for example the
// plugin queries something like SRV records those queries are done there.
//
// Today Configure() is expected to set the JWT file using bi.SetProvisionJWTFile() and that the file should exist before probisioning will
// happen, this will be revisited in future. See the shouldProvision() function in server_run.go for current logic that would trigger a
// server into provisioning.
Configure(context.Context, *config.Config, *logrus.Entry)
}

var mu = &sync.Mutex{}
Expand All @@ -43,15 +51,15 @@ func RegisterTargetResolver(r TargetResolver) error {
}

// Configure allows the resolver to adjust configuration
func Configure(cfg *config.Config, log *logrus.Entry) {
func Configure(ctx context.Context, cfg *config.Config, log *logrus.Entry) {
mu.Lock()
defer mu.Unlock()

if resolver == nil {
return
}

resolver.Configure(cfg, log)
resolver.Configure(ctx, cfg, log)
}

// Targets is a list of brokers to connect to
Expand Down
2 changes: 1 addition & 1 deletion providers/provtarget/provtarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func TestServer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Provtarget")
RunSpecs(t, "Providers/Provtarget")
}

var _ = Describe("Provision", func() {
Expand Down
56 changes: 32 additions & 24 deletions providers/provtarget/target_resolver_mock_test.go

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

0 comments on commit ae96bf3

Please sign in to comment.