From 676078210b5527d1a25c49c16e945be2e5e28134 Mon Sep 17 00:00:00 2001 From: Richard Cox Date: Thu, 10 Oct 2024 10:08:58 +0100 Subject: [PATCH] Update & Improvement - Update label with latest value - Improve fn that updates cloud cred fn - make it more efficient when fetching cluster - add consistent error handling --- shell/config/labels-annotations.js | 2 +- shell/models/cloudcredential.js | 32 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/shell/config/labels-annotations.js b/shell/config/labels-annotations.js index 5af91654907..12cf285d244 100644 --- a/shell/config/labels-annotations.js +++ b/shell/config/labels-annotations.js @@ -174,5 +174,5 @@ export const SYSTEM_LABELS = [ ]; export const CLOUD_CREDENTIALS = [ - 'cattle.io/expiration-timestamp' + 'rancher.io/expiration-timestamp', ]; diff --git a/shell/models/cloudcredential.js b/shell/models/cloudcredential.js index 0be8d54a8d9..11794e1b666 100644 --- a/shell/models/cloudcredential.js +++ b/shell/models/cloudcredential.js @@ -18,26 +18,26 @@ const renew = { }, renewBulk: async({ cloudCredentials, $ctx }) => { // A harvester cloud credential (at the moment) is a kubeconfig complete with expiring token - // So to renew we just need to generate a new one and save it to the cc (similar to shell/cloud-credential/harvester.vue) + // So to renew we just need to generate a new kubeconfig and save it to the cc (similar to shell/cloud-credential/harvester.vue) + await Promise.all(cloudCredentials.map(async(cc) => { + try { + if (!cc.harvestercredentialConfig?.clusterId) { + throw new Error(`credential has no matching harvester cluster`); + } + const mgmtCluster = $ctx.rootGetters['management/byId'](MANAGEMENT.CLUSTER, cc.harvestercredentialConfig.clusterId); - const mgmtClusters = $ctx.rootGetters['management/all'](MANAGEMENT.CLUSTER); + if (!mgmtCluster) { + throw new Error(`cannot find harvester cluster`); + } - await Promise.all(cloudCredentials.map((cc) => { - const mgmtCluster = mgmtClusters.find((x) => x.id === cc.harvestercredentialConfig?.clusterId); + const kubeconfigContent = await mgmtCluster.generateKubeConfig(); - if (!mgmtCluster) { - throw new Error(`Unable to refresh credentials cloud credential '${ cc.id }'`); - } + cc.setData('kubeconfigContent', kubeconfigContent); - return mgmtCluster.generateKubeConfig() - .then((kubeconfigContent) => { - cc.setData('kubeconfigContent', kubeconfigContent); - - return cc.save(); - }) - .catch((err) => { - console.error('Unable to save cloud credential', err); // eslint-disable-line no-console - }); + await cc.save(); + } catch (error) { + console.error(`Unable to refresh harvester cloud credential '${ cc.id }'`, error); // eslint-disable-line no-console + } })); } }