Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide option to force kong to sync with config #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 './syncConfigs';
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/syncConfigs.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;
};
119 changes: 119 additions & 0 deletions test/fixtures/syncConfigs-base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"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": "testPlugin1" },
{ "name": "basic-auth" },
{ "name": "testPlugin2" },
{ "name": "response-transformer" },
{ "name": "testPlugin3" }
],

"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