Skip to content

Commit

Permalink
Merge pull request rancher#9440 from richard-cox/safer-chart-install
Browse files Browse the repository at this point in the history
Add additional error handling to install chart page
  • Loading branch information
richard-cox authored Aug 2, 2023
2 parents c23dcf5 + 267eeee commit c906b55
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 28 deletions.
16 changes: 13 additions & 3 deletions shell/models/management.cattle.io.cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import { KONTAINER_TO_DRIVER } from './management.cattle.io.kontainerdriver';
// If the logo is not named with the provider name, add an override here
const PROVIDER_LOGO_OVERRIDE = {};

function findRelationship(verb, type, relationships = []) {
const from = `${ verb }Type`;
const id = `${ verb }Id`;

return relationships.find((r) => r[from] === type)?.[id];
}

export default class MgmtCluster extends HybridModel {
get details() {
const out = [
Expand Down Expand Up @@ -440,9 +447,12 @@ export default class MgmtCluster extends HybridModel {
// cluster has the less human readable management cluster ID in it: fleet-default/c-khk48

const verb = this.isLocal || isRKE1 || this.isHostedKubernetesProvider ? 'to' : 'from';
const from = `${ verb }Type`;
const id = `${ verb }Id`;
const res = findRelationship(verb, CAPI.RANCHER_CLUSTER, this.metadata?.relationships);

if (res) {
return res;
}

return this.metadata.relationships.find((r) => r[from] === CAPI.RANCHER_CLUSTER)?.[id];
return findRelationship(verb === 'to' ? 'from' : 'to', CAPI.RANCHER_CLUSTER, this.metadata?.relationships);
}
}
99 changes: 74 additions & 25 deletions shell/pages/c/_cluster/apps/charts/install.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export default {
],
async fetch() {
this.errors = [];
// IMPORTANT! Any exception thrown before this.value is set will result in an empty page
/*
fetchChart is defined in shell/mixins. It first checks the URL
query for an app name and namespace. It uses those values to check
Expand All @@ -91,23 +94,43 @@ export default {
it checks for target name and namespace values defined in the
Helm chart itself.
*/
await this.fetchChart();
try {
await this.fetchChart();
} catch (e) {
console.warn('Unable to fetch chart: ', e); // eslint-disable-line no-console
}
await this.fetchAutoInstallInfo();
this.errors = [];
try {
await this.fetchAutoInstallInfo();
} catch (e) {
console.warn('Unable to determine if other charts require install: ', e); // eslint-disable-line no-console
}
// If the chart doesn't contain system `systemDefaultRegistry` properties there's no point applying them
if (this.showCustomRegistry) {
// Note: Cluster scoped registry is only supported for node driver clusters
this.clusterRegistry = await this.getClusterRegistry();
this.globalRegistry = await this.getGlobalRegistry();
try {
this.clusterRegistry = await this.getClusterRegistry();
} catch (e) {
console.warn('Unable to get cluster registry: ', e); // eslint-disable-line no-console
}
try {
this.globalRegistry = await this.getGlobalRegistry();
} catch (e) {
console.warn('Unable to get global registry: ', e); // eslint-disable-line no-console
}
this.defaultRegistrySetting = this.clusterRegistry || this.globalRegistry;
}
this.serverUrlSetting = await this.$store.dispatch('management/find', {
type: MANAGEMENT.SETTING,
id: 'server-url'
});
try {
this.serverUrlSetting = await this.$store.dispatch('management/find', {
type: MANAGEMENT.SETTING,
id: 'server-url'
});
} catch (e) {
console.error('Unable to fetch `server-url` setting: ', e); // eslint-disable-line no-console
}
/*
Figure out the namespace where the chart is
Expand Down Expand Up @@ -137,20 +160,38 @@ export default {
}
/* Check if the app is deprecated. */
this.legacyApp = this.existing ? await this.existing.deployedAsLegacy() : false;
try {
this.legacyApp = this.existing ? await this.existing.deployedAsLegacy() : false;
} catch (e) {
this.legacyApp = false;
console.warn('Unable to determine if existing install is a legacy app: ', e); // eslint-disable-line no-console
}
/* Check if the app is a multicluster deprecated app.
(Multicluster apps were replaced by Fleet.) */
this.mcapp = this.existing ? await this.existing.deployedAsMultiCluster() : false;
try {
this.mcapp = this.existing ? await this.existing.deployedAsMultiCluster() : false;
} catch (e) {
this.mcapp = false;
console.warn('Unable to determine if existing install is a mc app: ', e); // eslint-disable-line no-console
}
/* The form state is intialized as a chartInstallAction resource. */
this.value = await this.$store.dispatch('cluster/create', {
type: 'chartInstallAction',
metadata: {
namespace: this.forceNamespace || this.$store.getters['defaultNamespace'],
name: this.existing?.spec?.name || this.query.appName || '',
}
});
try {
this.value = await this.$store.dispatch('cluster/create', {
type: 'chartInstallAction',
metadata: {
namespace: this.forceNamespace || this.$store.getters['defaultNamespace'],
name: this.existing?.spec?.name || this.query.appName || '',
}
});
} catch (e) {
console.error('Unable to create object of type `chartInstallAction`: ', e); // eslint-disable-line no-console
// Nothing's going to work without a `value`. See https://github.com/rancher/dashboard/issues/9452 to handle this and other catches.
return;
}
/* Logic for when the Helm chart is not already installed */
if ( !this.existing) {
Expand Down Expand Up @@ -803,12 +844,19 @@ export default {
if (hasPermissionToSeeProvCluster) {
const mgmCluster = this.$store.getters['currentCluster'];
const provCluster = mgmCluster?.provClusterId ? await this.$store.dispatch('management/find', {
type: CAPI.RANCHER_CLUSTER,
id: mgmCluster.provClusterId
}) : {};
const provClusterId = mgmCluster?.provClusterId;
let provCluster;
try {
provCluster = provClusterId ? await this.$store.dispatch('management/find', {
type: CAPI.RANCHER_CLUSTER,
id: provClusterId
}) : {};
} catch (e) {
console.error(`Unable to fetch prov cluster '${ provClusterId }': `, e); // eslint-disable-line no-console
}
if (provCluster.isRke2) { // isRke2 returns true for both RKE2 and K3s clusters.
if (provCluster?.isRke2) { // isRke2 returns true for both RKE2 and K3s clusters.
const agentConfig = provCluster.spec?.rkeConfig?.machineSelectorConfig?.find((x) => !x.machineLabelSelector).config;
// If a cluster scoped registry exists,
Expand All @@ -819,10 +867,11 @@ export default {
return clusterRegistry;
}
}
if (provCluster.isRke1) {
if (provCluster?.isRke1) {
// For RKE1 clusters, the cluster scoped private registry is on the management
// cluster, not the provisioning cluster.
const rke1Registries = mgmCluster.spec?.rancherKubernetesEngineConfig?.privateRegistries;
const rke1Registries = mgmCluster?.spec?.rancherKubernetesEngineConfig?.privateRegistries;
if (rke1Registries?.length > 0) {
const defaultRegistry = rke1Registries.find((registry) => {
Expand Down

0 comments on commit c906b55

Please sign in to comment.