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

fix: Better method to detect helm chart version in template tests #3781

Merged
merged 6 commits into from
Jul 16, 2024

Conversation

harshshahsumo
Copy link
Contributor

This change finds a new method using regex for updating the helm chart version without affecting any other version that matches it
Find the Ticket

Checklist

  • Changelog updated or skip changelog label added
  • Documentation updated
  • Template tests added for new features
  • Integration tests added or modified for major features

@harshshahsumo harshshahsumo requested a review from a team as a code owner June 25, 2024 18:04
@sumo-drosiek
Copy link
Contributor

Please add unit tests for this to prevent regression in case of change

Comment on lines 136 to 143
patternString := fmt.Sprintf(
`(?m)^(\s*app\.kubernetes\.io\/version:\s*"%s"\s*)$`+
`|^(\s*chart:\s*"sumologic-%s"\s*)$`+
`|^(\s*chart:\s*sumologic-%s\s*)$`+
`|^(\s*client:\s*k8s_%s\s*)$`+
`|^(\s*value:\s*"%s"\s*)$`,
chartVersion, chartVersion, chartVersion, chartVersion, chartVersion)
pattern := regexp.MustCompile(patternString)
Copy link
Contributor

@sumo-drosiek sumo-drosiek Jul 8, 2024

Choose a reason for hiding this comment

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

As it's correct, it's not so easy to understand the regex. I would recommend to create list of strings (regexes if strings won't be sufficient), and then iterate over them and replace them in the output. I wouldn't want to maintain it in actual form after all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed

Copy link
Contributor

@sumo-drosiek sumo-drosiek left a comment

Choose a reason for hiding this comment

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

I believe this is better. It's following more DRY concept

Comment on lines 136 to 142
replacements := map[string]string{
fmt.Sprintf("app.kubernetes.io/version: \"%s\"", chartVersion): "app.kubernetes.io/version: \"%CURRENT_CHART_VERSION%\"",
fmt.Sprintf("chart: \"sumologic-%s\"", chartVersion): "chart: \"sumologic-%CURRENT_CHART_VERSION%\"",
fmt.Sprintf("chart: sumologic-%s", chartVersion): "chart: sumologic-%CURRENT_CHART_VERSION%",
fmt.Sprintf("client: k8s_%s", chartVersion): "client: k8s_%CURRENT_CHART_VERSION%",
fmt.Sprintf("value: \"%s\"", chartVersion): "value: \"%CURRENT_CHART_VERSION%\"",
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
replacements := map[string]string{
fmt.Sprintf("app.kubernetes.io/version: \"%s\"", chartVersion): "app.kubernetes.io/version: \"%CURRENT_CHART_VERSION%\"",
fmt.Sprintf("chart: \"sumologic-%s\"", chartVersion): "chart: \"sumologic-%CURRENT_CHART_VERSION%\"",
fmt.Sprintf("chart: sumologic-%s", chartVersion): "chart: sumologic-%CURRENT_CHART_VERSION%",
fmt.Sprintf("client: k8s_%s", chartVersion): "client: k8s_%CURRENT_CHART_VERSION%",
fmt.Sprintf("value: \"%s\"", chartVersion): "value: \"%CURRENT_CHART_VERSION%\"",
}
replacements :=[]string{
"app.kubernetes.io/version: \"%s\"",
"chart: \"sumologic-%s\"",
"chart: sumologic-%s",
"client: k8s_%s",
"value: \"%s\"",
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed

Comment on lines 146 to 148
for oldString, newString := range replacements {
output = strings.ReplaceAll(output, oldString, newString)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for oldString, newString := range replacements {
output = strings.ReplaceAll(output, oldString, newString)
}
for_, r := range replacements {
output = strings.ReplaceAll(output, fmt.Sprintf(r, chartVersion), fmt.Sprintf(r, "%CURRENT_CHART_VERSION%"))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed

Copy link
Contributor

@echlebek echlebek left a comment

Choose a reason for hiding this comment

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

Almost good to go, a few things need fixing still

Comment on lines 136 to 142
// replacements := map[string]string{
// fmt.Sprintf("app.kubernetes.io/version: \"%s\"", chartVersion): "app.kubernetes.io/version: \"%CURRENT_CHART_VERSION%\"",
// fmt.Sprintf("chart: \"sumologic-%s\"", chartVersion): "chart: \"sumologic-%CURRENT_CHART_VERSION%\"",
// fmt.Sprintf("chart: sumologic-%s", chartVersion): "chart: sumologic-%CURRENT_CHART_VERSION%",
// fmt.Sprintf("client: k8s_%s", chartVersion): "client: k8s_%CURRENT_CHART_VERSION%",
// fmt.Sprintf("value: \"%s\"", chartVersion): "value: \"%CURRENT_CHART_VERSION%\"",
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

Commented out code should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

Comment on lines 144 to 148
"app.kubernetes.io/version: \"%s\"",
"chart: \"sumologic-%s\"",
"chart: sumologic-%s",
"client: k8s_%s",
"value: \"%s\"",
Copy link
Contributor

Choose a reason for hiding this comment

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

Use %q instead of %s for items that need to be surrounded in quotes, as this allows you to avoid escapes here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed

Copy link
Contributor

@echlebek echlebek left a comment

Choose a reason for hiding this comment

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

Looks good 🚢

@harshshahsumo harshshahsumo merged commit 4e68f0c into main Jul 16, 2024
55 checks passed
@harshshahsumo harshshahsumo deleted the osc665_newMethodForTemplateTests branch July 16, 2024 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants