Skip to content

Commit

Permalink
feat: multiple user menu
Browse files Browse the repository at this point in the history
  • Loading branch information
yongheng2016 committed Jun 18, 2024
1 parent 7774953 commit 834ca0a
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 98 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Create an local_config.yaml file with the following
```
server:
apiServer:
url: https://monitoring.webos-acount.myterminus.com
wsUrl: https://monitoring.webos-acount.myterminus.com
url: https://dashboard.webos-acount.myterminus.com
wsUrl: https://dashboard.webos-acount.myterminus.com
```

Expand Down
16 changes: 16 additions & 0 deletions apps/adminConsole/src/assets/gpu.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 8 additions & 56 deletions apps/adminConsole/src/pages/ApplicationSpaces/IndexPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getNamespacesList } from 'src/network';
import { getNamespacesGroup } from 'src/network';
import MyTree from '@packages/ui/src/components/Menu/MyTree.vue';
import { get } from 'lodash-es';
import {
getNamespaceIcon,
systemNamespaces,
systemNamespacesOther,
customNamesapceIcon,
checkClusterScoped
} from './config';
import { getNamespaceIcon, customNamesapceIcon } from './config';
import MenuHeader from 'src/layouts/MenuHeader.vue';
const menuOptions = {
Expand All @@ -47,69 +41,25 @@ const userType = 'User Projects';
const systmeType = 'System Projects';
const defaultOpeneds = ref([userType, systmeType, route.params.namespace]);
const workspaceSplit = (data: any): any => {
const systemWorkspace: any = [];
const UserWorkspace: any = [];
data.forEach((item: any) => {
if (!owner) {
owner = item.metadata.labels['bytetrade.io/ns-owner'];
}
/** Manual grouping of namespace */
if (
(get(item, 'metadata.labels.["kubesphere.io/workspace"]') ===
'system-workspace' &&
systemNamespaces(owner).includes(get(item, 'metadata.name'))) ||
systemNamespacesOther.includes(get(item, 'metadata.name')) ||
checkClusterScoped(get(item, 'metadata.name'))
) {
systemWorkspace.push(item);
} else {
UserWorkspace.push(item);
}
});
return [
{
title: defaultOpeneds.value[0],
data: UserWorkspace
},
{
title: defaultOpeneds.value[1],
data: systemWorkspace
}
];
};
const fetchData = () => {
const params = {
sortBy: 'createTime',
labelSelector: 'kubesphere.io/workspace!=kubesphere.io/devopsproject'
};
loading.value = true;
getNamespacesList(params)
getNamespacesGroup(params)
.then((res) => {
const result = res.data.items;
const data = workspaceSplit(result);
const userReg = new RegExp(`-${owner}$`);
let title = '';
const result = res.data;
const data: any = result;
const newData = data.map((workspace: any) => ({
title: workspace.title,
id: workspace.title,
selectable: false,
icon: getNamespaceIcon('default'),
children: workspace.data.map((item: any) => {
title = item.metadata.name;
if (
workspace.title === userType &&
!Object.keys(customNamesapceIcon(owner)).includes(title)
) {
title = title.replace(userReg, '');
}
return {
title,
title: item.metadata.name,
id: item.metadata.name,
img: getNamespaceIcon(item.metadata.name),
route: {
Expand All @@ -122,6 +72,8 @@ const fetchData = () => {
list.value = newData.filter(
(item: any) => item.children && item.children.length > 0
);
defaultOpeneds.value = [data[0].title];
})
.finally(() => {
loading.value = false;
Expand Down
23 changes: 3 additions & 20 deletions apps/adminConsole/src/pages/ApplicationSpaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import userSpace from 'src/assets/user-space.svg';
import osSystem from 'src/assets/os-system.svg';
import userSystem from 'src/assets/user-system.svg';
import defaultIcon from 'src/assets/default.svg';
import gpuIcon from 'src/assets/gpu.svg';
import { useAppDetailStore } from 'src/stores/AppDetail';
import kubeIcon from 'src/assets/kube.png';
import kubesphereIcon from 'src/assets/kubesphere.png';
Expand Down Expand Up @@ -32,7 +33,8 @@ export const namespaceIcon: any = (username: string) => ({
'kubesphere-monitoring-system': kubesphereIcon,
'kube-system': kubeIcon,
'kube-public': kubeIcon,
'kube-node-lease': kubeIcon
'kube-node-lease': kubeIcon,
'gpu-system': gpuIcon
});

export const getNamespaceIcon = (namespace: string) => {
Expand All @@ -53,22 +55,3 @@ export const getNamespaceIcon = (namespace: string) => {
? icons[app]
: defaultIcon;
};

export const systemNamespaces = (username: string) => [
'os-system',
'kubesphere-monitoring-federated',
'kubesphere-controls-system',
'kubesphere-system',
'kubesphere-monitoring-system',
'default',
'kube-system',
'kube-public',
'kube-node-lease'
];

export const systemNamespacesOther = ['kubekey-system'];

export const checkClusterScoped = (namespace: string) => {
const target = appList.data.find((item) => item.namespace === namespace);
return target ? target.isClusterScoped : false;
};
8 changes: 8 additions & 0 deletions apps/common/src/network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ export const getNamespacesList = (
});
};

export const getNamespacesGroup = (
params: Pagination
): Promise<AxiosResponse<CustomresourcesResponse>> => {
return api.get('/capi/namespaces/group', {
params
});
};

export const getNamespaceDeployments = (
namespace: string,
params?: Pagination
Expand Down
3 changes: 2 additions & 1 deletion apps/server/server/cache/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const systemNamespaces = [
'default',
'kube-system',
'kube-public',
'kube-node-lease'
'kube-node-lease',
'gpu-system'
];

const resources_filter = (ctx, query) => {
Expand Down
67 changes: 53 additions & 14 deletions apps/server/server/controllers/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const {
getMyApps,
getAllMetric,
getUserMetric,
getClusterMetric
getClusterMetric,
getUsers,
getNamespaces
} = require('../services/session');
const {
setUserInfo,
Expand Down Expand Up @@ -168,13 +170,13 @@ const monitoringMetric = async (ctx) => {

cluster_cpu_utilisation.data.result[0][valueName] = isValues
? cluster_cpu_utilisation_value.map((item, index) => [
item[0],
item[1] / cluster_cpu_total_target[index][1]
])
item[0],
item[1] / cluster_cpu_total_target[index][1]
])
: [
cluster_cpu_utilisation_value[0],
cluster_cpu_utilisation_value[1] / cluster_cpu_total_target[1]
];
cluster_cpu_utilisation_value[0],
cluster_cpu_utilisation_value[1] / cluster_cpu_total_target[1]
];

const cluster_memory_utilisation = cloneDeep(
find(list, {
Expand All @@ -185,13 +187,13 @@ const monitoringMetric = async (ctx) => {
cluster_memory_utilisation.data.result[0][valueName];
cluster_memory_utilisation.data.result[0][valueName] = isValues
? cluster_memory_utilisation_value.map((item, index) => [
item[0],
item[1] / cluster_memory_total_target[index][1]
])
item[0],
item[1] / cluster_memory_total_target[index][1]
])
: [
cluster_memory_utilisation_value[0],
cluster_memory_utilisation_value[1] / cluster_memory_total_target[1]
];
cluster_memory_utilisation_value[0],
cluster_memory_utilisation_value[1] / cluster_memory_total_target[1]
];

list = list.concat([
{ ...cluster_cpu_utilisation, metric_name: 'cluster_cpu_utilisation' },
Expand All @@ -205,6 +207,42 @@ const monitoringMetric = async (ctx) => {
ctx.body = { results: list };
};

function endsWith(str, name) {
const regex = new RegExp(`-${name}$`); //;
return regex.test(str);
}

const namespaceGroup = async (ctx) => {
const namespaces = await getNamespaces(ctx);
const users = await getUsers(ctx)
const SYSTEM = 'System'
const usersData = users.data.items;
usersData.sort((a, b) => a.creation_timestamp - b.creation_timestamp)
const system = {
name: SYSTEM,
}
const list = [...usersData, system]

const result = list.map(item => {
if (item.name === SYSTEM) {
const data = namespaces.items.filter(namespace => !usersData.some(str => endsWith(namespace.metadata.name, str.name)))
return {
title: item.name,
data: data,
}
} else {
const data = namespaces.items.filter(namespace => endsWith(namespace.metadata.name, item.name))
return {
title: item.name,
data: data,
}
}

});

ctx.body = result;
}

const cacheUser = async (ctx, next) => {
const target = checkUrl(ctx.path);
const user = getUserInfo(ctx);
Expand Down Expand Up @@ -247,5 +285,6 @@ module.exports = {
userDetail,
cacheUser,
appList,
monitoringMetric
monitoringMetric,
namespaceGroup
};
4 changes: 3 additions & 1 deletion apps/server/server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ const {
userDetail,
cacheUser,
appList,
monitoringMetric
monitoringMetric,
namespaceGroup
} = require('./controllers/view');

const router = new Router();

router
.get('/capi/app/detail', userDetail)
.get('/capi/app/myapps', appList)
.get('/capi/namespaces/group', namespaceGroup)

.all('/(k)?api(s)?/(.*)', cacheUser)
.use(proxy('/(k)?api(s)?/(.*)', k8sResourceProxy))
Expand Down
2 changes: 1 addition & 1 deletion apps/server/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const app = new Koa();
const serverConfig = getServerConfig().server;

global.HOSTNAME = serverConfig.http.hostname || 'localhost';
global.PORT = serverConfig.http.port || 8000;
global.PORT = 8003

app.keys = ['kubesphere->_<'];

Expand Down
36 changes: 33 additions & 3 deletions apps/server/server/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const getUserDetail = async (token, clusterRole, isMulticluster) => {
}

user.globalRules = roles;
} catch (error) {}
} catch (error) { }

return user;
};
Expand Down Expand Up @@ -404,7 +404,7 @@ const getSupportGpuList = async (ctx) => {

gpuKinds = [...defaultGpu, ...otherGpus];
}
} catch (error) {}
} catch (error) { }

return gpuKinds;
};
Expand All @@ -418,6 +418,34 @@ const getGitOpsEngine = async (ctx) => {
return 'argocd';
};

const getNamespaces = async (ctx) => {
const token = ctx.cookies.get('auth_token');

const resp = await send_gateway_request({
method: 'GET',
url: `/kapis/resources.kubesphere.io/v1alpha3/namespaces`,
params: {
sortBy: 'createTime',
labelSelector: 'kubesphere.io/workspace!=kubesphere.io/devopsproject'
},
token
});
return resp;

}

const getUsers = async (ctx, clusterRole, isMulticluster) => {
const token = ctx.cookies.get('auth_token');


const resp = await send_gateway_request({
method: 'GET',
url: `/bfl/iam/v1alpha1/users`,
token
});
return resp;
};

const getCurrentUser = async (ctx, clusterRole, isMulticluster) => {
const token = ctx.cookies.get('auth_token');

Expand Down Expand Up @@ -532,5 +560,7 @@ module.exports = {
getMyApps,
getAllMetric,
getUserMetric,
getClusterMetric
getClusterMetric,
getUsers,
getNamespaces
};

0 comments on commit 834ca0a

Please sign in to comment.