Last active
August 17, 2016 05:29
-
-
Save huangsam/2958522312b9d58f9c618fde08701c6e to your computer and use it in GitHub Desktop.
Try Kubernetes out via REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kube: | |
protocol: http | |
# this is a weak default. Must use KUBE_HOST to set host | |
host: localhost | |
# this is a weak default. Must use KUBE_PORT to set port | |
port: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var config = require('config'); | |
var rp = require('request-promise'); | |
var KUBE = config.get('kube.protocol') + '://' + config.get('kube.host') + ':' + config.get('kube.port'); | |
var api = module.exports; | |
/* | |
* Returns: | |
* name of pod | |
*/ | |
api.createNamespacedPod = function createNamespacedPod(namespace, spec) { | |
var options = { | |
uri: KUBE + '/api/v1/namespaces/' + namespace + '/pods', | |
json: true, | |
body: spec | |
}; | |
return rp.post(options).then(function (res) { | |
return res.metadata.name; | |
}); | |
}; | |
/* | |
* Returns: | |
* entire pod payload | |
*/ | |
api.deleteNamespacedPod = function deleteNamespacedPod(namespace, name) { | |
var options = { | |
uri: KUBE + '/api/v1/namespaces/' + namespace + '/pods/' + name, | |
json: true | |
}; | |
return rp.del(options); | |
}; | |
/* | |
* Returns: | |
* raw plaintext from container logs | |
*/ | |
api.readNamespacedPodLog = function readNamespacedPodLog(namespace, name) { | |
var options = { | |
uri: KUBE + '/api/v1/namespaces/' + namespace + '/pods/' + name + '/log', | |
json: true | |
}; | |
return rp.get(options); | |
}; | |
/* | |
* Returns: | |
* Status object with its attributes listed below... | |
* phase: string | |
* conditions: array of {type, status, lastProbeTime, lastTransitionTime} | |
* hostIP: string | |
* podIP: string | |
* startTime: datetime | |
* containerStatuses: array of | |
* {name, state, lastState, ready, restartCount, image, imageID, containerID} | |
*/ | |
api.readNamespacedPod = function readNamespacedPod(namespace, name) { | |
var options = { | |
uri: KUBE + '/api/v1/namespaces/' + namespace + '/pods/' + name, | |
json: true | |
}; | |
return rp.get(options).then(function (res) { | |
return res.status; | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name" : "try-kube", | |
"description" : "Try Kubernetes out via REST", | |
"dependencies": { | |
"bluebird": "^3.2.2", | |
"config": "^1.16.0", | |
"js-yaml": "^3.4.3", | |
"request": "^2.73.0", | |
"request-promise": "^4.0.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var api = require('./kubeapi'); | |
var pod = { | |
"kind": "Pod", | |
"apiVersion": "v1", | |
"metadata": { | |
"generateName": "cache-", | |
"namespace": "default", | |
"labels": { | |
"app": "redis", | |
"role": "master" | |
} | |
}, | |
"spec": { | |
"containers": [{ | |
"name": "redis-master", | |
"image": "redis:2.8.23", | |
"ports": [{ | |
"name": "redis-server", | |
"containerPort": 6379 | |
}] | |
}] | |
} | |
}; | |
function showData(data) { | |
if (data && typeof data === 'object') { | |
console.log(JSON.stringify(data, null, 2)); | |
} else { | |
console.log(data); | |
} | |
} | |
//api.getCodeVersion().then(showData); | |
//api.createNamespacedPod('default', pod).then(showData); | |
//api.readNamespacedPod('default', 'cache-5736j').then(showData); | |
//api.readNamespacedPodLog('default', 'cache-5736j').then(showData); | |
//api.deleteNamespacedPod('default', 'cache-5736j').then(showData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment