|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + semver "github.com/blang/semver/v4" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | +) |
| 12 | + |
| 13 | +type kernelSupportVersion struct { |
| 14 | + // Latest is the kernel version in which the issue was fixed upstream. |
| 15 | + // Any version greater than this is considered to have the fix. |
| 16 | + Latest string |
| 17 | + |
| 18 | + // Backports contains base kernel version prefixes that received backported fixes. |
| 19 | + // Matching logic: match the first 4 version segments, and the 5th segment should be greater |
| 20 | + // than or equal to the backported entry to consider the fix as present. |
| 21 | + Backports []string |
| 22 | +} |
| 23 | + |
| 24 | +var ( |
| 25 | + CephxKeyRotaionKernelSupportVersion = kernelSupportVersion{ |
| 26 | + Latest: "5.14.0-570.22.1", |
| 27 | + Backports: []string{"5.14.0-570.22.1"}, |
| 28 | + } |
| 29 | +) |
| 30 | + |
| 31 | +func GetKernelVersionFromAllNodes(ctx context.Context, cli client.Client) ([]string, error) { |
| 32 | + |
| 33 | + var nodeList corev1.NodeList |
| 34 | + if err := cli.List(ctx, &nodeList); err != nil { |
| 35 | + return nil, fmt.Errorf("listing nodes: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + var kernelVersions []string |
| 39 | + var kernelVersionsMap map[string]bool = make(map[string]bool) |
| 40 | + for _, node := range nodeList.Items { |
| 41 | + // Check if the node has a kernel version |
| 42 | + if node.Status.NodeInfo.KernelVersion == "" { |
| 43 | + return nil, fmt.Errorf("node %s has empty kernel version", node.Name) |
| 44 | + } |
| 45 | + |
| 46 | + if _, ok := kernelVersionsMap[node.Status.NodeInfo.KernelVersion]; !ok { |
| 47 | + kernelVersionsMap[node.Status.NodeInfo.KernelVersion] = true |
| 48 | + // Append the kernel version to the slice |
| 49 | + kernelVersions = append(kernelVersions, node.Status.NodeInfo.KernelVersion) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return kernelVersions, nil |
| 54 | +} |
| 55 | + |
| 56 | +func IsKernelVersionSupported(currentVersion string, supportedVersions kernelSupportVersion) (bool, error) { |
| 57 | + |
| 58 | + parsedCurrentVersion, err := semver.Make(RemoveUnderscores(currentVersion)) |
| 59 | + if err != nil { |
| 60 | + return false, err |
| 61 | + } |
| 62 | + |
| 63 | + parsedSupportedVersion, err := semver.Make(RemoveUnderscores(supportedVersions.Latest)) |
| 64 | + if err != nil { |
| 65 | + return false, err |
| 66 | + } |
| 67 | + |
| 68 | + if parsedCurrentVersion.GT(parsedSupportedVersion) { |
| 69 | + return true, nil |
| 70 | + } |
| 71 | + |
| 72 | + for _, supportedVersion := range supportedVersions.Backports { |
| 73 | + parsedSupportedVersion, err := semver.Make(RemoveUnderscores(supportedVersion)) |
| 74 | + if err != nil { |
| 75 | + return false, err |
| 76 | + } |
| 77 | + |
| 78 | + // match first 4 words and 5th one should be greater |
| 79 | + if parsedCurrentVersion.Major == parsedSupportedVersion.Major && |
| 80 | + parsedCurrentVersion.Minor == parsedSupportedVersion.Minor && |
| 81 | + parsedCurrentVersion.Patch == parsedSupportedVersion.Patch && |
| 82 | + parsedCurrentVersion.Pre[0].VersionNum == parsedSupportedVersion.Pre[0].VersionNum && |
| 83 | + parsedCurrentVersion.Pre[1].VersionNum >= parsedSupportedVersion.Pre[1].VersionNum { |
| 84 | + return true, nil |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return false, nil |
| 89 | +} |
| 90 | + |
| 91 | +func AreKernelVersionsSupported(currentVersions []string, supportedVersions kernelSupportVersion) (bool, error) { |
| 92 | + |
| 93 | + for _, kversion := range currentVersions { |
| 94 | + if isSupported, err := IsKernelVersionSupported(kversion, supportedVersions); !isSupported || err != nil { |
| 95 | + return isSupported, err |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return true, nil |
| 100 | +} |
| 101 | + |
| 102 | +func RemoveUnderscores(version string) string { |
| 103 | + return strings.ReplaceAll(version, "_", "") |
| 104 | +} |
0 commit comments