Improve DATABRICKS_CLUSTER_ID handling for non-development modes

This commit is contained in:
Lennart Kats 2023-07-07 09:38:30 +02:00
parent 36a6e9348b
commit be2dd4f769
3 changed files with 97 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package mutator
import (
"context"
"fmt"
"os"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
@ -32,11 +33,17 @@ func overrideJobCompute(j *resources.Job, compute string) {
}
func (m *overrideCompute) Apply(ctx context.Context, b *bundle.Bundle) error {
if b.Config.Bundle.Compute == "" {
if b.Config.Bundle.Mode != config.Development {
if b.Config.Bundle.Compute != "" {
return fmt.Errorf("cannot override compute for an environment that does not use 'mode: development'")
}
return nil
}
if b.Config.Bundle.Mode != config.Development {
return fmt.Errorf("cannot override compute for an environment that does not use 'mode: debug'")
if os.Getenv("DATABRICKS_CLUSTER_ID") != "" {
b.Config.Bundle.Compute = os.Getenv("DATABRICKS_CLUSTER_ID")
}
if b.Config.Bundle.Compute == "" {
return nil
}
r := b.Config.Resources

View File

@ -2,6 +2,7 @@ package mutator_test
import (
"context"
"os"
"testing"
"github.com/databricks/cli/bundle"
@ -14,7 +15,8 @@ import (
"github.com/stretchr/testify/require"
)
func TestOverrideCompute(t *testing.T) {
func TestOverrideDevelopment(t *testing.T) {
os.Setenv("DATABRICKS_CLUSTER_ID", "")
bundle := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
@ -46,3 +48,87 @@ func TestOverrideCompute(t *testing.T) {
assert.Equal(t, "newClusterID", bundle.Config.Resources.Jobs["job1"].Tasks[0].ExistingClusterId)
assert.Equal(t, "newClusterID", bundle.Config.Resources.Jobs["job1"].Tasks[1].ExistingClusterId)
}
func TestOverrideDevelopmentEnv(t *testing.T) {
os.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
bundle := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job1": {JobSettings: &jobs.JobSettings{
Name: "job1",
Tasks: []jobs.Task{
{
NewCluster: &compute.ClusterSpec{},
},
{
ExistingClusterId: "cluster2",
},
},
}},
},
},
},
}
m := mutator.OverrideCompute()
err := m.Apply(context.Background(), bundle)
require.NoError(t, err)
assert.Equal(t, "cluster2", bundle.Config.Resources.Jobs["job1"].Tasks[1].ExistingClusterId)
}
func TestOverrideProduction(t *testing.T) {
bundle := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Compute: "newClusterID",
},
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job1": {JobSettings: &jobs.JobSettings{
Name: "job1",
Tasks: []jobs.Task{
{
NewCluster: &compute.ClusterSpec{},
},
{
ExistingClusterId: "cluster2",
},
},
}},
},
},
},
}
m := mutator.OverrideCompute()
err := m.Apply(context.Background(), bundle)
require.Error(t, err)
}
func TestOverrideProductionEnv(t *testing.T) {
os.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
bundle := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job1": {JobSettings: &jobs.JobSettings{
Name: "job1",
Tasks: []jobs.Task{
{
NewCluster: &compute.ClusterSpec{},
},
{
ExistingClusterId: "cluster2",
},
},
}},
},
},
},
}
m := mutator.OverrideCompute()
err := m.Apply(context.Background(), bundle)
require.NoError(t, err)
}

View File

@ -1,8 +1,6 @@
package bundle
import (
"os"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/phases"
"github.com/spf13/cobra"
@ -21,10 +19,6 @@ var deployCmd = &cobra.Command{
}
func deploy(cmd *cobra.Command, b *bundle.Bundle) error {
if computeID == "" {
computeID = os.Getenv("DATABRICKS_CLUSTER_ID")
}
// If `--force` is specified, force acquisition of the deployment lock.
b.Config.Bundle.Lock.Force = forceDeploy
b.Config.Bundle.Compute = computeID