Commit 50153250 authored by kewei.jia's avatar kewei.jia

更换钉钉的通知地址(qa-home)

parent caa22554
...@@ -2,9 +2,11 @@ const path = require('path') ...@@ -2,9 +2,11 @@ const path = require('path')
const Client = require('kubernetes-client').Client const Client = require('kubernetes-client').Client
const config = require('kubernetes-client').config const config = require('kubernetes-client').config
const _ = require('lodash') const _ = require('lodash')
const moment = require('moment')
const yaml = require('js-yaml') const yaml = require('js-yaml')
const logger = require('koa-log4').getLogger('kubeService') const logger = require('koa-log4').getLogger('kubeService')
const yamls = require('../yamls') const yamls = require('../yamls')
const dict = require('./dictService')
const client = new Client({ const client = new Client({
config: config.fromKubeconfig( config: config.fromKubeconfig(
...@@ -13,12 +15,11 @@ const client = new Client({ ...@@ -13,12 +15,11 @@ const client = new Client({
version: '1.10', version: '1.10',
}) })
const serviceGet = async (namespace, service) => client.api.v1.namespaces(namespace).services(service).get() const makeManifest = (data) => {
const podGet = async namespace => client.api.v1.namespaces(namespace).pods.get() if (!data.debug) {
data.debug = '"0"'
const serviceCreate = async (data) => { }
const { type, namespace, serviceName } = data const yamlManifest = yamls[data.type].replace(/{{([A-Za-z0-9_\.]+)}}/g, function () {
const yamlManifest = yamls[type].replace(/{{([A-Za-z0-9_\.]+)}}/g, function () {
if (_.get(data, arguments[1], null) === null) { if (_.get(data, arguments[1], null) === null) {
throw new Error(`缺少模板所需变量: ${arguments[1]}`) throw new Error(`缺少模板所需变量: ${arguments[1]}`)
} }
...@@ -26,6 +27,12 @@ const serviceCreate = async (data) => { ...@@ -26,6 +27,12 @@ const serviceCreate = async (data) => {
}) })
const yamlArray = yamlManifest.split('---') const yamlArray = yamlManifest.split('---')
return yamlArray
}
const serviceCreate = async (data) => {
const { namespace, serviceName } = data
const yamlArray = makeManifest(data)
for (const item of yamlArray) { for (const item of yamlArray) {
const jsonObj = yaml.load(item); const jsonObj = yaml.load(item);
...@@ -58,6 +65,19 @@ const serviceCreate = async (data) => { ...@@ -58,6 +65,19 @@ const serviceCreate = async (data) => {
} }
} }
const serviceUpdate = async (data) => {
const { namespace, serviceName } = data
const yamlArray = makeManifest(data)
for (const item of yamlArray) {
const jsonObj = yaml.load(item);
if (jsonObj.kind === 'Deployment') {
logger.info('Deployment:', JSON.stringify(jsonObj))
await client.apis.apps.v1beta1.namespaces(namespace).deployments(serviceName).put({ body: jsonObj })
}
}
}
const formatServiceInfo = (obj) => { const formatServiceInfo = (obj) => {
const portObj = {} const portObj = {}
obj.spec.ports.forEach((i) => { obj.spec.ports.forEach((i) => {
...@@ -66,27 +86,49 @@ const formatServiceInfo = (obj) => { ...@@ -66,27 +86,49 @@ const formatServiceInfo = (obj) => {
return _.assign(portObj, { return _.assign(portObj, {
clusterIp: obj.spec.clusterIP, clusterIp: obj.spec.clusterIP,
ports: obj.spec.ports, portMappings: obj.spec.ports,
userLabels: obj.metadata.labels, // labels: obj.metadata.labels,
}) })
} }
const getPodStatus = (podInfo) => {
if (podInfo.status.phase === 'Pending') {
return 'Pending'
}
if (podInfo.status.conditions[2].status === 'False') {
return 'PodScheduling'
}
if (podInfo.status.conditions[0].status === 'False') {
return 'Initializing'
}
if (podInfo.status.conditions[1].status === 'False') {
return 'Waiting'
}
if (podInfo.metadata.deletionTimestamp) {
return 'Terminating'
}
return 'Normal'
}
const formatPodInfo = (podInfo) => { const formatPodInfo = (podInfo) => {
const podStatus = podInfo.status.phase === 'Running' && podInfo.status.conditions.every(i => i.status === 'True') // if (podInfo.metadata.name.indexOf('xyqb') !== -1) {
? 'Running' : 'Pending' // console.log(1, JSON.stringify(podInfo), podInfo.metadata.labels['qcloud-app'])
// }
const podStatus = getPodStatus(podInfo)
const containerImage = _.get(podInfo.status.containerStatuses, '[0].image', '') // const containerImage = _.get(podInfo.status.containerStatuses, '[0].image', '')
const containerImage = _.get(podInfo.spec.containers, '[0].image', '')
const ret = { const ret = {
serviceName: (podInfo.metadata.labels && podInfo.metadata.labels['qcloud-app']) || podInfo.metadata.name, serviceName: (podInfo.metadata.labels && podInfo.metadata.labels['qcloud-app']) || podInfo.metadata.name,
type: podInfo.metadata.labels.tier, podName: podInfo.metadata.name,
pod_name: podInfo.metadata.name, status: podStatus,
pod_status: podStatus, podIp: podInfo.status.podIP,
pod_ip: podInfo.status.podIP, lanIp: podInfo.status.hostIP,
host_ip: podInfo.status.hostIP, startTime: podInfo.status.startTime,
start_time: podInfo.status.startTime, createdAt: moment(new Date(podInfo.status.startTime)).startOf('minute').fromNow(),
// live: moment(new Date(podInfo.status.startTime)).startOf('minute').fromNow(),
image: containerImage, image: containerImage,
labels: podInfo.metadata.labels,
} }
if (containerImage !== '') { if (containerImage !== '') {
...@@ -98,15 +140,19 @@ const formatPodInfo = (podInfo) => { ...@@ -98,15 +140,19 @@ const formatPodInfo = (podInfo) => {
return ret return ret
} }
const formatIngressInfo = obj => ({ host: obj.spec && obj.spec.rules && obj.spec.rules[0].host }) const formatIngressInfo = obj => ({ host: _.get(obj.spec, 'rules[0].host', '') })
const getPods = async (namespace) => {
const podData = await client.api.v1.namespaces(namespace).pods.get()
return podData.body.items
}
const servicesGet = async (namespace) => { const getServices = async (namespace) => {
const ret = [] const ret = []
const service = {} const service = {}
const res = await Promise.all([ const res = await Promise.all([
client.api.v1.namespaces(namespace).pods.get(), client.api.v1.namespaces(namespace).pods.get(),
// client.api.v1.namespaces(namespace).pods.get({ qs: { labelSelector: 'tier!=job' } }),
client.api.v1.namespaces(namespace).services.get(), client.api.v1.namespaces(namespace).services.get(),
client.apis.extensions.v1beta1.namespaces(namespace).ingresses.get(), client.apis.extensions.v1beta1.namespaces(namespace).ingresses.get(),
]) ])
...@@ -118,7 +164,6 @@ const servicesGet = async (namespace) => { ...@@ -118,7 +164,6 @@ const servicesGet = async (namespace) => {
res[1].body.items.forEach(async (item) => { res[1].body.items.forEach(async (item) => {
console.log(2, item)
if (service[item.metadata.name]) { if (service[item.metadata.name]) {
service[item.metadata.name] = _.assign(service[item.metadata.name], formatServiceInfo(item)) service[item.metadata.name] = _.assign(service[item.metadata.name], formatServiceInfo(item))
} }
...@@ -139,9 +184,39 @@ const servicesGet = async (namespace) => { ...@@ -139,9 +184,39 @@ const servicesGet = async (namespace) => {
return ret return ret
} }
const getServiceDetail = async (namespace, name) => {
const res = await Promise.all([
client.api.v1.namespaces(namespace).pods.get({ qs: { labelSelector: `qcloud-app=${name},tier!=job` } }),
client.api.v1.namespaces(namespace).services(name).get(),
])
res[0] = formatPodInfo(res[0].body.items[0])
res[1] = formatServiceInfo(res[1].body)
if (!dict.commonService.includes(name)) {
res[3] = await client.apis.extensions.v1beta1.namespaces(namespace).ingresses(name).get()
res[3] = formatIngressInfo(res[3].body)
}
return _.assign({}, res[0], res[3], res[1])
}
const serviceRestart = async (namespace, name) => {
await client.api.v1.namespaces(namespace).pods(name).delete()
}
const serviceDelete = async (namespace, name, rsName) => {
await client.apis.apps.v1beta1.namespaces(namespace).deployments(name).delete()
await client.apis.apps.v1.namespaces(namespace).replicasets(rsName).delete()
await client.api.v1.namespaces(namespace).services(name).delete()
}
module.exports = { module.exports = {
serviceGet, getServiceDetail,
servicesGet, getServices,
podGet, getPods,
serviceCreate, serviceCreate,
serviceUpdate,
serviceRestart,
serviceDelete,
} }
Markdown is supported
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