From 425319189e65b01217be22d47d3392c1d63ada30 Mon Sep 17 00:00:00 2001 From: Jo Date: Tue, 4 Jun 2024 11:42:06 +0200 Subject: [PATCH] fix: wait for rancher ns roles ready (#488) --- .../pre-deploy/01-rancher-namespaces.js | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/plugins/contrib/pre-deploy/01-rancher-namespaces.js b/plugins/contrib/pre-deploy/01-rancher-namespaces.js index e6a106866a..e15af0f4a6 100644 --- a/plugins/contrib/pre-deploy/01-rancher-namespaces.js +++ b/plugins/contrib/pre-deploy/01-rancher-namespaces.js @@ -1,10 +1,12 @@ +const retry = require("async-retry") + module.exports = async ( manifests, _options, { utils, config, logger, kubectl } ) => { const { kubeEnsureNamespace } = utils - const { kubeconfig, kubeconfigContext } = config + const { kubeconfig, kubeconfigContext, surviveOnBrokenCluster } = config const rancherNamespacesManifests = manifests.filter( (manifest) => @@ -27,5 +29,49 @@ module.exports = async ( kubeEnsureNamespace({ kubeconfig, kubeconfigContext, manifest, kubectl }) ) ) + + await Promise.all( + rancherNamespacesManifests.map((manifest) => + kubeEnsureNamespace({ kubeconfig, kubeconfigContext, manifest, kubectl }) + ) + ) + + const ensureRancherNamespaceReady = async (namespace) => { + const retryOptions = { + retries: 10, + factor: 2, + minTimeout: 1000, + maxTimeout: 60000, + randomize: true, + } + await retry(async (_bail) => { + const json = await kubectl(`get ns ${namespace} -o json`, { + kubeconfig, + kubeconfigContext, + logInfo: false, + ignoreErrors: ["NotFound", "Forbidden"], + logger, + surviveOnBrokenCluster, + retryOptions, + }) + const data = JSON.parse(json) + const rancherStatusJSON = data?.metadata.annotations?.["cattle.io/status"] + if (rancherStatusJSON) { + const rancherStatus = JSON.parse(rancherStatusJSON) + const InitialRolesPopulated = rancherStatus.Conditions.find( + ({ Type }) => Type === "InitialRolesPopulated" + ) + if (InitialRolesPopulated) { + if (InitialRolesPopulated.Status === "True") { + return true + } + } + } + throw Error(`♉ rancher namespace "${namespace}" is not ready`) + }, retryOptions) + } + + await Promise.all(namespaces.map(ensureRancherNamespaceReady)) + logger.debug({ namespaces }, "namespaces ready") }