Skip to content

Commit

Permalink
Disable log from process_target_mode to allow configurability
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartkats-db committed Mar 29, 2024
1 parent b7e43af commit 5221b31
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bundle/config/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ type Deployment struct {
FailOnActiveRuns bool `json:"fail_on_active_runs,omitempty"`

// Lock configures locking behavior on deployment.
Lock Lock `json:"lock" bundle:"readonly"`
Lock Lock `json:"lock"`
}
13 changes: 12 additions & 1 deletion bundle/config/lock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

type Lock struct {
// Enabled toggles deployment lock. True by default.
// Enabled toggles deployment lock. True by default except in development mode.
// Use a pointer value so that only explicitly configured values are set
// and we don't merge configuration with zero-initialized values.
Enabled *bool `json:"enabled,omitempty"`
Expand All @@ -11,9 +11,20 @@ type Lock struct {
Force bool `json:"force,omitempty"`
}

// IsEnabled checks if the deployment lock is enabled.
func (lock Lock) IsEnabled() bool {
if lock.Enabled != nil {
return *lock.Enabled
}
return true
}

// IsAlwaysEnabled checks if the deployment lock is explicitly enabled.
// Only returns true if locking is explicitly set using a command-line
// flag or configuration file.
func (lock Lock) IsExplicitlyEnabled() bool {
if lock.Enabled != nil {
return *lock.Enabled
}
return false
}
19 changes: 18 additions & 1 deletion bundle/config/mutator/process_target_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/auth"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/log"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/databricks/databricks-sdk-go/service/ml"
Expand All @@ -29,12 +30,20 @@ func (m *processTargetMode) Name() string {
// Mark all resources as being for 'development' purposes, i.e.
// changing their their name, adding tags, and (in the future)
// marking them as 'hidden' in the UI.
func transformDevelopmentMode(b *bundle.Bundle) error {
func transformDevelopmentMode(ctx context.Context, b *bundle.Bundle) error {
r := b.Config.Resources

shortName := b.Config.Workspace.CurrentUser.ShortName
prefix := "[dev " + shortName + "] "

if !b.Config.Bundle.Deployment.Lock.IsExplicitlyEnabled() {
log.Infof(ctx, "Development mode: disabling deployment lock since bundle.deployment.lock.enabled is not set to true")
err := disableDeploymentLock(b)
if err != nil {
return err
}
}

// Generate a normalized version of the short name that can be used as a tag value.
tagValue := b.Tagging.NormalizeValue(shortName)

Expand Down Expand Up @@ -100,6 +109,14 @@ func transformDevelopmentMode(b *bundle.Bundle) error {
return nil
}

func disableDeploymentLock(b *bundle.Bundle) error {
return b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
return dyn.Map(v, "bundle.deployment.lock", func(_ dyn.Path, v dyn.Value) (dyn.Value, error) {
return dyn.Set(v, "enabled", dyn.V(false))
})
})
}

func validateDevelopmentMode(b *bundle.Bundle) error {
if path := findNonUserPath(b); path != "" {
return fmt.Errorf("%s must start with '~/' or contain the current username when using 'mode: development'", path)
Expand Down
20 changes: 20 additions & 0 deletions bundle/config/mutator/process_target_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,23 @@ func TestAllResourcesRenamed(t *testing.T) {
}
}
}

func TestDisableLocking(t *testing.T) {
ctx := context.Background()
b := mockBundle(config.Development)

err := transformDevelopmentMode(ctx, b)
require.NoError(t, err)
assert.False(t, b.Config.Bundle.Deployment.Lock.IsEnabled())
}

func TestDisableLockingDisabled(t *testing.T) {
ctx := context.Background()
b := mockBundle(config.Development)
explicitlyEnabled := true
b.Config.Bundle.Deployment.Lock.Enabled = &explicitlyEnabled

err := transformDevelopmentMode(ctx, b)
require.NoError(t, err)
assert.True(t, b.Config.Bundle.Deployment.Lock.IsEnabled(), "Deployment lock should remain enabled in development mode when explicitly enabled")
}
12 changes: 12 additions & 0 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ func (r *Root) updateWithDynamicValue(nv dyn.Value) error {
return nil
}

// Mutate applies a transformation to the dynamic configuration value of a Root object.
//
// Parameters:
// - fn: A function that mutates a dyn.Value object
//
// Example usage, setting bundle.deployment.lock.enabled to false:
//
// err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
// return dyn.Map(v, "bundle.deployment.lock", func(_ dyn.Path, v dyn.Value) (dyn.Value, error) {
// return dyn.Set(v, "enabled", dyn.V(false))
// })
// })
func (r *Root) Mutate(fn func(dyn.Value) (dyn.Value, error)) error {
err := r.initializeDynamicValue()
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions bundle/deploy/lock/acquire.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/locker"
"github.com/databricks/cli/libs/log"
Expand Down Expand Up @@ -40,10 +39,6 @@ func (m *acquire) Apply(ctx context.Context, b *bundle.Bundle) error {
log.Infof(ctx, "Skipping; locking is disabled")
return nil
}
if b.Config.Bundle.Mode == config.Development {
log.Infof(ctx, "Skipping; locking is disabled in development mode")
return nil
}

err := m.init(b)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions bundle/deploy/lock/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/locker"
"github.com/databricks/cli/libs/log"
)
Expand Down Expand Up @@ -37,10 +36,6 @@ func (m *release) Apply(ctx context.Context, b *bundle.Bundle) error {
log.Infof(ctx, "Skipping; locking is disabled")
return nil
}
if b.Config.Bundle.Mode == config.Development {
log.Infof(ctx, "Skipping; locking is disabled in development mode")
return nil
}

// Return early if the locker is not set.
// It is likely an error occurred prior to initialization of the locker instance.
Expand Down

0 comments on commit 5221b31

Please sign in to comment.