Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start and pause will modify ingress #5321

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading