From b0dab5b2f93bb31436221fc1deb5801b8744ec0f Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Sun, 15 Nov 2026 23:47:01 +0000 Subject: [PATCH 1/4] WIP: implement Co-authored-by: Isaac --- bundle/configsync/diff.go | 3 +++ bundle/configsync/diff_test.go | 41 ++++++++++++++++++++++++++++++ bundle/configsync/lifecycle.go | 37 +++++++++++++++++++++++++++ bundle/configsync/runas.go | 6 ++++- bundle/direct/bundle_plan.go | 38 ++------------------------- bundle/direct/dresources/config.go | 35 +++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 37 deletions(-) create mode 100644 bundle/configsync/lifecycle.go diff --git a/bundle/configsync/diff.go b/bundle/configsync/diff.go index 37c296e1ffc..b8f31f20a74 100644 --- a/bundle/configsync/diff.go +++ b/bundle/configsync/diff.go @@ -162,6 +162,9 @@ func ExtractChanges(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan !keepSkippedRunAsChange(fullPath, changeDesc) { continue } + if skipByLifecycle(resourceKey, path, changeDesc) { + continue + } change, err := convertChangeDesc(fullPath, changeDesc) if err != nil { diff --git a/bundle/configsync/diff_test.go b/bundle/configsync/diff_test.go index b16885f6b71..51f82ee8371 100644 --- a/bundle/configsync/diff_test.go +++ b/bundle/configsync/diff_test.go @@ -298,3 +298,44 @@ func TestExtractChangesSkipsPermissionsAndGrants(t *testing.T) { require.True(t, hasNamedPerms, "a resource named permissions must not be skipped") assert.Contains(t, namedPerms, "description") } + +func TestExtractChangesSkipsLifecycleFields(t *testing.T) { + plan := &deployplan.Plan{Plan: map[string]*deployplan.PlanEntry{ + "resources.genie_spaces.my_space": { + Changes: deployplan.Changes{ + // OverrideChangeDesc promotes etag drift to an update for deploy. + "etag": {Action: deployplan.Update, Old: "1", Remote: "2"}, + "title": {Action: deployplan.Update, Old: "a", New: "a", Remote: "b"}, + }, + }, + "resources.pipelines.my_pipeline": { + Changes: deployplan.Changes{ + // Not one of the backend default's allowed values, so it is a real edit. + "configuration['spark.sql.ansi.enabled']": {Action: deployplan.Update, Old: "true", Remote: "false"}, + }, + }, + "resources.jobs.my_job": { + Changes: deployplan.Changes{ + // input_only: never returned by GET, so a local edit would be removed. + "trigger.table_update.condition": {Action: deployplan.Update, Old: "ANY_UPDATED", New: "ALL_UPDATED"}, + // Backend default the config no longer sets. + "format": {Action: deployplan.Update, Old: "MULTI_TASK", Remote: "MULTI_TASK"}, + // A principal kind switch must still be synced. + "run_as.service_principal_name": {Action: deployplan.Skip, Reason: deployplan.ReasonBackendDefault, Remote: "sp"}, + }, + }, + }} + + changes, err := ExtractChanges(t.Context(), &bundle.Bundle{}, plan) + require.NoError(t, err) + + assert.Equal(t, ResourceChanges{ + "title": {Operation: OperationReplace, Value: "b"}, + }, changes["resources.genie_spaces.my_space"]) + assert.Equal(t, ResourceChanges{ + "configuration['spark.sql.ansi.enabled']": {Operation: OperationAdd, Value: "false", LocalEdit: true}, + }, changes["resources.pipelines.my_pipeline"]) + assert.Equal(t, ResourceChanges{ + "run_as.service_principal_name": {Operation: OperationAdd, Value: "sp"}, + }, changes["resources.jobs.my_job"]) +} diff --git a/bundle/configsync/lifecycle.go b/bundle/configsync/lifecycle.go new file mode 100644 index 00000000000..00fcab98ab6 --- /dev/null +++ b/bundle/configsync/lifecycle.go @@ -0,0 +1,37 @@ +package configsync + +import ( + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/bundle/deployplan" + "github.com/databricks/cli/bundle/direct/dresources" + "github.com/databricks/cli/libs/structs/structpath" +) + +// skipByLifecycle reports whether the direct engine's lifecycle metadata says the +// remote value of a change must never be written back to config: a field under +// ignore_remote_changes (output-only, input-only or backend-managed), or a backend +// default the config does not set. The plan action is not enough on its own: it +// is still an update when the config has an undeployed edit to such a field, or +// when a resource's OverrideChangeDesc promotes it (a genie space etag). +func skipByLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) bool { + path, err := structpath.ParsePath(fieldPath) + if err != nil { + return false + } + resourceType := config.GetResourceTypeFromKey(resourceKey) + backendDefault := cd.New == nil && cd.Remote != nil && !isJobRunAsKey(resourceKey+"."+fieldPath) + for _, rc := range []*dresources.ResourceLifecycleConfig{ + dresources.GetResourceConfig(resourceType), + dresources.GetGeneratedResourceConfig(resourceType), + } { + for _, rule := range rc.IgnoreRemoteChanges { + if path.HasPatternPrefix(rule.Field) { + return true + } + } + if backendDefault && rc.IsBackendDefault(path, cd.Remote) { + return true + } + } + return false +} diff --git a/bundle/configsync/runas.go b/bundle/configsync/runas.go index 3faea944f41..43807219f68 100644 --- a/bundle/configsync/runas.go +++ b/bundle/configsync/runas.go @@ -8,8 +8,12 @@ import "github.com/databricks/cli/bundle/deployplan" // and dropping it while removing the old key leaves "run_as: {}". const jobRunAsChildPattern = "resources.jobs.*.run_as.*" +func isJobRunAsKey(path string) bool { + return matchPattern(jobRunAsChildPattern, path) +} + // keepSkippedRunAsChange reports whether a run_as change the plan skipped as a // backend default should still be considered for syncing. func keepSkippedRunAsChange(path string, cd *deployplan.ChangeDesc) bool { - return cd.Reason == deployplan.ReasonBackendDefault && matchPattern(jobRunAsChildPattern, path) + return cd.Reason == deployplan.ReasonBackendDefault && isJobRunAsKey(path) } diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 43a58291ab2..145402cdef2 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -1189,7 +1189,7 @@ func shouldSkipBackendDefault(cfg *dresources.ResourceLifecycleConfig, path *str if cfg == nil || ch.Old != nil || ch.New != nil || ch.Remote == nil { return "", false } - if matchesAnyBackendDefault(cfg, path, ch.Remote) { + if cfg.IsBackendDefault(path, ch.Remote) { return deployplan.ReasonBackendDefault, true } @@ -1205,47 +1205,13 @@ func shouldSkipBackendDefault(cfg *dresources.ResourceLifecycleConfig, path *str iter := rv.MapRange() for iter.Next() { childPath := structpath.NewBracketString(path, iter.Key().String()) - if !matchesAnyBackendDefault(cfg, childPath, iter.Value().Interface()) { + if !cfg.IsBackendDefault(childPath, iter.Value().Interface()) { return "", false } } return deployplan.ReasonBackendDefault, true } -// matchesAnyBackendDefault reports whether the remote value at path matches any of -// the resource's configured backend-default rules (and the rule's allowed values, -// if specified). -func matchesAnyBackendDefault(cfg *dresources.ResourceLifecycleConfig, path *structpath.PathNode, remote any) bool { - for _, rule := range cfg.BackendDefaults { - if !path.HasPatternPrefix(rule.Field) { - continue - } - if len(rule.Values) == 0 { - return true - } - if matchesAllowedValue(remote, rule.Values) { - return true - } - } - return false -} - -// matchesAllowedValue checks if the remote value matches one of the allowed JSON values. -// Each json.RawMessage is unmarshaled into the same type as remote for comparison. -func matchesAllowedValue(remote any, values []json.RawMessage) bool { - remoteType := reflect.TypeOf(remote) - for _, raw := range values { - candidate := reflect.New(remoteType).Interface() - if err := json.Unmarshal(raw, candidate); err != nil { - continue - } - if structdiff.IsEqual(remote, reflect.ValueOf(candidate).Elem().Interface()) { - return true - } - } - return false -} - func allEmpty(values ...any) bool { for _, v := range values { if v == nil { diff --git a/bundle/direct/dresources/config.go b/bundle/direct/dresources/config.go index 7adcc36086a..b630b7e1176 100644 --- a/bundle/direct/dresources/config.go +++ b/bundle/direct/dresources/config.go @@ -6,9 +6,11 @@ import ( "fmt" "io/fs" "path" + "reflect" "strings" "sync" + "github.com/databricks/cli/libs/structs/structdiff" "github.com/databricks/cli/libs/structs/structpath" "go.yaml.in/yaml/v3" ) @@ -49,6 +51,39 @@ func (b *BackendDefaultRule) UnmarshalYAML(unmarshal func(any) error) error { return nil } +// IsBackendDefault reports whether remote, the remote value at path, matches any of +// the backend-default rules (and the rule's allowed values, if specified). +func (c *ResourceLifecycleConfig) IsBackendDefault(path *structpath.PathNode, remote any) bool { + for _, rule := range c.BackendDefaults { + if !path.HasPatternPrefix(rule.Field) { + continue + } + if len(rule.Values) == 0 { + return true + } + if matchesAllowedValue(remote, rule.Values) { + return true + } + } + return false +} + +// matchesAllowedValue checks if the remote value matches one of the allowed JSON values. +// Each json.RawMessage is unmarshaled into the same type as remote for comparison. +func matchesAllowedValue(remote any, values []json.RawMessage) bool { + remoteType := reflect.TypeOf(remote) + for _, raw := range values { + candidate := reflect.New(remoteType).Interface() + if err := json.Unmarshal(raw, candidate); err != nil { + continue + } + if structdiff.IsEqual(remote, reflect.ValueOf(candidate).Elem().Interface()) { + return true + } + } + return false +} + // RemoteAdditionRule marks a sub-object whose contents the backend co-owns whenever the // object's WhenSet field is set. Inside such an object, a field the config never declared // coming back set from the remote is an addition by the backend, not drift. From e183f069d6a536563cee212ac6160d126e4ef903 Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Sun, 15 Nov 2026 23:57:40 +0000 Subject: [PATCH 2/4] Apply lifecycle rules to parent-level changes Co-authored-by: Isaac --- bundle/configsync/diff.go | 8 +++- bundle/configsync/diff_test.go | 27 +++++++++++ bundle/configsync/lifecycle.go | 86 +++++++++++++++++++++++++++++----- bundle/configsync/runas.go | 6 +-- 4 files changed, 108 insertions(+), 19 deletions(-) diff --git a/bundle/configsync/diff.go b/bundle/configsync/diff.go index b8f31f20a74..c580d46e5df 100644 --- a/bundle/configsync/diff.go +++ b/bundle/configsync/diff.go @@ -162,11 +162,15 @@ func ExtractChanges(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan !keepSkippedRunAsChange(fullPath, changeDesc) { continue } - if skipByLifecycle(resourceKey, path, changeDesc) { + synced, skip, err := applyLifecycle(resourceKey, path, changeDesc) + if err != nil { + return nil, fmt.Errorf("failed to apply lifecycle rules for path %s: %w", path, err) + } + if skip { continue } - change, err := convertChangeDesc(fullPath, changeDesc) + change, err := convertChangeDesc(fullPath, synced) if err != nil { return nil, fmt.Errorf("failed to compute config change for path %s: %w", path, err) } diff --git a/bundle/configsync/diff_test.go b/bundle/configsync/diff_test.go index 51f82ee8371..16b87c8edbf 100644 --- a/bundle/configsync/diff_test.go +++ b/bundle/configsync/diff_test.go @@ -5,6 +5,7 @@ import ( "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/deployplan" + "github.com/databricks/databricks-sdk-go/service/serving" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -314,8 +315,27 @@ func TestExtractChangesSkipsLifecycleFields(t *testing.T) { "configuration['spark.sql.ansi.enabled']": {Action: deployplan.Update, Old: "true", Remote: "false"}, }, }, + "resources.model_serving_endpoints.my_endpoint": { + Changes: deployplan.Changes{ + // GET returns no telemetry_config, so the plan reports the whole + // object, and table_names under it is input_only. + "telemetry_config": { + Action: deployplan.Update, + Old: &serving.TelemetryConfig{TableNames: &serving.UnityCatalogTableNames{LogsTable: "main.default.logs"}}, + New: &serving.TelemetryConfig{TableNames: &serving.UnityCatalogTableNames{LogsTable: "main.default.logs"}}, + }, + }, + }, "resources.jobs.my_job": { Changes: deployplan.Changes{ + // A task added remotely keeps its fields, minus the backend-managed ones. + "tasks[task_key='loop']": {Action: deployplan.Update, Remote: map[string]any{ + "task_key": "loop", + "for_each_task": map[string]any{"task": map[string]any{"new_cluster": map[string]any{ + "spark_version": "16.4.x-scala2.12", + "aws_attributes": map[string]any{"availability": "SPOT"}, + }}}, + }}, // input_only: never returned by GET, so a local edit would be removed. "trigger.table_update.condition": {Action: deployplan.Update, Old: "ANY_UPDATED", New: "ALL_UPDATED"}, // Backend default the config no longer sets. @@ -335,7 +355,14 @@ func TestExtractChangesSkipsLifecycleFields(t *testing.T) { assert.Equal(t, ResourceChanges{ "configuration['spark.sql.ansi.enabled']": {Operation: OperationAdd, Value: "false", LocalEdit: true}, }, changes["resources.pipelines.my_pipeline"]) + assert.NotContains(t, changes, "resources.model_serving_endpoints.my_endpoint") assert.Equal(t, ResourceChanges{ + "tasks[task_key='loop']": {Operation: OperationAdd, Value: map[string]any{ + "task_key": "loop", + "for_each_task": map[string]any{"task": map[string]any{"new_cluster": map[string]any{ + "spark_version": "16.4.x-scala2.12", + }}}, + }}, "run_as.service_principal_name": {Operation: OperationAdd, Value: "sp"}, }, changes["resources.jobs.my_job"]) } diff --git a/bundle/configsync/lifecycle.go b/bundle/configsync/lifecycle.go index 00fcab98ab6..a4727693ac8 100644 --- a/bundle/configsync/lifecycle.go +++ b/bundle/configsync/lifecycle.go @@ -1,37 +1,99 @@ package configsync import ( + "fmt" + "reflect" + "slices" + "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct/dresources" "github.com/databricks/cli/libs/structs/structpath" ) -// skipByLifecycle reports whether the direct engine's lifecycle metadata says the -// remote value of a change must never be written back to config: a field under -// ignore_remote_changes (output-only, input-only or backend-managed), or a backend -// default the config does not set. The plan action is not enough on its own: it -// is still an update when the config has an undeployed edit to such a field, or -// when a resource's OverrideChangeDesc promotes it (a genie space etag). -func skipByLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) bool { +// applyLifecycle applies the direct engine's lifecycle metadata to a change, whatever +// action the plan gave it. The plan action is not enough on its own: it is still an +// update when the config has an undeployed edit to such a field, or when a resource's +// OverrideChangeDesc promotes it (a genie space etag). +// +// A remote value under ignore_remote_changes (output-only, input-only or backend-managed) +// is never written back, and neither is a backend default the config does not set. When +// the plan reports a whole object (one side is nil), the ignored fields inside it are +// left out of both sides, and the change is skipped if nothing else differs. +func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (*deployplan.ChangeDesc, bool, error) { path, err := structpath.ParsePath(fieldPath) if err != nil { - return false + return nil, false, err } resourceType := config.GetResourceTypeFromKey(resourceKey) - backendDefault := cd.New == nil && cd.Remote != nil && !isJobRunAsKey(resourceKey+"."+fieldPath) + backendDefault := cd.New == nil && cd.Remote != nil && !matchPattern(jobRunAsChildPattern, resourceKey+"."+fieldPath) + + var nested []*structpath.PatternNode for _, rc := range []*dresources.ResourceLifecycleConfig{ dresources.GetResourceConfig(resourceType), dresources.GetGeneratedResourceConfig(resourceType), } { for _, rule := range rc.IgnoreRemoteChanges { if path.HasPatternPrefix(rule.Field) { - return true + return nil, true, nil + } + if rule.Field.Len() > path.Len() && path.HasPatternPrefix(rule.Field.Prefix(path.Len())) { + nested = append(nested, rule.Field) } } if backendDefault && rc.IsBackendDefault(path, cd.Remote) { - return true + return nil, true, nil + } + } + if len(nested) == 0 { + return cd, false, nil + } + + newValue, err := normalizeValue(cd.New) + if err != nil { + return nil, false, fmt.Errorf("failed to normalize config value: %w", err) + } + remoteValue, err := normalizeValue(cd.Remote) + if err != nil { + return nil, false, fmt.Errorf("failed to normalize remote value: %w", err) + } + newValue = stripIgnoredFields(nested, path, newValue) + remoteValue = stripIgnoredFields(nested, path, remoteValue) + if reflect.DeepEqual(newValue, remoteValue) { + return nil, true, nil + } + + stripped := *cd + stripped.New = newValue + stripped.Remote = remoteValue + return &stripped, false, nil +} + +// stripIgnoredFields drops the fields of value, the normalized value at path, that +// match one of the rules. A map left empty only by dropping fields becomes nil. +func stripIgnoredFields(rules []*structpath.PatternNode, path *structpath.PathNode, value any) any { + switch v := value.(type) { + case map[string]any: + result := make(map[string]any, len(v)) + for key, val := range v { + child := structpath.NewStringKey(path, key) + if slices.ContainsFunc(rules, child.HasPatternPrefix) { + continue + } + if s := stripIgnoredFields(rules, child, val); s != nil || val == nil { + result[key] = s + } + } + if len(result) == 0 && len(v) > 0 { + return nil + } + return result + case []any: + result := make([]any, len(v)) + for i, elem := range v { + result[i] = stripIgnoredFields(rules, structpath.NewIndex(path, i), elem) } + return result } - return false + return value } diff --git a/bundle/configsync/runas.go b/bundle/configsync/runas.go index 43807219f68..3faea944f41 100644 --- a/bundle/configsync/runas.go +++ b/bundle/configsync/runas.go @@ -8,12 +8,8 @@ import "github.com/databricks/cli/bundle/deployplan" // and dropping it while removing the old key leaves "run_as: {}". const jobRunAsChildPattern = "resources.jobs.*.run_as.*" -func isJobRunAsKey(path string) bool { - return matchPattern(jobRunAsChildPattern, path) -} - // keepSkippedRunAsChange reports whether a run_as change the plan skipped as a // backend default should still be considered for syncing. func keepSkippedRunAsChange(path string, cd *deployplan.ChangeDesc) bool { - return cd.Reason == deployplan.ReasonBackendDefault && isJobRunAsKey(path) + return cd.Reason == deployplan.ReasonBackendDefault && matchPattern(jobRunAsChildPattern, path) } From 2dfcc6dd294d96585057ce07e20c4a775d3806b0 Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Mon, 16 Nov 2026 00:02:24 +0000 Subject: [PATCH 3/4] Drop nested backend defaults pinned to a value Co-authored-by: Isaac --- bundle/configsync/diff.go | 4 +- bundle/configsync/diff_test.go | 18 ++++++++- bundle/configsync/lifecycle.go | 70 +++++++++++++++++++++------------- 3 files changed, 63 insertions(+), 29 deletions(-) diff --git a/bundle/configsync/diff.go b/bundle/configsync/diff.go index c580d46e5df..4e59f178e13 100644 --- a/bundle/configsync/diff.go +++ b/bundle/configsync/diff.go @@ -162,11 +162,11 @@ func ExtractChanges(ctx context.Context, b *bundle.Bundle, plan *deployplan.Plan !keepSkippedRunAsChange(fullPath, changeDesc) { continue } - synced, skip, err := applyLifecycle(resourceKey, path, changeDesc) + synced, err := applyLifecycle(resourceKey, path, changeDesc) if err != nil { return nil, fmt.Errorf("failed to apply lifecycle rules for path %s: %w", path, err) } - if skip { + if synced == nil { continue } diff --git a/bundle/configsync/diff_test.go b/bundle/configsync/diff_test.go index 16b87c8edbf..817c638b3ff 100644 --- a/bundle/configsync/diff_test.go +++ b/bundle/configsync/diff_test.go @@ -315,6 +315,15 @@ func TestExtractChangesSkipsLifecycleFields(t *testing.T) { "configuration['spark.sql.ansi.enabled']": {Action: deployplan.Update, Old: "true", Remote: "false"}, }, }, + "resources.pipelines.other_pipeline": { + Changes: deployplan.Changes{ + // A remote-only map holding a backend default next to a real addition. + "configuration": {Action: deployplan.Update, Remote: map[string]string{ + "spark.sql.ansi.enabled": "true", + "custom.key": "value", + }}, + }, + }, "resources.model_serving_endpoints.my_endpoint": { Changes: deployplan.Changes{ // GET returns no telemetry_config, so the plan reports the whole @@ -328,13 +337,16 @@ func TestExtractChangesSkipsLifecycleFields(t *testing.T) { }, "resources.jobs.my_job": { Changes: deployplan.Changes{ - // A task added remotely keeps its fields, minus the backend-managed ones. + // A task added remotely keeps its fields, including node_type_id, minus the + // backend-managed ones and defaults pinned to a value. "tasks[task_key='loop']": {Action: deployplan.Update, Remote: map[string]any{ "task_key": "loop", "for_each_task": map[string]any{"task": map[string]any{"new_cluster": map[string]any{ "spark_version": "16.4.x-scala2.12", + "node_type_id": "i3.xlarge", "aws_attributes": map[string]any{"availability": "SPOT"}, }}}, + "run_if": "ALL_SUCCESS", }}, // input_only: never returned by GET, so a local edit would be removed. "trigger.table_update.condition": {Action: deployplan.Update, Old: "ANY_UPDATED", New: "ALL_UPDATED"}, @@ -355,12 +367,16 @@ func TestExtractChangesSkipsLifecycleFields(t *testing.T) { assert.Equal(t, ResourceChanges{ "configuration['spark.sql.ansi.enabled']": {Operation: OperationAdd, Value: "false", LocalEdit: true}, }, changes["resources.pipelines.my_pipeline"]) + assert.Equal(t, ResourceChanges{ + "configuration": {Operation: OperationAdd, Value: map[string]any{"custom.key": "value"}}, + }, changes["resources.pipelines.other_pipeline"]) assert.NotContains(t, changes, "resources.model_serving_endpoints.my_endpoint") assert.Equal(t, ResourceChanges{ "tasks[task_key='loop']": {Operation: OperationAdd, Value: map[string]any{ "task_key": "loop", "for_each_task": map[string]any{"task": map[string]any{"new_cluster": map[string]any{ "spark_version": "16.4.x-scala2.12", + "node_type_id": "i3.xlarge", }}}, }}, "run_as.service_principal_name": {Operation: OperationAdd, Value: "sp"}, diff --git a/bundle/configsync/lifecycle.go b/bundle/configsync/lifecycle.go index a4727693ac8..1979fdea56f 100644 --- a/bundle/configsync/lifecycle.go +++ b/bundle/configsync/lifecycle.go @@ -12,75 +12,93 @@ import ( ) // applyLifecycle applies the direct engine's lifecycle metadata to a change, whatever -// action the plan gave it. The plan action is not enough on its own: it is still an -// update when the config has an undeployed edit to such a field, or when a resource's -// OverrideChangeDesc promotes it (a genie space etag). +// action the plan gave it, and returns nil when nothing is left to sync. The plan action +// is not enough on its own: it is still an update when the config has an undeployed edit +// to such a field, or when a resource's OverrideChangeDesc promotes it (a genie space etag). // // A remote value under ignore_remote_changes (output-only, input-only or backend-managed) // is never written back, and neither is a backend default the config does not set. When -// the plan reports a whole object (one side is nil), the ignored fields inside it are -// left out of both sides, and the change is skipped if nothing else differs. -func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (*deployplan.ChangeDesc, bool, error) { +// the plan reports a whole object (one side is nil), these fields are left out of it, and +// the change is skipped if nothing else differs. Inside a remote-only object, only +// defaults pinned to allowed values are left out: a field like node_type_id may be what +// the user picked in the UI. +func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (*deployplan.ChangeDesc, error) { path, err := structpath.ParsePath(fieldPath) if err != nil { - return nil, false, err + return nil, err } resourceType := config.GetResourceTypeFromKey(resourceKey) backendDefault := cd.New == nil && cd.Remote != nil && !matchPattern(jobRunAsChildPattern, resourceKey+"."+fieldPath) + isNested := func(pattern *structpath.PatternNode) bool { + return pattern.Len() > path.Len() && path.HasPatternPrefix(pattern.Prefix(path.Len())) + } - var nested []*structpath.PatternNode + var ignored []*structpath.PatternNode + var defaults dresources.ResourceLifecycleConfig for _, rc := range []*dresources.ResourceLifecycleConfig{ dresources.GetResourceConfig(resourceType), dresources.GetGeneratedResourceConfig(resourceType), } { for _, rule := range rc.IgnoreRemoteChanges { if path.HasPatternPrefix(rule.Field) { - return nil, true, nil + return nil, nil } - if rule.Field.Len() > path.Len() && path.HasPatternPrefix(rule.Field.Prefix(path.Len())) { - nested = append(nested, rule.Field) + if isNested(rule.Field) { + ignored = append(ignored, rule.Field) } } if backendDefault && rc.IsBackendDefault(path, cd.Remote) { - return nil, true, nil + return nil, nil + } + if cd.New == nil { + for _, rule := range rc.BackendDefaults { + if len(rule.Values) > 0 && isNested(rule.Field) { + defaults.BackendDefaults = append(defaults.BackendDefaults, rule) + } + } } } - if len(nested) == 0 { - return cd, false, nil + if len(ignored) == 0 && len(defaults.BackendDefaults) == 0 { + return cd, nil } newValue, err := normalizeValue(cd.New) if err != nil { - return nil, false, fmt.Errorf("failed to normalize config value: %w", err) + return nil, fmt.Errorf("failed to normalize config value: %w", err) } remoteValue, err := normalizeValue(cd.Remote) if err != nil { - return nil, false, fmt.Errorf("failed to normalize remote value: %w", err) + return nil, fmt.Errorf("failed to normalize remote value: %w", err) + } + isIgnored := func(p *structpath.PathNode, _ any) bool { + return slices.ContainsFunc(ignored, p.HasPatternPrefix) } - newValue = stripIgnoredFields(nested, path, newValue) - remoteValue = stripIgnoredFields(nested, path, remoteValue) + newValue = stripFields(path, newValue, isIgnored) + remoteValue = stripFields(path, remoteValue, func(p *structpath.PathNode, v any) bool { + return isIgnored(p, v) || defaults.IsBackendDefault(p, v) + }) if reflect.DeepEqual(newValue, remoteValue) { - return nil, true, nil + return nil, nil } stripped := *cd stripped.New = newValue stripped.Remote = remoteValue - return &stripped, false, nil + return &stripped, nil } -// stripIgnoredFields drops the fields of value, the normalized value at path, that -// match one of the rules. A map left empty only by dropping fields becomes nil. -func stripIgnoredFields(rules []*structpath.PatternNode, path *structpath.PathNode, value any) any { +// stripFields drops the fields of value, the normalized value at path, for which drop +// returns true. A map left empty only by dropping fields becomes nil. +func stripFields(path *structpath.PathNode, value any, drop func(*structpath.PathNode, any) bool) any { switch v := value.(type) { case map[string]any: result := make(map[string]any, len(v)) for key, val := range v { child := structpath.NewStringKey(path, key) - if slices.ContainsFunc(rules, child.HasPatternPrefix) { + if drop(child, val) { continue } - if s := stripIgnoredFields(rules, child, val); s != nil || val == nil { + if s := stripFields(child, val, drop); s != nil || val == nil { result[key] = s } } @@ -91,7 +109,7 @@ func stripIgnoredFields(rules []*structpath.PatternNode, path *structpath.PathNo case []any: result := make([]any, len(v)) for i, elem := range v { - result[i] = stripIgnoredFields(rules, structpath.NewIndex(path, i), elem) + result[i] = stripFields(structpath.NewIndex(path, i), elem, drop) } return result } From 2d69c062bf9ee061d30aec5ac95220a8a4de8041 Mon Sep 17 00:00:00 2001 From: "ilya.kuznetsov" Date: Mon, 16 Nov 2026 00:07:10 +0000 Subject: [PATCH 4/4] Use named helpers instead of closures Co-authored-by: Isaac --- bundle/configsync/lifecycle.go | 44 +++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/bundle/configsync/lifecycle.go b/bundle/configsync/lifecycle.go index 1979fdea56f..26e1065c006 100644 --- a/bundle/configsync/lifecycle.go +++ b/bundle/configsync/lifecycle.go @@ -29,12 +29,8 @@ func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (* } resourceType := config.GetResourceTypeFromKey(resourceKey) backendDefault := cd.New == nil && cd.Remote != nil && !matchPattern(jobRunAsChildPattern, resourceKey+"."+fieldPath) - isNested := func(pattern *structpath.PatternNode) bool { - return pattern.Len() > path.Len() && path.HasPatternPrefix(pattern.Prefix(path.Len())) - } - var ignored []*structpath.PatternNode - var defaults dresources.ResourceLifecycleConfig + var f nestedFieldFilter for _, rc := range []*dresources.ResourceLifecycleConfig{ dresources.GetResourceConfig(resourceType), dresources.GetGeneratedResourceConfig(resourceType), @@ -43,8 +39,8 @@ func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (* if path.HasPatternPrefix(rule.Field) { return nil, nil } - if isNested(rule.Field) { - ignored = append(ignored, rule.Field) + if isNestedPattern(path, rule.Field) { + f.ignored = append(f.ignored, rule.Field) } } if backendDefault && rc.IsBackendDefault(path, cd.Remote) { @@ -52,13 +48,13 @@ func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (* } if cd.New == nil { for _, rule := range rc.BackendDefaults { - if len(rule.Values) > 0 && isNested(rule.Field) { - defaults.BackendDefaults = append(defaults.BackendDefaults, rule) + if len(rule.Values) > 0 && isNestedPattern(path, rule.Field) { + f.defaults.BackendDefaults = append(f.defaults.BackendDefaults, rule) } } } } - if len(ignored) == 0 && len(defaults.BackendDefaults) == 0 { + if len(f.ignored) == 0 && len(f.defaults.BackendDefaults) == 0 { return cd, nil } @@ -70,13 +66,8 @@ func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (* if err != nil { return nil, fmt.Errorf("failed to normalize remote value: %w", err) } - isIgnored := func(p *structpath.PathNode, _ any) bool { - return slices.ContainsFunc(ignored, p.HasPatternPrefix) - } - newValue = stripFields(path, newValue, isIgnored) - remoteValue = stripFields(path, remoteValue, func(p *structpath.PathNode, v any) bool { - return isIgnored(p, v) || defaults.IsBackendDefault(p, v) - }) + newValue = stripFields(path, newValue, f.dropFromConfig) + remoteValue = stripFields(path, remoteValue, f.dropFromRemote) if reflect.DeepEqual(newValue, remoteValue) { return nil, nil } @@ -87,6 +78,25 @@ func applyLifecycle(resourceKey, fieldPath string, cd *deployplan.ChangeDesc) (* return &stripped, nil } +// isNestedPattern reports whether pattern names a field inside the value at path. +func isNestedPattern(path *structpath.PathNode, pattern *structpath.PatternNode) bool { + return pattern.Len() > path.Len() && path.HasPatternPrefix(pattern.Prefix(path.Len())) +} + +// nestedFieldFilter holds the lifecycle rules that name fields inside a whole-object change. +type nestedFieldFilter struct { + ignored []*structpath.PatternNode + defaults dresources.ResourceLifecycleConfig +} + +func (f *nestedFieldFilter) dropFromConfig(path *structpath.PathNode, _ any) bool { + return slices.ContainsFunc(f.ignored, path.HasPatternPrefix) +} + +func (f *nestedFieldFilter) dropFromRemote(path *structpath.PathNode, value any) bool { + return f.dropFromConfig(path, value) || f.defaults.IsBackendDefault(path, value) +} + // stripFields drops the fields of value, the normalized value at path, for which drop // returns true. A map left empty only by dropping fields becomes nil. func stripFields(path *structpath.PathNode, value any, drop func(*structpath.PathNode, any) bool) any {