Skip to content

Commit

Permalink
fix(template): laf template
Browse files Browse the repository at this point in the history
  • Loading branch information
xudaotutou committed Nov 27, 2023
1 parent 7a8fffa commit 3309222
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 21 deletions.
68 changes: 64 additions & 4 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions frontend/providers/template/delRoleBinding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { ApiResp } from '@/services/kubernet';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { instanceName } = req.query as { instanceName: string };
if (!instanceName) {
throw new Error('deploy name is empty');
}

const { namespace, k8sAuth } = await getK8s({
kubeconfig: await authSession(req.headers)
});

const result = await k8sAuth.deleteClusterRoleBinding(instanceName, namespace);

jsonRes(res, { data: result });
} catch (err: any) {
jsonRes(res, {
code: 500,
error: err
});
}
}
2 changes: 2 additions & 0 deletions frontend/providers/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"next-i18next": "^13.3.0",
"nprogress": "^0.2.0",
"octokit": "^3.1.1",
"pluralize": "^8.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.46.2",
Expand All @@ -62,6 +63,7 @@
"@types/lodash": "^4.14.199",
"@types/node": "18.13.0",
"@types/nprogress": "^0.2.1",
"@types/pluralize": "^0.0.33",
"@types/react": "18.2.37",
"@types/react-dom": "18.0.10",
"@types/react-syntax-highlighter": "^15.5.7",
Expand Down
20 changes: 16 additions & 4 deletions frontend/providers/template/src/api/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export const delRoleBindingByName = (instanceName: string) =>

export const delServiceAccountByName = (instanceName: string) =>
DELETE('/api/resource/delServiceAccount', { instanceName });

const deleteResourceByKind: Record<ResourceKindType, (instanceName: string) => void> = {
export const delPersistentVolumeClaim = (instanceName: string) =>
DELETE('/api/resource/delPersistentVolumeClaim', { instanceName });
export const delCR = (data: Record<'kind' | 'name' | 'apiVersion', string>) =>
DELETE('/api/resource/delCR', data);
const deleteResourceByKind: Record<string, undefined | ((instanceName: string) => void)> = {
CronJob: (instanceName: string) => delCronJobByName(instanceName),
App: (instanceName: string) => deleteAppCRD(instanceName),
Secret: (instanceName: string) => deleteSecret(instanceName),
Expand All @@ -49,12 +52,21 @@ const deleteResourceByKind: Record<ResourceKindType, (instanceName: string) => v
Issuer: (instanceName: string) => delIssuerByName(instanceName),
Role: (instanceName: string) => delRoleByName(instanceName),
RoleBinding: (instanceName: string) => delRoleBindingByName(instanceName),
ServiceAccount: (instanceName: string) => delServiceAccountByName(instanceName)
ServiceAccount: (instanceName: string) => delServiceAccountByName(instanceName),
PersistentVolumeClaim: delPersistentVolumeClaim
};

export const deleteAllResources = async (resources: BaseResourceType[]) => {
const deletePromises = resources.map((resource) => {
return deleteResourceByKind[resource.kind](resource.name);
const fn = deleteResourceByKind[resource.kind];
console.log(fn);
if (!!fn) return fn(resource.name);
else
delCR({
name: resource.name,
apiVersion: resource.apiVersion!,
kind: resource.kind!
});
});
const reuslt = await Promise.allSettled(deletePromises);
console.log(reuslt);
Expand Down
86 changes: 83 additions & 3 deletions frontend/providers/template/src/pages/api/app/listOtherByName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
undefined,
labelSelector
);

// issuer
const certIssuerPromise = k8sCustomObjects.listNamespacedCustomObject(
'cert-manager.io',
Expand Down Expand Up @@ -112,6 +111,83 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
undefined,
`${labelSelector},!${dbProviderKey},!${deployManagerKey}`
);
// promi
const prometheusPromiseRule = k8sCustomObjects.listNamespacedCustomObject(
'monitoring.coreos.com',
'v1',
namespace,
'prometheusrules',
undefined,
undefined,
undefined,
undefined,
labelSelector
) as Promise<{
response: IncomingMessage;
body: {
items: { kind?: string }[];
kind: 'PromehteusRuleList';
};
}>;
const prometheusPromise = k8sCustomObjects.listNamespacedCustomObject(
'monitoring.coreos.com',
'v1',
namespace,
'prometheuses',
undefined,
undefined,
undefined,
undefined,
labelSelector
) as Promise<{
response: IncomingMessage;
body: {
items: { kind?: string }[];
kind: 'PromehteusList';
};
}>;
const prometheusPvcPromise = k8sCore.listNamespacedPersistentVolumeClaim(
namespace,
undefined,
undefined,
undefined,
undefined,
`app.kubernetes.io/name=prometheus,prometheus=${instanceName}`
);
const servicemonitorPromise = k8sCustomObjects.listNamespacedCustomObject(
'monitoring.coreos.com',
'v1',
namespace,
'servicemonitors',
undefined,
undefined,
undefined,
undefined,
labelSelector
) as Promise<{
response: IncomingMessage;
body: {
items: { kind?: string }[];
kind: 'ServiceMonitorList';
};
}>;
const probePromise = k8sCustomObjects.listNamespacedCustomObject(
'monitoring.coreos.com',
'v1',
namespace,
'probes',
undefined,
undefined,
undefined,
undefined,
labelSelector
) as Promise<{
response: IncomingMessage;
body: {
items: { kind?: string }[];
kind: 'ProbeList';
};
}>;
// 使用 Promise.allSettled 获取所有结果 [secretResult, jobResult, customResourceResult]
const result = await Promise.allSettled([
secretPromise,
Expand All @@ -121,9 +197,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
rolePromise,
roleBindingPromise,
saPromise,
configMapPromise
configMapPromise,
prometheusPromise,
prometheusPromiseRule,
prometheusPvcPromise,
servicemonitorPromise,
probePromise
]);

const data = result
.map((res) => {
if (res.status === 'fulfilled') {
Expand Down
37 changes: 37 additions & 0 deletions frontend/providers/template/src/pages/api/resource/delCR.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { ApiResp } from '@/services/kubernet';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import pluralize from 'pluralize';
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { name, apiVersion, kind } = req.query as Record<
'name' | 'apiVersion' | 'kind',
undefined | null | string
>;
if (!name || !apiVersion || !kind) {
return jsonRes(res, { message: 'bad request', code: 400 });
}
const [group, version] = apiVersion.split('/');
if (!version || !group) return jsonRes(res, { message: 'bad request', code: 400 });
const client = await getK8s({
kubeconfig: await authSession(req.headers)
});
const plural = pluralize.plural(kind.toLocaleLowerCase());
const result = await client.k8sCustomObjects.deleteNamespacedCustomObject(
group,
version,
client.namespace,
plural,
name
);

jsonRes(res, { data: result });
} catch (err: any) {
jsonRes(res, {
code: 500,
error: err
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { ApiResp } from '@/services/kubernet';
import { authSession } from '@/services/backend/auth';
import { K8sApi, getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { instanceName } = req.query as { instanceName: string };
if (!instanceName) {
throw new Error('pvc name is empty');
}

const { namespace, k8sCore } = await getK8s({
kubeconfig: await authSession(req.headers)
});

// 删除 pvc
const result = await k8sCore.deleteNamespacedPersistentVolumeClaim(instanceName, namespace);
jsonRes(res, { data: result });
} catch (err: any) {
jsonRes(res, {
code: 500,
error: err
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
kubeconfig: await authSession(req.headers)
});

const result = await k8sAuth.deleteClusterRoleBinding(instanceName, namespace);
const result = await k8sAuth.deleteNamespacedRoleBinding(instanceName, namespace);

jsonRes(res, { data: result });
} catch (err: any) {
Expand Down
Loading

0 comments on commit 3309222

Please sign in to comment.