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 item crud when changing clusters #1707

Merged
merged 2 commits into from
Feb 19, 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
52 changes: 29 additions & 23 deletions frontend/src/components/common/Resource/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { getCluster } from '../../../lib/cluster';
import { apply } from '../../../lib/k8s/apiProxy';
import { KubeObjectInterface } from '../../../lib/k8s/cluster';
import { clusterAction } from '../../../redux/clusterActionSlice';
Expand All @@ -28,29 +29,31 @@ export default function CreateButton(props: CreateButtonProps) {
const { t } = useTranslation(['translation']);
const dispatchCreateEvent = useEventCallback(HeadlampEventType.CREATE_RESOURCE);

const applyFunc = async (newItems: KubeObjectInterface[]) => {
await Promise.allSettled(newItems.map(newItem => apply(newItem))).then((values: any) => {
values.forEach((value: any, index: number) => {
if (value.status === 'rejected') {
let msg;
const kind = newItems[index].kind;
const name = newItems[index].metadata.name;
const apiVersion = newItems[index].apiVersion;
if (newItems.length === 1) {
msg = t('translation|Failed to create {{ kind }} {{ name }}.', { kind, name });
} else {
msg = t('translation|Failed to create {{ kind }} {{ name }} in {{ apiVersion }}.', {
kind,
name,
apiVersion,
});
const applyFunc = async (newItems: KubeObjectInterface[], clusterName: string) => {
await Promise.allSettled(newItems.map(newItem => apply(newItem, clusterName))).then(
(values: any) => {
values.forEach((value: any, index: number) => {
if (value.status === 'rejected') {
let msg;
const kind = newItems[index].kind;
const name = newItems[index].metadata.name;
const apiVersion = newItems[index].apiVersion;
if (newItems.length === 1) {
msg = t('translation|Failed to create {{ kind }} {{ name }}.', { kind, name });
} else {
msg = t('translation|Failed to create {{ kind }} {{ name }} in {{ apiVersion }}.', {
kind,
name,
apiVersion,
});
}
setErrorMessage(msg);
setOpenDialog(true);
throw msg;
}
setErrorMessage(msg);
setOpenDialog(true);
throw msg;
}
});
});
});
}
);
};

function handleSave(newItemDefs: KubeObjectInterface[]) {
Expand Down Expand Up @@ -78,8 +81,11 @@ export default function CreateButton(props: CreateButtonProps) {
// all resources name
const resourceNames = massagedNewItemDefs.map(newItemDef => newItemDef.metadata.name);
setOpenDialog(false);

const clusterName = getCluster() || '';

dispatch(
clusterAction(() => applyFunc(massagedNewItemDefs), {
clusterAction(() => applyFunc(massagedNewItemDefs, clusterName), {
startMessage: t('translation|Applying {{ newItemName }}…', {
newItemName: resourceNames.join(','),
}),
Expand Down
24 changes: 16 additions & 8 deletions frontend/src/lib/k8s/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,11 @@ export function makeKubeObject<T extends KubeObjectInterface | KubeEvent>(
class KubeObject {
static apiEndpoint: ReturnType<typeof apiFactoryWithNamespace | typeof apiFactory>;
jsonData: T | null = null;
private readonly _clusterName: string;

constructor(json: T) {
this.jsonData = json;
this._clusterName = getCluster() || '';
}

static get className(): string {
Expand Down Expand Up @@ -632,11 +634,11 @@ export function makeKubeObject<T extends KubeObjectInterface | KubeEvent>(
args.unshift(this.getNamespace()!);
}

return this._class().apiEndpoint.delete(...args);
return this._class().apiEndpoint.delete(...args, {}, this._clusterName);
}

update(data: KubeObjectInterface) {
return this._class().put(data);
return this._class().apiEndpoint.put(data, {}, this._clusterName);
}

static put(data: KubeObjectInterface) {
Expand All @@ -655,14 +657,20 @@ export function makeKubeObject<T extends KubeObjectInterface | KubeEvent>(

type ApiEndpointWithScale = {
scale: {
put: (data: { metadata: KubeMetadata; spec: { replicas: number } }) => Promise<any>;
put: (
data: { metadata: KubeMetadata; spec: { replicas: number } },
clusterName?: string
) => Promise<any>;
};
};

return (this._class().apiEndpoint as ApiEndpointWithScale).scale.put({
metadata: this.metadata,
spec,
});
return (this._class().apiEndpoint as ApiEndpointWithScale).scale.put(
{
metadata: this.metadata,
spec,
},
this._clusterName
);
}

patch(body: OpPatch[]) {
Expand All @@ -674,7 +682,7 @@ export function makeKubeObject<T extends KubeObjectInterface | KubeEvent>(
}

args.push(this.getName());
return this._class().apiEndpoint.patch(...args);
return this._class().apiEndpoint.patch(...args, {}, this._clusterName);
}

/** Performs a request to check if the user has the given permission.
Expand Down
Loading