2023-07-12 06:51:54 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/resources"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/libs/env"
|
2023-07-12 06:51:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type overrideCompute struct{}
|
|
|
|
|
|
|
|
func OverrideCompute() bundle.Mutator {
|
|
|
|
return &overrideCompute{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *overrideCompute) Name() string {
|
|
|
|
return "OverrideCompute"
|
|
|
|
}
|
|
|
|
|
|
|
|
func overrideJobCompute(j *resources.Job, compute string) {
|
|
|
|
for i := range j.Tasks {
|
2024-04-12 09:53:29 +00:00
|
|
|
var task = &j.Tasks[i]
|
|
|
|
|
|
|
|
if task.ForEachTask != nil {
|
|
|
|
task = &task.ForEachTask.Task
|
|
|
|
}
|
|
|
|
|
2024-04-16 12:03:21 +00:00
|
|
|
if task.NewCluster != nil || task.ExistingClusterId != "" || task.EnvironmentKey != "" || task.JobClusterKey != "" {
|
2023-07-12 06:51:54 +00:00
|
|
|
task.NewCluster = nil
|
2023-08-28 07:51:35 +00:00
|
|
|
task.JobClusterKey = ""
|
2024-04-16 12:03:21 +00:00
|
|
|
task.EnvironmentKey = ""
|
2023-07-12 06:51:54 +00:00
|
|
|
task.ExistingClusterId = compute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *overrideCompute) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-11-13 08:43:31 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
|
|
|
|
if b.Config.Bundle.Mode == config.Production {
|
2024-09-23 10:42:34 +00:00
|
|
|
if b.Config.Bundle.ClusterId != "" {
|
2024-11-13 08:43:31 +00:00
|
|
|
diags = diags.Extend(diag.Warningf("overriding compute for a target that uses 'mode: production' is not recommended"))
|
|
|
|
}
|
|
|
|
if env.Get(ctx, "DATABRICKS_CLUSTER_ID") != "" {
|
|
|
|
// The DATABRICKS_CLUSTER_ID may be set by accident when doing a production deploy.
|
|
|
|
// Overriding the cluster in production is almost always a mistake since customers
|
|
|
|
// want consistency in production and not compute that is different each deploy.
|
|
|
|
// For this reason we log a warning and ignore the environment variable.
|
|
|
|
return diag.Warningf("the DATABRICKS_CLUSTER_ID variable is set but is ignored since the current target uses 'mode: production'")
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-11 08:18:43 +00:00
|
|
|
if v := env.Get(ctx, "DATABRICKS_CLUSTER_ID"); v != "" {
|
2024-09-23 10:42:34 +00:00
|
|
|
b.Config.Bundle.ClusterId = v
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 10:42:34 +00:00
|
|
|
if b.Config.Bundle.ClusterId == "" {
|
2024-11-13 08:43:31 +00:00
|
|
|
return diags
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := b.Config.Resources
|
|
|
|
for i := range r.Jobs {
|
2024-09-23 10:42:34 +00:00
|
|
|
overrideJobCompute(r.Jobs[i], b.Config.Bundle.ClusterId)
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 08:43:31 +00:00
|
|
|
return diags
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|