Skip to content

Commit

Permalink
fix(server): fix not reapply service when restart app (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
HUAHUAI23 authored Oct 12, 2023
1 parent f71ea31 commit bb9e8e2
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions server/src/instance/instance.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
V1Deployment,
V1DeploymentSpec,
V1ServiceSpec,
V2HorizontalPodAutoscaler,
V2HorizontalPodAutoscalerSpec,
} from '@kubernetes/client-node'
Expand Down Expand Up @@ -31,7 +32,7 @@ export class InstanceService {

public async create(appid: string) {
const app = await this.applicationService.findOneUnsafe(appid)
const labels = this.getRuntimeLabel(appid)
const labels: Record<string, string> = this.getRuntimeLabel(appid)
// const region = app.region

// // Although a namespace has already been created during application creation,
Expand Down Expand Up @@ -112,8 +113,8 @@ export class InstanceService {
public async restart(appid: string) {
const app = await this.applicationService.findOneUnsafe(appid)
const region = app.region
const { deployment, hpa } = await this.get(appid)
if (!deployment) {
const { deployment, hpa, service } = await this.get(appid)
if (!deployment || !service) {
await this.create(appid)
return
}
Expand All @@ -124,19 +125,35 @@ export class InstanceService {
)
const appsV1Api = this.cluster.makeAppsV1Api(region)
const namespace = GetApplicationNamespace(region, appid)
const res = await appsV1Api.replaceNamespacedDeployment(
const deploymentResult = await appsV1Api.replaceNamespacedDeployment(
app.appid,
namespace,
deployment,
)

this.logger.log(`restart k8s deployment ${res.body?.metadata?.name}`)
this.logger.log(
`restart k8s deployment ${deploymentResult.body?.metadata?.name}`,
)

// reapply service
service.spec = this.makeServiceSpec(service.metadata.labels)
const coreV1Api = this.cluster.makeCoreV1Api(region)
const serviceResult = await coreV1Api.replaceNamespacedService(
service.metadata.name,
namespace,
service,
)

this.logger.log(`restart k8s service ${serviceResult.body?.metadata?.name}`)

// reapply hpa when application is restarted
await this.reapplyHorizontalPodAutoscaler(app, hpa)
}

private async createDeployment(app: ApplicationWithRelations, labels: any) {
private async createDeployment(
app: ApplicationWithRelations,
labels: Record<string, string>,
) {
const appid = app.appid
const region = app.region
assert(region, 'region is required')
Expand All @@ -156,23 +173,20 @@ export class InstanceService {
return res.body
}

private async createService(app: ApplicationWithRelations, labels: any) {
private async createService(
app: ApplicationWithRelations,
labels: Record<string, string>,
) {
const region = app.region
assert(region, 'region is required')

const namespace = GetApplicationNamespace(region, app.appid)
const serviceName = app.appid
const coreV1Api = this.cluster.makeCoreV1Api(region)
const spec = this.makeServiceSpec(labels)
const res = await coreV1Api.createNamespacedService(namespace, {
metadata: { name: serviceName, labels },
spec: {
selector: labels,
type: 'ClusterIP',
ports: [
{ port: 8000, targetPort: 8000, protocol: 'TCP', name: 'http' },
{ port: 9000, targetPort: 9000, protocol: 'TCP', name: 'storage' },
],
},
spec: spec,
})
this.logger.log(`create k8s service ${res.body?.metadata?.name}`)
return res.body
Expand Down Expand Up @@ -211,9 +225,21 @@ export class InstanceService {
}
}

private makeServiceSpec(labels: Record<string, string>) {
const spec: V1ServiceSpec = {
selector: labels,
type: 'ClusterIP',
ports: [
{ port: 8000, targetPort: 8000, protocol: 'TCP', name: 'http' },
{ port: 9000, targetPort: 9000, protocol: 'TCP', name: 'storage' },
],
}
return spec
}

private async makeDeploymentSpec(
app: ApplicationWithRelations,
labels: any,
labels: Record<string, string>,
): Promise<V1DeploymentSpec> {
const appid = app.appid
const region = app.region
Expand Down Expand Up @@ -412,7 +438,7 @@ export class InstanceService {

private async createHorizontalPodAutoscaler(
app: ApplicationWithRelations,
labels: any,
labels: Record<string, string>,
) {
if (!app.bundle.autoscaling.enable) return null

Expand Down Expand Up @@ -566,7 +592,7 @@ export class InstanceService {

private getRuntimeLabel(appid: string) {
const SEALOS = 'cloud.sealos.io/app-deploy-manager'
const labels = {
const labels: Record<string, string> = {
[LABEL_KEY_APP_ID]: appid,
[SEALOS]: appid,
app: appid,
Expand Down

0 comments on commit bb9e8e2

Please sign in to comment.