-
Notifications
You must be signed in to change notification settings - Fork 456
[Fix] Fix drift in databricks_cluster
when is_single_node
is set to true
#4416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@alexott please take a stab at it when u get a slot |
Please add tests for it - we have example of test for |
@mgyucht given the work on porting clusters to the plugin framework - do we need this PR? |
That work may take a bit longer than we thought to finish, so I'm OK merging incremental improvements like this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation seems OK. Several comments can be addressed.
Please include any testing you did in the PR description.
Can you add a unit test to verify that the diff is as expected? Create a new test, and use a qa.ResourceFixture where you supply the HCL
, InstanceState
, and ExpectedDiff
fields, in addition to the Resource
field. Take a look at catalog/resource_sql_table_test.go
TestResourceSqlTable_Diff_ExistingResource
for an example of how to do this. Add your test to clusters/resource_cluster_test.go
.
clusters/resource_cluster.go
Outdated
new, okNew := n.(map[string]interface{}) | ||
|
||
if !okNew || !okOld { | ||
return fmt.Errorf("internal type casting error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you include the actual type of o/n here with %T
?
clusters/resource_cluster.go
Outdated
Delete: resourceClusterDelete, | ||
Schema: clusterSchema, | ||
CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff) error { | ||
if isSingleNode, _ := d.GetOk("is_single_node"); isSingleNode.(bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially avoid interface conversion panic.
if isSingleNode, _ := d.GetOk("is_single_node"); isSingleNode.(bool) { | |
if isSingleNode, ok := d.GetOk("is_single_node"); ok && isSingleNode.(bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! Fixed 🙌
expectedDiff["default_tags.%"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} | ||
expectedDiff["driver_instance_pool_id"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} | ||
expectedDiff["driver_node_type_id"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} | ||
expectedDiff["enable_elastic_disk"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} | ||
expectedDiff["enable_local_disk_encryption"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} | ||
expectedDiff["state"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} | ||
expectedDiff["url"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} | ||
|
||
instanceState := testCase.instanceState | ||
instanceState["cluster_id"] = "abc" | ||
instanceState["autotermination_minutes"] = "60" | ||
instanceState["cluster_name"] = "Single Node Cluster" | ||
instanceState["data_security_mode"] = "SINGLE_USER" | ||
instanceState["spark_version"] = "15.4.x-scala2.12" | ||
instanceState["single_user_name"] = "testuser" | ||
instanceState["runtime_engine"] = "STANDARD" | ||
instanceState["num_workers"] = "0" | ||
instanceState["node_type_id"] = "Standard_F4s" | ||
instanceState["kind"] = "CLASSIC_PREVIEW" | ||
instanceState["is_single_node"] = "true" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of this; if there's a way to simplify this please lemme know
3cde764
to
e397efd
Compare
68e13be
to
77067f2
Compare
There is still a drift in |
If integration tests don't run automatically, an authorized user can run them manually by following the instructions below: Trigger: Inputs:
Checks will be approved automatically on success. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a drift issue in the databricks_cluster
resource when is_single_node
is set to true
, addressing issue #4360. The Databricks API automatically sets certain properties for single-node clusters, causing Terraform to detect unwanted drift.
- Adds a
CustomizeDiff
function to prevent drift by preserving auto-configured properties - Marks
spark_conf
andcustom_tags
as computed and optional in the schema - Includes comprehensive unit tests to verify the drift prevention behavior
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
clusters/resource_cluster.go | Implements CustomizeDiff logic and schema changes to prevent drift for single-node clusters |
clusters/resource_cluster_test.go | Adds unit tests to verify CustomizeDiff behavior with various configurations |
NEXT_CHANGELOG.md | Documents the bug fix in the changelog |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
new, okNew := n.(map[string]interface{}) | ||
|
||
if !okNew || !okOld { | ||
return fmt.Errorf("internal type casting error n: %T, o: %T", n, o) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message 'internal type casting error' is not very helpful for debugging. Consider providing more context about what was expected and what was received, such as 'failed to cast custom_tags/spark_conf values to map[string]interface{}: expected map but got %T for new value and %T for old value'.
return fmt.Errorf("internal type casting error n: %T, o: %T", n, o) | |
return fmt.Errorf("failed to cast %s values to map[string]interface{}: expected map but got %T for new value and %T for old value", key, n, o) |
Copilot uses AI. Check for mistakes.
return fmt.Errorf("internal type casting error n: %T, o: %T", n, o) | ||
} | ||
|
||
log.Printf("[DEBUG] values for key %s, old: %v, new: %v", key, old, new) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging the full contents of custom_tags and spark_conf may expose sensitive information. Consider logging only the keys being processed or redacting sensitive values.
log.Printf("[DEBUG] values for key %s, old: %v, new: %v", key, old, new) | |
log.Printf("[DEBUG] values for key %s, old keys: %v, new keys: %v", key, getMapKeys(old), getMapKeys(new)) |
Copilot uses AI. Check for mistakes.
expectedDiff = make(map[string]*terraform.ResourceAttrDiff) | ||
} | ||
|
||
expectedDiff["default_tags.%"] = &terraform.ResourceAttrDiff{Old: "", New: "", NewComputed: true, NewRemoved: false, RequiresNew: false, Sensitive: false} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The repeated terraform.ResourceAttrDiff structs with identical patterns (lines 1704-1710) could be extracted into a helper function to reduce duplication and improve maintainability.
Copilot uses AI. Check for mistakes.
Changes
Fixes #4360
Tests
make test
run locallydocs/
folderinternal/acceptance
In addition to the unit tests, deployed provider locally with following resource definition and verified diff