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

Fix: Resolve bug introduced by launchpad JSON patching #4935

Merged
merged 1 commit into from
Jul 31, 2024
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
11 changes: 6 additions & 5 deletions frontend/providers/applaunchpad/src/pages/app/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,21 @@ const EditApp = ({ appName, tabType }: { appName?: string; tabType: string }) =>
async (yamlList: YamlItemType[]) => {
setIsLoading(true);
try {
const yamls = yamlList.map((item) => item.value);
const parsedNewYamlList = yamlList.map((item) => item.value);

if (appName) {
const patch = patchYamlList({
formOldYamlList: formOldYamls.current.map((item) => item.value),
newYamlList: yamls,
crYamlList: crOldYamls.current
parsedOldYamlList: formOldYamls.current.map((item) => item.value),
parsedNewYamlList: parsedNewYamlList,
originalYamlList: crOldYamls.current
});
await putApp({
patch,
appName,
stateFulSetYaml: yamlList.find((item) => item.filename === 'statefulSet.yaml')?.value
});
} else {
await postDeployApp(yamls);
await postDeployApp(parsedNewYamlList);
}

router.replace(`/app/detail?name=${formHook.getValues('appName')}`);
Expand Down
4 changes: 3 additions & 1 deletion frontend/providers/applaunchpad/src/types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export interface AppListItemType {
labels: { [key: string]: string };
}

export type ProtocolType = 'HTTP' | 'GRPC' | 'WS';

export interface AppEditType {
appName: string;
imageName: string;
Expand All @@ -71,7 +73,7 @@ export interface AppEditType {
networkName: string;
portName: string;
port: number;
protocol: 'HTTP' | 'GRPC' | 'WS';
protocol: ProtocolType;
openPublicDomain: boolean;
publicDomain: string; // default domain
customDomain: string; // custom domain
Expand Down
17 changes: 10 additions & 7 deletions frontend/providers/applaunchpad/src/utils/adapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import type {
AppDetailType,
PodMetrics,
PodEvent,
HpaTarget
HpaTarget,
ProtocolType
} from '@/types/app';
import {
appStatusMap,
Expand Down Expand Up @@ -266,16 +267,18 @@ export const adaptAppDetail = (configs: DeployKindsType[]): AppDetailType => {
) as V1Ingress;
const domain = ingress?.spec?.rules?.[0].host || '';

const backendProtocol = ingress?.metadata?.annotations?.[
'nginx.ingress.kubernetes.io/backend-protocol'
] as ProtocolType;

const protocol =
backendProtocol ?? (item.protocol === 'TCP' ? 'HTTP' : (item.protocol as ProtocolType));

return {
networkName: ingress?.metadata?.name || '',
portName: item.name || '',
port: item.port,
protocol:
(ingress?.metadata?.annotations?.[
'nginx.ingress.kubernetes.io/backend-protocol'
] as AppEditType['networks'][0]['protocol']) || item.protocol === 'TCP'
? 'HTTP'
: (item.protocol as AppEditType['networks'][number]['protocol']),
protocol: protocol,
openPublicDomain: !!ingress,
...(domain.endsWith(SEALOS_DOMAIN)
? {
Expand Down
68 changes: 34 additions & 34 deletions frontend/providers/applaunchpad/src/utils/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,21 @@ export function downLoadBold(content: BlobPart, type: string, fileName: string)
* patch yamlList and get action
*/
export const patchYamlList = ({
formOldYamlList,
newYamlList,
crYamlList
parsedOldYamlList,
parsedNewYamlList,
originalYamlList
}: {
formOldYamlList: string[];
newYamlList: string[];
crYamlList: DeployKindsType[];
parsedOldYamlList: string[];
parsedNewYamlList: string[];
originalYamlList: DeployKindsType[];
}) => {
console.log(formOldYamlList, newYamlList, crYamlList, '=======');

const oldFormJsonList = formOldYamlList
const oldFormJsonList = parsedOldYamlList
.map((item) => yaml.loadAll(item))
.flat() as DeployKindsType[];
const newFormJsonList = newYamlList.map((item) => yaml.loadAll(item)).flat() as DeployKindsType[];

console.log(oldFormJsonList, newFormJsonList, crYamlList, '===patchYamlList===');
const newFormJsonList = parsedNewYamlList
.map((item) => yaml.loadAll(item))
.flat() as DeployKindsType[];

const actions: AppPatchPropsType = [];

Expand All @@ -271,6 +270,7 @@ export const patchYamlList = ({
});
}
});

// find create and patch
newFormJsonList.forEach((newYamlJson) => {
const oldFormJson = oldFormJsonList.find(
Expand All @@ -287,7 +287,7 @@ export const patchYamlList = ({
const actionsJson = (() => {
try {
/* find cr json */
let crOldYamlJson = crYamlList.find(
let crOldYamlJson = originalYamlList.find(
(item) =>
item.kind === oldFormJson?.kind &&
item?.metadata?.name === oldFormJson?.metadata?.name
Expand All @@ -314,20 +314,26 @@ export const patchYamlList = ({
}

/* generate new json */
const _patchRes: jsonpatch.Operation[] = patchRes.map((item) => {
let jsonPatchError = jsonpatch.validate([item], crOldYamlJson);
if (
jsonPatchError?.operation &&
jsonPatchError?.name === 'OPERATION_PATH_UNRESOLVABLE'
) {
return {
...jsonPatchError.operation,
op: 'add'
};
} else {
const _patchRes: jsonpatch.Operation[] = patchRes
.map((item) => {
let jsonPatchError = jsonpatch.validate([item], crOldYamlJson);
if (jsonPatchError?.name === 'OPERATION_PATH_UNRESOLVABLE') {
switch (item.op) {
case 'add':
case 'replace':
return {
...item,
op: 'add' as const,
value: item.value ?? ''
};
default:
return null;
}
}
return item;
}
});
})
.filter((op): op is jsonpatch.Operation => op !== null);

const patchResYamlJson = jsonpatch.applyPatch(crOldYamlJson, _patchRes, true).newDocument;

// delete invalid field
Expand All @@ -344,7 +350,7 @@ export const patchYamlList = ({

return patchResYamlJson;
} catch (error) {
console.log('ACTIONS JSON ERROR\n', error);
console.error('ACTIONS JSON ERROR:\n', error);
return newYamlJson;
}
})();
Expand Down Expand Up @@ -373,13 +379,7 @@ export const patchYamlList = ({
}
}

console.log(
'patch result===',
oldFormJson.metadata?.name,
oldFormJson.kind,
patchRes,
actionsJson
);
console.log('patch result:', oldFormJson.metadata?.name, oldFormJson.kind, actionsJson);

actions.push({
type: 'patch',
Expand All @@ -394,7 +394,7 @@ export const patchYamlList = ({
});
}
});
console.log(actions, 'actions');

return actions;
};

Expand Down
Loading