Skip to content

Commit

Permalink
[chore] Use the factory to get the config (#14975)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier authored Oct 28, 2024
1 parent 2655869 commit dabb922
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
20 changes: 20 additions & 0 deletions core/services/job/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ type WorkflowSpec struct {
SpecType WorkflowSpecType `toml:"spec_type" db:"spec_type"`
sdkWorkflow *sdk.WorkflowSpec
rawSpec []byte
config []byte
}

var (
Expand Down Expand Up @@ -949,6 +950,25 @@ func (w *WorkflowSpec) RawSpec(ctx context.Context) ([]byte, error) {
return rs, nil
}

func (w *WorkflowSpec) GetConfig(ctx context.Context) ([]byte, error) {
if w.config != nil {
return w.config, nil
}

workflowSpecFactory, ok := workflowSpecFactories[w.SpecType]
if !ok {
return nil, fmt.Errorf("unknown spec type %s", w.SpecType)
}

rs, err := workflowSpecFactory.Config(ctx, w.Config)
if err != nil {
return nil, err
}

w.config = rs
return rs, nil
}

type OracleFactoryConfig struct {
Enabled bool `toml:"enabled"`
BootstrapPeers []string `toml:"bootstrap_peers"` // e.g.,["12D3KooWEBVwbfdhKnicois7FTYVsBFGFcoMhMCKXQC57BQyZMhz@localhost:6690"]
Expand Down
15 changes: 12 additions & 3 deletions core/services/job/wasm_file_spec_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type WasmFileSpecFactory struct{}

func (w WasmFileSpecFactory) Spec(ctx context.Context, workflow, configLocation string) (sdk.WorkflowSpec, []byte, string, error) {
config, err := os.ReadFile(configLocation)
config, err := w.Config(ctx, configLocation)
if err != nil {
return sdk.WorkflowSpec{}, nil, "", err
}
Expand All @@ -43,8 +43,8 @@ func (w WasmFileSpecFactory) Spec(ctx context.Context, workflow, configLocation
return *spec, compressedBinary, sha, nil
}

func (w WasmFileSpecFactory) RawSpec(_ context.Context, workflow, configLocation string) ([]byte, error) {
config, err := os.ReadFile(configLocation)
func (w WasmFileSpecFactory) RawSpec(ctx context.Context, workflow, configLocation string) ([]byte, error) {
config, err := w.Config(ctx, configLocation)
if err != nil {
return nil, err
}
Expand All @@ -53,6 +53,15 @@ func (w WasmFileSpecFactory) RawSpec(_ context.Context, workflow, configLocation
return raw, err
}

func (w WasmFileSpecFactory) Config(_ context.Context, configLocation string) ([]byte, error) {
config, err := os.ReadFile(configLocation)
if err != nil {
return nil, err
}

return config, nil
}

// rawSpecAndSha returns the brotli compressed version of the raw wasm file, alongside the sha256 hash of the raw wasm file
func (w WasmFileSpecFactory) rawSpecAndSha(wf string, config []byte) ([]byte, string, error) {
read, err := os.ReadFile(wf)
Expand Down
8 changes: 8 additions & 0 deletions core/services/job/wasm_file_spec_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func TestWasmFileSpecFactory(t *testing.T) {

assert.Equal(t, b.Bytes(), rawSpec)
})

t.Run("Config", func(t *testing.T) {
factory := job.WasmFileSpecFactory{}
actual, err3 := factory.Config(testutils.Context(t), configLocation)
require.NoError(t, err3)

assert.Equal(t, config, actual)
})
}

func createTestBinary(t *testing.T) string {
Expand Down
1 change: 1 addition & 0 deletions core/services/job/workflow_spec_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type WorkflowSpecFactory interface {
Spec(ctx context.Context, workflow, config string) (sdk.WorkflowSpec, []byte, string, error)
RawSpec(ctx context.Context, workflow, config string) ([]byte, error)
Config(ctx context.Context, config string) ([]byte, error)
}

var workflowSpecFactories = map[WorkflowSpecType]WorkflowSpecFactory{
Expand Down
4 changes: 4 additions & 0 deletions core/services/job/yaml_spec_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func (y YAMLSpecFactory) Spec(_ context.Context, workflow, _ string) (sdk.Workfl
func (y YAMLSpecFactory) RawSpec(_ context.Context, workflow, _ string) ([]byte, error) {
return []byte(workflow), nil
}

func (y YAMLSpecFactory) Config(_ context.Context, config string) ([]byte, error) {
return []byte(config), nil
}
9 changes: 9 additions & 0 deletions core/services/job/yaml_spec_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,12 @@ func TestYamlSpecFactory_GetSpec(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%x", sha256.Sum256([]byte(anyYamlSpec))), actualSha)
assert.Equal(t, anyYamlSpec, string(raw))
}

func TestYamlSpecFactory_Config(t *testing.T) {
t.Parallel()

config := "config"
actual, err := job.YAMLSpecFactory{}.Config(testutils.Context(t), config)
require.NoError(t, err)
assert.Equal(t, []byte(config), actual)
}
7 changes: 6 additions & 1 deletion core/services/workflows/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, spec job.Job) ([]job.Ser
return nil, err
}

config, err := spec.WorkflowSpec.GetConfig(ctx)
if err != nil {
return nil, err
}

cfg := Config{
Lggr: d.logger,
Workflow: sdkSpec,
Expand All @@ -55,7 +60,7 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, spec job.Job) ([]job.Ser
WorkflowName: spec.WorkflowSpec.WorkflowName,
Registry: d.registry,
Store: d.store,
Config: []byte(spec.WorkflowSpec.Config),
Config: config,
Binary: binary,
SecretsFetcher: d.secretsFetcher,
}
Expand Down

0 comments on commit dabb922

Please sign in to comment.