|
| 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "regexp" |
| 23 | + "slices" |
| 24 | + "strings" |
| 25 | + |
| 26 | + common_helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper" |
| 27 | + uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 28 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + // OpenStackLightspeedOCPVectorDBPath - base path for OCP vector databases |
| 34 | + OpenStackLightspeedOCPVectorDBPath = "/rag/ocp_vector_db/ocp" |
| 35 | + |
| 36 | + // OpenStackLightspeedOCPIndexPrefix - prefix for OCP index names |
| 37 | + OpenStackLightspeedOCPIndexPrefix = "ocp-product-docs" |
| 38 | + |
| 39 | + // Supported OCP versions in the RAG database |
| 40 | + OCPVersion416 = "4.16" |
| 41 | + OCPVersion418 = "4.18" |
| 42 | + OCPVersionLatest = "latest" |
| 43 | +) |
| 44 | + |
| 45 | +// SupportedOCPVersions lists the OCP versions available in the RAG database |
| 46 | +var SupportedOCPVersions = []string{OCPVersion416, OCPVersion418, OCPVersionLatest} |
| 47 | + |
| 48 | +// DetectOCPVersion detects the OpenShift cluster version |
| 49 | +func DetectOCPVersion(ctx context.Context, helper *common_helper.Helper) (string, error) { |
| 50 | + // Use raw client to access cluster-scoped resources |
| 51 | + rawClient, err := GetRawClient(helper) |
| 52 | + if err != nil { |
| 53 | + return "", fmt.Errorf("failed to get raw client: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Get ClusterVersion object |
| 57 | + clusterVersion := &uns.Unstructured{} |
| 58 | + clusterVersion.SetGroupVersionKind(schema.GroupVersionKind{ |
| 59 | + Group: "config.openshift.io", |
| 60 | + Version: "v1", |
| 61 | + Kind: "ClusterVersion", |
| 62 | + }) |
| 63 | + |
| 64 | + err = rawClient.Get(ctx, client.ObjectKey{Name: "version"}, clusterVersion) |
| 65 | + if err != nil { |
| 66 | + return "", fmt.Errorf("failed to get ClusterVersion: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Extract version from status.desired.version |
| 70 | + // NOTE: We intentionally use desired.version rather than history[0].version because: |
| 71 | + // - During OCP upgrades, desired.version reflects the target version |
| 72 | + // - Users troubleshooting upgrade issues need docs for the NEW version |
| 73 | + // - This provides proactive access to relevant documentation |
| 74 | + version, found, err := uns.NestedString(clusterVersion.Object, "status", "desired", "version") |
| 75 | + if err != nil { |
| 76 | + return "", fmt.Errorf("failed to extract version from ClusterVersion: %w", err) |
| 77 | + } |
| 78 | + if !found { |
| 79 | + return "", fmt.Errorf("version field not found in ClusterVersion status.desired.version") |
| 80 | + } |
| 81 | + |
| 82 | + // Parse version to get major.minor (e.g., "4.15.0" -> "4.15") |
| 83 | + majorMinor, err := ParseMajorMinorVersion(version) |
| 84 | + if err != nil { |
| 85 | + return "", fmt.Errorf("failed to parse version %s: %w", version, err) |
| 86 | + } |
| 87 | + |
| 88 | + return majorMinor, nil |
| 89 | +} |
| 90 | + |
| 91 | +// ParseMajorMinorVersion extracts major.minor version from full version string |
| 92 | +// Example: "4.15.0-0.nightly-2024-01-15-123456" -> "4.15" |
| 93 | +func ParseMajorMinorVersion(fullVersion string) (string, error) { |
| 94 | + // Match major.minor pattern at the start |
| 95 | + re := regexp.MustCompile(`^(\d+\.\d+)`) |
| 96 | + matches := re.FindStringSubmatch(fullVersion) |
| 97 | + |
| 98 | + if len(matches) < 2 { |
| 99 | + return "", fmt.Errorf("invalid version format: %s", fullVersion) |
| 100 | + } |
| 101 | + |
| 102 | + return matches[1], nil |
| 103 | +} |
| 104 | + |
| 105 | +// GetOCPIndexName converts version to index name format |
| 106 | +// Example: "4.16" -> "ocp-product-docs-4_16" |
| 107 | +// |
| 108 | +// "latest" -> "ocp-product-docs-latest" |
| 109 | +func GetOCPIndexName(version string) string { |
| 110 | + // Replace dots with underscores (no-op for "latest") |
| 111 | + versionFormatted := strings.ReplaceAll(version, ".", "_") |
| 112 | + return fmt.Sprintf("%s-%s", OpenStackLightspeedOCPIndexPrefix, versionFormatted) |
| 113 | +} |
| 114 | + |
| 115 | +// GetOCPVectorDBPath returns the full path to OCP vector DB for given version |
| 116 | +// Example: "4.16" -> "/rag/ocp_vector_db/ocp-4.16" |
| 117 | +// |
| 118 | +// "latest" -> "/rag/ocp_vector_db/ocp-latest" |
| 119 | +func GetOCPVectorDBPath(version string) string { |
| 120 | + return fmt.Sprintf("%s-%s", OpenStackLightspeedOCPVectorDBPath, version) |
| 121 | +} |
| 122 | + |
| 123 | +// IsSupportedOCPVersion checks if the version is explicitly supported in RAG DB |
| 124 | +func IsSupportedOCPVersion(version string) bool { |
| 125 | + return slices.Contains(SupportedOCPVersions, version) |
| 126 | +} |
| 127 | + |
| 128 | +// ResolveOCPVersion determines the OCP version to use for RAG configuration |
| 129 | +// Returns (version, isFallback, error) |
| 130 | +// - version: The version to use (might be "latest" as fallback) |
| 131 | +// - isFallback: true if falling back to "latest" for unsupported version |
| 132 | +// - error: any error during version resolution |
| 133 | +func ResolveOCPVersion(detectedVersion, overrideVersion string, enableOCPRAG bool) (string, bool, error) { |
| 134 | + if !enableOCPRAG { |
| 135 | + return "", false, nil |
| 136 | + } |
| 137 | + |
| 138 | + // Use override if provided |
| 139 | + if overrideVersion != "" { |
| 140 | + return overrideVersion, false, nil |
| 141 | + } |
| 142 | + |
| 143 | + if detectedVersion == "" { |
| 144 | + return "", false, fmt.Errorf("no OCP version detected") |
| 145 | + } |
| 146 | + |
| 147 | + // Check if detected version is supported |
| 148 | + if IsSupportedOCPVersion(detectedVersion) { |
| 149 | + return detectedVersion, false, nil |
| 150 | + } |
| 151 | + |
| 152 | + // Fallback to latest for unsupported versions |
| 153 | + return OCPVersionLatest, true, nil |
| 154 | +} |
0 commit comments