Skip to content

Commit

Permalink
chore: test command,fix: change process of create cr (labring#3868)
Browse files Browse the repository at this point in the history
* doc: add multiNs

* chore: update test command
  • Loading branch information
xudaotutou authored Sep 8, 2023
1 parent 48a1d69 commit 78bd02e
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 110 deletions.
6 changes: 6 additions & 0 deletions frontend/READMD.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ Refer to other apps to add some configuration.
5. frontend/providers/app/deploy/manifests/deploy.yaml
6. frontend/providers/app/deploy/manifests/ingress.yaml.tmpl
7. makefile

## multiple namespaces

In order to support multiple namespaces, the method of obtaining 'namespace' in the
backend should be replaced with the method of obtaining it from 'kubeconfig' instead
of adding 'ns-' to 'user' for generation purpose.
17 changes: 0 additions & 17 deletions frontend/desktop/inlang.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"test:w": "jest --watch --runInBand",
"test:e2e": "jest --testPathPattern=/e2e/ --runInBand",
"test:ci": "jest --runInBand"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions frontend/desktop/scripts/cleanCluster.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $itmes = kubectl get user -o json | ConvertFrom-Json | Select-Object { $_.items
foreach ($item in $items) {
$name = $item.metadata.name
if ($name -ne "admin") {
kubectl delete user $name
kubectl.exe delete user $name
}
}
$items = kubectl get ns -o json |
Expand All @@ -16,5 +16,5 @@ Where-Object { $_.metadata.name -match "ns-*" -and $_.metadata.name -ne "ns-admi

foreach ($item in $items) {
$name = $item.metadata.name
kubectl delete ns $name
kubectl.exe delete ns $name
}
5 changes: 5 additions & 0 deletions frontend/desktop/scripts/getMongodbUri.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$_mongodbUri = (kubectl.exe get secret/desktop-frontend-secret -nsealos -o json |
ConvertFrom-Json).data.mongodb_uri
Write-Host $_mongodbUri
$mongodbUri = ConvertFrom-Base64ToString $_mongodbUri
Write-Host $mongodbUri
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { Db, MongoClient } from 'mongodb';
import request from '@/__tests__/api/request';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
const abdicateRequest = _abdicateRequest(request);
const createRequest = _createRequest(request);
const inviteMemberRequest = _inviteMemberRequest(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { _passwordLoginRequest } from '@/api/auth';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
import { _createRequest } from '@/api/namespace';
import request from '../request';
import request from '@/__tests__/api/request';
import { Db, MongoClient } from 'mongodb';
describe('Login create', () => {
let session: Session;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { _passwordLoginRequest } from '@/api/auth';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
import { _createRequest, _deleteTeamRequest, _swi, _switchRequest } from '@/api/namespace';
import request from '../request';
import request from '@/__tests__/api/request';
import { Db, MongoClient } from 'mongodb';
describe('delete team', () => {
let session: Session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { Db, MongoClient } from 'mongodb';
import request from '@/__tests__/api/request';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
import { INVITE_LIMIT } from '@/types';
const createRequest = _createRequest(request);
const inviteMemberRequest = _inviteMemberRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { Db, MongoClient } from 'mongodb';
import request from '@/__tests__/api/request';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
const createRequest = _createRequest(request);
const inviteMemberRequest = _inviteMemberRequest(request);
const verifyInviteRequest = _verifyInviteRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { Db, MongoClient } from 'mongodb';
import request from '@/__tests__/api/request';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
const createRequest = _createRequest(request);
const inviteMemberRequest = _inviteMemberRequest(request);
const verifyInviteRequest = _verifyInviteRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { Db, MongoClient } from 'mongodb';
import request from '@/__tests__/api/request';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
const abdicateRequest = _abdicateRequest(request);
const createRequest = _createRequest(request);
const inviteMemberRequest = _inviteMemberRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Session } from 'sealos-desktop-sdk/*';
import * as k8s from '@kubernetes/client-node';
import { Db, MongoClient } from 'mongodb';
import request from '@/__tests__/api/request';
import { _setAuth, cleanDb, cleanK8s } from '../tools';
import { _setAuth, cleanDb, cleanK8s } from '@/__tests__/api/tools';
const createRequest = _createRequest(request);
const inviteMemberRequest = _inviteMemberRequest(request);
const verifyInviteRequest = _verifyInviteRequest(request);
Expand Down
2 changes: 1 addition & 1 deletion frontend/desktop/src/__tests__/jest-sequencer.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class CustomSequencer extends TestSequencer {
}
}

module.exports = TestSequencer;
module.exports = CustomSequencer;
41 changes: 24 additions & 17 deletions frontend/desktop/src/pages/api/auth/namespace/abdicate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { authSession } from '@/services/backend/auth';
import { queryUsersByNamespace } from '@/services/backend/db/userToNamespace';
import { changeOwnerBinding, queryUsersByNamespace } from '@/services/backend/db/userToNamespace';
import { jsonRes } from '@/services/backend/response';
import { modifyBinding, modifyTeamRole, unbindingRole } from '@/services/backend/team';
import { InvitedStatus, NSType, UserRole } from '@/types/team';
Expand Down Expand Up @@ -33,22 +33,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
);
if (!targetUtn || targetUtn.status !== InvitedStatus.Accepted)
return jsonRes(res, { code: 404, message: 'the targetUser is not in namespace' });
// 升级为 owner
const bindResult = await modifyBinding({
k8s_username: targetUsername,
namespaceId: ns_uid,
role: UserRole.Owner,
userId: targetUserId
});
if (!bindResult) throw new Error('fail to binding role');
// 降级为 developer
const unbindResult = await modifyBinding({
k8s_username: payload.user.k8s_username,
role: UserRole.Developer,
userId: payload.user.uid,
namespaceId: ns_uid
});
if (!unbindResult) throw new Error('fail to unbinding role');

await modifyTeamRole({
action: 'Change',
Expand All @@ -58,6 +42,29 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
role: UserRole.Owner,
namespace: ownUtn.namespace
});
// 升级为 owner
// const bindResult = await modifyBinding({
// k8s_username: targetUsername,
// namespaceId: ns_uid,
// role: UserRole.Owner,
// userId: targetUserId
// });
// if (!bindResult) throw new Error('fail to binding role');
// // 降级为 developer
// const unbindResult = await modifyBinding({
// k8s_username: payload.user.k8s_username,
// role: UserRole.Developer,
// userId: payload.user.uid,
// namespaceId: ns_uid
// });
// if (!unbindResult) throw new Error('fail to unbinding role');
await changeOwnerBinding({
userId: payload.user.uid,
k8s_username: payload.user.k8s_username,
namespaceId: ns_uid,
tUserId: targetUserId,
tK8sUsername: targetUsername
});
jsonRes(res, {
code: 200,
message: 'Successfully'
Expand Down
20 changes: 6 additions & 14 deletions frontend/desktop/src/pages/api/auth/namespace/details.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import { authSession } from '@/services/backend/auth';
import { queryNS, queryNSByUid } from '@/services/backend/db/namespace';
import { queryUsersByNamespace } from '@/services/backend/db/userToNamespace';
import { jsonRes } from '@/services/backend/response';
import { checkInNS } from '@/services/backend/team';
import { NamespaceDto } from '@/types/team';
import { TeamUserDto, UserDto } from '@/types/user';
import { TeamUserDto } from '@/types/user';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const payload = await authSession(req.headers);
if (!payload) return jsonRes(res, { code: 401, message: 'token verify error' });
const { ns_uid } = req.body;
if (!ns_uid) return jsonRes(res, { code: 400, message: 'nsid is required' });
if (
!checkInNS({
userId: payload.user.uid,
k8s_username: payload.user.k8s_username,
namespaceId: payload.user.nsid
})
)
return jsonRes(res, { code: 403, message: 'not in namespace' });
const rawNamespace = await queryNSByUid({ uid: ns_uid });
if (!rawNamespace) return jsonRes(res, { code: 404, message: 'namespace not founded' });
const utnWithUser = await queryUsersByNamespace({ namespaceId: ns_uid });

if (utnWithUser.length <= 0)
return jsonRes(res, { code: 404, message: 'namespace not founded!' });
const rawNamespace = utnWithUser[0].namespace;
const namespace: NamespaceDto = {
uid: rawNamespace.uid,
id: rawNamespace.id,
createTime: rawNamespace.createTime,
teamName: rawNamespace.teamName,
nstype: rawNamespace.nstype
};
const utnWithUser = await queryUsersByNamespace({ namespaceId: ns_uid });
const users = utnWithUser.map<TeamUserDto>((x) => ({
uid: x.userId,
k8s_username: x.k8s_username,
Expand Down
11 changes: 5 additions & 6 deletions frontend/desktop/src/pages/api/auth/namespace/modifyRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// 权限一致,不用管
if (tUtn.role === tRole) return jsonRes(res, { code: 200, message: 'Successfully' });

const updateResult = await modifyBinding({
...tUtn,
role: tRole
});
if (!updateResult) throw new Error('modify utn error');
await modifyTeamRole({
k8s_username: tK8s_username,
role: tRole,
Expand All @@ -57,7 +52,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
userId: tUserId,
pre_role: tUtn.role
});

const updateResult = await modifyBinding({
...tUtn,
role: tRole
});
if (!updateResult) throw new Error('modify utn error');
jsonRes(res, {
code: 200,
message: 'Successfully'
Expand Down
14 changes: 7 additions & 7 deletions frontend/desktop/src/pages/api/auth/namespace/removeUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
} else if (InvitedStatus.Accepted === utn.status) {
// 删除权限
unbinding_result = await unbindingRole({
k8s_username: tK8s_username,
userId: tUserId,
ns_uid: namespace.uid
});
if (!unbinding_result)
return jsonRes(res, { code: 500, message: 'fail to remove team memeber role' });

await modifyTeamRole({
k8s_username: tK8s_username,
role,
Expand All @@ -71,7 +65,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
userId: tUserId,
pre_role: utn.role
});
unbinding_result = await unbindingRole({
k8s_username: tK8s_username,
userId: tUserId,
ns_uid: namespace.uid
});
}
if (!unbinding_result) throw new Error('fail to remove team memeber role');
jsonRes(res, {
code: 200,
message: 'Successfully',
Expand Down
2 changes: 1 addition & 1 deletion frontend/desktop/src/pages/api/auth/namespace/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { queryUsersByNamespace } from '@/services/backend/db/userToNamespace';
import { switchNamespace } from '@/services/backend/kubernetes/user';
import { jsonRes } from '@/services/backend/response';
import * as jsYaml from 'js-yaml';
import { Session } from '@/types';
import { Session } from 'sealos-desktop-sdk';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
Expand Down
13 changes: 6 additions & 7 deletions frontend/desktop/src/pages/api/auth/namespace/verifyInvite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const utn = await queryUTN({ userId: uid, k8s_username, namespaceId: ns_uid });
if (!utn) return jsonRes(res, { code: 404, message: "you're not invited" });
if (action === reciveAction.Accepte) {
const result = await acceptInvite({
k8s_username,
ns_uid,
userId: uid
});
if (!result) throw new Error('failed to change Status');
// 接受以后再调用这个modifyTeamRole
await modifyTeamRole({
k8s_username,
role: utn.role,
userId: uid,
namespace,
action: 'Grant'
});
const result = await acceptInvite({
k8s_username,
ns_uid,
userId: uid
});
if (!result) throw new Error('failed to change Status');
} else if (action === reciveAction.Reject) {
const unbindingResult = await unbindingRole({ k8s_username, ns_uid, userId: uid });
if (!unbindingResult) throw new Error('fail to unbinding');
Expand Down
Loading

0 comments on commit 78bd02e

Please sign in to comment.