Commit a2d7eee6 authored by 智勇's avatar 智勇

重构镜像仓库接口 qa-test

parent e87e88ae
...@@ -73,7 +73,7 @@ const create = async (ctx) => { ...@@ -73,7 +73,7 @@ const create = async (ctx) => {
type: app.type, type: app.type,
serviceName: app.appname, serviceName: app.appname,
namespace: body.namespace, namespace: body.namespace,
image: app.tag, image: app.tag.replace(`qa-${app.type}`, 'qa-test'),
label: app.label, label: app.label,
domain: app.domain, domain: app.domain,
debug: '"0"', debug: '"0"',
......
const Router = require('koa-router')
const logger = require('koa-log4').getLogger()
const bodyParser = require('koa-bodyparser')
const ProConfig = require('../service/mongoService').ProConfig
const repoServiceNew = require('../service/repoServiceNew')
const router = new Router()
module.exports = router
const QA = 'qa-'
const BASETYPE = 'base'
const QABASE = 'qa-base'
const QATEST = 'qa-test'
const formatData = (obj) => {
const data = []
// eslint-disable-next-line guard-for-in
for (const i in obj) {
data.push({
type: i,
repos: obj[i],
})
}
data.sort((a, b) => (a.type > b.type ? 1 : -1))
return data
}
const getAllRepos = async (ctx) => {
const ns = [{ namespace: QATEST }, { namespace: QABASE }]
ctx._domains = await ProConfig.getActiveProjectsHostName()
const task = []
for (let i = 0; i < ns.length; i += 1) {
logger.info('要查询的namespace:', ns[i])
ns[i].type = ns[i].namespace.replace(QA, '')
task.push(repoServiceNew.getRepoOfNs(ctx, ns[i]))
}
await Promise.all(task)
const obj = {}
for (const n of ns) {
if (n.type === BASETYPE) {
obj[n.type] = n.repos
} else {
for (const repo of n.repos) {
if (!obj[repo.type]) obj[repo.type] = []
obj[repo.type].push(repo)
}
}
}
const data = formatData(obj)
ctx.body = ctx.ok(data)
}
const getBaseRepos = async (ctx) => {
const ns = {
type: BASETYPE,
namespace: QABASE,
}
await repoServiceNew.getRepoOfNs(ctx, ns)
ctx.body = ctx.ok([ns])
}
const getAppRepos = async (ctx) => {
const ns = { namespace: QATEST }
ctx._domains = await ProConfig.getActiveProjectsHostName()
await repoServiceNew.getRepoOfNs(ctx, ns)
const obj = {}
for (const repo of ns.repos) {
if (!obj[repo.type]) obj[repo.type] = []
obj[repo.type].push(repo)
}
const data = formatData(obj)
ctx.body = ctx.ok(data)
}
const getSingleTypeApp = async (ctx) => {
let data = []
const ns = { namespace: ctx.request.body.namespace }
ctx._domains = await ProConfig.getActiveProjectsHostName()
await repoServiceNew.getRepoOfNs(ctx, ns)
for (const repo of ns.repos) {
if (repo.label === ctx.request.body.type) {
data.push(repo)
}
}
// qa-base 需要去重
if (ctx.request.body.type === 'base') {
const obj = {}
data = data.reduce((a, b) => {
obj[b.type] ? '' : obj[b.type] = true && a.push(b)
return a
}, [])
}
ctx.body = ctx.ok(data)
}
router
.use(bodyParser())
.get('/getAll', getAllRepos)
.get('/getBase', getBaseRepos)
.get('/getApp', getAppRepos)
.post('/getSingleTypeApp', getSingleTypeApp)
...@@ -43,7 +43,8 @@ const getCpu = async (ctx) => { ...@@ -43,7 +43,8 @@ const getCpu = async (ctx) => {
const getCpuInfo = async (ctx) => { const getCpuInfo = async (ctx) => {
const query = handleQuery(ctx.request.query) const query = handleQuery(ctx.request.query)
const list = await CpuResource.getCpu2(query, { _id: 0 }, { Percentage: 1 }) let list = await CpuResource.getCpu2(query, { _id: 0 }, { Percentage: 1 })
list = list.filter(i => i.name.split('-').length - 2 === ctx.request.query.name.split('-').length)
ctx.body = ctx.ok(list) ctx.body = ctx.ok(list)
} }
...@@ -56,7 +57,8 @@ const getMemory = async (ctx) => { ...@@ -56,7 +57,8 @@ const getMemory = async (ctx) => {
const getMemoryInfo = async (ctx) => { const getMemoryInfo = async (ctx) => {
const query = handleQuery(ctx.request.query) const query = handleQuery(ctx.request.query)
const list = await MemoryResource.getMemory2(query, { _id: 0 }, { Percentage: 1 }) let list = await MemoryResource.getMemory2(query, { _id: 0 }, { Percentage: 1 })
list = list.filter(i => i.name.split('-').length - 2 === ctx.request.query.name.split('-').length)
ctx.body = ctx.ok(list) ctx.body = ctx.ok(list)
} }
......
...@@ -23,6 +23,7 @@ const binlog2sql = require('./controller/binlog2sql') ...@@ -23,6 +23,7 @@ const binlog2sql = require('./controller/binlog2sql')
const harborHook = require('./controller/harborHook') const harborHook = require('./controller/harborHook')
const log = require('./controller/log') const log = require('./controller/log')
const repo = require('./controller/repo') const repo = require('./controller/repo')
const repoNew = require('./controller/repoNew')
const envTemplate = require('./controller/envTemplate') const envTemplate = require('./controller/envTemplate')
const env = require('./controller/env') const env = require('./controller/env')
const pipeline = require('./controller/pipeline') const pipeline = require('./controller/pipeline')
...@@ -51,6 +52,7 @@ router ...@@ -51,6 +52,7 @@ router
.use('/harborHook', harborHook.routes()) .use('/harborHook', harborHook.routes())
.use('/log', log.routes()) .use('/log', log.routes())
.use('/repo', repo.routes()) .use('/repo', repo.routes())
.use('/repoNew', repoNew.routes())
.use('/envTemplate', envTemplate.routes()) .use('/envTemplate', envTemplate.routes())
.use('/env', env.routes()) .use('/env', env.routes())
.use('/pipeline', pipeline.routes()) .use('/pipeline', pipeline.routes())
......
...@@ -16,6 +16,7 @@ async function job() { ...@@ -16,6 +16,7 @@ async function job() {
{namespace!~%22kube-system|monitor|rc-test%22}/(100*1000))%20by%20(pod_name)`, {namespace!~%22kube-system|monitor|rc-test%22}/(100*1000))%20by%20(pod_name)`,
method: 'GET', method: 'GET',
}) })
logger.info('container_spec_cpu_quota', cpuLimitRes.status, cpuLimitRes.data.result.length)
cpuLimitRes.data.result.forEach((item) => { cpuLimitRes.data.result.forEach((item) => {
if (item.metric.pod_name) { if (item.metric.pod_name) {
...@@ -27,9 +28,10 @@ async function job() { ...@@ -27,9 +28,10 @@ async function job() {
const cpuUsageRes = await awaitRequest({ const cpuUsageRes = await awaitRequest({
url: `${config.prometheus}/api/v1/query_range?query=sum (rate (container_cpu_usage_seconds_total url: `${config.prometheus}/api/v1/query_range?query=sum (rate (container_cpu_usage_seconds_total
{image!="",name=~"^k8s_.*",io_kubernetes_container_name!="POD",pod_name=~"^()()().*$"}[1m])) by {image!="",name=~"^k8s_.*",io_kubernetes_container_name!="POD",pod_name=~"^()()().*$"}[1m])) by
(pod_name)&start=${startTime}&end=${endTime}&step=90`, (pod_name)&start=${startTime}&end=${endTime}&step=60`,
method: 'GET', method: 'GET',
}) })
logger.info('container_cpu_usage_seconds_total', cpuUsageRes.status, cpuUsageRes.data.result.length)
cpuUsageRes.data.result.forEach((item) => { cpuUsageRes.data.result.forEach((item) => {
if (item.metric.pod_name) { if (item.metric.pod_name) {
...@@ -48,7 +50,7 @@ async function job() { ...@@ -48,7 +50,7 @@ async function job() {
name: item, name: item,
cpuLimit: finalCpuLimit[item], cpuLimit: finalCpuLimit[item],
cpuMaxUsage: finalCpuUsage[item], cpuMaxUsage: finalCpuUsage[item],
Percentage: `${(finalCpuUsage[item] / finalCpuLimit[item]).toFixed(3)}`, Percentage: `${(finalCpuUsage[item] / finalCpuLimit[item]).toFixed(4)}`,
} }
CpuResource.saveCpu(data) CpuResource.saveCpu(data)
}) })
......
...@@ -37,7 +37,7 @@ async function job() { ...@@ -37,7 +37,7 @@ async function job() {
name: item, name: item,
memoryLimit: Math.round(finalMemoryLimit[item]), memoryLimit: Math.round(finalMemoryLimit[item]),
memoryMaxUsage: Math.round(finalMemoryUsage[item]), memoryMaxUsage: Math.round(finalMemoryUsage[item]),
Percentage: `${(finalMemoryUsage[item] / finalMemoryLimit[item]).toFixed(3)}`, Percentage: `${(finalMemoryUsage[item] / finalMemoryLimit[item]).toFixed(4)}`,
} }
MemoryResource.saveMemory(data) MemoryResource.saveMemory(data)
}) })
......
...@@ -42,7 +42,7 @@ const schema = new Schema({ ...@@ -42,7 +42,7 @@ const schema = new Schema({
}) })
schema.statics.getActiveProjectsHostName = function () { schema.statics.getActiveProjectsHostName = function () {
return this.find({ des: { $ne: '海外' }, is_active: 1 }).select(['host_name', 'project_name']).lean().then((res) => { return this.find({ des: { $ne: '海外' }, is_active: 1 }).select(['host_name', 'project_name', 'type']).lean().then((res) => {
const reg = /^([\w-\.]+)\.[\w-]+\.[a-z]+$/ const reg = /^([\w-\.]+)\.[\w-]+\.[a-z]+$/
// 转为对象形式 // 转为对象形式
const o = res.reduce((prev, next) => { const o = res.reduce((prev, next) => {
...@@ -50,6 +50,7 @@ schema.statics.getActiveProjectsHostName = function () { ...@@ -50,6 +50,7 @@ schema.statics.getActiveProjectsHostName = function () {
prev[next.project_name] = { prev[next.project_name] = {
hostname: next.host_name, hostname: next.host_name,
domain: regRes ? regRes[1] : '', domain: regRes ? regRes[1] : '',
type: next.type,
} }
return prev return prev
}, {}) }, {})
......
const _ = require('lodash')
const config = require(global.configPath)
const QA = 'qa-'
const BASEREPO = 'qa-base'
const LASTEST = 'latest'
async function getTagOfRepo(ctx, repo) {
const res = await ctx.curl({
uri: `${config.api.tke_api}/repository/getTags`,
method: 'POST',
body: {
reponame: repo.reponame,
},
})
repo.tags = _.get(res, 'body.data.tagInfo', []).map(item => item.tagName)
}
async function getRepoOfNs(ctx, ns) {
const isBaseRepo = BASEREPO === ns.namespace
// 服务部署后,在运行中环境,通过label信息来展示列表
const label = ns.namespace.replace(QA, '')
const res = await ctx.curl({
uri: `${config.api.tke_api}/repository`,
method: 'POST',
body: {
namespace: ns.namespace,
},
})
let repos = _.get(res, 'body.data.repoInfo', []).map((item) => {
const appname = item.reponame.split('/')[1]
const o = {
reponame: item.reponame,
appname,
}
if (item.reponame.indexOf(BASEREPO) === -1) {
o.hostname = (ctx._domains[appname] && ctx._domains[appname].hostname) || ''
o.domain = (ctx._domains[appname] && ctx._domains[appname].domain) || ''
}
return o
})
const task = []
for (let i = 0, len = repos.length; i < len; i += 1) {
if (isBaseRepo) {
task.push(getTagOfRepo(ctx, repos[i]))
}
}
await Promise.all(task)
// 把tag信息拉平到appname上
const m = []
repos.forEach((item) => {
const tags = item.tags || [LASTEST]
tags.forEach((tag) => {
// console.log(1, item, ctx._domains[item.appname])
m.push({
// 部署时用于选择服务模板
type: isBaseRepo ? item.appname : ctx._domains[item.appname].type,
label: isBaseRepo ? label : ctx._domains[item.appname].type,
appname: isBaseRepo ? `${item.appname}:${tag}` : item.appname,
tag: `${item.reponame}:${tag}`,
// domain用于部署服务时的域名映射
domain: isBaseRepo ? `${item.appname}${tag}` : item.domain,
})
})
})
repos = m
repos.sort((a, b) => (a.appname > b.appname ? 1 : -1))
ns.repos = repos
delete ns.repoCount
delete ns.creationTime
delete ns.namespace
}
exports.getRepoOfNs = getRepoOfNs
...@@ -56,5 +56,6 @@ module.exports = { ...@@ -56,5 +56,6 @@ module.exports = {
+ '=fae6b46742f9f261997a7f4f07bb6ba21f274dc3e602065fff5c4f76cf1dbf53', + '=fae6b46742f9f261997a7f4f07bb6ba21f274dc3e602065fff5c4f76cf1dbf53',
sonarHost: 'http://sonar.quantgroups.com', sonarHost: 'http://sonar.quantgroups.com',
qahomeHost: 'http://qa2.liangkebang.com', qahomeHost: 'http://qa2.liangkebang.com',
snorDingRobotAddr: 'https://oapi.dingtalk.com/robot/send?access_token=0da7a1066b42bb23f079dacec3b7975e8cc12cee7efc5316a9b5b1900779a407', snorDingRobotAddr: 'https://oapi.dingtalk.com/robot/send?access_token'
+ '=0da7a1066b42bb23f079dacec3b7975e8cc12cee7efc5316a9b5b1900779a407',
} }
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