Skip to content

Commit

Permalink
Merge 883673b into c99f5e7
Browse files Browse the repository at this point in the history
  • Loading branch information
aearly committed Jul 19, 2019
2 parents c99f5e7 + 883673b commit 2bce093
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function build () {
description: 'the path or URL to the mcgonagall specification'
},
verbose: {
alias: 'v',
description: 'output verbose logging (status check output for hikaru)',
default: false,
boolean: true
Expand Down
56 changes: 37 additions & 19 deletions lib/reconcile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const nunjucks = require('nunjucks')
const crypto = require('crypto')
const Ajv = require('ajv')
const log = require('bole')('fabrik8')

const ajv = new Ajv()
const validate = ajv.compile(require('@npm-wharf/cluster-info-client/schema.json'))
Expand Down Expand Up @@ -49,26 +50,35 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
* @return {Object} parameters to pass to fabrik8 api
*/
async function processArgv (argv) {
let {
var {
environment = 'production',
specification,
clusterName,
slug
slug,
urlArg,
domainArg
} = argv

log.info('fetching default cluster info...')
let commonDefaultConfig = await clusterInfo.getCommon()

if (slug) {
if (slug && (!urlArg || !domainArg)) {
try {
log.info(`fetching existing cluster info for ${slug}...`)
var info = await clusterInfo.getCluster(slug)
} catch (e) {
console.error(e.stack)
log.error(e.stack)
throw new Error(`cluster ${slug} doesn't appear to exist`)
}

// fetch service accounts
const props = await _reifyServiceAccounts(info.value)
const { cluster, tokens, common, spec } = props
log.info(`${slug} found, re-applying config...`)
const merged = _extendDefaults(commonDefaultConfig, info.value)
const props = await _reifyServiceAccounts(merged)

const { cluster, tokens, spec, applicationCredentials } = props

cluster.applicationCredentials = cluster.applicationCredentials || applicationCredentials

if (typeof cluster.credentials === 'string') {
cluster.credentials = JSON.parse(cluster.credentials)
Expand All @@ -78,14 +88,11 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
cluster.applicationCredentials = JSON.parse(cluster.applicationCredentials)
}

_renderDefaults(commonDefaultConfig, common)
log.debug('final config:', JSON.stringify({ cluster, tokens }, null, 2))

return {
kubeformSettings: cluster, // kubeform params
hikaruSettings: {
...commonDefaultConfig.tokens,
...tokens
}, // hikaru tokens
hikaruSettings: tokens, // hikaru tokens
specification: spec
}
}
Expand All @@ -101,15 +108,18 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
let commonSettings = { slug, name, domain, url, environment }

try {
log.info(`checking for existing cluster info for ${slug}...`)
var { value: existingClusterConfig } = await clusterInfo.getCluster(slug)
} catch (e) {}

let inputSettings

if (!existingClusterConfig) {
inputSettings = _extendDefaults(commonDefaultConfig, { common: commonSettings }, argv)
log.info(`no existing cluster info, using input configuration...`)
inputSettings = _extendDefaults(commonDefaultConfig, { common: commonSettings })
} else {
inputSettings = _extendDefaults(commonDefaultConfig, existingClusterConfig, argv)
log.info(`${slug} found, re-applying config...`)
inputSettings = _extendDefaults(commonDefaultConfig, existingClusterConfig)
}

let projectId = argv.projectId ||
Expand Down Expand Up @@ -154,6 +164,11 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
...tokens
}

log.debug('final config:', JSON.stringify({
cluster: kubeformSettings,
tokens: hikaruSettings
}, null, 2))

return {
kubeformSettings, // kubeform params
hikaruSettings, // hikaru tokens
Expand Down Expand Up @@ -187,7 +202,7 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
}))
}

const secretProps = {
const props = {
cluster,
tokens,
common,
Expand All @@ -199,10 +214,12 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
}, {})
}

const valid = validate(secretProps)
const valid = validate(props)
if (!valid) throw new Error('data invalid:\n' + JSON.stringify(validate.errors, null, 2))

await clusterInfo.registerCluster(tokens.slug, environment, secretProps, [environment])
log.info(`storing cluster info for ${tokens.slug}...`)
log.debug(JSON.stringify(props, null, 2))
await clusterInfo.registerCluster(tokens.slug, environment, props, [environment])
}

/* helper funcs */
Expand Down Expand Up @@ -236,16 +253,16 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
}

// merge existing cluster data into the defaults, generate default values
function _extendDefaults (commonDefaultConfig, inputSettings, argv) {
_renderDefaults(commonDefaultConfig, inputSettings.common)
function _extendDefaults (commonDefaultConfig, inputSettings) {
const newSettings = JSON.parse(JSON.stringify(commonDefaultConfig))
_renderDefaults(newSettings, inputSettings.common)

const {
common: inputCommon,
cluster: inputCluster,
tokens: inputTokens
} = inputSettings
// clone defaults
const newSettings = JSON.parse(JSON.stringify(commonDefaultConfig))
const { common = {}, tokens = {}, cluster = {} } = newSettings

// in case they don't exist
Expand Down Expand Up @@ -317,6 +334,7 @@ module.exports = function (clusterInfo, uuid = require('uuid')) {
async function _reifyServiceAccounts (clusterData) {
const { serviceAccounts = {}, ...newData } = clusterData

log.info('fetching service accounts...')
const results = await Promise.all(Object.keys(serviceAccounts).map(async key => {
const email = serviceAccounts[key]
return [key, await clusterInfo.getServiceAccount(email)]
Expand Down

0 comments on commit 2bce093

Please sign in to comment.