Skip to content

Commit

Permalink
feat: ci namespace fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo committed Jul 24, 2023
1 parent c4d2a2c commit 82739a8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
66 changes: 58 additions & 8 deletions packages/common/config/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const slug = require("../utils/slug")
const normalizeRepositoryUrl = require("../utils/normalize-repository-url")

const gitEnv = require("../utils/git-env")
const kubectl = require("../utils/kubectl-retry")

const loadDependencies = require("./load-dependencies")

Expand All @@ -55,6 +56,8 @@ const loadConfig = async (
) => {
const env = ctx.get("env") || process.env

configMeta.__cachedResults = configMeta.__cachedResults || {}

const setConfigMeta = (configKey, key, value) => {
if (!configMeta[configKey]) {
configMeta[configKey] = {}
Expand Down Expand Up @@ -478,14 +481,6 @@ const loadConfig = async (
return `${webhookUri}/api/v1/oas/artifacts/status?${query}`
},
},
ciNamespace: {
option: "ci-namespace",
env: "KS_CI_NAMESPACE",
defaultFunction: (config) =>
config.repositoryName
? `${config.projectName || config.repositoryName}-ci`
: undefined,
},
ci: {
env: "KS_CI",
defaultFunction: () => ciDetect(),
Expand Down Expand Up @@ -544,6 +539,61 @@ const loadConfig = async (
return kubeconfigContext[config.environment]
},
},
ciNamespaceDefaultFallback: {
default: false,
},
ciNamespace: {
option: "ci-namespace",
env: "KS_CI_NAMESPACE",
envParser: envParserCastArray,
defaultFunction: (config) =>
config.ciNamespaceDefaultFallback
? [
`ci-${config.projectName || config.repositoryName}`,
`${config.projectName || config.repositoryName}-ci`,
]
: `${config.projectName || config.repositoryName}-ci`,
transform: async (value, config) => {
if (!Array.isArray(value)) {
return value
}
if (value.length <= 1) {
return value[0]
}

const cached = configMeta.__cachedResults
if (!cached.ciNamespaces) {
cached.ciNamespaces = {}
}
const cachedCiNs = cached.ciNamespaces

for (const ns of value) {
if (cachedCiNs[ns] !== undefined) {
if (cachedCiNs[ns]) {
return ns
}
continue
}
try {
await kubectl(`get ns ${ns}`, {
kubeconfig: config.kubeconfig,
kubeconfigContext: config.kubeconfigContext,
logInfo: false,
logError: false,
logger,
})
cachedCiNs[ns] = true
return ns
} catch (err) {
cachedCiNs[ns] = false
// do nothing
}
}
throw new Error(
`no matching ci namespace found, looked for: ${value.join(", ")}`
)
},
},
linksSelfLocation: {
default: "socialgouv/kontinuous",
},
Expand Down
5 changes: 4 additions & 1 deletion packages/kontinuous/src/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ module.exports.deployWith = new Option(
"set deploy-with plugin to handle the deployment"
)

module.exports.ciNamespace = new Option("--ci-namespace <ns>", "ci namespace")
module.exports.ciNamespace = new Option(
"--ci-namespace <ns...>",
"ci namespace"
)

module.exports.rancherProjectId = new Option(
"--rancher-project-id <project-id>",
Expand Down
3 changes: 1 addition & 2 deletions plugins/contrib/values-compilers/06-global-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ module.exports = async (values, options, { config, utils, ctx }) => {

const { projectName } = config

const ciNamespace =
config.ciNamespace || `${projectName || repositoryName}-ci`
const { ciNamespace } = config

const replicas = isProd ? 2 : 1

Expand Down

0 comments on commit 82739a8

Please sign in to comment.