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: #52

Removes the need for `ensure: "removed"` in configuration files.
  • Loading branch information
dgarlitt committed Dec 9, 2017
1 parent 795fe15 commit a53faf3
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 6 deletions.
4 changes: 3 additions & 1 deletion 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,7 +37,8 @@
"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": [
Expand Down
30 changes: 25 additions & 5 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 Down Expand Up @@ -68,8 +71,25 @@ 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) {
(async () => {
try {
let remoteConfig = await readKongApi(adminApi({host, https, ignoreConsumers}));
config = sync(config, remoteConfig, ignoreConsumers);
run();
} catch (error) {
console.error(`${error}`.red, '\n', error.stack);
}
})();
} 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;
};
156 changes: 156 additions & 0 deletions test/fixtures/config-base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"apis": [
{
"name": "placement-service",
"plugins": []
},
{
"name": "testAPI1",
"plugins": [
{
"name": "request-termination"
},
{
"name": "key-auth"
}
]
},{
"name": "lookup-service",
"plugins": []
},
{
"name": "testAPI2",
"plugins": [
{
"name": "key-auth"
}
]
}
],
"consumers": [
{
"username": "testConsumer1",
"credentials": [
{
"name": "key-auth",
"attributes": {
"key": "1234A"
}
}
]
},
{
"username": "testConsumer2",
"credentials": [
{
"name": "basic-auth",
"attributes": {
"username": "jdoe"
}
},
{
"name": "key-auth",
"attributes": {
"key": "5678B"
}
}
]
},
{
"username": "anonymous",
"credentials": [
{
"name": "key-auth",
"attributes": {
"key": "9012C"
}
}
]
},
{
"username": "iosapp",
"credentials": [
{
"name": "key-auth",
"attributes": {
"key": "3456D"
}
}
]
},
{
"username": "android",
"credentials": [
{
"name": "key-auth",
"attributes": {
"key": "7890E"
}
},
{
"name": "key-auth",
"attributes": {
"key": "1234F"
}
}
]
},
{
"username": "testConsumer3",
"credentials": [
{
"name": "oauth2",
"attributes": {
"client_id": "5678G"
}
}
]
}
],
"plugins": [
{
"name": "syslog"
},
{
"name": "basic-auth"
},
{
"name": "key-auth"
},
{
"name": "response-transformer"
},
{
"name": "oauth2"
}
],
"upstreams": [
{
"name": "lookupService",
"targets": [
{
"target": "app.lookup.service.mydomain.io:8080"
}
]
},
{
"name": "testUpstream1",
"targets": [
{
"target": "test.target1.com:8080"
}
]
},
{
"name": "testUpstream2",
"targets": [
{
"target": "test.target2a.com:8080"
},
{
"target": "test.target2b.com:8080"
}
]
}
]
}
Loading

0 comments on commit a53faf3

Please sign in to comment.