-
Notifications
You must be signed in to change notification settings - Fork 43
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
Provider2 upgrade state rewrite #1998
Conversation
Let's confirm this code is covered by tests? I'd like to get this in quickly as it demonstrably reduces adapter bugs for PlanResourceChange resources. Hoping this is isolated enough that no downstream check failures will be triggered but we should find out. Thank you! |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1998 +/- ##
==========================================
+ Coverage 60.82% 60.85% +0.02%
==========================================
Files 332 332
Lines 44727 44754 +27
==========================================
+ Hits 27206 27235 +29
+ Misses 15998 15994 -4
- Partials 1523 1525 +2 ☔ View full report in Codecov by Sentry. |
pkg/tfshim/sdk-v2/provider2.go
Outdated
|
||
ty := res.CoreConfigSchema().ImpliedType() | ||
|
||
jsonIsh, err := schema.StateValueToJSONMap(state.stateValue, ty) |
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.
Calling schema.StateValueToJSONMap
looses information. The implementation of schema.StateValueToJSONMap
is:
// StateValueToJSONMap converts a cty.Value to generic JSON map via the cty JSON
// encoding.
func StateValueToJSONMap(val cty.Value, ty cty.Type) (map[string]interface{}, error) {
js, err := ctyjson.Marshal(val, ty)
if err != nil {
return nil, err
}
var m map[string]interface{}
if err := json.Unmarshal(js, &m); err != nil {
return nil, err
}
return m, nil
}
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.
Which information is lost here?
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.
Appreciate unpacking the concern a bit so we can address!
This is extracted from #1971 which we started taking as far as downstream tests.
I noticed in your previous rewrite @iwahbe you prefer passing the information via flatmap
IN here we don't have easy access to flatmap but we're working with cty.Value:
type v2InstanceState2 struct {
resourceType string
stateValue cty.Value
// Also known as private state.
meta map[string]interface{}
}
If there's a way to go from cty.Value to flatmap that's better than going from cty.Value to json-ish we could use it here.
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.
Also noting that ty := res.CoreConfigSchema().ImpliedType()
is not good and we need to revisit this. This is introducing the current provider's schema into the process of serializing inputs for the state upgrader which is not good - something that's going to break big time when the schema is changing.
For now we should consider using the current value's implied schema, at least that is closer to the desired outcome in spirit:
state.stateValue.Type()
Ideally we'd have some flatmap representation that is as-is preserved from the last write to Pulumi state but we do not have that yet without further work.
The fact that this was not caught also makes me think we need a test that exercises actually changing the provider schema in a breaking way and using a state upgrader to compensate for this; this would verify that bridged providers can support this. This test would have probably failed here.
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.
We don't need to use flatmap. I used it previously because that was what we were doing before. We should use JSON for inputs.
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.
I'm thinking state.stateValue.Type() can backfire as well as it can't get schema Set functions from schema etc.. Eh, no good options here until #1667 lands. Should at least comment and link that.
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.
I think this is an improvement, and would be happy to merge.
We should have at least basic unit test coverage before merging. If we believe that this fixes any bugs, then the new fixes should be under test.
This doesn't help with translating large numbers. To accommodate large numbers, we need to merge #2001 into #1998.
I'd actually really like to see an integration test when we're upgrading a resource with
unless we have one test already maybe it's just not seeing into PlanResourceChange flag. It's important high-level functionality that's great to confirm. If out of time I can contribute a test here. |
4a57f17
to
8933992
Compare
@iwahbe @t0yv0 I believe I addressed all the comments here and added some unit tests for the upgrade state function which seems to work well. I tried to also add an integration test but that does not work and I believe it is because of #1667 - we need the old schema in order to convert the state correctly in order to pass it to upgrade state - LMK if this does not seem correct. Overall, this certainly fixes some issues around resources which don't require an upgrade as shown in the now partly passing input cross tests, so still worth merging even if not all tf state upgrades can work. |
Do you have a sense if TestPlanResourceChangeStateUpgrade doesn't work because of changes in this PR or it's something that didn't work before either? |
TestPlanResourceChangeStateUpgrade did not work before either and the changes here did not change it. Unfortunately it fails during
|
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.
Very unfortunate it's a pre-existing condition. I think historically state upgraders did generally work and slightly unfortunate we might have introduced rigidity to make them less likely to work. But onwards and upwards, once we have the proper handling of RawState they will work precisely. Thank you!
recoverAndCoerceCtyValueWithSchema failure: please note makeResourceRawConfigClassic. This is used as a fallback for these failures. I think I was initially very skeptical of this as it feels like an imprecise method, but if we're choosing between failing entirely and using an imprecise method, in a system like this having an imprecise fallback that increases the probability of a program working buys us time until we have retrofitted precision into the system. Consider adding makeResourceRawConfigClassic as fallback for provider2 as well? Might turn some upgrade tests into passing tests. |
Stacked on top of #1998 --- This PR allows provider2.upgradeState to handle 64 bit integer numbers. The substance of the change is pulumi/terraform-plugin-sdk#39. The only change here is a test.
This were fixed in #1998
fixes #1916
fixes #1914
This rewrites the provider2 upgrade state implementation to use the upstream GRPC method instead of the bridge implementation. This should cut out a bunch of unnecessary code and some type conversions of the data.
Originally done as part of #1971 but I'm pulling this out to merge separately.
@t0yv0 did a lot of the heavy lifting here.