-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support devbox token to fetch details API
- Loading branch information
Showing
8 changed files
with
345 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
frontend/providers/devbox/app/api/v1/getDevboxDetail/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { NextRequest } from 'next/server' | ||
|
||
import { devboxKey } from '@/constants/devbox' | ||
import { getPayloadWithoutVerification, verifyToken } from '@/services/backend/auth' | ||
import { getK8s } from '@/services/backend/kubernetes' | ||
import { jsonRes } from '@/services/backend/response' | ||
import { KBDevboxType } from '@/types/k8s' | ||
import { adaptDBListItem, adaptDevboxListItem, adaptIngressListItem } from '@/utils/adapt' | ||
import { KbPgClusterType } from '@/types/cluster' | ||
import { V1Ingress } from '@kubernetes/client-node' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const { payload, token } = getPayloadWithoutVerification(req.headers) | ||
if (!payload || !token) { | ||
return jsonRes({ | ||
code: 401, | ||
error: 'Unauthorized' | ||
}) | ||
} | ||
const devboxName = payload.devboxName | ||
const namespace = payload.namespace | ||
|
||
const { k8sCore, k8sCustomObjects, k8sNetworkingApp } = await getK8s({ | ||
useDefaultConfig: true | ||
}) | ||
|
||
const response = await k8sCore.readNamespacedSecret(devboxName, namespace) | ||
|
||
const jwtSecret = Buffer.from( | ||
response.body.data?.['SEALOS_DEVBOX_JWT_SECRET'] as string, | ||
'base64' | ||
).toString('utf-8') | ||
|
||
if (!verifyToken(token, jwtSecret)) { | ||
return jsonRes({ | ||
code: 401, | ||
error: 'Unauthorized' | ||
}) | ||
} | ||
|
||
const results = await Promise.allSettled([ | ||
k8sCustomObjects.getNamespacedCustomObject( | ||
'devbox.sealos.io', | ||
'v1alpha1', | ||
namespace, | ||
'devboxes', | ||
devboxName | ||
), | ||
k8sCustomObjects.listNamespacedCustomObject( | ||
'apps.kubeblocks.io', | ||
'v1alpha1', | ||
namespace, | ||
'clusters' | ||
), | ||
k8sNetworkingApp.listNamespacedIngress( | ||
namespace, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
`${devboxKey}=${devboxName}` | ||
) | ||
]) | ||
|
||
const [devboxResult, clustersResult, ingressesResult] = results | ||
|
||
let devbox, clusters, ingresses | ||
|
||
if (devboxResult.status === 'fulfilled') { | ||
devbox = adaptDevboxListItem(devboxResult.value.body as KBDevboxType) | ||
} | ||
|
||
if (clustersResult.status === 'fulfilled') { | ||
clusters = (clustersResult.value.body as { items: KbPgClusterType[] }).items.map( | ||
adaptDBListItem | ||
) | ||
} | ||
|
||
if (ingressesResult.status === 'fulfilled') { | ||
ingresses = ingressesResult.value.body.items.map(adaptIngressListItem) | ||
} | ||
|
||
return jsonRes({ | ||
data: { | ||
devbox, | ||
clusters, | ||
ingresses | ||
} | ||
}) | ||
} catch (err: any) { | ||
return jsonRes({ | ||
code: 500, | ||
error: err?.body || err | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
export enum DBTypeEnum { | ||
postgresql = 'postgresql', | ||
mongodb = 'mongodb', | ||
mysql = 'apecloud-mysql', | ||
redis = 'redis', | ||
kafka = 'kafka', | ||
qdrant = 'qdrant', | ||
nebula = 'nebula', | ||
weaviate = 'weaviate', | ||
milvus = 'milvus' | ||
} | ||
|
||
export enum DBStatusEnum { | ||
Creating = 'Creating', | ||
Starting = 'Starting', | ||
Stopping = 'Stopping', | ||
Stopped = 'Stopped', | ||
Running = 'Running', | ||
Updating = 'Updating', | ||
SpecUpdating = 'SpecUpdating', | ||
Rebooting = 'Rebooting', | ||
Upgrade = 'Upgrade', | ||
VerticalScaling = 'VerticalScaling', | ||
VolumeExpanding = 'VolumeExpanding', | ||
Failed = 'Failed', | ||
UnKnow = 'UnKnow', | ||
Deleting = 'Deleting' | ||
} | ||
|
||
export type KbPgClusterType = { | ||
apiVersion: 'apps.kubeblocks.io/v1alpha1' | ||
kind: 'Cluster' | ||
metadata: { | ||
annotations: Record<string, string> | ||
creationTimestamp: Date | ||
labels: { | ||
'clusterdefinition.kubeblocks.io/name': `${DBTypeEnum}` | ||
'clusterversion.kubeblocks.io/name': string | ||
'sealos-db-provider/postgresql': string | ||
[key: string]: string | ||
} | ||
name: string | ||
namespace: string | ||
uid: string | ||
} | ||
spec: KubeBlockClusterSpec | ||
status?: KubeBlockClusterStatus | ||
} | ||
|
||
export interface KubeBlockClusterSpec { | ||
clusterDefinitionRef: `${DBTypeEnum}` | ||
clusterVersionRef: string | ||
terminationPolicy: string | ||
componentSpecs: { | ||
componentDefRef: `${DBTypeEnum}` | ||
name: `${DBTypeEnum}` | ||
replicas: number | ||
resources: { | ||
limits: { | ||
cpu: string | ||
memory: string | ||
} | ||
requests: { | ||
cpu: string | ||
memory: string | ||
} | ||
} | ||
volumeClaimTemplates: { | ||
name: 'data' | ||
spec: { | ||
accessModes: ['ReadWriteOnce'] | ||
resources: { | ||
requests: { | ||
storage: string | ||
} | ||
} | ||
} | ||
}[] | ||
}[] | ||
backup: { | ||
enabled: boolean | ||
cronExpression: string | ||
method: string | ||
pitrEnabled: boolean | ||
repoName: string | ||
retentionPeriod: string | ||
} | ||
} | ||
export interface KubeBlockClusterStatus { | ||
clusterDefGeneration: number | ||
components: object | ||
conditions: k8s.V1Condition[] | ||
observedGeneration: number | ||
phase: `${DBStatusEnum}` | ||
} | ||
|
||
export interface DBListItemType { | ||
id: string | ||
name: string | ||
dbType: string | ||
createTime: string | ||
cpu: number | ||
memory: number | ||
storage: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export interface IngressListItemType { | ||
metadata: { | ||
name: string | ||
namespace: string | ||
creationTimestamp?: Date | ||
labels?: { [key: string]: string } | ||
} | ||
spec: { | ||
rules: { | ||
host: string | ||
http: { | ||
paths: { | ||
path: string | ||
pathType: string | ||
backend: { | ||
service: { | ||
name: string | ||
port: number | ||
} | ||
} | ||
}[] | ||
} | ||
}[] | ||
tls?: { | ||
hosts: string[] | ||
secretName: string | ||
}[] | ||
} | ||
} |
Oops, something went wrong.