|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +const BASE_KUBERNETES_ISO_URL = 'https://download.cloudstack.org/cks/' |
| 19 | + |
| 20 | +function getDefaultLatestKubernetesIsoParams (arch) { |
| 21 | + return { |
| 22 | + name: 'v1.33.1-calico-' + arch, |
| 23 | + semanticversion: '1.33.1', |
| 24 | + url: BASE_KUBERNETES_ISO_URL + 'setup-v1.33.1-calico-' + arch + '.iso', |
| 25 | + arch: arch, |
| 26 | + mincpunumber: 2, |
| 27 | + minmemory: 2048 |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Returns the latest Kubernetes ISO info for the given architecture. |
| 33 | + * Falls back to a hardcoded default if fetching fails. |
| 34 | + * @param {string} arch |
| 35 | + * @returns {Promise<{name: string, semanticversion: string, url: string, arch: string}>} |
| 36 | + */ |
| 37 | +export async function getLatestKubernetesIsoParams (arch) { |
| 38 | + arch = arch || 'x86_64' |
| 39 | + try { |
| 40 | + const html = await fetch(BASE_KUBERNETES_ISO_URL, { cache: 'no-store' }).then(r => r.text()) |
| 41 | + |
| 42 | + // Grab all .iso hrefs from the index page |
| 43 | + const hrefs = [...html.matchAll(/href="([^"]+\.iso)"/gi)].map(m => m[1]) |
| 44 | + |
| 45 | + // Prefer files that explicitly include the arch (e.g. ...-x86_64.iso) |
| 46 | + let isoHrefs = hrefs.filter(h => new RegExp(`${arch}\\.iso$`, 'i').test(h)) |
| 47 | + |
| 48 | + // Fallback: older files without arch suffix (e.g. setup-1.28.4.iso) |
| 49 | + if (isoHrefs.length === 0) { |
| 50 | + isoHrefs = hrefs.filter(h => /setup-\d+\.\d+\.\d+\.iso$/i.test(h)) |
| 51 | + } |
| 52 | + |
| 53 | + // Map to { name, semanticversion, url, arch } |
| 54 | + const entries = isoHrefs.map(h => { |
| 55 | + const m = h.match(/setup-(?:v)?(\d+\.\d+\.\d+)(?:-calico)?(?:-(x86_64|arm64))?/i) |
| 56 | + return m |
| 57 | + ? { |
| 58 | + name: h.replace('.iso', ''), |
| 59 | + semanticversion: m[1], |
| 60 | + url: new URL(h, BASE_KUBERNETES_ISO_URL).toString(), |
| 61 | + arch: m[2] || arch, |
| 62 | + mincpunumber: 2, |
| 63 | + minmemory: 2048 |
| 64 | + } |
| 65 | + : null |
| 66 | + }).filter(Boolean) |
| 67 | + |
| 68 | + if (entries.length === 0) throw new Error('No matching ISOs found') |
| 69 | + |
| 70 | + // Semver-style sort descending |
| 71 | + entries.sort((a, b) => { |
| 72 | + const pa = a.semanticversion.split('.').map(Number) |
| 73 | + const pb = b.semanticversion.split('.').map(Number) |
| 74 | + for (let i = 0; i < 3; i++) { |
| 75 | + if ((pb[i] ?? 0) !== (pa[i] ?? 0)) return (pb[i] ?? 0) - (pa[i] ?? 0) |
| 76 | + } |
| 77 | + return 0 |
| 78 | + }) |
| 79 | + |
| 80 | + return entries[0] |
| 81 | + } catch { |
| 82 | + return { ...getDefaultLatestKubernetesIsoParams(arch) } |
| 83 | + } |
| 84 | +} |
0 commit comments