Commit 3bd6d361 authored by Mohamed, Fawzi Roberto (fawzi)'s avatar Mohamed, Fawzi Roberto (fawzi)
Browse files

update kubernetes-client to latest (6.3.0)

callback -> promises
always return request with result in the body attributes
parent bd61b28a
...@@ -16,6 +16,7 @@ const resolveCache = require('../safe-memory-cache/map.js')({ ...@@ -16,6 +16,7 @@ const resolveCache = require('../safe-memory-cache/map.js')({
}) })
// gets pods with the given labels // gets pods with the given labels
// returns the raw response
function getPods(labels, next) { function getPods(labels, next) {
let selector = "" let selector = ""
let first = true let first = true
...@@ -26,7 +27,7 @@ function getPods(labels, next) { ...@@ -26,7 +27,7 @@ function getPods(labels, next) {
selector += ',' selector += ','
selector += `${k}=${labels[k]}` selector += `${k}=${labels[k]}`
} }
k8.ns(config.k8component.namespace).pods.get({ qs: { labelSelector: selector } }, next) k8.api.v1.ns(config.k8component.namespace).pods.get({ qs: { labelSelector: selector } }).then(function(data){ next(null, data); }, function(err){ next(err, null) })
} }
// gets json api formatted pods // gets json api formatted pods
...@@ -36,7 +37,7 @@ function jsonApiPods(labels, next, {details = true}={}) { ...@@ -36,7 +37,7 @@ function jsonApiPods(labels, next, {details = true}={}) {
if (err) { if (err) {
next(err, []) next(err, [])
} else { } else {
let podList = pods.items let podList = pods.body.items
if (podList) if (podList)
podList = podList.map(function(pod){ podList = podList.map(function(pod){
let secondsSinceCreation = (Date.now() - Date.parse(pod.metadata.creationTimestamp))/ 1000.0 let secondsSinceCreation = (Date.now() - Date.parse(pod.metadata.creationTimestamp))/ 1000.0
...@@ -88,6 +89,8 @@ function jsonApiPods(labels, next, {details = true}={}) { ...@@ -88,6 +89,8 @@ function jsonApiPods(labels, next, {details = true}={}) {
podInfo.attributes.data = pod podInfo.attributes.data = pod
return podInfo return podInfo
}) })
else
podList=[]
next(null, podList) next(null, podList)
} }
}) })
...@@ -153,14 +156,12 @@ function createPod(podName, repl, next) { ...@@ -153,14 +156,12 @@ function createPod(podName, repl, next) {
}, null) }, null)
} else { } else {
const templateValue = yaml.safeLoad(template, 'utf8') const templateValue = yaml.safeLoad(template, 'utf8')
k8.ns(config.k8component.namespace).pod.post({ body: templateValue}, function(err, res2){ k8.api.v1.ns(config.k8component.namespace).pod.post({ body: templateValue}).then(function(res2){
if(err) {
logger.error(`Cannot start pod ${podName}, error: ${stringify(err)}, \n====\ntemplate was ${template}\n====`);
next(err, null)
} else {
logger.info(`Created pod ${podName}: ${stringify(res2)}`) logger.info(`Created pod ${podName}: ${stringify(res2)}`)
next(null, res2) next(null, res2.body)
} }, function(err) {
logger.error(`Cannot start pod ${podName}, error: ${err.message} ${stringify(err)}, \n====\ntemplate was ${template}\n====`);
next(err, null)
}) })
} }
}) })
...@@ -171,49 +172,56 @@ function createPod(podName, repl, next) { ...@@ -171,49 +172,56 @@ function createPod(podName, repl, next) {
/// functions that either gives the running pod or starts it /// functions that either gives the running pod or starts it
function getOrCreatePod(podName, repl, shouldCreate, next) { function getOrCreatePod(podName, repl, shouldCreate, next) {
k8.ns(config.k8component.namespace).pod.get(podName, function(err, result) { k8.api.v1.ns(config.k8component.namespace).pod(podName).get().then(function(result) {
if(err || result && if (result && result.body) {
(result.status && ['Error', 'Failed', 'Succeeded'].includes(result.status.phase) || let pod = result.body
result.metadata && result.metadata.deletionTimestamp)) { if (pod.metadata && pod.metadata.deletionTimestamp) {
if (result && result.metadata && result.metadata.deletionTimestamp) {
let error = { let error = {
error: 'pod shutting down', error: 'pod shutting down',
detail: `Pod ${podName} is shutting down, need to wait to restart` detail: `Pod ${podName} is shutting down, need to wait to restart`
} }
logger.warn(error.detail) logger.warn(error.detail)
next(error, null) next(error, null)
} else if (result && result.status && ['Error', 'Failed', 'Succeeded'].includes(result.status.phase)) { } else if (pod.status && ['Error', 'Failed', 'Succeeded'].includes(pod.status.phase)) {
if (shouldCreate) { if (shouldCreate) {
k8.ns(config.k8component.namespace).pods.delete({ name: podName }, function (err, result) { k8.api.v1.ns(config.k8component.namespace).pods.delete({ name: podName }).then(function (delResult) {
if (!err) {
logger.info(`Deleted stopped pod ${podName} to restart it`) logger.info(`Deleted stopped pod ${podName} to restart it`)
createPod(podName, repl, next) // wait & return 'pod shutting down' instead? createPod(podName, repl, next) // wait & return 'pod shutting down' instead?
} else { }, function(err) {
let error = { let error = {
error: 'failed deleting pod', error: 'failed deleting pod',
detail: `Error deleting pod ${podName} while trying to restart it: ${stringify(err)}` detail: `Error deleting pod ${podName} while trying to restart it: ${stringify(err)}`
} }
logger.warn(error.detail) logger.warn(error.detail)
next(err, null) next(err, null)
}
}); });
}
} else {
//logger.debug(`looked up ${podName}: ${stringify(result)}`)
next(null, pod)
}
} else {
if (shouldCreate) {
createPod(podName, repl, next)
} else { } else {
let error = { let error = {
error: 'pod failed', error: 'pod failed',
detail: `Requested pod ${podName} which failed but should not be created, error: ${stringify(err)}` detail: `Requested pod ${podName} which does not exist and should not be created`
} }
logger.error(error.detail); logger.error(error.detail);
next(error, null) next(error, null)
} }
} else if (shouldCreate) { }
}, function(err) {
if (shouldCreate) {
createPod(podName, repl, next) createPod(podName, repl, next)
} else { } else {
logger.error(`Requested pod ${podName} which does not exist and should not be created, error: ${stringify(err)}`); let error = {
next(err, null) error: 'pod failed',
detail: `Requested pod ${podName} which failed but should not be created, error: ${stringify(err)}`
} }
} else { logger.error(error.detail);
//logger.debug(`looked up ${podName}: ${stringify(result)}`) next(error, null)
next(null, result)
} }
}); });
} }
...@@ -222,10 +230,11 @@ function resolvePod(repl, next) { ...@@ -222,10 +230,11 @@ function resolvePod(repl, next) {
const podName = components.podNameForRepl(repl) const podName = components.podNameForRepl(repl)
var v = resolveCache.get(podName) var v = resolveCache.get(podName)
if (v === undefined) { if (v === undefined) {
getOrCreatePod(podName, repl, config.k8component.image.autoRestart, function (err, pod) { getOrCreatePod(podName, repl, config.k8component.image.autoRestart, function (err, reply) {
if (err) { if (err) {
next(err, null) next(err, null)
} else { } else {
let pod = reply
const portNr = pod.spec.containers[0].ports[0].containerPort const portNr = pod.spec.containers[0].ports[0].containerPort
const podIp = pod.status.podIP const podIp = pod.status.podIP
if (podIp) { if (podIp) {
...@@ -281,34 +290,29 @@ function resolvePod(repl, next) { ...@@ -281,34 +290,29 @@ function resolvePod(repl, next) {
function deletePod(podName, next) { function deletePod(podName, next) {
k8.ns(config.k8component.namespace).pods.delete({ name: podName }, function (err, result) { k8.api.v1.ns(config.k8component.namespace).pods.delete({ name: podName }).then(function (result) {
resolveCache.set(podName, undefined) resolveCache.set(podName, undefined)
if (!err) {
logger.info(`deleted pod ${podName}`) logger.info(`deleted pod ${podName}`)
next(null, result) next(null, result.body)
} else { }, function(err) {
logger.warn(`Error deleting pod ${podName}: ${stringify(err)}`) logger.warn(`Error deleting pod ${podName}: ${stringify(err)}`)
next(err, null) next(err, null)
}
}) })
} }
// returns info about services indexed by service // returns info about services indexed by service
function getServiceInfo(namespace, next, {details = false} = {}) { function getServiceInfo(namespace, next, {details = false} = {}) {
k8.ns(namespace).services.get({}, function(err, res) { k8.api.v1.ns(namespace).services.get({}).then(function(res) {
if (err) {
next(err, null)
} else {
let services = {} let services = {}
let master = config.k8Api.url let master = config.k8Api.url
let masterHostname = new url.URL(master).hostname let masterHostname = new url.URL(master).hostname
let node = config.k8Api.node let node = config.k8Api.node
let frontendUrl = config.api.frontendUrl let frontendUrl = config.app.frontendUrl
let frontendHostname = new url.URL(frontendUrl).hostname let frontendHostname = new url.URL(frontendUrl).hostname
let frontendProtocol = new url.URL(frontendUrl).protocol let frontendProtocol = new url.URL(frontendUrl).protocol
if (res.items) if (res.body && res.body.items)
for (let is in res.items) { for (let is in res.body.items) {
let s = res.items[is] let s = res.body.items[is]
let service = { let service = {
name: s.metadata.name, name: s.metadata.name,
namespace: s.metadata.namespace, namespace: s.metadata.namespace,
...@@ -330,7 +334,8 @@ function getServiceInfo(namespace, next, {details = false} = {}) { ...@@ -330,7 +334,8 @@ function getServiceInfo(namespace, next, {details = false} = {}) {
services[service.name]=[service] services[service.name]=[service]
} }
next(null, services) next(null, services)
} }, function(err) {
next(err, null)
}) })
} }
......
const K8Api = require('kubernetes-client'), const K8Api = require('kubernetes-client');
fs = require('fs'), const fs = require('fs');
config = require('config'); const config = require('config');
const Client = K8Api.Client;
const k8Config = K8Api.config;
const client = new Client({ config: k8Config.fromKubeconfig(config.k8Api.kubeconfig), version: '1.9' });
var k8options = { var k8options = {
url: config.k8Api.url, url: config.k8Api.url,
...@@ -14,6 +17,6 @@ if(config.k8Api.ca){ ...@@ -14,6 +17,6 @@ if(config.k8Api.ca){
k8options.key = fs.readFileSync(config.k8Api.key); k8options.key = fs.readFileSync(config.k8Api.key);
} }
const k8 = new K8Api.Core(k8options); const k8 = client // new K8Api.Core(k8options);
module.exports = k8 module.exports = k8
...@@ -108,15 +108,15 @@ module.exports = function (app, redirect, config, proxyServer, proxyRouter, k8, ...@@ -108,15 +108,15 @@ module.exports = function (app, redirect, config, proxyServer, proxyRouter, k8,
} }
if (podIp && ready && podInfo.status.phase == 'Running') { // we have a valid and running pod if (podIp && ready && podInfo.status.phase == 'Running') { // we have a valid and running pod
if (cconf.execCommand) { if (cconf.execCommand) {
if (k8.ns(cconf.namespace).pod(podName).exec) { k8.api.v1.ns(cconf.namespace).pod(podName).exec.post({ qs: {
k8.ns(cconf.namespace).pod(podName).exec.post({ qs: {
command: cconf.execCommand, command: cconf.execCommand,
stdin: false, stdin: false,
stderr: false, stderr: true,
stdout: false, stdout: true,
tty: false tty: false
} }, function(err, value) { } }).then(function(value){
if (err) { res.redirect(302, target);
}, function(err) {
logger.warn(`command ${stringify(execCommand)} on pod ${podName} failed ${stringify(err)}`) logger.warn(`command ${stringify(execCommand)} on pod ${podName} failed ${stringify(err)}`)
components.evalHtmlTemplate("failedCommand.html", { components.evalHtmlTemplate("failedCommand.html", {
podName: podName, podName: podName,
...@@ -126,14 +126,7 @@ module.exports = function (app, redirect, config, proxyServer, proxyRouter, k8, ...@@ -126,14 +126,7 @@ module.exports = function (app, redirect, config, proxyServer, proxyRouter, k8,
}, function(err, templateHtml) { }, function(err, templateHtml) {
res.status(500).send(templateHtml) res.status(500).send(templateHtml)
}) })
} else {
res.redirect(302, target);
}
}) })
} else {
logger.warn(`command ${stringify(execCommand)} on pod ${podName} not yet implemented (should update kubernetes-client)`)
res.redirect(302, target);
}
} else { } else {
res.redirect(302, target); res.redirect(302, target);
} }
...@@ -211,18 +204,18 @@ module.exports = function (app, redirect, config, proxyServer, proxyRouter, k8, ...@@ -211,18 +204,18 @@ module.exports = function (app, redirect, config, proxyServer, proxyRouter, k8,
var podName = req.params.podname; var podName = req.params.podname;
var podInfo = components.infoForPodName(podName) var podInfo = components.infoForPodName(podName)
if (podInfo.user && loggedUsername === podInfo.user) { if (podInfo.user && loggedUsername === podInfo.user) {
k8.ns(cconf.namespace).pods.get({ name: podName }, function (err, result) { k8.api.v1.ns(cconf.namespace).pod(podName).get().then(function (result) {
if (!err) {
res.type('application/vnd.api+json').json({data:{ id: podName, res.type('application/vnd.api+json').json({data:{ id: podName,
type: 'pod', type: 'pod',
attributes: { attributes: {
data: result data: result.body
} }
} }
}, null, 2); }, null, 2);
} else res.type('application/vnd.api+json').json({errors:[{ }, function(err) {
res.type('application/vnd.api+json').json({errors:[{
id: 'no pod', id: 'no pod',
detail: `error getting info onn pod ${podName}`, detail: `error getting info on pod ${podName}`,
data: err data: err
}]}); }]});
}); });
......
...@@ -29,6 +29,7 @@ usersettings_redis: { ...@@ -29,6 +29,7 @@ usersettings_redis: {
host: "user-settings-db-redis-master" host: "user-settings-db-redis-master"
} }
k8Api: { k8Api: {
kubeconfig: "kube-certs/config"
url: "https://labdev3-nomad.esc.rzg.mpg.de:6443" url: "https://labdev3-nomad.esc.rzg.mpg.de:6443"
node: "labdev3-nomad.esc.rzg.mpg.de" node: "labdev3-nomad.esc.rzg.mpg.de"
ca: "kube-certs/ca.crt" ca: "kube-certs/ca.crt"
......
...@@ -342,22 +342,28 @@ echo " pushd kube-certs" ...@@ -342,22 +342,28 @@ echo " pushd kube-certs"
echo " elif [ -e ../kube-certs ] ; then" echo " elif [ -e ../kube-certs ] ; then"
echo " pushd ../kube-certs" echo " pushd ../kube-certs"
echo " elif [ -e ~/.minikube ] ; then" echo " elif [ -e ~/.minikube ] ; then"
echo " pushd ~/.minikube" echo " mkdir -p ./kube-certs"
echo " pushd kube-certs"
echo " cp ~/.minikube/ca.crt ~/.minikube/client.crt ~/.minikube/client.key ."
echo " sed \"s|$HOME/.minikube|/usr/src/app/kube-certs|g\" ~/.kube/config > config"
if [ -z "$KUBERNETES_SERVER_URL" ] ; then
echo " echo https://\$(minikube ip):8443 > server.url"
else
echo " echo \"$KUBERNETES_SERVER_URL\" > server.url"
fi
if [ -z "$KUBERNETES_NODE" ] ; then
echo " echo \$(minikube ip) >node.addr"
else
echo " echo \"$KUBERNETES_NODE\" > node.addr"
fi
echo " else" echo " else"
echo " pushd ." echo " pushd ."
echo " fi" echo " fi"
if [ -z "$KUBERNETES_SERVER_URL" -a -e "$HOME/.minikube" ] ; then
KUBERNETES_SERVER_URL="https://$(minikube ip):8443"
fi
if [ -z "$KUBERNETES_NODE" -a -e "$HOME/.minikube" ] ; then
KUBERNETES_NODE="$(minikube ip)"
fi
if [ -n "$KUBERNETES_SERVER_URL" ]; then if [ -n "$KUBERNETES_SERVER_URL" ]; then
echo " kubectl create secret generic kube-certs --from-file=ca.crt=ca.crt --from-file=client.crt=client.crt --from-file=client.key=client.key --from-literal=server.url=\"$KUBERNETES_SERVER_URL\" --from-literal=node.addr=\"$KUBERNETES_NODE\"" echo " kubectl create secret generic kube-certs --from-file=ca.crt=ca.crt --from-file=client.crt=client.crt --from-file=client.key=client.key --from-literal=server.url=\"$KUBERNETES_SERVER_URL\" --from-literal=node.addr=\"$KUBERNETES_NODE\" --from-file=config=config"
else else
echo " kubectl create secret generic kube-certs --from-file=ca.crt=ca.crt --from-file=client.crt=client.crt --from-file=client.key=client.key --from-file=server.url=server.url --from-file=node.addr=node.addr" echo " kubectl create secret generic kube-certs --from-file=ca.crt=ca.crt --from-file=client.crt=client.crt --from-file=client.key=client.key --from-file=server.url=server.url --from-file=node.addr=node.addr --from-file=config=config"
fi fi
echo " popd" echo " popd"
echo "# create secret with web certificates" echo "# create secret with web certificates"
......
...@@ -2,11 +2,7 @@ const repl = require('repl') ...@@ -2,11 +2,7 @@ const repl = require('repl')
const config = require('config'); const config = require('config');
const k8 = require('./app/kubernetes')(config); const k8 = require('./app/kubernetes')(config);
const components = require('./app/components') const components = require('./app/components')
var ns = k8.ns(config.k8component.namespace) var ns = k8.api.v1.ns(config.k8component.namespace)
function getService() {
ns.service.get(config.k8component.imageType + '-svc-fawzi2')
}
function testComponent() { function testComponent() {
components.templateForImageType("beaker", "fawzi2", {'session': 'pippo'}, function(err,data,repl) { components.templateForImageType("beaker", "fawzi2", {'session': 'pippo'}, function(err,data,repl) {
...@@ -18,6 +14,5 @@ const r = repl.start('> '); ...@@ -18,6 +14,5 @@ const r = repl.start('> ');
r.context.config = config r.context.config = config
r.context.k8 = k8 r.context.k8 = k8
r.context.ns = ns r.context.ns = ns
r.context.getService = getService
r.context.components = components r.context.components = components
r.context.testComponent = testComponent r.context.testComponent = testComponent
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment