Skip to content
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

Remove panic on imports for TF Plugin Framework #1317

Closed
wants to merge 6 commits into from

Conversation

t0yv0
Copy link
Member

@t0yv0 t0yv0 commented Jul 31, 2023

This PR removes the panics on getDefaultValue on the Type, Attr, and Block schemas in the TF Plugin Framework schemashim, as they are causing issues when importing resources using the Plugin Framework.

Instead, we emit nil and an error, which we log using our standard logger, allowing the code to run without panic. I have build and run this against the user reported panic and verified that this resolves the issue.

We will file a followup issue to implement properly handling default values directly inside the pf package.

Fixes pulumi/pulumi-cloudflare#460.

@@ -120,7 +120,7 @@ func (*blockSchema) DefaultFunc() shim.SchemaDefaultFunc {
}

func (*blockSchema) DefaultValue() (interface{}, error) {
panic("DefaultValue() should not be called during schema generation")
return nil, nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy the comment here from another location ..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And there's another place I think where we do this.


func newTestnest() resource.Resource {
return &testnest{}
}

func (*testnest) schema() rschema.Schema {
return rschema.Schema{
Attributes: map[string]rschema.Attribute{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding as requirement for testing import against this resource.

@github-actions
Copy link

Diff for pulumi-azuread with merge commit d92c3b1

@github-actions
Copy link

Diff for pulumi-random with merge commit d92c3b1

@github-actions
Copy link

github-actions bot commented Aug 2, 2023

Diff for pulumi-azuread with merge commit def673f

@github-actions
Copy link

github-actions bot commented Aug 2, 2023

Diff for pulumi-azuread with merge commit 4328799

@guineveresaenger guineveresaenger changed the title Toward fixing Cloudflare import panic Remove panic on imports for TF Plugin Framework Aug 2, 2023
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

Diff for pulumi-azuread with merge commit 084e286

@github-actions
Copy link

github-actions bot commented Aug 2, 2023

Diff for pulumi-azuread with merge commit ac38249

@guineveresaenger guineveresaenger requested review from iwahbe and a team and removed request for guineveresaenger August 2, 2023 23:11
@guineveresaenger guineveresaenger marked this pull request as ready for review August 2, 2023 23:13
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

Diff for pulumi-azuread with merge commit f703fcc

Copy link
Member

@iwahbe iwahbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that this will swallow a generic error. Can we give fmt.Errorf("default values not supported") a concrete global value and then only swallow that value (which cannot be user generated).

// internal
var SchemaTimeDefaultErr = fmt.Errorf("default values not supported")

...

// pf/internal/schemashim/attr_schema.go
func (*attrSchema) DefaultValue() (interface{}, error) {
	return nil, SchemaTimeDefaultErr
}

...

// pkg/tfbridge/schema.go
func getDefaultValue(tfs shim.Schema, ps *SchemaInfo) interface{} {
	if dv, err := tfs.DefaultValue(); dv != nil &&
	err != nil &&
	!errors.Is(err, SchemaTimeDefaultErr) {
		if err != nil {
			return nil, err
		}
		return dv
	}
	return nil
}

if err != nil {
// Log error output but continue otherwise.
// This avoids a panic on preview such as https://github.com/pulumi/pulumi-cloudflare/issues/460.
glog.V(9).Infof(err.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user default value function returns an error, I would expect us to halt the pulumi up and display the error to the user. Swallowing a generic error, especially on v9, seems dangerous.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue here is that there’s no fix for the user. We need to implement proper default value handling in the plug-in framework as a followup. Adding this error but emitting it as Info I felt was more informative than simply returning nil.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we can't halt pulumi up in this case! This is the entire point of this PR. The code path is used to try to make import generated code better by taking default values into account, but Plugin Framework does not at this point support detecting default values, so the code must proceed with best-effort like returning nil. I think the behavior as written is what we want (modulo code style).

@@ -1407,7 +1406,12 @@ func extractInputs(oldInput, newState resource.PropertyValue, tfs shim.Schema, p
}

func getDefaultValue(tfs shim.Schema, ps *SchemaInfo) interface{} {
if dv, _ := tfs.DefaultValue(); dv != nil {
if dv, err := tfs.DefaultValue(); dv != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have changed DefaultValue to return nil, fmt.Errorf("default values not supported"). The error won't even be logged here, since dv == nil. We need to also check that err != nil.

@guineveresaenger
Copy link
Contributor

Using a custom error is the first thing I tried; the issue is that we can't declare it inside tf/internal and have it available from another package. Hopefully moving it to tfbridge, where we look for it, makes sense here.

We've been swallowing an error on tfs.DefaultValue for quite a while. I'm somewhat concerned that explicitly returning an error here is going to have other effects I'm not aware of.

@github-actions
Copy link

github-actions bot commented Aug 3, 2023

Diff for pulumi-azuread with merge commit bb64958

@github-actions
Copy link

github-actions bot commented Aug 3, 2023

Diff for pulumi-random with merge commit bb64958

// SchemaDefaultValueErr catches the situation where pf/schemashim.DefaultValue() is called by ExtractInputsFromOutputs.
// To avoid a panic, we return this error and log it to Info.
// See https://github.com/pulumi/pulumi-cloudflare/issues/460
var SchemaDefaultValueErr = fmt.Errorf("default values not supported")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just say Internal also in the comment - not something we expect users to need to link to.

@t0yv0
Copy link
Member Author

t0yv0 commented Aug 3, 2023

I think the is good to go modulo lint issues.

make lint should get you there.

  Running [/home/runner/golangci-lint-1.53.3-linux-amd64/golangci-lint run --out-format=github-actions] in [] ...
  Warning: error-naming: error var SchemaDefaultValueErr should have name of the form ErrFoo (revive)

@t0yv0
Copy link
Member Author

t0yv0 commented Aug 3, 2023

@guineveresaenger can you reopen a PR in your name since you're driving this work. Then I can approve. thanks.

@t0yv0 t0yv0 closed this Aug 3, 2023
@guineveresaenger guineveresaenger deleted the t0yv0/wip-cloudflare-panic branch February 23, 2024 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reading cloudflare:index:Ruleset Causes Panic
3 participants