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-12-10 10:02:44 +00:00
"github.com/databricks/cli/libs/cmdio"
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-12-10 10:02:44 +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-12-10 10:02:44 +00:00
// Overriding compute via a command-line flag for production works, but is not recommended.
diags = diags . Extend ( diag . Diagnostics { {
Summary : "Setting a cluster override for a target that uses 'mode: production' is not recommended" ,
Detail : "It is recommended to always use the same compute for production target for consistency." ,
} } )
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-12-10 10:02:44 +00:00
// For historical reasons, we allow setting the cluster ID via the DATABRICKS_CLUSTER_ID
// when development mode is used. Sometimes, this is done by accident, so we log an info message.
if b . Config . Bundle . Mode == config . Development {
cmdio . LogString ( ctx , "Setting a cluster override because DATABRICKS_CLUSTER_ID is set. It is recommended to use --cluster-id instead, which works in any target mode." )
} else {
// We don't allow using DATABRICKS_CLUSTER_ID in any other mode, it's too error-prone.
return diag . Warningf ( "The DATABRICKS_CLUSTER_ID variable is set but is ignored since the current target does not use 'mode: development'" )
}
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-12-10 10:02:44 +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-12-10 10:02:44 +00:00
return diags
2023-07-12 06:51:54 +00:00
}