Skip to content

Commit

Permalink
fix: wait for rancher ns roles ready (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo authored Jun 4, 2024
1 parent f8dd395 commit 4253191
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion plugins/contrib/pre-deploy/01-rancher-namespaces.js
Original file line number Diff line number Diff line change
@@ -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) =>
Expand All @@ -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")
}

0 comments on commit 4253191

Please sign in to comment.