Skip to content

Commit

Permalink
check version compatibility when there are multiple matchings + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mo Mesgin authored and Mo Mesgin committed Jan 23, 2025
1 parent a320b51 commit dfc0865
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
83 changes: 56 additions & 27 deletions shell/models/catalog.cattle.io.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default class CatalogApp extends SteveModel {
set(this, 'skipCRDs', false);
set(this, 'timeout', 300);
set(this, 'wait', true);
set(this, 'foundMultipleUpgradeMatches', false);
set(this, 'upgradeAvailableVersion', '');
}

Expand All @@ -49,11 +48,11 @@ export default class CatalogApp extends SteveModel {
return null;
}

matchingChart(includeHidden) {
matchingCharts(includeHidden) {
const chart = this.spec?.chart;

if ( !chart ) {
return;
return [];
}

const chartName = chart.metadata?.name;
Expand All @@ -67,7 +66,7 @@ export default class CatalogApp extends SteveModel {
}) || [];

if (matchingCharts.length === 0) {
return;
return [];
}

// Filtering matches by verifying if the current version is in the matched chart's available versions, and that the home value matches as well
Expand All @@ -76,32 +75,23 @@ export default class CatalogApp extends SteveModel {
for (let i = 0; i < versions.length; i++) {
const { version, home } = versions[i];

if (version === this.currentVersion && home === thisHome) {
if (version === this.currentVersion && (!home || !thisHome || home === thisHome)) {
return true;
}
}

return false;
});

if (bestMatches.length === 0) {
return;
}

if (bestMatches.length === 1) {
return bestMatches[0];
}

// found multiple matches but couldn't choose one unique match
this.foundMultipleUpgradeMatches = true;
return bestMatches;
}

get currentVersion() {
return this.spec?.chart?.metadata?.version;
}

get upgradeAvailable() {
// one the following statuses gets returned:
// one of the following statuses gets returned:
// NOT_APPLICABLE - managed by fleet
// NO_UPGRADE - no upgrade found
// SINGLE_UPGRADE - a version available to upgrade to
Expand All @@ -114,18 +104,27 @@ export default class CatalogApp extends SteveModel {
// Things managed by fleet shouldn't show upgrade available even if there might be.
return APP_UPGRADE_STATUS.NOT_APPLICABLE;
}
const chart = this.matchingChart(false);

if (this.foundMultipleUpgradeMatches) {
return APP_UPGRADE_STATUS.MULTIPLE_UPGRADES;
}
const charts = this.matchingCharts(false);

if ( !chart ) {
if (charts.length === 0) {
return APP_UPGRADE_STATUS.NO_UPGRADE;
}

const workerOSs = this.$rootGetters['currentCluster'].workerOSs;
// Handle single chart logic
if (charts.length === 1) {
return this.evaluateUpgradeForChart(charts[0]);
}

// Handle multiple upgrade matches
return this.handleMultipleUpgradeMatches(charts);
}

/**
* Evaluates upgrade status for a single chart.
*/
evaluateUpgradeForChart(chart) {
const workerOSs = this.$rootGetters['currentCluster'].workerOSs;
const showPreRelease = this.$rootGetters['prefs/get'](SHOW_PRE_RELEASE);

let versions = chart.versions;
Expand All @@ -139,12 +138,42 @@ export default class CatalogApp extends SteveModel {
const newestChart = versions?.[0];
const newestVersion = newestChart?.version;

if ( !this.currentVersion || !newestVersion ) {
if (!this.currentVersion || !newestVersion) {
return APP_UPGRADE_STATUS.NO_UPGRADE;
}

if ( compare(this.currentVersion, newestVersion) < 0 ) {
// set the available upgrade version to be used in other places
if (compare(this.currentVersion, newestVersion) < 0) {
// Set the available upgrade version to be used in other places
this.upgradeAvailableVersion = cleanupVersion(newestVersion);

return APP_UPGRADE_STATUS.SINGLE_UPGRADE;
}

return APP_UPGRADE_STATUS.NO_UPGRADE;
}

/**
* Handles the case where multiple upgrade matches are found.
* @param charts - Array of matching charts
*/
handleMultipleUpgradeMatches(charts) {
const qualifiedCharts = [];

for (const chart of charts) {
const status = this.evaluateUpgradeForChart(chart);

if (status === APP_UPGRADE_STATUS.SINGLE_UPGRADE) {
qualifiedCharts.push(chart);
}
}

if (qualifiedCharts.length > 1) {
return APP_UPGRADE_STATUS.MULTIPLE_UPGRADES;
}

if (qualifiedCharts.length === 1) {
const newestVersion = qualifiedCharts[0]?.versions?.[0]?.version;

this.upgradeAvailableVersion = cleanupVersion(newestVersion);

return APP_UPGRADE_STATUS.SINGLE_UPGRADE;
Expand All @@ -164,7 +193,7 @@ export default class CatalogApp extends SteveModel {
get currentVersionCompatible() {
const workerOSs = this.$rootGetters['currentCluster'].workerOSs;

const chart = this.matchingChart(false);
const chart = this.matchingChart(false)[0];

if (!chart) {
return true;
Expand Down Expand Up @@ -194,7 +223,7 @@ export default class CatalogApp extends SteveModel {
}

goToUpgrade(forceVersion, fromTools) {
const match = this.matchingChart(true);
const match = this.matchingChart(true)[0];
const query = {
[NAMESPACE]: this.metadata.namespace,
[NAME]: this.metadata.name,
Expand Down
2 changes: 1 addition & 1 deletion shell/pages/c/_cluster/apps/charts/install.vue
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export default {
},
charts() {
const current = this.existing?.matchingChart(true);
const current = this.existing?.matchingChart(true)[0];
const out = this.$store.getters['catalog/charts'].filter((x) => {
if ( x.key === current?.key || x.chartName === current?.chartName ) {
Expand Down
2 changes: 1 addition & 1 deletion shell/pages/c/_cluster/explorer/tools/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default {
const out = {};
for (const app of (this.installedApps || [])) {
const matching = app.matchingChart();
const matching = app.matchingCharts()[0];
if ( !matching ) {
continue;
Expand Down

0 comments on commit dfc0865

Please sign in to comment.