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(gateway): refactor gateway in server instead of crd #649

Merged
merged 1 commit into from
Jan 18, 2023
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
8 changes: 1 addition & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
"alipay",
"apiextensions",
"appid",
"appv",
"automount",
"bitnami",
"bodyparser",
"bson",
"buildah",
Expand All @@ -46,17 +44,13 @@
"finalizers",
"fullname",
"ghaction",
"gonanoid",
"healthz",
"hokify",
"hostpath",
"Kube",
"kubebuilder",
"kubeconfig",
"Kubefile",
"Kubernetes",
"Kustomization",
"kustomize",
"labring",
"lafjs",
"lafyun",
Expand All @@ -65,7 +59,6 @@
"MINIO",
"moby",
"MONOG",
"mycrd",
"nestjs",
"objs",
"openebs",
Expand All @@ -75,6 +68,7 @@
"readwrite",
"Referer",
"rolebinding",
"roundrobin",
"runc",
"runtimes",
"runtimev",
Expand Down
35 changes: 29 additions & 6 deletions server/package-lock.json

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

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@nestjs/terminus": "^9.1.2",
"@nestjs/throttler": "^3.1.0",
"@prisma/client": "^4.7.1",
"axios": "^1.2.3",
"bson": "^4.7.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
Expand Down
40 changes: 36 additions & 4 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ model Application {
configuration ApplicationConfiguration?
storageUser StorageUser?
database Database?
domain ApplicationDomain?
}

type EnvironmentVariable {
Expand Down Expand Up @@ -215,13 +216,14 @@ enum BucketPolicy {
}

model StorageBucket {
id String @id @default(auto()) @map("_id") @db.ObjectId
id String @id @default(auto()) @map("_id") @db.ObjectId
appid String
name String @unique
name String @unique
shortName String
policy BucketPolicy
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
domain BucketDomain?
}

// database schemas
Expand Down Expand Up @@ -314,3 +316,33 @@ model CronTrigger {

cloudFunction CloudFunction @relation(fields: [appid, target], references: [appid, name])
}

// gateway schemas

enum DomainState {
Active
Inactive
}

model ApplicationDomain {
id String @id @default(auto()) @map("_id") @db.ObjectId
appid String @unique
domain String @unique
state DomainState @default(Active)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

application Application @relation(fields: [appid], references: [appid])
}

model BucketDomain {
id String @id @default(auto()) @map("_id") @db.ObjectId
appid String
bucketName String @unique
domain String @unique
state DomainState @default(Active)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

bucket StorageBucket @relation(fields: [bucketName], references: [name])
}
4 changes: 2 additions & 2 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AppService } from './app.service'
import { WebsitesModule } from './websites/websites.module'
import { FunctionModule } from './function/function.module'
import { HttpModule } from '@nestjs/axios'
import { CoreModule } from './core/core.module'
import { ApplicationModule } from './application/application.module'
import { AuthModule } from './auth/auth.module'
import { ThrottlerModule } from '@nestjs/throttler'
Expand All @@ -18,6 +17,7 @@ import { LogModule } from './log/log.module'
import { DependencyModule } from './dependency/dependency.module'
import { TriggerModule } from './trigger/trigger.module'
import { RegionModule } from './region/region.module'
import { GatewayModule } from './gateway/gateway.module'

@Module({
imports: [
Expand All @@ -30,7 +30,6 @@ import { RegionModule } from './region/region.module'
WebsitesModule,
HttpModule,
AuthModule,
CoreModule,
ApplicationModule,
InitializerModule,
InstanceModule,
Expand All @@ -40,6 +39,7 @@ import { RegionModule } from './region/region.module'
DependencyModule,
TriggerModule,
RegionModule,
GatewayModule,
],
controllers: [AppController],
providers: [AppService, PrismaService],
Expand Down
20 changes: 13 additions & 7 deletions server/src/application/application-task.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Injectable, Logger } from '@nestjs/common'
import { Cron, CronExpression } from '@nestjs/schedule'
import { Application, ApplicationPhase } from '@prisma/client'
import { isConditionTrue } from '../utils/getter'
import { GatewayCoreService } from '../core/gateway.cr.service'
import { PrismaService } from '../prisma.service'
import * as assert from 'node:assert'
import { PrismaService } from '../prisma.service'
import { StorageService } from '../storage/storage.service'
import { DatabaseService } from '../database/database.service'
import { ClusterService } from 'src/region/cluster/cluster.service'
import { RegionService } from 'src/region/region.service'
import { GatewayService } from 'src/gateway/gateway.service'

@Injectable()
export class ApplicationTaskService {
Expand All @@ -17,9 +16,9 @@ export class ApplicationTaskService {
constructor(
private readonly regionService: RegionService,
private readonly clusterService: ClusterService,
private readonly gatewayCore: GatewayCoreService,
private readonly storageService: StorageService,
private readonly databaseService: DatabaseService,
private readonly gatewayService: GatewayService,
private readonly prisma: PrismaService,
) {}

Expand Down Expand Up @@ -108,13 +107,13 @@ export class ApplicationTaskService {
}

// reconcile gateway
let gateway = await this.gatewayCore.findOne(appid)
let gateway = await this.gatewayService.findOne(appid)
if (!gateway) {
this.logger.debug(`Creating gateway for application ${appid}`)
gateway = await this.gatewayCore.create(app.appid)
gateway = await this.gatewayService.create(appid)
}

if (!isConditionTrue('Ready', gateway?.status?.conditions)) return
if (!gateway) return
if (!storage) return
if (!database) return

Expand Down Expand Up @@ -163,6 +162,13 @@ export class ApplicationTaskService {
return
}

// delete gateway
const gateway = await this.gatewayService.findOne(appid)
if (gateway) {
await this.gatewayService.delete(appid)
return
}

// update phase
await this.prisma.application.updateMany({
where: {
Expand Down
9 changes: 4 additions & 5 deletions server/src/application/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
ApiResponse,
ApiTags,
} from '@nestjs/swagger'
import { IRequest } from '../utils/types'
import { IRequest } from '../utils/interface'
import { JwtAuthGuard } from '../auth/jwt.auth.guard'
import { ResponseUtil } from '../utils/response'
import { ApplicationAuthGuard } from '../auth/application.auth.guard'
Expand All @@ -25,8 +25,8 @@ import { UpdateApplicationDto } from './dto/update-application.dto'
import { ApplicationService } from './application.service'
import { FunctionService } from '../function/function.service'
import { StorageService } from 'src/storage/storage.service'
import { GatewayCoreService } from 'src/core/gateway.cr.service'
import { RegionService } from 'src/region/region.service'
import { GatewayService } from 'src/gateway/gateway.service'

@ApiTags('Application')
@Controller('applications')
Expand All @@ -37,7 +37,7 @@ export class ApplicationController {
private readonly appService: ApplicationService,
private readonly funcService: FunctionService,
private readonly regionService: RegionService,
private readonly gatewayCore: GatewayCoreService,
private readonly gatewayService: GatewayService,
private readonly storageService: StorageService,
) {}

Expand Down Expand Up @@ -88,9 +88,9 @@ export class ApplicationController {
async findOne(@Param('appid') appid: string) {
const data = await this.appService.findOne(appid, {
configuration: true,
domain: true,
})

const gateway = await this.gatewayCore.findOne(appid)
const storage = await this.storageService.findOne(appid)

// Security Warning: Do not response this region object to client since it contains sensitive information
Expand All @@ -109,7 +109,6 @@ export class ApplicationController {

const res = {
...data,
gateway,
storage: {
...storage,
credentials,
Expand Down
4 changes: 2 additions & 2 deletions server/src/application/application.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Module } from '@nestjs/common'
import { ApplicationController } from './application.controller'
import { CoreModule } from '../core/core.module'
import { ApplicationService } from './application.service'
import { PrismaService } from '../prisma.service'
import { ApplicationTaskService } from './application-task.service'
Expand All @@ -12,9 +11,10 @@ import { EnvironmentVariableController } from './environment.controller'
import { StorageModule } from '../storage/storage.module'
import { RegionModule } from '../region/region.module'
import { DatabaseModule } from 'src/database/database.module'
import { GatewayModule } from 'src/gateway/gateway.module'

@Module({
imports: [CoreModule, StorageModule, RegionModule, DatabaseModule],
imports: [StorageModule, RegionModule, DatabaseModule, GatewayModule],
controllers: [ApplicationController, EnvironmentVariableController],
providers: [
ApplicationService,
Expand Down
1 change: 1 addition & 0 deletions server/src/application/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class ApplicationService {
bundle: include?.bundle,
runtime: include?.runtime,
configuration: include?.configuration,
domain: include?.domain,
},
})

Expand Down
2 changes: 1 addition & 1 deletion server/src/auth/application.auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@nestjs/common'
import { User } from '@prisma/client'
import { ApplicationService } from '../application/application.service'
import { IRequest } from '../utils/types'
import { IRequest } from '../utils/interface'

@Injectable()
export class ApplicationAuthGuard implements CanActivate {
Expand Down
2 changes: 1 addition & 1 deletion server/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@nestjs/swagger'
import { Response } from 'express'
import { ApiResponseUtil, ResponseUtil } from '../utils/response'
import { IRequest } from '../utils/types'
import { IRequest } from '../utils/interface'
import { UserDto } from '../user/dto/user.response'
import { AuthService } from './auth.service'
import { JwtAuthGuard } from './jwt.auth.guard'
Expand Down
Loading