Skip to content

Commit

Permalink
Making changes and fixes to up the k8s code to v1beta3. Also changed …
Browse files Browse the repository at this point in the history
…the label selector to be inline with the k8s standard.
  • Loading branch information
cvallance committed May 18, 2015
1 parent a7b6739 commit 2465311
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .foreverignore
@@ -0,0 +1,2 @@
**/.idea/**
**/.git/**
2 changes: 1 addition & 1 deletion 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": {
Expand Down
23 changes: 14 additions & 9 deletions 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]
Expand All @@ -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()
};
19 changes: 11 additions & 8 deletions src/lib/k8s.js
Expand Up @@ -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);
}
}
Expand All @@ -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;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/mongo.js
Expand Up @@ -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);
});
Expand Down
26 changes: 15 additions & 11 deletions src/lib/worker.js
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 2465311

Please sign in to comment.