Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pkg/operator/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/blang/semver/v4"
"github.com/openshift/api/features"

"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -244,6 +246,11 @@ func (o *OperatorOptions) getFeatureGateMappingFromDisk(ctx context.Context, con
clusterProfileAnnotation = "include.release.openshift.io/self-managed-high-availability"
}

operatorVersion, err := semver.ParseTolerant(o.OperatorVersion)
if err != nil {
return nil, fmt.Errorf("unable to parse operator version: %w", err)
}

ret := map[configv1.FeatureSet]*features.FeatureGateEnabledDisabled{}

err = filepath.Walk(o.AuthoritativeFeatureGateDir,
Expand Down Expand Up @@ -274,6 +281,11 @@ func (o *OperatorOptions) getFeatureGateMappingFromDisk(ctx context.Context, con
}
}

if excludesOperatorVersion(featureGate.Annotations, operatorVersion.Major) {
// This manifest includes a range of versions it applies to, but it does not apply to our current version.
return nil
}

featureGateValues := &features.FeatureGateEnabledDisabled{}
for _, possibleGates := range featureGate.Status.FeatureGates {
if possibleGates.Version != o.OperatorVersion {
Expand Down Expand Up @@ -352,3 +364,43 @@ func extractOperatorStatus(obj *unstructured.Unstructured, fieldManager string)
}
return &ret.Status.OperatorStatusApplyConfiguration, nil
}

func excludesOperatorVersion(annotations map[string]string, operatorVersion uint64) bool {
var versionsRaw string

for k, v := range annotations {
if k == "release.openshift.io/major-version" {
versionsRaw = v
break
}
}

if versionsRaw == "" {
return false
}

versions := strings.Split(versionsRaw, ",")

hasOperatorVersion, err := includesDesiredVersion(versions, operatorVersion)
if err != nil {
// Malformed annotation so should be excluded.
return true
}

return !hasOperatorVersion
}
Comment on lines +368 to +391
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Trim whitespace when parsing version list.

The version strings are split by comma but not trimmed of whitespace. If an annotation contains spaces (e.g., "4, 5"), the parsing in includesDesiredVersion will fail, causing the manifest to be incorrectly excluded.

🔎 Proposed fix to trim whitespace
 	versions := strings.Split(versionsRaw, ",")
+	for i := range versions {
+		versions[i] = strings.TrimSpace(versions[i])
+	}
 
 	hasOperatorVersion, err := includesDesiredVersion(versions, operatorVersion)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func excludesOperatorVersion(annotations map[string]string, operatorVersion uint64) bool {
var versionsRaw string
for k, v := range annotations {
if k == "release.openshift.io/major-version" {
versionsRaw = v
break
}
}
if versionsRaw == "" {
return false
}
versions := strings.Split(versionsRaw, ",")
hasOperatorVersion, err := includesDesiredVersion(versions, operatorVersion)
if err != nil {
// Malformed annotation so should be excluded.
return true
}
return !hasOperatorVersion
}
func excludesOperatorVersion(annotations map[string]string, operatorVersion uint64) bool {
var versionsRaw string
for k, v := range annotations {
if k == "release.openshift.io/major-version" {
versionsRaw = v
break
}
}
if versionsRaw == "" {
return false
}
versions := strings.Split(versionsRaw, ",")
for i := range versions {
versions[i] = strings.TrimSpace(versions[i])
}
hasOperatorVersion, err := includesDesiredVersion(versions, operatorVersion)
if err != nil {
// Malformed annotation so should be excluded.
return true
}
return !hasOperatorVersion
}
🤖 Prompt for AI Agents
In pkg/operator/starter.go around lines 368 to 391, the code splits the
annotation value by comma but does not trim whitespace so entries like "4, 5"
produce values with leading spaces and cause includesDesiredVersion to misparse
them; fix by iterating the versions slice after strings.Split, applying
strings.TrimSpace to each element and filtering out any empty strings, then pass
the cleaned slice to includesDesiredVersion (or alternatively trim when building
the versionsRaw) so parsing succeeds for annotations containing spaces.


func includesDesiredVersion(versions []string, operatorVersion uint64) (bool, error) {
for _, version := range versions {
version, err := strconv.ParseUint(version, 10, 64)
if err != nil {
// Malformed annotation so should be excluded
return false, fmt.Errorf("malformed annotation: %s", version)
}
if version == operatorVersion {
return true, nil
}
}

return false, nil
}