Skip to content

Commit

Permalink
feat(web&server): control the activation of the ddb & change the loca…
Browse files Browse the repository at this point in the history
…tion of deploy manifests (#1810)

* chore: change deploy manifest location

* feat(server): support controlling the activation of the ddb

* feat(web): support controlling the activation of the ddb
  • Loading branch information
0fatal committed Jan 22, 2024
1 parent fd8453d commit a8bd80e
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 46 deletions.
2 changes: 0 additions & 2 deletions build/charts/laf-server/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ spec:
value: {{ .Values.default_region.tls.wildcard_certificate_secret_name | quote}}
- name: DEFAULT_REGION_PROMETHEUS_URL
value: {{ .Values.default_region.prometheus_url }}
- name: DEFAULT_REGION_DEPLOY_MANIFEST
value: {{ .Values.default_region.deploy_manifest }}
- name: SITE_NAME
value: {{ .Values.siteName | quote}}
{{- with .Values.nodeSelector }}
Expand Down
1 change: 0 additions & 1 deletion build/charts/laf-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ default_region:
runtime_exporter_secret: ""
# prometheus
prometheus_url: ""
deploy_manifest: ""
jwt:
secret: laf_server_abc123
expires_in: 7d
Expand Down
14 changes: 2 additions & 12 deletions build/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ set -e
set -x

sed "s/\$CAPACITY/${DB_PV_SIZE:-5Gi}/g" mongodb.yaml | kubectl apply -n ${NAMESPACE} -f -
kubectl wait --for=condition=exists --timeout=120s secret/mongodb-conn-credential -n ${NAMESPACE}
kubectl wait --for=condition=Ready --timeout=120s cluster.apps.kubeblocks.io/mongodb -n ${NAMESPACE}

DB_USERNAME=$(kubectl get secret -n ${NAMESPACE} mongodb-conn-credential -ojsonpath='{.data.username}' | base64 -d)
DB_PASSWORD=$(kubectl get secret -n ${NAMESPACE} mongodb-conn-credential -ojsonpath='{.data.password}' | base64 -d)
DB_ENDPOINT=$(kubectl get secret -n ${NAMESPACE} mongodb-conn-credential -ojsonpath='{.data.headlessEndpoint}' | base64 -d)
DATABASE_URL="mongodb://${DB_USERNAME}:${DB_PASSWORD}@${DB_ENDPOINT}/sys_db?authSource=admin&replicaSet=rs0&w=majority"
DATABASE_URL="mongodb://${DB_USERNAME}:${DB_PASSWORD}@${DB_ENDPOINT}/sys_db?authSource=admin&replicaSet=mongodb-mongodb&w=majority"

## 2. install prometheus
PROMETHEUS_URL=http://prometheus-operated.${NAMESPACE}.svc.cluster.local:9090
Expand Down Expand Up @@ -67,15 +67,6 @@ helm install minio -n ${NAMESPACE} \
## 4. install laf-server
SERVER_JWT_SECRET=$PASSWD_OR_SECRET
RUNTIME_EXPORTER_SECRET=$PASSWD_OR_SECRET

DEPLOY_MANIFEST=$(jq -n '{}')
for file in deploy-manifest/*.yaml; do
content=$(cat "$file" | jq -Rs .)
key=$(basename "$file")
DEPLOY_MANIFEST=$(echo "$DEPLOY_MANIFEST" | jq --arg key "$key" --arg value "$content" '. + {($key): $value}')
done
DEPLOY_MANIFEST=$(echo "$DEPLOY_MANIFEST" | sed "s/\$NAMESPACE/$NAMESPACE/g")

helm install server -n ${NAMESPACE} \
--set databaseUrl=${DATABASE_URL} \
--set jwt.secret=${SERVER_JWT_SECRET} \
Expand All @@ -94,7 +85,6 @@ helm install server -n ${NAMESPACE} \
--set default_region.tls.enabled=false \
$([ "$ENABLE_MONITOR" = "true" ] && echo "--set default_region.runtime_exporter_secret=${RUNTIME_EXPORTER_SECRET}") \
$([ "$ENABLE_MONITOR" = "true" ] && echo "--set default_region.prometheus_url=${PROMETHEUS_URL}") \
--set default_region.deploy_manifest=${DEPLOY_MANIFEST}
./charts/laf-server

## 5. install laf-web
Expand Down
12 changes: 9 additions & 3 deletions server/src/application/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ export class ApplicationController {
}

// check regionId exists
const region = await this.region.findOneDesensitized(
new ObjectId(dto.regionId),
)
const region = await this.region.findOne(new ObjectId(dto.regionId))
if (!region) {
return ResponseUtil.error(`region ${dto.regionId} not found`)
}
Expand Down Expand Up @@ -119,6 +117,13 @@ export class ApplicationController {
}
}

if (
dto.dedicatedDatabase &&
!region.databaseConf.dedicatedDatabase.enabled
) {
return ResponseUtil.error('dedicated database is not enabled')
}

// check if a user exceeds the resource limit in a region
const limitResource = await this.quotaServiceTsService.resourceLimit(
user._id,
Expand Down Expand Up @@ -204,6 +209,7 @@ export class ApplicationController {

/** This is the redundant field of Region */
tls: region.gatewayConf.tls.enabled,
dedicatedDatabase: region.databaseConf.dedicatedDatabase.enabled,
}

return ResponseUtil.ok(res)
Expand Down
4 changes: 0 additions & 4 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ dotenv.config({ path: '.env.local' })
dotenv.config()

export class ServerConfig {
static get DEFAULT_REGION_DEPLOY_MANIFEST() {
return process.env.DEFAULT_REGION_DEPLOY_MANIFEST
}

static get DATABASE_URL() {
if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL is not defined')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
sealos-db-provider-cr: <%- name %>
annotations: {}
name: <%- name %>
namespace: $NAMESPACE
namespace: laf-system
spec:
affinity:
nodeLabels: {}
Expand Down
18 changes: 15 additions & 3 deletions server/src/initializer/initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
AuthProviderState,
} from 'src/authentication/entities/auth-provider'
import { Setting, SettingKey } from 'src/setting/entities/setting'
import * as path from 'path'
import { readFileSync, readdirSync } from 'node:fs'

@Injectable()
export class InitializerService {
Expand Down Expand Up @@ -42,6 +44,15 @@ export class InitializerService {
if (ServerConfig.DEFAULT_REGION_NAMESPACE) {
mode = ApplicationNamespaceMode.Fixed
}

const files = readdirSync('./deploy-manifest')
const manifest = files.reduce((prev, file) => {
const key = file.slice(0, -path.extname(file).length)
const value = readFileSync(key, 'utf8')
prev[key] = value
return prev
}, {})

const res = await this.db.collection<Region>('Region').insertOne({
name: 'default',
displayName: 'Default',
Expand All @@ -64,6 +75,9 @@ export class InitializerService {
driver: 'mongodb',
connectionUri: ServerConfig.DEFAULT_REGION_DATABASE_URL,
controlConnectionUri: ServerConfig.DEFAULT_REGION_DATABASE_URL,
dedicatedDatabase: {
enabled: false,
},
},
storageConf: {
driver: 'minio',
Expand Down Expand Up @@ -94,9 +108,7 @@ export class InitializerService {
prometheusConf: {
apiUrl: ServerConfig.DEFAULT_REGION_PROMETHEUS_URL,
},
deployManifest: JSON.parse(
ServerConfig.DEFAULT_REGION_DEPLOY_MANIFEST || '{}',
),
deployManifest: manifest,
updatedAt: new Date(),
createdAt: new Date(),
state: 'Active',
Expand Down
3 changes: 3 additions & 0 deletions server/src/region/entities/region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export type RegionDatabaseConf = {
driver: string
connectionUri: string
controlConnectionUri: string
dedicatedDatabase: {
enabled: boolean
}
}

export type TLSConf = {
Expand Down
2 changes: 2 additions & 0 deletions server/src/region/region.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class RegionService {
name: 1,
displayName: 1,
state: 1,
dedicatedDatabase: '$databaseConf.dedicatedDatabase.enabled',
}

const region = await this.db
Expand All @@ -54,6 +55,7 @@ export class RegionService {
displayName: 1,
state: 1,
bundles: 1,
dedicatedDatabase: '$databaseConf.dedicatedDatabase.enabled',
}

const regions = await this.db
Expand Down
4 changes: 1 addition & 3 deletions web/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,9 @@
"Performance": "Performance",
"Capacity": "Capacity"
},
"DatabaseBundleControlTip": "Once a database is created, its type and number of instances cannot be temporarily modified, and its capacity can only increase and not decrease. If you have special needs, please contact customer service",
"MoveFunction": "Move function",
"UpgradeVersionTip": {
"Title": "Laf is ready to update!",
"Description": "Click to update"
}
}

}
5 changes: 2 additions & 3 deletions web/public/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@
"DedicatedType": "专用型",
"SharedType": "共享型",
"ApplicationSecification": "应用规格",
"DatabaseCreateTip": "数据库一旦创建后,暂时无法修改类型和实例数,容量只增不减,如有特殊需要请联系客服"
"DatabaseCreateTip": "数据库一旦创建,暂时无法修改类型和实例数,容量只增不减,如有特殊需要请联系客服"
},
"Storage Threshold": "内存阈值",
"Yesterday": "昨天",
Expand Down Expand Up @@ -747,10 +747,9 @@
"Performance": "性能",
"Capacity": "容量"
},
"DatabaseBundleControlTip": "数据库一旦创建后,暂时无法修改类型和实例数,容量只增不减,如有特殊需要请联系客服",
"MoveFunction": "移动函数",
"UpgradeVersionTip": {
"Title": "Laf 新版本已经准备好了!",
"Description": "点击立即更新"
}
}
}
5 changes: 2 additions & 3 deletions web/public/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@
"DedicatedType": "专用型",
"SharedType": "共享型",
"ApplicationSecification": "应用规格",
"DatabaseCreateTip": "数据库一旦创建后,暂时无法修改类型和实例数,容量只增不减,如有特殊需要请联系客服"
"DatabaseCreateTip": "数据库一旦创建,暂时无法修改类型和实例数,容量只增不减,如有特殊需要请联系客服"
},
"Storage Threshold": "内存阈值",
"Yesterday": "昨天",
Expand Down Expand Up @@ -747,10 +747,9 @@
"Performance": "性能",
"Capacity": "容量"
},
"DatabaseBundleControlTip": "数据库一旦创建后,暂时无法修改类型和实例数,容量只增不减,如有特殊需要请联系客服",
"MoveFunction": "移动函数",
"UpgradeVersionTip": {
"Title": "Laf 新版本已经准备好了!",
"Description": "点击立即更新"
}
}
}
1 change: 1 addition & 0 deletions web/src/apis/typing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export type TRegion = {
displayName: string;
state: string;
bundles: TBundle[];
dedicatedDatabase: boolean;
};

export type TBucket = {
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/app/mods/StatusBar/MonitorBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function MonitorBar() {
useEffect(() => {
if (allDataLoaded) {
const { cpuUsage, memoryUsage, storageUsage } = instantMonitorData || {};
if ("cpu" in resourceData) {
if (resourceData && "cpu" in resourceData) {
const databaseUsage = resourceData.dataSize.find(
(item: any) => item.metric.database === appid,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import {
HStack,
Expand All @@ -23,14 +23,14 @@ export default function DatabaseBundleControl(props: {
bundle: TypeBundle;
originCapacity?: number;
resourceOptions: any;
disabledChangeType: boolean;
type: "create" | "change";
defaultDatabaseCapacity?: number;
defaultDedicatedDatabaseBundle?: any;
onBundleItemChange: (k: string, v?: number) => any;
}) {
const {
bundle,
disabledChangeType,
type,
onBundleItemChange,
resourceOptions,
defaultDatabaseCapacity,
Expand All @@ -39,18 +39,34 @@ export default function DatabaseBundleControl(props: {
} = props;
const { t } = useTranslation();
const darkMode = useColorMode().colorMode === COLOR_MODE.dark;
const { regions } = useGlobalStore();
const currentRegion = useMemo(() => regions?.[0], [regions]);

const [databaseType, setDatabaseType] = useState<"dedicated" | "shared">(
bundle.dedicatedDatabase ? "dedicated" : "shared",
(type === "change" && bundle.dedicatedDatabase) ||
(type === "create" && currentRegion?.dedicatedDatabase)
? "dedicated"
: "shared",
);

const disabledChangeType = useMemo(
() => type === "change" || !currentRegion?.dedicatedDatabase,
[type, currentRegion],
);

useEffect(() => {
if (type === "create" && currentRegion) {
setDatabaseType(currentRegion?.dedicatedDatabase ? "dedicated" : "shared");
}
}, [currentRegion, type]);

const { showInfo } = useGlobalStore(({ showInfo }) => ({ showInfo }));

useEffect(() => {
showInfo(
"数据库一旦创建后,暂时无法修改类型和实例数,容量只增不减,如有特殊需要请联系客服",
5000,
);
}, []);
if (databaseType === "dedicated") {
showInfo(t("application.DatabaseCreateTip"), 5000);
}
}, [databaseType]);

useEffect(() => {
if (databaseType === "dedicated") {
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/home/mods/CreateAppModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ const CreateAppModal = (props: {
});
setCalculating(true);
}}
disabledChangeType={type === "change"}
type={type}
defaultDatabaseCapacity={defaultBundle.databaseCapacity}
defaultDedicatedDatabaseBundle={defaultBundle.dedicatedDatabase}
resourceOptions={billingResourceOptionsRes?.data}
Expand Down

0 comments on commit a8bd80e

Please sign in to comment.