2022-04-07 19:05:33 +00:00
|
|
|
package engine
|
2019-07-01 19:34:19 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
2019-07-01 19:34:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// getProperty fetches the child property with the indicated key from the given property value. If the key does not
|
|
|
|
// exist, it returns an empty `PropertyValue`.
|
|
|
|
func getProperty(key interface{}, v resource.PropertyValue) resource.PropertyValue {
|
|
|
|
switch {
|
|
|
|
case v.IsArray():
|
|
|
|
index, ok := key.(int)
|
|
|
|
if !ok || index < 0 || index >= len(v.ArrayValue()) {
|
|
|
|
return resource.PropertyValue{}
|
|
|
|
}
|
|
|
|
return v.ArrayValue()[index]
|
|
|
|
case v.IsObject():
|
|
|
|
k, ok := key.(string)
|
|
|
|
if !ok {
|
|
|
|
return resource.PropertyValue{}
|
|
|
|
}
|
|
|
|
return v.ObjectValue()[resource.PropertyKey(k)]
|
|
|
|
case v.IsComputed() || v.IsOutput() || v.IsSecret():
|
|
|
|
// We consider the contents of these values opaque and return them as-is, as we cannot know whether or not the
|
|
|
|
// value will or does contain an element with the given key.
|
|
|
|
return v
|
|
|
|
default:
|
|
|
|
return resource.PropertyValue{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addDiff inserts a diff of the given kind at the given path into the parent ValueDiff.
|
|
|
|
//
|
|
|
|
// If the path consists of a single element, a diff of the indicated kind is inserted directly. Otherwise, if the
|
|
|
|
// property named by the first element of the path exists in both parents, we snip off the first element of the path
|
|
|
|
// and recurse into the property itself. If the property does not exist in one parent or the other, the diff kind is
|
|
|
|
// disregarded and the change is treated as either an Add or a Delete.
|
2019-07-31 16:39:07 +00:00
|
|
|
func addDiff(path resource.PropertyPath, kind plugin.DiffKind, parent *resource.ValueDiff,
|
2023-03-03 16:36:39 +00:00
|
|
|
oldParent, newParent resource.PropertyValue,
|
|
|
|
) {
|
2023-02-15 01:28:14 +00:00
|
|
|
contract.Requiref(len(path) > 0, "path", "must not be empty")
|
2019-07-01 19:34:19 +00:00
|
|
|
|
|
|
|
element := path[0]
|
|
|
|
|
|
|
|
old, new := getProperty(element, oldParent), getProperty(element, newParent)
|
|
|
|
|
|
|
|
switch element := element.(type) {
|
|
|
|
case int:
|
|
|
|
if parent.Array == nil {
|
|
|
|
parent.Array = &resource.ArrayDiff{
|
|
|
|
Adds: make(map[int]resource.PropertyValue),
|
|
|
|
Deletes: make(map[int]resource.PropertyValue),
|
|
|
|
Sames: make(map[int]resource.PropertyValue),
|
|
|
|
Updates: make(map[int]resource.ValueDiff),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For leaf diffs, the provider tells us exactly what to record. For other diffs, we will derive the
|
|
|
|
// difference from the old and new property values.
|
|
|
|
if len(path) == 1 {
|
|
|
|
switch kind {
|
|
|
|
case plugin.DiffAdd, plugin.DiffAddReplace:
|
|
|
|
parent.Array.Adds[element] = new
|
|
|
|
case plugin.DiffDelete, plugin.DiffDeleteReplace:
|
|
|
|
parent.Array.Deletes[element] = old
|
|
|
|
case plugin.DiffUpdate, plugin.DiffUpdateReplace:
|
2019-07-08 23:33:21 +00:00
|
|
|
valueDiff := resource.ValueDiff{Old: old, New: new}
|
|
|
|
if d := old.Diff(new); d != nil {
|
|
|
|
valueDiff = *d
|
|
|
|
}
|
|
|
|
parent.Array.Updates[element] = valueDiff
|
2019-07-01 19:34:19 +00:00
|
|
|
default:
|
|
|
|
contract.Failf("unexpected diff kind %v", kind)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case old.IsNull() && !new.IsNull():
|
|
|
|
parent.Array.Adds[element] = new
|
|
|
|
case !old.IsNull() && new.IsNull():
|
|
|
|
parent.Array.Deletes[element] = old
|
|
|
|
default:
|
|
|
|
ed := parent.Array.Updates[element]
|
|
|
|
addDiff(path[1:], kind, &ed, old, new)
|
|
|
|
parent.Array.Updates[element] = ed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
if parent.Object == nil {
|
|
|
|
parent.Object = &resource.ObjectDiff{
|
|
|
|
Adds: make(resource.PropertyMap),
|
|
|
|
Deletes: make(resource.PropertyMap),
|
|
|
|
Sames: make(resource.PropertyMap),
|
|
|
|
Updates: make(map[resource.PropertyKey]resource.ValueDiff),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e := resource.PropertyKey(element)
|
|
|
|
if len(path) == 1 {
|
|
|
|
switch kind {
|
|
|
|
case plugin.DiffAdd, plugin.DiffAddReplace:
|
|
|
|
parent.Object.Adds[e] = new
|
|
|
|
case plugin.DiffDelete, plugin.DiffDeleteReplace:
|
|
|
|
parent.Object.Deletes[e] = old
|
|
|
|
case plugin.DiffUpdate, plugin.DiffUpdateReplace:
|
2019-07-08 23:33:21 +00:00
|
|
|
valueDiff := resource.ValueDiff{Old: old, New: new}
|
|
|
|
if d := old.Diff(new); d != nil {
|
|
|
|
valueDiff = *d
|
|
|
|
}
|
|
|
|
parent.Object.Updates[e] = valueDiff
|
2019-07-01 19:34:19 +00:00
|
|
|
default:
|
|
|
|
contract.Failf("unexpected diff kind %v", kind)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case old.IsNull() && !new.IsNull():
|
|
|
|
parent.Object.Adds[e] = new
|
|
|
|
case !old.IsNull() && new.IsNull():
|
|
|
|
parent.Object.Deletes[e] = old
|
|
|
|
default:
|
|
|
|
ed := parent.Object.Updates[e]
|
|
|
|
addDiff(path[1:], kind, &ed, old, new)
|
|
|
|
parent.Object.Updates[e] = ed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
contract.Failf("unexpected path element type: %T", element)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 19:05:33 +00:00
|
|
|
// TranslateDetailedDiff converts the detailed diff stored in the step event into an ObjectDiff that is appropriate
|
2019-07-01 19:34:19 +00:00
|
|
|
// for display.
|
Change `pulumi refresh` to report diff relative to desired state instead of relative to only output changes (#16146)
Presently, the behaviour of diffing during refresh steps is incomplete,
returning only an "output diff" that presents the changes in outputs.
This commit changes refresh steps so that:
* they compute a diff similar to the one that would be computed if a
`preview` were run immediately after the refresh, which is more
typically what users expect and want; and
* `IgnoreChanges` resource options are respected when performing the new
desired-state diffs, so that property additions or changes reported by a
refresh can be ignored.
In particular, `IgnoreChanges` can now be used to acknowledge that part
or all of a resource may change in the provider, but the user is OK with
this and doesn't want to be notified about it during a refresh.
Importantly, this means that the diff won't be reported, but also that
the changes won't be applied to state.
The implementation covers the following:
* A diff is computed using the inputs from the program and then
inverting the result, since in the case of a refresh the diff is being
driven by the provider side and not the program. This doesn't change
what is stored back into the state, but it does produce a diff that is
more aligned with the "true changes to the desired state".
* `IgnoreChanges` resource options are now stored in state, so that this
information can be used in refresh operations that do not have access
to/run the program.
* In the context of a refresh operation, `IgnoreChanges` applies to
*both* input and output properties. This differs from the behaviour of a
normal update operation, where `IgnoreChanges` only considers input
properties.
* The special `"*"` value for `IgnoreChanges` can be used to ignore all
properties. It _also_ ignores the case where the resource cannot be
found in the provider, and instead keeps the resource intact in state
with its existing input and output properties.
Because the program is not run for refresh operations, `IgnoreChanges`
options must be applied separately before a refresh takes place. This
can be accomplished using e.g. a `pulumi up` that applies the options
prior to a refresh. We should investigate perhaps providing a `pulumi
state set ...`-like CLI to make these sorts of changes directly to a
state.
For use cases relying on the legacy refresh diff provider, the
`PULUMI_USE_LEGACY_REFRESH_DIFF` environment variable can be set, which
will disable desired-state diff computation. We only need to perform
checks in `RefreshStep.{ResultOp,Apply}`, since downstream code will
work correctly based on the presence or absence of a `DetailedDiff` in
the step.
### Notes
- https://github.com/pulumi/pulumi/issues/16144 affects some of these
cases - though its technically orthogonal
- https://github.com/pulumi/pulumi/issues/11279 is another technically
orthogonal issue that many providers (at least TFBridge ones) - do not
report back changes to input properties on Read when the input property
(or property path) was missing on the inputs. This is again technically
orthogonal - but leads to cases that appear "wrong" in terms of what is
stored back into the state still - though the same as before this
change.
- Azure Native doesn't seem to handle `ignoreChanges` passed to Diff, so
the ability to ignore changes on refresh doesn't currently work for
Azure Native.
### Fixes
* Fixes #16072
* Fixes #16278
* Fixes #16334
* Not quite #12346, but likely replaces the need for that
Co-authored-by: Will Jones <will@sacharissa.co.uk>
2024-06-12 16:17:05 +00:00
|
|
|
func TranslateDetailedDiff(step *StepEventMetadata, refresh bool) *resource.ObjectDiff {
|
2023-02-15 01:28:14 +00:00
|
|
|
contract.Assertf(step.DetailedDiff != nil, "%v step has no detailed diff", step.Op)
|
2019-07-01 19:34:19 +00:00
|
|
|
|
|
|
|
// The rich diff is presented as a list of simple JS property paths and corresponding diffs. We translate this to
|
|
|
|
// an ObjectDiff by iterating the list and inserting ValueDiffs that reflect the changes in the detailed diff. Old
|
|
|
|
// values are always taken from a step's Outputs; new values are always taken from its Inputs.
|
|
|
|
|
|
|
|
var diff resource.ValueDiff
|
|
|
|
for path, pdiff := range step.DetailedDiff {
|
2019-07-31 16:39:07 +00:00
|
|
|
elements, err := resource.ParsePropertyPath(path)
|
2021-01-22 11:00:44 +00:00
|
|
|
if err != nil {
|
|
|
|
elements = []interface{}{path}
|
|
|
|
}
|
2019-07-01 19:34:19 +00:00
|
|
|
|
|
|
|
olds := resource.NewObjectProperty(step.Old.Outputs)
|
|
|
|
if pdiff.InputDiff {
|
|
|
|
olds = resource.NewObjectProperty(step.Old.Inputs)
|
|
|
|
}
|
Change `pulumi refresh` to report diff relative to desired state instead of relative to only output changes (#16146)
Presently, the behaviour of diffing during refresh steps is incomplete,
returning only an "output diff" that presents the changes in outputs.
This commit changes refresh steps so that:
* they compute a diff similar to the one that would be computed if a
`preview` were run immediately after the refresh, which is more
typically what users expect and want; and
* `IgnoreChanges` resource options are respected when performing the new
desired-state diffs, so that property additions or changes reported by a
refresh can be ignored.
In particular, `IgnoreChanges` can now be used to acknowledge that part
or all of a resource may change in the provider, but the user is OK with
this and doesn't want to be notified about it during a refresh.
Importantly, this means that the diff won't be reported, but also that
the changes won't be applied to state.
The implementation covers the following:
* A diff is computed using the inputs from the program and then
inverting the result, since in the case of a refresh the diff is being
driven by the provider side and not the program. This doesn't change
what is stored back into the state, but it does produce a diff that is
more aligned with the "true changes to the desired state".
* `IgnoreChanges` resource options are now stored in state, so that this
information can be used in refresh operations that do not have access
to/run the program.
* In the context of a refresh operation, `IgnoreChanges` applies to
*both* input and output properties. This differs from the behaviour of a
normal update operation, where `IgnoreChanges` only considers input
properties.
* The special `"*"` value for `IgnoreChanges` can be used to ignore all
properties. It _also_ ignores the case where the resource cannot be
found in the provider, and instead keeps the resource intact in state
with its existing input and output properties.
Because the program is not run for refresh operations, `IgnoreChanges`
options must be applied separately before a refresh takes place. This
can be accomplished using e.g. a `pulumi up` that applies the options
prior to a refresh. We should investigate perhaps providing a `pulumi
state set ...`-like CLI to make these sorts of changes directly to a
state.
For use cases relying on the legacy refresh diff provider, the
`PULUMI_USE_LEGACY_REFRESH_DIFF` environment variable can be set, which
will disable desired-state diff computation. We only need to perform
checks in `RefreshStep.{ResultOp,Apply}`, since downstream code will
work correctly based on the presence or absence of a `DetailedDiff` in
the step.
### Notes
- https://github.com/pulumi/pulumi/issues/16144 affects some of these
cases - though its technically orthogonal
- https://github.com/pulumi/pulumi/issues/11279 is another technically
orthogonal issue that many providers (at least TFBridge ones) - do not
report back changes to input properties on Read when the input property
(or property path) was missing on the inputs. This is again technically
orthogonal - but leads to cases that appear "wrong" in terms of what is
stored back into the state still - though the same as before this
change.
- Azure Native doesn't seem to handle `ignoreChanges` passed to Diff, so
the ability to ignore changes on refresh doesn't currently work for
Azure Native.
### Fixes
* Fixes #16072
* Fixes #16278
* Fixes #16334
* Not quite #12346, but likely replaces the need for that
Co-authored-by: Will Jones <will@sacharissa.co.uk>
2024-06-12 16:17:05 +00:00
|
|
|
|
|
|
|
news := resource.NewObjectProperty(step.New.Inputs)
|
|
|
|
if refresh {
|
|
|
|
news = resource.NewObjectProperty(step.New.Outputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
addDiff(elements, pdiff.Kind, &diff, olds, news)
|
2019-07-01 19:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return diff.Object
|
|
|
|
}
|