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

Use RUNBOOK_URL_TEMPLATE env for the runbooks URL template #524

Merged
merged 1 commit into from Apr 25, 2023
Merged

Use RUNBOOK_URL_TEMPLATE env for the runbooks URL template #524

merged 1 commit into from Apr 25, 2023

Conversation

assafad
Copy link
Contributor

@assafad assafad commented Mar 14, 2023

What this PR does / why we need it:
When RUNBOOK_URL_TEMPLATE env variable is set, its value will be used as the runbooks URL template. Otherwise, keep using the default runbook URL template https://kubevirt.io/monitoring/runbooks/%s.
This allows using different runbooks versions, in accordance with the installation.

Which issue(s) this PR fixes:
Jira-ticket - https://issues.redhat.com/browse/CNV-14382

Special notes for your reviewer:

Release note:

None

@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Mar 14, 2023
@openshift-ci openshift-ci bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Mar 14, 2023
@openshift-ci
Copy link

openshift-ci bot commented Mar 14, 2023

Hi @assafad. Thanks for your PR.

I'm waiting for a kubevirt member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@assafad
Copy link
Contributor Author

assafad commented Mar 14, 2023

/hold

@kubevirt-bot kubevirt-bot added size/M do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Mar 14, 2023
@0xFelix
Copy link
Member

0xFelix commented Mar 15, 2023

Thanks! Can you add tests for the new feature please?

@akrejcir
Copy link
Collaborator

Does the env variable have to be a format string, or will it always be a prefix?

In my opinion, accepting a format string as an env variable doesn't seem safe. It could probably cause a panic, if the formatting parameters are incorrect.

@assafad
Copy link
Contributor Author

assafad commented Mar 19, 2023

Blocked by kubevirt/monitoring#165

@assafad
Copy link
Contributor Author

assafad commented Mar 19, 2023

Thanks! Can you add tests for the new feature please?

@0xFelix Hi, I've added some tests on internal/operands/metrics/reconcile_test.go. Beware that we also have this test, that checks if the runbooks are accessible. Please let me know if the new tests make sense.

@assafad
Copy link
Contributor Author

assafad commented Mar 19, 2023

Does the env variable have to be a format string, or will it always be a prefix?

In my opinion, accepting a format string as an env variable doesn't seem safe. It could probably cause a panic, if the formatting parameters are incorrect.

@akrejcir Not sure I fully understand your concern. We don't assume that RUNBOOK_URL_TEMPLATE is a string (os.LookUpEnv() returns a string), and don't assume it's a prefix. Different installations may use different runbooks versions, thus we only assume (and verify) the template contains exactly 1 %s, as runbook URLs contain the alerts names.

@0xFelix
Copy link
Member

0xFelix commented Mar 21, 2023

@akrejcir wanted to point out that you can inject arbitrary format specifiers when reading RUNBOOK_URL_TEMPLATE from the envs and using it as a format string (e.g. https://kubevirt.io/monitoring/runbooks/%d, what will happen?). Can't you make it a prefix instead, so it is safe to read from the envs?

@kubevirt-bot kubevirt-bot added release-note-none Denotes a PR that doesn't merit a release note. and removed release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Mar 22, 2023
@assafad
Copy link
Contributor Author

assafad commented Mar 22, 2023

@akrejcir wanted to point out that you can inject arbitrary format specifiers when reading RUNBOOK_URL_TEMPLATE from the envs and using it as a format string (e.g. https://kubevirt.io/monitoring/runbooks/%d, what will happen?). Can't you make it a prefix instead, so it is safe to read from the envs?

@0xFelix, @akrejcir For now, U/S URLs template is indeed a prefix, but different installations/versions have different documentation requirements. Thus, assuming the alerts names are suffix is not generic enough and might require future updates. I validated here that RUNBOOK_URL_TEMPLATE value contains exactly 1 %s, and in addition we have this test that verifies the runbooks are accessible. Let me know if expanding the %s validation can reduce your concern.

@akrejcir
Copy link
Collaborator

Ok, we can keep this for now. If the pattern is incorrect, the resulting string will contain an error message https://pkg.go.dev/fmt#hdr-Format_errors . So it will not panic.

@akrejcir
Copy link
Collaborator

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Mar 22, 2023
@0xFelix
Copy link
Member

0xFelix commented Mar 24, 2023

@akrejcir @assafad So it will not panic but instead will fail silently? IIUC the formatted string will contain the error message, but we don't check that?

@akrejcir
Copy link
Collaborator

@akrejcir @assafad So it will not panic but instead will fail silently? IIUC the formatted string will contain the error message, but we don't check that?

Right, it's not a good solution.

@assafad, can you change the code, so that the env variable RUNBOOK_URL_TEMPLATE will contain our own simple template format with a placeholder, that will be replaced using strings.Replace().
For example:

https://kubevirt.io/monitoring/runbooks/{}

Where {} will be replaced.

@akrejcir
Copy link
Collaborator

/lgtm cancel

@kubevirt-bot kubevirt-bot removed the lgtm Indicates that a PR is ready to be merged. label Mar 24, 2023
@assafad
Copy link
Contributor Author

assafad commented Apr 18, 2023

/retest
/unhold

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 18, 2023
}

if strings.Count(runbookURLTemplate, "%s") != 1 || strings.Count(runbookURLTemplate, "%") != 1 {
panic(errors.New("runbook URL template must have exactly 1 %s substring"))
Copy link
Member

Choose a reason for hiding this comment

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

Can you return an error instead of panicking? The error is caused by user input and that could be handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, please let me know if the change makes sense

@assafad
Copy link
Contributor Author

assafad commented Apr 18, 2023

/ok-to-test

@kubevirt-bot kubevirt-bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 18, 2023
@assafad
Copy link
Contributor Author

assafad commented Apr 19, 2023

/retest

internal/operands/metrics/reconcile_test.go Outdated Show resolved Hide resolved
internal/operands/metrics/resources.go Outdated Show resolved Hide resolved
internal/operands/metrics/resources.go Outdated Show resolved Hide resolved
Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

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

Just one more nit, otherwise looks good to me!

},
Entry("should use the default template when no ENV variable is set", defaultRunbookURLTemplate),
Entry("should use the ENV variable when a valid value is set", "valid/runbookURL/template/%s"),
Entry("should throw an error when an invalid ENV variable value is set", "invalid/runbookURL/template/"),
Copy link
Member

Choose a reason for hiding this comment

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

Add negative test case for another format specifier, e.g. %d?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

Signed-off-by: assafad <aadmi@redhat.com>
@sonarcloud
Copy link

sonarcloud bot commented Apr 24, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.2% 0.2% Duplication

@assafad
Copy link
Contributor Author

assafad commented Apr 25, 2023

/retest

@0xFelix
Copy link
Member

0xFelix commented Apr 25, 2023

Thank you! It looks good to me!

@akrejcir WDYT?

/approve

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 0xFelix

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 25, 2023
@akrejcir
Copy link
Collaborator

Looks good, thanks.

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Apr 25, 2023
@kubevirt-bot kubevirt-bot merged commit 77a99cd into kubevirt:master Apr 25, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants