fix(container): specify node name when get a container EE-6981 (#11748)

pull/11788/head
cmeng 2024-05-07 11:34:46 +12:00 committed by GitHub
parent 8a81d95253
commit 5f8fd99fe8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 6 deletions

View File

@ -66,9 +66,10 @@ export function useInitialValues(submitting: boolean, isWindows: boolean) {
const networksQuery = useNetworksForSelector();
const fromContainerQuery = useContainer(environmentId, from, {
const fromContainerQuery = useContainer(environmentId, from, nodeName, {
enabled: !submitting,
});
const runningContainersQuery = useContainers(environmentId, {
enabled: !!from,
});

View File

@ -8,10 +8,10 @@ import { Link } from '@@/Link';
export function LogView() {
const {
params: { endpointId: environmentId, id: containerId },
params: { endpointId: environmentId, id: containerId, nodeName },
} = useCurrentStateAndParams();
const containerQuery = useContainer(environmentId, containerId);
const containerQuery = useContainer(environmentId, containerId, nodeName);
if (!containerQuery.data || containerQuery.isLoading) {
return null;
}

View File

@ -7,6 +7,7 @@ import {
MountPoint,
NetworkSettings,
} from 'docker-types/generated/1.41';
import { RawAxiosRequestHeaders } from 'axios';
import { PortainerResponse } from '@/react/docker/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
@ -75,11 +76,15 @@ export interface ContainerJSON {
export function useContainer(
environmentId: EnvironmentId,
containerId?: ContainerId,
nodeName?: string,
{ enabled }: { enabled?: boolean } = {}
) {
return useQuery(
containerId ? queryKeys.container(environmentId, containerId) : [],
() => (containerId ? getContainer(environmentId, containerId) : undefined),
() =>
containerId
? getContainer(environmentId, containerId, nodeName)
: undefined,
{
meta: {
title: 'Failure',
@ -103,11 +108,19 @@ export type ContainerResponse = PortainerResponse<ContainerJSON>;
async function getContainer(
environmentId: EnvironmentId,
containerId: ContainerId
containerId: ContainerId,
nodeName?: string
) {
try {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
const { data } = await axios.get<ContainerResponse>(
urlBuilder(environmentId, containerId, 'json')
urlBuilder(environmentId, containerId, 'json'),
{ headers }
);
return data;
} catch (error) {