Skip to content

Commit

Permalink
add sync mesh namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangcheng870518 committed Mar 15, 2023
1 parent 2d6e424 commit c100fd1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
30 changes: 29 additions & 1 deletion packages/server/config/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,42 @@ async function syncOsm () {
}
});
}
const k8sCoreApi = kc.makeApiClient(k8s.CoreV1Api)
const allNs = await k8sCoreApi.listNamespace();
for (const item of allNs.body.items) {
const meshName = item.metadata.labels["openservicemesh.io/monitored-by"];
if (!meshName) continue;
let ns = await strapi.db
.query('api::namespace.namespace')
.findOne({ where: { name: item.metadata.name, registry: registry.id }, populate: true});
if (!ns) {
ns = await strapi.db
.query('api::namespace.namespace')
.create({ data: { name: item.metadata.name, registry: registry.id }, populate: true });
}

const mesh = await strapi.db.query('api::mesh.mesh').findOne({
where: {
name: meshName,
namespace: registry.namespaces.map((n) => n.id),
}, populate: true
});
if (!mesh) continue;
mesh.bindNamespaces = mesh.bindNamespaces.map((n) => n.id)
if (mesh.bindNamespaces.indexOf(ns.id) == -1) {
mesh.bindNamespaces.push(ns.id);
await strapi.db.query('api::mesh.mesh')
.update({where: {id: mesh.id}, data: {bindNamespaces: mesh.bindNamespaces}})
}
}
}
} catch (error) {
console.error(error);
}
}

module.exports = {
'*/40 * * * * *': () => {
'40 * * * * *': () => {
syncOsm()
},
'10 * * * * *': () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/api/registry/services/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ module.exports = createCoreService('api::registry.registry', {
Authorization: 'Basic ' + buf.toString('base64'),
Host: url.host
},
timeout: 30000
});
try {
const response = await eurekaAxios.get('/eureka/apps');
Expand Down Expand Up @@ -177,7 +178,6 @@ module.exports = createCoreService('api::registry.registry', {
async fetchK8sNamespace (registry) {
// strapi.log.debug(" --->>fetchK8sNamespace - registry = " + JSON.stringify(registry));
const regId = registry.id;
strapi.log.debug(" --->>fetchK8sNamespace - regId = " + regId);
const k8sApi = await this.getK8sApi(registry.config);
try {
const result = await k8sApi.listNamespace();
Expand Down Expand Up @@ -249,6 +249,7 @@ module.exports = createCoreService('api::registry.registry', {
'Content-Type': 'application/json',
Authorization: 'Basic ' + buf.toString('base64'),
},
timeout: 30000
});

try {
Expand Down

0 comments on commit c100fd1

Please sign in to comment.