diff --git a/frontend/src/components/common/Resource/CreateButton.tsx b/frontend/src/components/common/Resource/CreateButton.tsx index 27fb9ae189..7dce4352bd 100644 --- a/frontend/src/components/common/Resource/CreateButton.tsx +++ b/frontend/src/components/common/Resource/CreateButton.tsx @@ -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'; @@ -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[]) { @@ -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(','), }), diff --git a/frontend/src/lib/k8s/cluster.ts b/frontend/src/lib/k8s/cluster.ts index d8058d4771..781da7bf12 100644 --- a/frontend/src/lib/k8s/cluster.ts +++ b/frontend/src/lib/k8s/cluster.ts @@ -327,9 +327,11 @@ export function makeKubeObject( class KubeObject { static apiEndpoint: ReturnType; jsonData: T | null = null; + private readonly _clusterName: string; constructor(json: T) { this.jsonData = json; + this._clusterName = getCluster() || ''; } static get className(): string { @@ -632,11 +634,11 @@ export function makeKubeObject( 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) { @@ -655,14 +657,20 @@ export function makeKubeObject( type ApiEndpointWithScale = { scale: { - put: (data: { metadata: KubeMetadata; spec: { replicas: number } }) => Promise; + put: ( + data: { metadata: KubeMetadata; spec: { replicas: number } }, + clusterName?: string + ) => Promise; }; }; - 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[]) { @@ -674,7 +682,7 @@ export function makeKubeObject( } 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.