diff --git a/.foreverignore b/.foreverignore new file mode 100644 index 0000000..952c500 --- /dev/null +++ b/.foreverignore @@ -0,0 +1,2 @@ +**/.idea/** +**/.git/** \ No newline at end of file diff --git a/package.json b/package.json index 19c8a42..19cdc13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongo-k8s-sidecar", - "version": "0.0.1", + "version": "0.1.0", "description": "Kubernetes sidecar for mongo", "main": "src/index.js", "scripts": { diff --git a/src/lib/config.js b/src/lib/config.js index faf6dfe..7631bef 100644 --- a/src/lib/config.js +++ b/src/lib/config.js @@ -1,12 +1,16 @@ -var getPodLabels = function() { - if (!process.env.MONGO_SIDECAR_POD_LABELS) { - throw new Error('Enivronment variable "MONGO_SIDECAR_POD_LABELS" must be supplied. Is should contain a comma ' + - 'delimited list of values. E.g. "name:mongo,environment:dev".'); +var getMongoPodLabels = function() { + return process.env.LL_MONGO_POD_LABELS || false; +}; + +var getMongoPodLabelCollection = function() { + var podLabels = getMongoPodLabels(); + if (!podLabels) { + return false; } - var labels = process.env.MONGO_SIDECAR_POD_LABELS.split(','); + var labels = process.env.LL_MONGO_POD_LABELS.split(','); for (var i in labels) { - var keyAndValue = labels[i].split(':'); + var keyAndValue = labels[i].split('='); labels[i] = { key: keyAndValue[0], value: keyAndValue[1] @@ -21,9 +25,10 @@ var getKubernetesROServiceAddress = function() { }; module.exports = { - podLabelS: getPodLabels(), - kubernetesROServiceAddress: getKubernetesROServiceAddress(), loopSleepSeconds: process.env.MONGO_SIDECAR_SLEEP_SECONDS || 5, unhealthySeconds: process.env.MONGO_SIDECAR_UNHEALTHY_SECONDS || 15, - env: process.env.NODE_ENV || 'local' + env: process.env.NODE_ENV || 'local', + mongoPodLabels: getMongoPodLabels(), + mongoPodLabelCollection: getMongoPodLabelCollection(), + kubernetesROServiceAddress: getKubernetesROServiceAddress() }; diff --git a/src/lib/k8s.js b/src/lib/k8s.js index 41f7793..be1a5d1 100644 --- a/src/lib/k8s.js +++ b/src/lib/k8s.js @@ -4,19 +4,21 @@ var config = require('./config'); var client = new Client({ host: config.kubernetesROServiceAddress, protocol: 'http', - version: 'v1beta2' + version: 'v1beta3' }); var getMongoPods = function getPods(done) { - client.pods.get(function (err, pods) { + client.pods.get(function (err, podResult) { if (err) { return done(err); } + var pods = podResult[0].items; + var labels = config.mongoPodLabelCollection; var results = []; - for (var i in pods[0].items) { - var pod = pods[0].items[i]; - if (podContainsLabels(pod)) { + for (var i in pods) { + var pod = pods[i]; + if (podContainsLabels(pod, labels)) { results.push(pod); } } @@ -25,11 +27,12 @@ var getMongoPods = function getPods(done) { }); }; -var podContainsLabels = function podContainsLabels(pod) { - var labels = config.podLabelS; +var podContainsLabels = function podContainsLabels(pod, labels) { + if (!pod.metadata || !pod.metadata.labels) return false; + for (var i in labels) { var kvp = labels[i]; - if (!pod.labels[kvp.key] || pod.labels[kvp.key] != kvp.value) { + if (!pod.metadata.labels[kvp.key] || pod.metadata.labels[kvp.key] != kvp.value) { return false; } } diff --git a/src/lib/mongo.js b/src/lib/mongo.js index 85c0521..2a6ea5e 100644 --- a/src/lib/mongo.js +++ b/src/lib/mongo.js @@ -18,7 +18,9 @@ var getDb = function(host, done) { host = host || localhost; var mongoDb = new Db('local', new MongoServer(host, 27017)); mongoDb.open(function (err, db) { - if (err) return done(err); + if (err) { + return done(err); + } return done(null, db); }); diff --git a/src/lib/worker.js b/src/lib/worker.js index 649795d..666f7bc 100644 --- a/src/lib/worker.js +++ b/src/lib/worker.js @@ -44,8 +44,7 @@ var workloop = function workloop() { //Lets remove any pods that aren't running for (var i = pods.length - 1; i >= 0; i--) { var pod = pods[i]; - - if (pod.currentState.status !== 'Running') { + if (pod.status.phase !== 'Running') { pods.splice(i, 1); } } @@ -123,11 +122,11 @@ var primaryWork = function(db, pods, members, shouldForce, done) { var addrToAdd = []; for (var i in pods) { var pod = pods[i]; - if (pod.currentState.status !== 'Running') { + if (pod.status.phase !== 'Running') { continue; } - var podIp = pod.currentState.podIP; + var podIp = pod.status.podIP; var podAddr = podIp + ':27017'; var podInRs = false; for (var j in members) { @@ -169,15 +168,20 @@ var memberShouldBeRemoved = function(member) { }; var notInReplicaSet = function(db, pods, done) { + var createTestRequest = function(pod) { + return function(completed) { + mongo.isInReplSet(pod.status.podIP, completed); + }; + }; + //If we're not in a rs and others ARE in the rs, just continue, another path will ensure we will get added //If we're not in a rs and no one else is in a rs, elect one to kick things off var testRequests = []; for (var i in pods) { var pod = pods[i]; - if (pod.currentState.status === 'Running') { - testRequests.push(function(completed) { - mongo.isInReplSet(pod.currentState.podIP, completed); - }); + + if (pod.status.phase === 'Running') { + testRequests.push(createTestRequest(pod)); } } @@ -206,15 +210,15 @@ var podElection = function(pods) { //Because all the pods are going to be running this code independently, we need a way to consistently find the same //node to kick things off, the easiest way to do that is convert their ips into longs and find the highest pods.sort(function(a,b) { - var aIpVal = ip.toLong(a.currentState.podIP); - var bIpVal = ip.toLong(b.currentState.podIP); + var aIpVal = ip.toLong(a.status.podIP); + var bIpVal = ip.toLong(b.status.podIP); if (aIpVal < bIpVal) return -1; if (aIpVal > bIpVal) return 1; return 0; //Shouldn't get here... all pods should have different ips }); //Are we the lucky one? - return pods[0].currentState.podIP == hostIp; + return pods[0].status.podIP == hostIp; }; module.exports = {