|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/Masterminds/semver/v3" |
| 21 | + "github.com/go-logr/logr" |
| 22 | +) |
| 23 | + |
| 24 | +// ocpVersionBound maps an OCP version upper bound to a RHOSO version. |
| 25 | +type ocpVersionBound struct { |
| 26 | + maxOCPVersion string |
| 27 | + rhosoVersion string |
| 28 | +} |
| 29 | + |
| 30 | +// ocpToRHOSOVersionMap maps OCP version upper bounds to RHOSO versions. |
| 31 | +// Entries must be in ascending order of maxOCPVersion. |
| 32 | +// Versions above the highest bound fall back to OKPDefaultRHOSOVersion. |
| 33 | +// Add a new entry here when a new RHOSO version's content becomes available in the knowledge base. |
| 34 | +var ocpToRHOSOVersionMap = []ocpVersionBound{ |
| 35 | + {"4.21", "18.0"}, |
| 36 | + // When RHOSO 19.0 content is available, add: {"4.XX", "19.0"} |
| 37 | +} |
| 38 | + |
| 39 | +// detectRHOSOVersion returns the RHOSO version corresponding to the given OCP version. |
| 40 | +// Falls back to OKPDefaultRHOSOVersion if the version cannot be parsed or is above all defined bounds. |
| 41 | +func detectRHOSOVersion(ocpVersion string, logger logr.Logger) string { |
| 42 | + detected, err := semver.NewVersion(ocpVersion) |
| 43 | + if err != nil { |
| 44 | + logger.Info("Failed to parse OCP version, using default RHOSO version", |
| 45 | + "ocpVersion", ocpVersion, "default", OKPDefaultRHOSOVersion) |
| 46 | + return OKPDefaultRHOSOVersion |
| 47 | + } |
| 48 | + |
| 49 | + for _, entry := range ocpToRHOSOVersionMap { |
| 50 | + bound, err := semver.NewVersion(entry.maxOCPVersion) |
| 51 | + if err != nil { |
| 52 | + logger.Info("Invalid bound in RHOSO version map, using default", |
| 53 | + "bound", entry.maxOCPVersion, "default", OKPDefaultRHOSOVersion) |
| 54 | + return OKPDefaultRHOSOVersion |
| 55 | + } |
| 56 | + if detected.Compare(bound) <= 0 { |
| 57 | + return entry.rhosoVersion |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + logger.Info("OCP version above all known bounds, using default RHOSO version", |
| 62 | + "ocpVersion", ocpVersion, "default", OKPDefaultRHOSOVersion) |
| 63 | + return OKPDefaultRHOSOVersion |
| 64 | +} |
0 commit comments