2022-11-28 09:59:43 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-22 14:54:10 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2022-11-28 09:59:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Mutator is the interface type that mutates a bundle's configuration or internal state.
|
|
|
|
// This makes every mutation or action observable and debuggable.
|
|
|
|
type Mutator interface {
|
|
|
|
// Name returns the mutators name.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Apply mutates the specified bundle object.
|
2023-05-24 12:45:19 +00:00
|
|
|
Apply(context.Context, *Bundle) error
|
2022-11-28 09:59:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func Apply(ctx context.Context, b *Bundle, m Mutator) error {
|
2023-03-22 14:54:10 +00:00
|
|
|
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("mutator", m.Name()))
|
|
|
|
|
|
|
|
log.Debugf(ctx, "Apply")
|
Use dynamic configuration model in bundles (#1098)
## Changes
This is a fundamental change to how we load and process bundle
configuration. We now depend on the configuration being represented as a
`dyn.Value`. This representation is functionally equivalent to Go's
`any` (it is variadic) and allows us to capture metadata associated with
a value, such as where it was defined (e.g. file, line, and column). It
also allows us to represent Go's zero values properly (e.g. empty
string, integer equal to 0, or boolean false).
Using this representation allows us to let the configuration model
deviate from the typed structure we have been relying on so far
(`config.Root`). We need to deviate from these types when using
variables for fields that are not a string themselves. For example,
using `${var.num_workers}` for an integer `workers` field was impossible
until now (though not implemented in this change).
The loader for a `dyn.Value` includes functionality to capture any and
all type mismatches between the user-defined configuration and the
expected types. These mismatches can be surfaced as validation errors in
future PRs.
Given that many mutators expect the typed struct to be the source of
truth, this change converts between the dynamic representation and the
typed representation on mutator entry and exit. Existing mutators can
continue to modify the typed representation and these modifications are
reflected in the dynamic representation (see `MarkMutatorEntry` and
`MarkMutatorExit` in `bundle/config/root.go`).
Required changes included in this change:
* The existing interpolation package is removed in favor of
`libs/dyn/dynvar`.
* Functionality to merge job clusters, job tasks, and pipeline clusters
are now all broken out into their own mutators.
To be implemented later:
* Allow variable references for non-string types.
* Surface diagnostics about the configuration provided by the user in
the validation output.
* Some mutators use a resource's configuration file path to resolve
related relative paths. These depend on `bundle/config/paths.Path` being
set and populated through `ConfigureConfigFilePath`. Instead, they
should interact with the dynamically typed configuration directly. Doing
this also unlocks being able to differentiate different base paths used
within a job (e.g. a task override with a relative path defined in a
directory other than the base job).
## Tests
* Existing unit tests pass (some have been modified to accommodate)
* Integration tests pass
2024-02-16 19:41:58 +00:00
|
|
|
|
|
|
|
err := b.Config.MarkMutatorEntry(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "entry error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := b.Config.MarkMutatorExit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "exit error: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = m.Apply(ctx, b)
|
2023-03-22 14:54:10 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "Error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
Use dynamic configuration model in bundles (#1098)
## Changes
This is a fundamental change to how we load and process bundle
configuration. We now depend on the configuration being represented as a
`dyn.Value`. This representation is functionally equivalent to Go's
`any` (it is variadic) and allows us to capture metadata associated with
a value, such as where it was defined (e.g. file, line, and column). It
also allows us to represent Go's zero values properly (e.g. empty
string, integer equal to 0, or boolean false).
Using this representation allows us to let the configuration model
deviate from the typed structure we have been relying on so far
(`config.Root`). We need to deviate from these types when using
variables for fields that are not a string themselves. For example,
using `${var.num_workers}` for an integer `workers` field was impossible
until now (though not implemented in this change).
The loader for a `dyn.Value` includes functionality to capture any and
all type mismatches between the user-defined configuration and the
expected types. These mismatches can be surfaced as validation errors in
future PRs.
Given that many mutators expect the typed struct to be the source of
truth, this change converts between the dynamic representation and the
typed representation on mutator entry and exit. Existing mutators can
continue to modify the typed representation and these modifications are
reflected in the dynamic representation (see `MarkMutatorEntry` and
`MarkMutatorExit` in `bundle/config/root.go`).
Required changes included in this change:
* The existing interpolation package is removed in favor of
`libs/dyn/dynvar`.
* Functionality to merge job clusters, job tasks, and pipeline clusters
are now all broken out into their own mutators.
To be implemented later:
* Allow variable references for non-string types.
* Surface diagnostics about the configuration provided by the user in
the validation output.
* Some mutators use a resource's configuration file path to resolve
related relative paths. These depend on `bundle/config/paths.Path` being
set and populated through `ConfigureConfigFilePath`. Instead, they
should interact with the dynamically typed configuration directly. Doing
this also unlocks being able to differentiate different base paths used
within a job (e.g. a task override with a relative path defined in a
directory other than the base job).
## Tests
* Existing unit tests pass (some have been modified to accommodate)
* Integration tests pass
2024-02-16 19:41:58 +00:00
|
|
|
|
|
|
|
type funcMutator struct {
|
|
|
|
fn func(context.Context, *Bundle) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m funcMutator) Name() string {
|
|
|
|
return "<func>"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m funcMutator) Apply(ctx context.Context, b *Bundle) error {
|
|
|
|
return m.fn(ctx, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyFunc applies an inline-specified function mutator.
|
|
|
|
func ApplyFunc(ctx context.Context, b *Bundle, fn func(context.Context, *Bundle) error) error {
|
|
|
|
return Apply(ctx, b, funcMutator{fn})
|
|
|
|
}
|