Skip to content

Commit

Permalink
feat(server): impl bundle & runtime list api (#453)
Browse files Browse the repository at this point in the history
Signed-off-by: maslow <wangfugen@126.com>

Signed-off-by: maslow <wangfugen@126.com>
  • Loading branch information
maslow authored Nov 30, 2022
1 parent a3aebfd commit 4dae4da
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 46 deletions.
139 changes: 136 additions & 3 deletions server/package-lock.json

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

3 changes: 3 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
"casdoor-nodejs-sdk": "^1.3.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"compression": "^1.7.4",
"dotenv": "^16.0.3",
"fast-json-patch": "^3.1.1",
"helmet": "^6.0.0",
"mongodb": "^4.12.1",
"nanoid": "^3.3.4",
"passport": "^0.6.0",
Expand All @@ -54,6 +56,7 @@
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.2.0",
"@types/compression": "^1.7.2",
"@types/express": "^4.17.13",
"@types/jest": "28.1.8",
"@types/node": "^16.0.0",
Expand Down
5 changes: 5 additions & 0 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HttpModule } from '@nestjs/axios'
import { CoreModule } from './core/core.module'
import { ApplicationsModule } from './applications/applications.module'
import { AuthModule } from './auth/auth.module'
import { ThrottlerModule } from '@nestjs/throttler'

@Module({
imports: [
Expand All @@ -22,6 +23,10 @@ import { AuthModule } from './auth/auth.module'
AuthModule,
CoreModule,
ApplicationsModule,
ThrottlerModule.forRoot({
ttl: 60,
limit: 10,
}),
],
controllers: [AppController],
providers: [AppService],
Expand Down
7 changes: 5 additions & 2 deletions server/src/applications/applications.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Module } from '@nestjs/common'
import { ApplicationsService } from './applications.service'
import { ApplicationsController } from './applications.controller'
import { BundlesService } from './bundles.service'
import { RuntimesService } from './runtimes.service'
import { SpecsController } from './specs.controller'

@Module({
controllers: [ApplicationsController],
providers: [ApplicationsService],
controllers: [ApplicationsController, SpecsController],
providers: [ApplicationsService, BundlesService, RuntimesService],
exports: [ApplicationsService],
})
export class ApplicationsModule {}
18 changes: 18 additions & 0 deletions server/src/applications/bundles.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { BundlesService } from './bundles.service';

describe('BundlesService', () => {
let service: BundlesService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [BundlesService],
}).compile();

service = module.get<BundlesService>(BundlesService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
23 changes: 23 additions & 0 deletions server/src/applications/bundles.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable, Logger } from '@nestjs/common'
import { GetSystemNamespace } from 'src/common/getter'
import { KubernetesService } from 'src/core/kubernetes.service'
import { Bundle, BundleList } from './entities/bundle.entity'

@Injectable()
export class BundlesService {
private readonly logger = new Logger(BundlesService.name)
constructor(private readonly k8sClient: KubernetesService) {}

async findAll() {
const namespace = GetSystemNamespace()
const api = this.k8sClient.customObjectApi
const res = await api.listNamespacedCustomObject(
Bundle.GVK.group,
Bundle.GVK.version,
namespace,
Bundle.GVK.plural,
)

return BundleList.fromObject(res.body as any)
}
}
48 changes: 28 additions & 20 deletions server/src/applications/entities/bundle.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { KubernetesObject } from '@kubernetes/client-node'
import { KubernetesListObject, KubernetesObject } from '@kubernetes/client-node'
import { ApiProperty } from '@nestjs/swagger'
import { ObjectMeta } from '../../core/kubernetes.interface'
import { GroupVersionKind, ObjectMeta } from '../../core/kubernetes.interface'

export class BundleSpec {
@ApiProperty()
Expand Down Expand Up @@ -39,36 +39,37 @@ export class BundleSpec {

export class Bundle implements KubernetesObject {
@ApiProperty()
get apiVersion(): string {
return Bundle.GroupVersion
}
apiVersion: string

@ApiProperty()
get kind(): string {
return Bundle.Kind
}
kind: string

@ApiProperty()
metadata: ObjectMeta

@ApiProperty()
spec: BundleSpec

static readonly Group = 'application.laf.dev'
static readonly Version = 'v1'
static readonly PluralName = 'bundles'
static readonly Kind = 'Bundle'
static get GroupVersion() {
return `${this.Group}/${this.Version}`
}

static readonly GVK = new GroupVersionKind(
'application.laf.dev',
'v1',
'Bundle',
'bundles',
)
constructor(name: string, namespace: string) {
this.metadata = {
name,
namespace,
}
this.apiVersion = Bundle.GVK.apiVersion
this.kind = Bundle.GVK.kind
this.metadata = new ObjectMeta(name, namespace)
this.metadata.labels = {}
this.spec = new BundleSpec()
}

static fromObject(obj: KubernetesObject) {
const bundle = new Bundle(obj.metadata?.name, obj.metadata.namespace)
delete obj.metadata
Object.assign(bundle, obj)
return bundle
}
}

export class BundleList {
Expand All @@ -79,4 +80,11 @@ export class BundleList {
type: [Bundle],
})
items: Bundle[]

static fromObject(obj: KubernetesListObject<Bundle>) {
const list = new BundleList()
Object.assign(list, obj)
list.items = obj.items.map((item) => Bundle.fromObject(item))
return list
}
}
Loading

0 comments on commit 4dae4da

Please sign in to comment.