Skip to content

Commit

Permalink
feat: start and pause will modify ingress (#5321)
Browse files Browse the repository at this point in the history
* feat: start and pause will modify ingress

* chore: ingress compatibility

* fix: code back restartDevbox

* chore: adjust api
  • Loading branch information
mlhiter authored Jan 3, 2025
1 parent 684a4bd commit 5dcd1f9
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frontend/providers/devbox/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ NODE_TLS_REJECT_UNAUTHORIZED=
ROOT_RUNTIME_NAMESPACE=
DATABASE_URL=
RETAG_SVC_URL=
PRIVACY_URL=
PRIVACY_URL=
58 changes: 57 additions & 1 deletion frontend/providers/devbox/app/api/pauseDevbox/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NextRequest } from 'next/server'
import { jsonRes } from '@/services/backend/response'
import { authSession } from '@/services/backend/auth'
import { getK8s } from '@/services/backend/kubernetes'
import { devboxKey } from '@/constants/devbox'

export const dynamic = 'force-dynamic'

Expand All @@ -12,10 +13,65 @@ export async function POST(req: NextRequest) {

const headerList = req.headers

const { k8sCustomObjects, namespace } = await getK8s({
const { k8sCustomObjects, namespace, k8sNetworkingApp } = await getK8s({
kubeconfig: await authSession(headerList)
})

// get ingress and modify ingress annotations
const ingressesResponse = await k8sNetworkingApp.listNamespacedIngress(
namespace,
undefined,
undefined,
undefined,
undefined,
`${devboxKey}=${devboxName}`
)
const ingresses: any = (ingressesResponse.body as { items: any[] }).items

ingresses.forEach(async (ingress: any) => {
const annotationsIngressClass = ingress.metadata?.annotations?.['kubernetes.io/ingress.class']
const specIngressClass = ingress.spec?.ingressClassName

if (
(annotationsIngressClass && annotationsIngressClass === 'nginx') ||
(specIngressClass && specIngressClass === 'nginx')
) {
if (annotationsIngressClass) {
await k8sNetworkingApp.patchNamespacedIngress(
ingress.metadata.name,
namespace,
{ metadata: { annotations: { 'kubernetes.io/ingress.class': 'pause' } } },
undefined,
undefined,
undefined,
undefined,
undefined,
{
headers: {
'Content-Type': 'application/merge-patch+json'
}
}
)
} else if (specIngressClass) {
await k8sNetworkingApp.patchNamespacedIngress(
ingress.metadata.name,
namespace,
{ spec: { ingressClassName: 'pause' } },
undefined,
undefined,
undefined,
undefined,
undefined,
{
headers: {
'Content-Type': 'application/merge-patch+json'
}
}
)
}
}
})

await k8sCustomObjects.patchNamespacedCustomObject(
'devbox.sealos.io',
'v1alpha1',
Expand Down
1 change: 1 addition & 0 deletions frontend/providers/devbox/app/api/restartDevbox/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function POST(req: NextRequest) {
}
}
)

// 2.get devbox pod and ensure the devbox pod is deleted,when the devbox pod is deleted,the devbox will be restarted
let pods
const maxRetries = 10
Expand Down
58 changes: 57 additions & 1 deletion frontend/providers/devbox/app/api/startDevbox/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NextRequest } from 'next/server'
import { authSession } from '@/services/backend/auth'
import { getK8s } from '@/services/backend/kubernetes'
import { jsonRes } from '@/services/backend/response'
import { devboxKey } from '@/constants/devbox'

export const dynamic = 'force-dynamic'

Expand All @@ -12,10 +13,65 @@ export async function POST(req: NextRequest) {

const headerList = req.headers

const { k8sCustomObjects, namespace } = await getK8s({
const { k8sCustomObjects, namespace, k8sNetworkingApp } = await getK8s({
kubeconfig: await authSession(headerList)
})

// get ingress and modify ingress annotations
const ingressesResponse = await k8sNetworkingApp.listNamespacedIngress(
namespace,
undefined,
undefined,
undefined,
undefined,
`${devboxKey}=${devboxName}`
)
const ingresses: any = (ingressesResponse.body as { items: any[] }).items

ingresses.forEach(async (ingress: any) => {
const annotationsIngressClass = ingress.metadata?.annotations?.['kubernetes.io/ingress.class']
const specIngressClass = ingress.spec?.ingressClassName

if (
(annotationsIngressClass && annotationsIngressClass === 'pause') ||
(specIngressClass && specIngressClass === 'pause')
) {
if (annotationsIngressClass) {
await k8sNetworkingApp.patchNamespacedIngress(
ingress.metadata.name,
namespace,
{ metadata: { annotations: { 'kubernetes.io/ingress.class': 'nginx' } } },
undefined,
undefined,
undefined,
undefined,
undefined,
{
headers: {
'Content-Type': 'application/merge-patch+json'
}
}
)
} else if (specIngressClass) {
await k8sNetworkingApp.patchNamespacedIngress(
ingress.metadata.name,
namespace,
{ spec: { ingressClassName: 'nginx' } },
undefined,
undefined,
undefined,
undefined,
undefined,
{
headers: {
'Content-Type': 'application/merge-patch+json'
}
}
)
}
}
})

await k8sCustomObjects.patchNamespacedCustomObject(
'devbox.sealos.io',
'v1alpha1',
Expand Down

0 comments on commit 5dcd1f9

Please sign in to comment.