ROX-33465: Rework openshift auto-sensing in Helm chart#19482
ROX-33465: Rework openshift auto-sensing in Helm chart#19482mclasmeier wants to merge 12 commits intomasterfrom
Conversation
|
Skipping CI for Draft Pull Request. |
|
Images are ready for the commit at 7725a46. To use with deploy scripts, first |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #19482 +/- ##
==========================================
- Coverage 49.26% 49.26% -0.01%
==========================================
Files 2727 2727
Lines 205821 205821
==========================================
- Hits 101404 101399 -5
- Misses 96885 96888 +3
- Partials 7532 7534 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
9d45c67 to
219d34f
Compare
| {{/* Normalize user setting.*/}} | ||
| {{- if kindIs "string" $normalizedUserSpecifiedOpenShift -}} | ||
| {{/* User set something like env.openshift="4". */}} | ||
| {{- $normalizedUserSpecifiedOpenShift = int $normalizedUserSpecifiedOpenShift -}} |
There was a problem hiding this comment.
What happens if they specify env.openshift="true"? Does int cast that to 0 or 1?
There was a problem hiding this comment.
Just checked, it is parsed as 0. Not sure if we should also try to catch that (where to stop?).
There was a problem hiding this comment.
Good question. I guess it depends on what value types worked sensibly in the previous code. If this was already broken before, then we should right out reject such values, rather than trying to interpret them. 🤔
|
@porridge A missing conversion was caught by the |
|
@mclasmeier: The following test failed, say
Full PR test history. Your PR dashboard. DetailsInstructions 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-sigs/prow repository. I understand the commands that are listed here. |
There was a problem hiding this comment.
Hey - I've found 3 issues, and left some high level feedback:
- In
srox.autoSenseOpenshiftVersion, a couple ofprintfcalls pass extra arguments that are never interpolated (e.g. the warnings forenv.openshift=falseandenv.openshift=%d), so the auto-sensed value is not actually included in the message; consider fixing the format strings to surface both values or dropping the unused arguments. - The new
srox.failusage is inconsistent with the other helpers (srox.note,srox.warn), which are always called with(list $ <message>); ifsrox.failhas a similar signature, the currentinclude "srox.fail" $fmtmay not provide the context object as expected and could fail or produce less-informative output. - In
sensorGenerateOpenShiftCommand.ConstructOpenShift,s.openshiftVersionis now effectively ignored (no switch or validation), whileroxctl central generate k8sstill validates--openshift-version; consider either restoring basic validation or removing the flag to avoid confusing users with an option that has no effect.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `srox.autoSenseOpenshiftVersion`, a couple of `printf` calls pass extra arguments that are never interpolated (e.g. the warnings for `env.openshift=false` and `env.openshift=%d`), so the auto-sensed value is not actually included in the message; consider fixing the format strings to surface both values or dropping the unused arguments.
- The new `srox.fail` usage is inconsistent with the other helpers (`srox.note`, `srox.warn`), which are always called with `(list $ <message>)`; if `srox.fail` has a similar signature, the current `include "srox.fail" $fmt` may not provide the context object as expected and could fail or produce less-informative output.
- In `sensorGenerateOpenShiftCommand.ConstructOpenShift`, `s.openshiftVersion` is now effectively ignored (no switch or validation), while `roxctl central generate k8s` still validates `--openshift-version`; consider either restoring basic validation or removing the flag to avoid confusing users with an option that has no effect.
## Individual Comments
### Comment 1
<location path="image/templates/helm/shared/templates/_openshift.tpl" line_range="21-30" />
<code_context>
+{{- $normalizedUserSpecifiedOpenShift := $env.openshift -}}
+
+{{/* Normalize user setting. */}}
+{{- $userSpecifiedOpenShiftAsInt := int $normalizedUserSpecifiedOpenShift -}}
+{{- if kindIs "string" $normalizedUserSpecifiedOpenShift -}}
+ {{- if eq (printf "%d" $userSpecifiedOpenShiftAsInt) $normalizedUserSpecifiedOpenShift -}}
+ {{/* User set something like env.openshift="4". We allow this as well. */}}
+ {{- $normalizedUserSpecifiedOpenShift = $userSpecifiedOpenShiftAsInt -}}
+ {{- end -}}
+{{- end -}}
+{{- if kindIs "float64" $normalizedUserSpecifiedOpenShift -}}
+ {{/* YAML numeric values are sometimes parsed as float64... */}}
+ {{- $normalizedUserSpecifiedOpenShift = $userSpecifiedOpenShiftAsInt -}}
+{{- end -}}
+{{- if kindIs "int64" $normalizedUserSpecifiedOpenShift -}}
+ {{/* ... and sometimes as int64 (e.g. when doing helm --set env.openshift="4"). */}}
+ {{- $normalizedUserSpecifiedOpenShift = $userSpecifiedOpenShiftAsInt -}}
</code_context>
<issue_to_address>
**issue (bug_risk):** Casting `env.openshift` to `int` before validating its kind can cause template failures for unset or non-numeric values.
`int` is invoked on `$normalizedUserSpecifiedOpenShift` even when `env.openshift` is unset or non-numeric (e.g., `bool`), which can panic before the kind checks run. Move the `int` conversion into the branches that have already confirmed `$normalizedUserSpecifiedOpenShift` is a numeric or numeric-string value, and only then assign `$userSpecifiedOpenShiftAsInt`.
</issue_to_address>
### Comment 2
<location path="image/templates/helm/shared/templates/_openshift.tpl" line_range="80-97" />
<code_context>
+ {{/* User set env.openshift=false. */}}
+ {{- if gt $autoSensedOpenShift 0 -}}
+ {{/* Might indicate a configuration problem. */}}
+ {{- $fmt := printf
+ "User setting env.openshift=false is in contrast to the result of the auto-sensing, which yielded an OpenShift target cluster -- will proceed with user setting"
+ $autoSensedOpenShift -}}
+ {{- include "srox.warn" (list $ $fmt) -}}
+ {{- end -}}
+ {{- $_ := set $env "openshift" 0 -}}
+{{- else -}}
+ {{/* User set env.openshift=4. */}}
+ {{- if not (eq $normalizedUserSpecifiedOpenShift $autoSensedOpenShift) -}}
+ {{/* Might indicate a configuration problem. */}}
+ {{- $fmt := printf
+ "User setting env.openshift=%d is in contrast to the result of the auto-sensing, which yielded a non-OpenShift cluster -- will proceed with user setting"
+ $normalizedUserSpecifiedOpenShift $autoSensedOpenShift -}}
+ {{- include "srox.warn" (list $ $fmt) -}}
+ {{- end -}}
</code_context>
<issue_to_address>
**suggestion:** Several `printf` calls pass more arguments than there are format verbs, which is misleading and drops information.
In these warning branches, extra arguments are passed and then ignored by Go’s `printf` (e.g. the `env.openshift=false` case passes `$autoSensedOpenShift` but uses no `%` verbs, and the later message passes both `$normalizedUserSpecifiedOpenShift` and `$autoSensedOpenShift` but only has one `%d`). This hides the auto-sensed value and makes the intent harder to follow. Please either update the format strings to print all provided values or remove the unused arguments so calls and messages stay aligned.
```suggestion
{{/* User set env.openshift=false. */}}
{{- if gt $autoSensedOpenShift 0 -}}
{{/* Might indicate a configuration problem. */}}
{{- $fmt := printf
"User setting env.openshift=false is in contrast to the result of the auto-sensing, which yielded an OpenShift target cluster (auto-sensed value: %d) -- will proceed with user setting"
$autoSensedOpenShift -}}
{{- include "srox.warn" (list $ $fmt) -}}
{{- end -}}
{{- $_ := set $env "openshift" 0 -}}
{{- else -}}
{{/* User set env.openshift=4. */}}
{{- if not (eq $normalizedUserSpecifiedOpenShift $autoSensedOpenShift) -}}
{{/* Might indicate a configuration problem. */}}
{{- $fmt := printf
"User setting env.openshift=%d is in contrast to the result of the auto-sensing, which yielded a non-OpenShift cluster (auto-sensed value: %d) -- will proceed with user setting"
$normalizedUserSpecifiedOpenShift $autoSensedOpenShift -}}
{{- include "srox.warn" (list $ $fmt) -}}
{{- end -}}
```
</issue_to_address>
### Comment 3
<location path="roxctl/sensor/generate/openshift.go" line_range="21-23" />
<code_context>
func (s *sensorGenerateOpenShiftCommand) ConstructOpenShift() error {
s.cluster.Type = storage.ClusterType_OPENSHIFT4_CLUSTER
- switch s.openshiftVersion {
- case 0:
- case 3:
- s.cluster.Type = storage.ClusterType_OPENSHIFT_CLUSTER
- case 4:
- default:
- return errox.InvalidArgs.Newf("invalid OpenShift version %d, supported values are '3' and '4'", s.openshiftVersion)
- }
-
s.cluster.AdmissionControllerEvents = s.cluster.GetType() == storage.ClusterType_OPENSHIFT4_CLUSTER
// This is intentionally NOT feature-flagged, because we always want to set the correct (auto) value,
</code_context>
<issue_to_address>
**issue (bug_risk):** `openshiftVersion` is no longer validated and unsupported values are silently treated as OpenShift 4.
With the updated `ConstructOpenShift`, `s.cluster.Type` is always `OPENSHIFT4_CLUSTER` and `s.openshiftVersion` is ignored. This means values like `--openshift-version=3` or `--openshift-version=7` are silently treated as 4. In `roxctl/central/generate/k8s.go`, you still validate `openshiftVersion` and reject anything other than 0 or 4. For consistency and to avoid silent misconfiguration, please add equivalent validation here and return an error when `s.openshiftVersion` is non-zero and not 4.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| {{- $userSpecifiedOpenShiftAsInt := int $normalizedUserSpecifiedOpenShift -}} | ||
| {{- if kindIs "string" $normalizedUserSpecifiedOpenShift -}} | ||
| {{- if eq (printf "%d" $userSpecifiedOpenShiftAsInt) $normalizedUserSpecifiedOpenShift -}} | ||
| {{/* User set something like env.openshift="4". We allow this as well. */}} | ||
| {{- $normalizedUserSpecifiedOpenShift = $userSpecifiedOpenShiftAsInt -}} | ||
| {{- end -}} | ||
| {{- end -}} | ||
| {{- if kindIs "float64" $normalizedUserSpecifiedOpenShift -}} | ||
| {{/* YAML numeric values are sometimes parsed as float64... */}} | ||
| {{- $normalizedUserSpecifiedOpenShift = $userSpecifiedOpenShiftAsInt -}} |
There was a problem hiding this comment.
issue (bug_risk): Casting env.openshift to int before validating its kind can cause template failures for unset or non-numeric values.
int is invoked on $normalizedUserSpecifiedOpenShift even when env.openshift is unset or non-numeric (e.g., bool), which can panic before the kind checks run. Move the int conversion into the branches that have already confirmed $normalizedUserSpecifiedOpenShift is a numeric or numeric-string value, and only then assign $userSpecifiedOpenShiftAsInt.
| {{/* User set env.openshift=false. */}} | ||
| {{- if gt $autoSensedOpenShift 0 -}} | ||
| {{/* Might indicate a configuration problem. */}} | ||
| {{- $fmt := printf | ||
| "User setting env.openshift=false is in contrast to the result of the auto-sensing, which yielded an OpenShift target cluster -- will proceed with user setting" | ||
| $autoSensedOpenShift -}} | ||
| {{- include "srox.warn" (list $ $fmt) -}} | ||
| {{- end -}} | ||
| {{- $_ := set $env "openshift" 0 -}} | ||
| {{- else -}} | ||
| {{/* User set env.openshift=4. */}} | ||
| {{- if not (eq $normalizedUserSpecifiedOpenShift $autoSensedOpenShift) -}} | ||
| {{/* Might indicate a configuration problem. */}} | ||
| {{- $fmt := printf | ||
| "User setting env.openshift=%d is in contrast to the result of the auto-sensing, which yielded a non-OpenShift cluster -- will proceed with user setting" | ||
| $normalizedUserSpecifiedOpenShift $autoSensedOpenShift -}} | ||
| {{- include "srox.warn" (list $ $fmt) -}} | ||
| {{- end -}} |
There was a problem hiding this comment.
suggestion: Several printf calls pass more arguments than there are format verbs, which is misleading and drops information.
In these warning branches, extra arguments are passed and then ignored by Go’s printf (e.g. the env.openshift=false case passes $autoSensedOpenShift but uses no % verbs, and the later message passes both $normalizedUserSpecifiedOpenShift and $autoSensedOpenShift but only has one %d). This hides the auto-sensed value and makes the intent harder to follow. Please either update the format strings to print all provided values or remove the unused arguments so calls and messages stay aligned.
| {{/* User set env.openshift=false. */}} | |
| {{- if gt $autoSensedOpenShift 0 -}} | |
| {{/* Might indicate a configuration problem. */}} | |
| {{- $fmt := printf | |
| "User setting env.openshift=false is in contrast to the result of the auto-sensing, which yielded an OpenShift target cluster -- will proceed with user setting" | |
| $autoSensedOpenShift -}} | |
| {{- include "srox.warn" (list $ $fmt) -}} | |
| {{- end -}} | |
| {{- $_ := set $env "openshift" 0 -}} | |
| {{- else -}} | |
| {{/* User set env.openshift=4. */}} | |
| {{- if not (eq $normalizedUserSpecifiedOpenShift $autoSensedOpenShift) -}} | |
| {{/* Might indicate a configuration problem. */}} | |
| {{- $fmt := printf | |
| "User setting env.openshift=%d is in contrast to the result of the auto-sensing, which yielded a non-OpenShift cluster -- will proceed with user setting" | |
| $normalizedUserSpecifiedOpenShift $autoSensedOpenShift -}} | |
| {{- include "srox.warn" (list $ $fmt) -}} | |
| {{- end -}} | |
| {{/* User set env.openshift=false. */}} | |
| {{- if gt $autoSensedOpenShift 0 -}} | |
| {{/* Might indicate a configuration problem. */}} | |
| {{- $fmt := printf | |
| "User setting env.openshift=false is in contrast to the result of the auto-sensing, which yielded an OpenShift target cluster (auto-sensed value: %d) -- will proceed with user setting" | |
| $autoSensedOpenShift -}} | |
| {{- include "srox.warn" (list $ $fmt) -}} | |
| {{- end -}} | |
| {{- $_ := set $env "openshift" 0 -}} | |
| {{- else -}} | |
| {{/* User set env.openshift=4. */}} | |
| {{- if not (eq $normalizedUserSpecifiedOpenShift $autoSensedOpenShift) -}} | |
| {{/* Might indicate a configuration problem. */}} | |
| {{- $fmt := printf | |
| "User setting env.openshift=%d is in contrast to the result of the auto-sensing, which yielded a non-OpenShift cluster (auto-sensed value: %d) -- will proceed with user setting" | |
| $normalizedUserSpecifiedOpenShift $autoSensedOpenShift -}} | |
| {{- include "srox.warn" (list $ $fmt) -}} | |
| {{- end -}} |
| func (s *sensorGenerateOpenShiftCommand) ConstructOpenShift() error { | ||
| s.cluster.Type = storage.ClusterType_OPENSHIFT4_CLUSTER | ||
| switch s.openshiftVersion { | ||
| case 0: | ||
| case 3: | ||
| s.cluster.Type = storage.ClusterType_OPENSHIFT_CLUSTER | ||
| case 4: | ||
| default: | ||
| return errox.InvalidArgs.Newf("invalid OpenShift version %d, supported values are '3' and '4'", s.openshiftVersion) | ||
| } | ||
|
|
||
| s.cluster.AdmissionControllerEvents = s.cluster.GetType() == storage.ClusterType_OPENSHIFT4_CLUSTER |
There was a problem hiding this comment.
issue (bug_risk): openshiftVersion is no longer validated and unsupported values are silently treated as OpenShift 4.
With the updated ConstructOpenShift, s.cluster.Type is always OPENSHIFT4_CLUSTER and s.openshiftVersion is ignored. This means values like --openshift-version=3 or --openshift-version=7 are silently treated as 4. In roxctl/central/generate/k8s.go, you still validate openshiftVersion and reject anything other than 0 or 4. For consistency and to avoid silent misconfiguration, please add equivalent validation here and return an error when s.openshiftVersion is non-zero and not 4.
6f5f421 to
2e0e00d
Compare
2e0e00d to
c9408f1
Compare
Description
The auto-sensing for openshift is completely reorganized.
It now contains an additional (higher-priority) check for the
config.openshift.io/v1API (CRD based, hence more reliable).I had to keep the remaining logic because we also support OpenShift 3 and
config.openshift.io/v1is OpenShift 4-only.The new code structure allows improved logs in case we have a discrepancy between the auto-sensed cluster type and what the user has specified.
User-facing documentation
Testing and quality
Automated testing
How I validated my change
change me!