Skip to content

Commit

Permalink
fix: launchpad ingress (#5320)
Browse files Browse the repository at this point in the history
* fix ingress

* update
  • Loading branch information
zjy365 authored Jan 3, 2025
1 parent 226ddef commit 684a4bd
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
56 changes: 54 additions & 2 deletions frontend/providers/applaunchpad/src/pages/api/pauseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { ApiResp } from '@/services/kubernet';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { pauseKey } from '@/constants/app';
import { appDeployKey, pauseKey } from '@/constants/app';
import { PatchUtils } from '@kubernetes/client-node';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
try {
const { appName } = req.query as { appName: string };
if (!appName) {
throw new Error('appName is empty');
}
const { apiClient, k8sAutoscaling, getDeployApp, namespace } = await getK8s({
const { apiClient, k8sAutoscaling, getDeployApp, namespace, k8sNetworkingApp } = await getK8s({
kubeconfig: await authSession(req.headers)
});

Expand Down Expand Up @@ -47,6 +48,57 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
}
}

// handle ingress
try {
const { body: ingress } = await k8sNetworkingApp.listNamespacedIngress(
namespace,
undefined,
undefined,
undefined,
undefined,
`${appDeployKey}=${appName}`
);
if (ingress?.items?.length > 0) {
for (const ingressItem of ingress.items) {
if (ingressItem?.metadata?.name) {
const patchData: Record<string, any> = {};
if (ingressItem.metadata?.annotations?.['kubernetes.io/ingress.class'] === 'nginx') {
patchData.metadata = {
annotations: {
'kubernetes.io/ingress.class': 'pause'
}
};
}
if (ingressItem.spec?.ingressClassName === 'nginx') {
patchData.spec = {
ingressClassName: 'pause'
};
}

if (Object.keys(patchData).length > 0) {
requestQueue.push(
k8sNetworkingApp.patchNamespacedIngress(
ingressItem.metadata.name,
namespace,
patchData,
undefined,
undefined,
undefined,
undefined,
undefined,
{ headers: { 'Content-type': PatchUtils.PATCH_FORMAT_JSON_MERGE_PATCH } }
)
);
}
}
}
}
} catch (error: any) {
if (error?.statusCode !== 404) {
return Promise.reject('无法读取到ingress');
}
}

// replace source file
app.metadata.annotations[pauseKey] = JSON.stringify(restartAnnotations);
app.spec.replicas = 0;
Expand Down
56 changes: 54 additions & 2 deletions frontend/providers/applaunchpad/src/pages/api/startApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { ApiResp } from '@/services/kubernet';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { pauseKey, minReplicasKey, maxReplicasKey } from '@/constants/app';
import { pauseKey, minReplicasKey, maxReplicasKey, appDeployKey } from '@/constants/app';
import { json2HPA } from '@/utils/deployYaml2Json';
import { AppEditType } from '@/types/app';
import { PatchUtils } from '@kubernetes/client-node';

/* start app. */
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
Expand All @@ -14,7 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
if (!appName) {
throw new Error('appName is empty');
}
const { apiClient, getDeployApp, applyYamlList } = await getK8s({
const { apiClient, getDeployApp, applyYamlList, namespace, k8sNetworkingApp } = await getK8s({
kubeconfig: await authSession(req.headers)
});

Expand Down Expand Up @@ -60,6 +61,57 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
requestQueue.push(applyYamlList([hpaYaml], 'create'));
}

// handle ingress
try {
const { body: ingress } = await k8sNetworkingApp.listNamespacedIngress(
namespace,
undefined,
undefined,
undefined,
undefined,
`${appDeployKey}=${appName}`
);
if (ingress?.items?.length > 0) {
for (const ingressItem of ingress.items) {
if (ingressItem?.metadata?.name) {
const patchData: Record<string, any> = {};
if (ingressItem.metadata?.annotations?.['kubernetes.io/ingress.class'] === 'pause') {
patchData.metadata = {
annotations: {
'kubernetes.io/ingress.class': 'nginx'
}
};
}
if (ingressItem.spec?.ingressClassName === 'pause') {
patchData.spec = {
ingressClassName: 'nginx'
};
}

if (Object.keys(patchData).length > 0) {
requestQueue.push(
k8sNetworkingApp.patchNamespacedIngress(
ingressItem.metadata.name,
namespace,
patchData,
undefined,
undefined,
undefined,
undefined,
undefined,
{ headers: { 'Content-type': PatchUtils.PATCH_FORMAT_JSON_MERGE_PATCH } }
)
);
}
}
}
}
} catch (error: any) {
if (error?.statusCode !== 404) {
return Promise.reject('无法读取到ingress');
}
}

await Promise.all(requestQueue);

jsonRes(res);
Expand Down

0 comments on commit 684a4bd

Please sign in to comment.