Skip to content

Commit

Permalink
Provide option to force kong to sync with config
Browse files Browse the repository at this point in the history
This adds the ability to use the --force argument to meet the needs of
the feature requested in: mybuilder#52

Removes the need for `ensure: "removed"` in configuration files.
  • Loading branch information
dgarlitt committed Dec 6, 2017
1 parent 795fe15 commit 3500869
Show file tree
Hide file tree
Showing 6 changed files with 794 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ node_modules

# Publishing es5 in lib
lib

# other files to ignore
ignore
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"license": "MIT",
"dependencies": {
"babel-polyfill": "^6.2.0",
"clone": "^2.1.1",
"colors": "^1.1.2",
"commander": "^2.9.0",
"invariant": "^2.2.2",
Expand All @@ -36,14 +37,15 @@
"babel-preset-es2015": "^6.1.18",
"babel-preset-stage-2": "^6.1.18",
"expect.js": "^0.3.1",
"jest": "^20.0.4"
"jest": "^20.0.4",
"should": "^13.1.3"
},
"jest": {
"setupFiles": [
"<rootDir>/node_modules/babel-polyfill/dist/polyfill.js"
],
"testMatch": [
"<rootDir>/test/*.js",
"<rootDir>/my-test/*.js",
"<rootDir>/test-integration/*.test.js"
],
"transformIgnorePatterns": [
Expand Down
30 changes: 24 additions & 6 deletions src/cli-apply.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import execute from './core';
import adminApi from './adminApi';
import readKongApi from './readKongApi';
import sync from './sync-configs';
import colors from 'colors';
import configLoader from './configLoader';
import program from 'commander';
Expand All @@ -13,6 +15,7 @@ program
.option('--path <value>', 'Path to the configuration file')
.option('--host <value>', 'Kong admin host (default: localhost:8001)')
.option('--https', 'Use https for admin API requests')
.option('--force', 'remove any items from the host that are not in the local config')
.option('--no-cache', 'Do not cache kong state in memory')
.option('--ignore-consumers', 'Do not sync consumers')
.option('--header [value]', 'Custom headers to be added to all requests', (nextHeader, headers) => { headers.push(nextHeader); return headers }, [])
Expand All @@ -36,7 +39,7 @@ console.log(`Loading config ${program.path}`);
let config = configLoader(program.path);
let host = program.host || config.host || 'localhost:8001';
let https = program.https || config.https || false;
let ignoreConsumers = program.ignoreConsumers || !config.consumers || config.consumers.length === 0 || false;
let ignoreConsumers = program.ignoreConsumers || program.force || !config.consumers || config.consumers.length === 0 || false;
let cache = program.cache;

config.headers = config.headers || [];
Expand Down Expand Up @@ -68,8 +71,23 @@ else {

console.log(`Apply config to ${host}`.green);

execute(config, adminApi({host, https, ignoreConsumers, cache}), screenLogger)
.catch(error => {
console.error(`${error}`.red, '\n', error.stack);
process.exit(1);
});
if (program.force) {
console.log('ignoreConsumers', ignoreConsumers);
(async () => {
let remoteConfig = await readKongApi(adminApi({host, https, ignoreConsumers}));
config = sync(config, remoteConfig, ignoreConsumers);
console.log(config);
run();
})();
} else {
run();
}


function run() {
execute(config, adminApi({host, https, ignoreConsumers, cache}), screenLogger)
.catch(error => {
console.error(`${error}`.red, '\n', error.stack);
process.exit(1);
});
}
99 changes: 99 additions & 0 deletions src/sync-configs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

import clone from 'clone';
import { getSchema } from './consumerCredentials'

const rootKeys = ['apis', 'consumers', 'plugins', 'upstreams'];

const ensureRemoved = (obj) => {
obj.ensure = 'removed';
};

const markPluginsForRemoval = (local, remote) => {
remote.forEach(plugin => {
if (!local.find(p => p.name === plugin.name)) {
ensureRemoved(plugin);
local.unshift(plugin);
}
});
};

const markApisForRemoval = (local, remote) => {
remote.forEach(api => {
let found = local.find(a => a.name === api.name);
if (found && api.plugins) {
found.plugins = found.plugins ? found.plugins : [];
markPluginsForRemoval(found.plugins, api.plugins);
} else {
ensureRemoved(api);
local.unshift(api);
}
})
};

const markCredentialsForRemoval = (local, remote) => {
remote.forEach(cred => {
let key = getSchema(cred.name).id;
if (!local.find(c=> c.attributes[key] === cred.attributes[key])) {
ensureRemoved(cred);
local.unshift(cred);
}
});
};

const markConsumersForRemoval = (local, remote) => {
remote.forEach(consumer => {
let found = local.find(c => c.username === consumer.username);
if (found) {
found.credentials = found.credentials ? found.credentials : [];
markCredentialsForRemoval(found.credentials, consumer.credentials);
} else {
ensureRemoved(consumer);
local.unshift(consumer);
}
});
};

const markTargetsForRemoval = (local, remote) => {
remote.forEach(target => {
if (!local.find(t => t.target === target.target)) {
ensureRemoved(target);
local.unshift(target);
}
});
};

const markUpstreamsForRemoval = (local, remote) => {
remote.forEach(upstream => {
let found = local.find(u => u.name === upstream.name);
if (found) {
found.targets = found.targets ? found.targets : [];
markTargetsForRemoval(found.targets, upstream.targets);
} else {
ensureRemoved(upstream);
local.unshift(upstream);
}
});
};

export default (local, remote, ignoreConsumers) => {
const localKeys = Object.keys(local);
const remoteKeys = Object.keys(remote);

local = clone(local);
remote = clone(remote);

rootKeys.forEach(key => {
local[key] = localKeys.includes(key) ? local[key] : [];
remote[key] = remoteKeys.includes(key) ? remote[key] : [];
});

markApisForRemoval(local.apis, remote.apis);
markPluginsForRemoval(local.plugins, remote.plugins);
if (!ignoreConsumers) {
markConsumersForRemoval(local.consumers, remote.consumers);
}
markUpstreamsForRemoval(local.upstreams, remote.upstreams);

return local;
};
Loading

0 comments on commit 3500869

Please sign in to comment.