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

Save dashboard sha256sum instead of full config json in tfstate #222

Merged
merged 17 commits into from Feb 23, 2022

Conversation

fgouteroux
Copy link
Contributor

Hi,

In some cases we could have bigger grafana dashboard, that would cause:

  • the unability to review changes inside (even in git when we have thousand of lines)
  • a bigger statefile size and some errors like Increase Statefile Size  #199

I would like to add the possibility to save only the sha256sum of the config json, if the option config_json_sha256 is set to true.

Also notice that we can go back to the full config json if we disable this parameter by default to false, so there is no breaking change.

@fgouteroux
Copy link
Contributor Author

no feedback on it ?

@justinTM
Copy link
Contributor

justinTM commented Jan 28, 2022

oh this would be huge. thank you. since GitLab jobs have 4MB printout limit, i've just been redirecting the entire Apply to a file because we're at 300k lines of JSON and this would be much better

Copy link
Contributor

@inkel inkel left a comment

Choose a reason for hiding this comment

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

As far as the changes go it LGTM. Let me discuss it with the rest of the @grafana/terraform-provider team.

grafana/resource_dashboard.go Outdated Show resolved Hide resolved
@fgouteroux fgouteroux requested a review from a team as a code owner February 4, 2022 17:51
Copy link
Member

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, I actually thought I had commented this but I never clicked the button 😓

I don't think setting an env variable (the os.Setenv) and using it in the code below is the right approach. Here's what I think should be done:

  1. With a DefaultFunc, if GRAFANA_CONFIG_JSON_SHA256 is set to a truthy value (using strconv.ParseBool), then the default is true, otherwise it's false
  2. In the code, only use the value of config_json_sha256, at this point, the env variable is irrelevant, it serves as a way to set this globally, but it shouldn't be used in the resources themselves

What this allows is to be able to set this to false and have it true in a single resource or vice versa

@fgouteroux
Copy link
Contributor Author

Sorry for the delay, I actually thought I had commented this but I never clicked the button 😓

I don't think setting an env variable (the os.Setenv) and using it in the code below is the right approach. Here's what I think should be done:

  1. With a DefaultFunc, if GRAFANA_CONFIG_JSON_SHA256 is set to a truthy value (using strconv.ParseBool), then the default is true, otherwise it's false
  2. In the code, only use the value of config_json_sha256, at this point, the env variable is irrelevant, it serves as a way to set this globally, but it shouldn't be used in the resources themselves

What this allows is to be able to set this to false and have it true in a single resource or vice versa

@julienduchesne Thanks for the review. I agree with you that setting env var should be not used in the code but this env var is used in the StateFunc: normalizeDashboardConfigJSON of config_json input. I don't know how to get the value of config_json_sha256 input in this StateFunc.

@julienduchesne
Copy link
Member

Good point. It doesn't give you the resource data. Then there's no way to handle it on a resource basis so I'd make this a provider setting since it has to be a global setting. Currently, setting the value to true for a single dashboard will enable it for all, this could be confusing

And again, setting a global var on provider init is probably preferable to using os.Getenv at various points (this allows configuration from both a provider option or an env var)

Second thing for this to get merged, is that I'd like a test

@fgouteroux
Copy link
Contributor Author

I agree, but there is something not clear for me. How to give the provider value 'store_dashboard_sha256' into the state function of the variable 'config_json' in resource_dahboard.go needed for the diff ?

@julienduchesne
Copy link
Member

I agree, but there is something not clear for me. How to give the provider value 'store_dashboard_sha256' into the state function of the variable 'config_json' in resource_dahboard.go needed for the diff ?

I'd set it as a global var on provider startup. We don't have many options here unfortunately. At least, it'll be clear that it's global

@fgouteroux
Copy link
Contributor Author

@julienduchesne I'm not sure that this is what you mean with global var on provider startup. Am I on the good way or not ?

@julienduchesne
Copy link
Member

@julienduchesne I'm not sure that this is what you mean with global var on provider startup. Am I on the good way or not ?

Can you make that a regular var: var storeDashboardSHA256 bool in provider.go instead of an env var? That way you won't have to set/parse an envvar

@fgouteroux
Copy link
Contributor Author

@julienduchesne I'm not sure that this is what you mean with global var on provider startup. Am I on the good way or not ?

Can you make that a regular var: var storeDashboardSHA256 bool in provider.go instead of an env var? That way you won't have to set/parse an envvar

yes, it 's more cleaner...

@julienduchesne
Copy link
Member

Alright, this LGTM. Can you add an acceptance test for this?

@julienduchesne julienduchesne dismissed their stale review February 21, 2022 18:00

Just needs a test now

@fgouteroux
Copy link
Contributor Author

Is that test is enough ?

Copy link
Member

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

I think this needs a full acceptance test that uses the actual terraform test framework. This is a major change of behavior of the dashboard resource and we need to check various things like that there isn't drift right after an apply. It should apply the resource and check that the saved state is a sha256, also test an update

Also, I suspect that there is no test that checks that currently, we are saving valid json. We should validate that also in one of the existing tests

grafana/resource_dashboard_test.go Outdated Show resolved Hide resolved
@fgouteroux
Copy link
Contributor Author

fgouteroux commented Feb 22, 2022

@julienduchesne I added the acc test with create/update but it works only with setting env var GRAFANA_STORE_DASHBOARD_SHA256. I try to add this without success

// testAccPreCheckSHA256 verifies required provider testing configuration. It should
// be present in TestAccDashboard_isSHA256_stored acceptance test.
//
// These verifications and configuration are preferred at this level to prevent
// provider developers from experiencing less clear errors for every test.
func testAccPreCheckSHA256(t *testing.T) {
	for _, e := range testAccPreCheckEnv {
		if v := os.Getenv(e); v == "" {
			t.Fatal(e + " must be set for acceptance tests")
		}
	}
	testAccProviderConfigure.Do(func() {
		// Since we are outside the scope of the Terraform configuration we must
		// call Configure() to properly initialize the provider configuration.
		raw := map[string]interface{}{
			"store_dashboard_sha256": true,
		}
		err := testAccProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(raw))
		if err != nil {
			t.Fatal(err)
		}
	})
}

Copy link
Member

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

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

I fixed the tests. This LGTM!

},
},
})
for _, useSHA256 := range []bool{false, true} {
Copy link
Member

Choose a reason for hiding this comment

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

Review without whitespace changes. I reused the basic test but when it's a sha256, the expected state is different

Copy link
Contributor

Choose a reason for hiding this comment

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

@fgouteroux have you had a chance to look at this comment?

Copy link
Member

Choose a reason for hiding this comment

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

It wasn't a comment for him. Just a general "if you review this test, review it without whitespace". I wrote that test 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Aaaahhh ok, my bad, then.

So, just to see if I got this straight: the test fails?

Copy link
Member

Choose a reason for hiding this comment

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

No, the test passes. It just shows lots of changes because indentation changes

@julienduchesne julienduchesne requested review from inkel and a team February 22, 2022 15:17
@fgouteroux
Copy link
Contributor Author

I fixed the tests. This LGTM!

@julienduchesne many thanks

@inkel
Copy link
Contributor

inkel commented Feb 22, 2022

I'm reviewing, give me a couple minutes.

Copy link
Contributor

@inkel inkel left a comment

Choose a reason for hiding this comment

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

I've a minor request, once that is addressed I'll be happy to re-review and approve.

grafana/resource_dashboard.go Outdated Show resolved Hide resolved
Copy link
Contributor

@inkel inkel left a comment

Choose a reason for hiding this comment

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

Great work, @fgouteroux! Thank you for your contribution.

@inkel inkel merged commit 2828480 into grafana:master Feb 23, 2022
@fgouteroux
Copy link
Contributor Author

Great work, @fgouteroux! Thank you for your contribution.

@inkel @julienduchesne Thanks for your tips/feedback. I'm happy to see it merged ;-)

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.

None yet

4 participants