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

Ensure Alerting Rules Not Generated for Empty Alerting Fields on TF State File #939

Merged
merged 11 commits into from
Jun 12, 2023

Conversation

elainevuong
Copy link
Contributor

@elainevuong elainevuong commented Jun 10, 2023

Updates Terraform Provider to ensure Alerting rules are not generated for instances where the Alerting field on the Terraform state file is not specified by the user

This PR addresses the three scenarios on the state of the Alerting Field within a Terraform State file:

  • if the Alerting Block within the Terraform State file is populated, then the appropriate recording/alerting/labels are generated
  • if the Alerting Block within the Terraform State file is non-existent, then no alerting rules are generated
  • if the Alerting Block within the Terraform State file is an empty block ({}) then the default alerting rules are generated

… for instances where the Alerting field on the Terraform state file is not specified by the user
@elainevuong elainevuong requested a review from a team as a code owner June 10, 2023 00:08
@elainevuong elainevuong self-assigned this Jun 10, 2023
@github-actions
Copy link

In order to lower resource usage and have a faster runtime, PRs will not run Cloud tests automatically. To do so, a Grafana Labs employee must promote the Drone build.

For maintainers, it's better to run only the Cloud tests you need, rather than all of them. You can do so by setting the following parameter when promoting:

TESTARGS='-run=<testname>'

@elainevuong elainevuong marked this pull request as draft June 10, 2023 00:52
@elainevuong elainevuong marked this pull request as ready for review June 10, 2023 01:31
Copy link
Contributor

@joeblubaugh joeblubaugh left a comment

Choose a reason for hiding this comment

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

I "approve" this - I'll ask one of the provider maintainers to also give this a look today; since it's my first review of this codebase I want to have an expert review my review.

  • if the Alerting Block within the Terraform State file is populated, then the appropriate recording/alerting/labels are generated
  • if the Alerting Block within the Terraform State file is non-existent, then no alerting rules are generated
  • if the Alerting Block within the Terraform State file is an empty block ({}) then the default alerting rules are generated

👍

if len(alerting) > 0 {
alert := alerting[0].(map[string]interface{})
tfalerting = packAlerting(alert)
alerting, alertingOK := d.GetOk("alerting")
Copy link
Contributor

Choose a reason for hiding this comment

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

It's more idiomatic to collapse this into the conditional - you don't need to worry about alerting, and alertingOK "contaminating" the scope outside of the block in that case:

if alerting, ok := d.GetOk("alerting"); ok {
  ...
}

// alerting is no longer declared here, compile-time error:
alerting = nil

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed with ☝🏽

tfalerting = packAlerting(alert)
}

slo := gapi.Slo{
Copy link
Contributor

@joeblubaugh joeblubaugh Jun 12, 2023

Choose a reason for hiding this comment

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

Instead of an early return here, consider just modifying the Slo object and having a single return for a no- error execution. I think it reduces the mental burden of modifying it later:

slo := gapi.Slo{
	UUID:        d.Id(),
	Name:        tfname,
	Objectives:  tfobjective,
	Query:       tfquery,
	Alerting:    nil,
	Labels:      tflabels,
}

if alerting, ok := d.GetOk("alerting"); ok {
  ...
  tfAlerting := packAlerting(alert)
  slo.Alerting = &tfAlerting
}

return slo, nil

Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

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.

LGTM. I echo @joeblubaugh's comments

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.

LGTM but if you can address @joeblubaugh comments it will look much better 😬

if len(alerting) > 0 {
alert := alerting[0].(map[string]interface{})
tfalerting = packAlerting(alert)
alerting, alertingOK := d.GetOk("alerting")
Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed with ☝🏽

tfalerting = packAlerting(alert)
}

slo := gapi.Slo{
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@elainevuong elainevuong merged commit 90d45e7 into master Jun 12, 2023
@elainevuong elainevuong deleted the 938-empty-alerting-block branch June 12, 2023 17:48
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.

SLO: Empty Alerting Block on Terraform State File Generating Alerting Rules
4 participants