Skip to content

Commit

Permalink
feat(applaunchpad): Improve domain handling for legacy and new domains
Browse files Browse the repository at this point in the history
  • Loading branch information
zjy365 committed Sep 6, 2024
1 parent 9ca435c commit 07a660b
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 104 deletions.
3 changes: 2 additions & 1 deletion frontend/providers/applaunchpad/src/constants/editApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export const defaultEditVal: AppEditType = {
protocol: 'HTTP',
openPublicDomain: false,
publicDomain: '',
customDomain: ''
customDomain: '',
isLegacy: false
}
],
envs: [],
Expand Down
3 changes: 2 additions & 1 deletion frontend/providers/applaunchpad/src/mock/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ export const MOCK_APP_DETAIL: AppDetailType = {
protocol: 'HTTP',
openPublicDomain: false,
publicDomain: '',
customDomain: ''
customDomain: '',
isLegacy: false
}
],
envs: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export type Response = {
CURRENCY: Coin;
guideEnabled: boolean;
fileMangerConfig: FileMangerType;
SEALOS_USER_DOMAIN: string;
};

export const defaultAppConfig: AppConfigType = {
cloud: {
domain: 'cloud.sealos.io',
port: ''
port: '',
userDomain: 'cloud.sealos.io'
},
common: {
guideEnabled: false,
Expand Down Expand Up @@ -76,7 +78,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
FORM_SLIDER_LIST_CONFIG: global.AppConfig.launchpad.appResourceFormSliderConfig,
guideEnabled: global.AppConfig.common.guideEnabled,
fileMangerConfig: global.AppConfig.launchpad.fileManger,
CURRENCY: Coin.shellCoin
CURRENCY: Coin.shellCoin,
SEALOS_USER_DOMAIN: global.AppConfig.cloud.userDomain
}
});
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defaultSliderKey, ProtocolList } from '@/constants/app';
import { GpuAmountMarkList } from '@/constants/editApp';
import { useToast } from '@/hooks/useToast';
import { useGlobalStore } from '@/store/global';
import { SEALOS_DOMAIN } from '@/store/static';
import { SEALOS_DOMAIN, SEALOS_USER_DOMAIN } from '@/store/static';
import { useUserStore } from '@/store/user';
import type { QueryType } from '@/types';
import type { AppEditType } from '@/types/app';
Expand Down Expand Up @@ -801,7 +801,8 @@ const Form = ({
protocol: 'HTTP',
openPublicDomain: false,
publicDomain: '',
customDomain: ''
customDomain: '',
isLegacy: false
})
}
>
Expand Down Expand Up @@ -866,7 +867,9 @@ const Form = ({
<Box flex={1} userSelect={'all'} className="textEllipsis">
{network.customDomain
? network.customDomain
: `${network.publicDomain}.${SEALOS_DOMAIN}`}
: `${network.publicDomain}.${
network.isLegacy ? SEALOS_DOMAIN : SEALOS_USER_DOMAIN
}`}
</Box>
<Box
fontSize={'11px'}
Expand Down
2 changes: 2 additions & 0 deletions frontend/providers/applaunchpad/src/store/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getInitData } from '@/api/platform';
import { Coin } from '@/constants/app';

export let SEALOS_DOMAIN = 'cloud.sealos.io';
export let SEALOS_USER_DOMAIN = 'cloud.sealos.io';
export let DOMAIN_PORT = '';
export let INGRESS_SECRET = 'wildcard-cert';
export let SHOW_EVENT_ANALYZE = false;
Expand All @@ -13,6 +14,7 @@ export const loadInitData = async () => {
try {
const res = await getInitData();
SEALOS_DOMAIN = res.SEALOS_DOMAIN;
SEALOS_USER_DOMAIN = res.SEALOS_USER_DOMAIN;
DOMAIN_PORT = res.DOMAIN_PORT;
INGRESS_SECRET = res.INGRESS_SECRET;
SHOW_EVENT_ANALYZE = res.SHOW_EVENT_ANALYZE;
Expand Down
1 change: 1 addition & 0 deletions frontend/providers/applaunchpad/src/types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface AppEditType {
openPublicDomain: boolean;
publicDomain: string; // default domain
customDomain: string; // custom domain
isLegacy: boolean;
}[];
envs: {
key: string;
Expand Down
1 change: 1 addition & 0 deletions frontend/providers/applaunchpad/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type AppConfigType = {
cloud: {
domain: string;
port?: string;
userDomain: string;
};
common: {
guideEnabled: boolean;
Expand Down
106 changes: 11 additions & 95 deletions frontend/providers/applaunchpad/src/utils/adapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export enum YamlKindEnum {
}

export const adaptAppDetail = async (configs: DeployKindsType[]): Promise<AppDetailType> => {
const { SEALOS_DOMAIN } = await getInitData();
const { SEALOS_DOMAIN, SEALOS_USER_DOMAIN } = await getInitData();
const deployKindsMap: {
[YamlKindEnum.StatefulSet]?: V1StatefulSet;
[YamlKindEnum.Deployment]?: V1Deployment;
Expand Down Expand Up @@ -312,21 +312,22 @@ export const adaptAppDetail = async (configs: DeployKindsType[]): Promise<AppDet
const protocol =
backendProtocol ?? (item.protocol === 'TCP' ? 'HTTP' : (item.protocol as ProtocolType));

const isCustomDomain =
!domain.endsWith(SEALOS_DOMAIN) && !domain.endsWith(SEALOS_USER_DOMAIN);

const isLegacyDomain = domain.endsWith(SEALOS_DOMAIN);

return {
networkName: ingress?.metadata?.name || '',
portName: item.name || '',
port: item.port,
protocol: protocol,
openPublicDomain: !!ingress,
...(domain.endsWith(SEALOS_DOMAIN)
? {
publicDomain: domain.split('.')[0],
customDomain: ''
}
: {
publicDomain: ingress?.metadata?.labels?.[publicDomainKey] || '',
customDomain: domain
})
publicDomain: isCustomDomain
? ingress?.metadata?.labels?.[publicDomainKey] || ''
: domain.split('.')[0],
customDomain: isCustomDomain ? domain : '',
isLegacy: isLegacyDomain
};
}) || [],
hpa: deployKindsMap.HorizontalPodAutoscaler?.spec
Expand Down Expand Up @@ -390,91 +391,6 @@ export const adaptEditAppData = (app: AppDetailType): AppEditType => {
return res as AppEditType;
};

// yaml file adapt to edit form
export const adaptYamlToEdit = (yamlList: string[]) => {
const configs = yamlList.map((item) => yaml.loadAll(item) as DeployKindsType).flat();

const deployKindsMap: {
[YamlKindEnum.Deployment]?: V1Deployment;
[YamlKindEnum.Service]?: V1Service;
[YamlKindEnum.ConfigMap]?: V1ConfigMap;
[YamlKindEnum.Ingress]?: V1Ingress;
[YamlKindEnum.HorizontalPodAutoscaler]?: V2HorizontalPodAutoscaler;
[YamlKindEnum.Secret]?: V1Secret;
} = {};

configs.forEach((item) => {
if (item.kind) {
// @ts-ignore
deployKindsMap[item.kind] = item;
}
});

const domain = deployKindsMap?.Ingress?.spec?.rules?.[0].host;
const cpuStr =
deployKindsMap?.Deployment?.spec?.template?.spec?.containers?.[0]?.resources?.requests?.cpu;
const memoryStr =
deployKindsMap?.Deployment?.spec?.template?.spec?.containers?.[0]?.resources?.requests?.memory;

const res: Record<string, any> = {
imageName: deployKindsMap?.Deployment?.spec?.template?.spec?.containers?.[0]?.image,
runCMD:
deployKindsMap?.Deployment?.spec?.template?.spec?.containers?.[0]?.command?.join(' ') || '',
cmdParam:
deployKindsMap?.Deployment?.spec?.template?.spec?.containers?.[0]?.args?.join(' ') || '',
replicas: deployKindsMap?.Deployment?.spec?.replicas,
cpu: cpuStr ? cpuFormatToM(cpuStr) : undefined,
memory: memoryStr ? memoryFormatToMi(memoryStr) : undefined,
accessExternal: deployKindsMap?.Ingress
? {
use: true,
outDomain: domain?.split('.')[0],
selfDomain: domain
}
: undefined,
containerOutPort:
deployKindsMap?.Deployment?.spec?.template?.spec?.containers?.[0]?.ports?.[0]?.containerPort,
envs:
deployKindsMap?.Deployment?.spec?.template?.spec?.containers?.[0]?.env?.map((env) => ({
key: env.name,
value: env.value
})) || undefined,
hpa: deployKindsMap.HorizontalPodAutoscaler?.spec
? {
use: true,
target:
(deployKindsMap.HorizontalPodAutoscaler.spec.metrics?.[0]?.resource
?.name as HpaTarget) || 'cpu',
value:
deployKindsMap.HorizontalPodAutoscaler.spec.metrics?.[0]?.resource?.target
?.averageUtilization || 50,
minReplicas: deployKindsMap.HorizontalPodAutoscaler.spec?.maxReplicas,
maxReplicas: deployKindsMap.HorizontalPodAutoscaler.spec?.minReplicas
}
: undefined,
configMapList: deployKindsMap?.ConfigMap?.data
? Object.entries(deployKindsMap?.ConfigMap.data).map(([key, value]) => ({
mountPath: key,
value
}))
: undefined,
secret: deployKindsMap.Secret
? {
...defaultEditVal.secret,
use: true
}
: undefined
};

for (const key in res) {
if (res[key] === undefined) {
delete res[key];
}
}

return res;
};

export const sliderNumber2MarkList = ({
val,
type,
Expand Down
6 changes: 4 additions & 2 deletions frontend/providers/applaunchpad/src/utils/deployYaml2Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
minReplicasKey,
publicDomainKey
} from '@/constants/app';
import { INGRESS_SECRET, SEALOS_DOMAIN } from '@/store/static';
import { INGRESS_SECRET, SEALOS_DOMAIN, SEALOS_USER_DOMAIN } from '@/store/static';
import type { AppEditType } from '@/types/app';
import { pathFormat, pathToNameFormat, str2Num, strToBase64 } from '@/utils/tools';
import dayjs from 'dayjs';
Expand Down Expand Up @@ -270,9 +270,11 @@ export const json2Ingress = (data: AppEditType) => {
const result = data.networks
.filter((item) => item.openPublicDomain)
.map((network, i) => {
console.log(network, 'network');

const host = network.customDomain
? network.customDomain
: `${network.publicDomain}.${SEALOS_DOMAIN}`;
: `${network.publicDomain}.${network.isLegacy ? SEALOS_DOMAIN : SEALOS_USER_DOMAIN}`;

const secretName = network.customDomain ? network.networkName : INGRESS_SECRET;

Expand Down

0 comments on commit 07a660b

Please sign in to comment.