Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(workloads): gate discovery on canonical targets

rcourtman10b067396af1
diff --git a/docs/release-control/v6/internal/subsystems/performance-and-scalability.md b/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
index ce9ae0c1c0..f9213985f0 100644
--- a/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
+++ b/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
@@ -248,10 +248,13 @@ That same workload hot path also owns the split between canonical
app-container routing and Docker-only actions. `frontend-modern/src/hooks/useWorkloads.ts`,
`frontend-modern/src/components/Dashboard/workloadTopology.ts`, and
`frontend-modern/src/components/Dashboard/useGuestRowState.ts` must preserve a
-generic app-container host path for discovery and navigation while keeping
-Docker runtime action identifiers explicit. TrueNAS app-containers may reuse
-runtime metadata such as image and runtime strings, but they must not inherit
-Docker-only update affordances or fallback host IDs on the dashboard row path.
+canonical app-container navigation path while keeping Docker runtime action
+identifiers explicit. Discovery affordances on the dashboard drawer must follow
+the canonical `discoveryTarget` contract, not generic app-container host
+fallbacks. TrueNAS app-containers may reuse runtime metadata such as image and
+runtime strings, but they must not inherit Docker-only update affordances or
+agent-only Discovery tabs on the dashboard row path unless the unified
+resource contract explicitly supplies discovery ownership.
That same row-and-selection boundary also requires canonical app-container
identity to stay intact on the workload surface. `/workloads` rows, deep links,
drawer selection, anomaly correlation, and route-owned filters must key on the
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index 7272762a60..302db15ec4 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -1165,8 +1165,12 @@ That same contract applies to the frontend workload state boundary:
shared `DockerData` for runtime metadata, but they must keep Docker-only
action identifiers and update affordances scoped to Docker-managed
app-containers. TrueNAS-backed `app-container` workloads must still navigate
-and discover through the canonical app-container host path without
-populating Docker-only action IDs.
+through the canonical app-container host path without populating Docker-only
+action IDs, but discovery support must come from canonical
+`discoveryTarget` ownership rather than synthesized `node` or `instance`
+fallbacks. API-backed workloads such as TrueNAS apps may not expose agent-only
+Discovery affordances unless the unified resource contract explicitly carries a
+discovery target.
That same resolved-context ownership also governs read diagnostics. When
`internal/ai/tools/tools_read.go` executes `pulse_read action="logs"` against
a canonical `app-container` resource, TrueNAS-backed app containers must
diff --git a/frontend-modern/src/components/Dashboard/GuestDrawer.test.tsx b/frontend-modern/src/components/Dashboard/GuestDrawer.test.tsx
index 7c2c7d8374..94f2cfe053 100644
--- a/frontend-modern/src/components/Dashboard/GuestDrawer.test.tsx
+++ b/frontend-modern/src/components/Dashboard/GuestDrawer.test.tsx
@@ -98,6 +98,26 @@ describe('GuestDrawer', () => {
expect(screen.getByText('Discovery')).toBeInTheDocument();
});
+ it('hides Discovery when the workload has no canonical discovery target', () => {
+ render(() => (
+ <GuestDrawer
+ guest={makeGuest({
+ id: 'app-container:truenas-main:nextcloud',
+ type: 'app-container',
+ workloadType: 'app-container',
+ platformType: 'truenas',
+ dockerHostId: '',
+ node: 'truenas-main',
+ instance: 'truenas-main',
+ })}
+ onClose={vi.fn()}
+ />
+ ));
+ expect(screen.getByText('Overview')).toBeInTheDocument();
+ expect(screen.queryByText('Discovery')).toBeNull();
+ expect(screen.queryByTestId('discovery-tab')).toBeNull();
+ });
+
it('starts on the Overview tab (discovery content hidden)', () => {
const { container } = render(() => <GuestDrawer guest={makeGuest()} onClose={vi.fn()} />);
const panels = container.querySelectorAll('[style*="overflow-anchor"]');
@@ -580,6 +600,8 @@ describe('GuestDrawer', () => {
<GuestDrawer
guest={makeGuest({
workloadType: 'app-container',
+ type: 'app-container',
+ platformType: 'docker',
dockerHostId: 'dh-1',
id: 'container-abc',
})}
@@ -591,6 +613,48 @@ describe('GuestDrawer', () => {
expect(screen.getByTestId('disc-resource-id').textContent).toBe('container-abc');
});
+ it('does not render DiscoveryTab for TrueNAS app-containers without explicit discovery support', () => {
+ render(() => (
+ <GuestDrawer
+ guest={makeGuest({
+ id: 'app-container:truenas-main:nextcloud',
+ workloadType: 'app-container',
+ type: 'app-container',
+ platformType: 'truenas',
+ dockerHostId: '',
+ node: 'truenas-main',
+ instance: 'truenas-main',
+ })}
+ onClose={vi.fn()}
+ />
+ ));
+ expect(screen.queryByTestId('discovery-tab')).toBeNull();
+ });
+
+ it('honors explicit canonical discovery targets for API-backed app-containers', () => {
+ render(() => (
+ <GuestDrawer
+ guest={makeGuest({
+ id: 'app-container:truenas-main:nextcloud',
+ workloadType: 'app-container',
+ type: 'app-container',
+ platformType: 'truenas',
+ dockerHostId: '',
+ discoveryTarget: {
+ resourceType: 'app-container',
+ agentId: 'truenas-helper',
+ resourceId: 'nextcloud',
+ },
+ })}
+ onClose={vi.fn()}
+ />
+ ));
+ expect(screen.getByText('Discovery')).toBeInTheDocument();
+ expect(screen.getByTestId('disc-resource-type').textContent).toBe('app-container');
+ expect(screen.getByTestId('disc-agent-id').textContent).toBe('truenas-helper');
+ expect(screen.getByTestId('disc-resource-id').textContent).toBe('nextcloud');
+ });
+
it('passes canonical pod resourceType for pod workloads', () => {
render(() => (
<GuestDrawer
diff --git a/frontend-modern/src/components/Dashboard/GuestDrawer.tsx b/frontend-modern/src/components/Dashboard/GuestDrawer.tsx
index d1733d3faf..a77c6bac6d 100644
--- a/frontend-modern/src/components/Dashboard/GuestDrawer.tsx
+++ b/frontend-modern/src/components/Dashboard/GuestDrawer.tsx
@@ -18,6 +18,7 @@ export const GuestDrawer: Component<GuestDrawerProps> = (props) => {
discoveryResourceType,
guestId,
hasAgentInfo,
+ hasDiscoverySupport,
hasFilesystemDetails,
hasNetworkInterfaces,
hasOsInfo,
@@ -46,17 +47,21 @@ export const GuestDrawer: Component<GuestDrawerProps> = (props) => {
<div class="absolute bottom-0 left-0 right-0 h-0.5 bg-blue-600 dark:bg-blue-400 rounded-t-full" />
)}
</button>
- <button
- onClick={() => switchTab('discovery')}
- class={`pb-2 text-sm font-medium transition-colors relative ${
- activeTab() === 'discovery' ? 'text-blue-600 dark:text-blue-400' : ' hover:text-muted'
- }`}
- >
- Discovery
- {activeTab() === 'discovery' && (
- <div class="absolute bottom-0 left-0 right-0 h-0.5 bg-blue-600 dark:bg-blue-400 rounded-t-full" />
- )}
- </button>
+ {hasDiscoverySupport() && (
+ <button
+ onClick={() => switchTab('discovery')}
+ class={`pb-2 text-sm font-medium transition-colors relative ${
+ activeTab() === 'discovery'
+ ? 'text-blue-600 dark:text-blue-400'
+ : ' hover:text-muted'
+ }`}
+ >
+ Discovery
+ {activeTab() === 'discovery' && (
+ <div class="absolute bottom-0 left-0 right-0 h-0.5 bg-blue-600 dark:bg-blue-400 rounded-t-full" />
+ )}
+ </button>
+ )}
</div>
<div class="flex justify-end">
<button
@@ -95,26 +100,28 @@ export const GuestDrawer: Component<GuestDrawerProps> = (props) => {
{/* Always rendered, hidden via CSS. Wrapped in a local Suspense
so DiscoveryTab's createResource loading state doesn't bubble
up to the app-level Suspense and replace the entire page. */}
- <div
- class={activeTab() === 'discovery' ? '' : 'hidden'}
- style={{ 'overflow-anchor': 'none' }}
- >
- <Suspense
- fallback={
- <div class="flex items-center justify-center py-8">
- <div class="animate-spin h-6 w-6 border-2 border-blue-500 border-t-transparent rounded-full" />
- <span class="ml-2 text-sm text-muted">{discoveryLoadingState.text}</span>
- </div>
- }
+ {hasDiscoverySupport() && (
+ <div
+ class={activeTab() === 'discovery' ? '' : 'hidden'}
+ style={{ 'overflow-anchor': 'none' }}
>
- <DiscoveryTab
- resourceType={discoveryResourceType()}
- agentId={discoveryAgentId()}
- resourceId={discoveryResourceId()}
- hostname={props.guest.name}
- />
- </Suspense>
- </div>
+ <Suspense
+ fallback={
+ <div class="flex items-center justify-center py-8">
+ <div class="animate-spin h-6 w-6 border-2 border-blue-500 border-t-transparent rounded-full" />
+ <span class="ml-2 text-sm text-muted">{discoveryLoadingState.text}</span>
+ </div>
+ }
+ >
+ <DiscoveryTab
+ resourceType={discoveryResourceType()!}
+ agentId={discoveryAgentId()}
+ resourceId={discoveryResourceId()}
+ hostname={props.guest.name}
+ />
+ </Suspense>
+ </div>
+ )}
</div>
);
};
diff --git a/frontend-modern/src/components/Dashboard/__tests__/workloadTopology.test.ts b/frontend-modern/src/components/Dashboard/__tests__/workloadTopology.test.ts
index a3dcfd043f..0080176cea 100644
--- a/frontend-modern/src/components/Dashboard/__tests__/workloadTopology.test.ts
+++ b/frontend-modern/src/components/Dashboard/__tests__/workloadTopology.test.ts
@@ -182,6 +182,18 @@ describe('workloadTopology', () => {
node: 'truenas-main',
instance: 'truenas-main',
});
+ const truenasWithExplicitDiscovery = makeGuest(2, {
+ id: 'app-container:truenas-main:nextcloud',
+ type: 'app-container',
+ workloadType: 'app-container',
+ platformType: 'truenas',
+ dockerHostId: '',
+ discoveryTarget: {
+ resourceType: 'app-container',
+ agentId: 'truenas-helper',
+ resourceId: 'nextcloud',
+ },
+ });
const k8s = makeGuest(3, {
id: 'k8s:cluster-a:pod:pod-uid-1',
type: 'pod',
@@ -202,10 +214,10 @@ describe('workloadTopology', () => {
expect(getDiscoveryResourceIdForWorkload(docker)).toBe(
'app-container:docker-host-1:container-abc123',
);
- expect(getDiscoveryHostIdForWorkload(truenas)).toBe('truenas-main');
- expect(getDiscoveryResourceIdForWorkload(truenas)).toBe(
- 'app-container:truenas-main:nextcloud',
- );
+ expect(getDiscoveryHostIdForWorkload(truenas)).toBe('');
+ expect(getDiscoveryResourceIdForWorkload(truenas)).toBe('');
+ expect(getDiscoveryHostIdForWorkload(truenasWithExplicitDiscovery)).toBe('truenas-helper');
+ expect(getDiscoveryResourceIdForWorkload(truenasWithExplicitDiscovery)).toBe('nextcloud');
expect(getDiscoveryHostIdForWorkload(k8s)).toBe('k8s-agent-1');
expect(getDiscoveryResourceIdForWorkload(k8s)).toBe('pod-uid-1');
diff --git a/frontend-modern/src/components/Dashboard/useGuestDrawerState.ts b/frontend-modern/src/components/Dashboard/useGuestDrawerState.ts
index b64e07efe0..c258a96810 100644
--- a/frontend-modern/src/components/Dashboard/useGuestDrawerState.ts
+++ b/frontend-modern/src/components/Dashboard/useGuestDrawerState.ts
@@ -1,9 +1,10 @@
-import { createMemo, createSignal } from 'solid-js';
+import { createEffect, createMemo, createSignal } from 'solid-js';
import { getDiscoveryLoadingState } from '@/utils/discoveryPresentation';
import {
getCanonicalWorkloadId,
getDiscoveryResourceTypeForWorkload,
+ hasDiscoverySupportForWorkload,
getWebInterfaceTargetLabelForWorkload,
} from '@/utils/workloads';
import { buildInfrastructureHrefForWorkload } from '@/routing/resourceLinks';
@@ -51,6 +52,7 @@ export function useGuestDrawerState(props: GuestDrawerProps) {
const backupPresentation = createMemo(() =>
props.guest.lastBackup ? getGuestDrawerBackupPresentation(props.guest.lastBackup) : null,
);
+ const hasDiscoverySupport = createMemo(() => hasDiscoverySupportForWorkload(props.guest));
const discoveryAgentId = createMemo(() => getDiscoveryHostIdForWorkload(props.guest));
const discoveryResourceId = createMemo(() => getDiscoveryResourceIdForWorkload(props.guest));
const discoveryResourceType = createMemo(() =>
@@ -64,6 +66,12 @@ export function useGuestDrawerState(props: GuestDrawerProps) {
setActiveTab(tab);
};
+ createEffect(() => {
+ if (activeTab() === 'discovery' && !hasDiscoverySupport()) {
+ setActiveTab('overview');
+ }
+ });
+
return {
activeTab,
agentLabel,
@@ -76,6 +84,7 @@ export function useGuestDrawerState(props: GuestDrawerProps) {
guestId,
guestOsSummary,
hasAgentInfo,
+ hasDiscoverySupport,
hasFilesystemDetails,
hasNetworkInterfaces,
hasOsInfo,
diff --git a/frontend-modern/src/components/Dashboard/workloadTopology.ts b/frontend-modern/src/components/Dashboard/workloadTopology.ts
index 69e9577866..b160e36332 100644
--- a/frontend-modern/src/components/Dashboard/workloadTopology.ts
+++ b/frontend-modern/src/components/Dashboard/workloadTopology.ts
@@ -1,6 +1,10 @@
import type { Node } from '@/types/api';
import type { WorkloadGuest } from '@/types/workloads';
-import { getCanonicalWorkloadId, resolveWorkloadType } from '@/utils/workloads';
+import {
+ getCanonicalWorkloadId,
+ resolveDiscoveryTargetForWorkload,
+ resolveWorkloadType,
+} from '@/utils/workloads';
export const workloadNodeScopeId = (guest: WorkloadGuest): string =>
`${(guest.instance || '').trim()}-${(guest.node || '').trim()}`;
@@ -29,30 +33,11 @@ export const getWorkloadContainerHostId = (guest: WorkloadGuest): string => {
};
export const getDiscoveryHostIdForWorkload = (guest: WorkloadGuest): string => {
- const type = resolveWorkloadType(guest);
- if (type === 'app-container') {
- return getWorkloadContainerHostId(guest);
- }
- if (type === 'pod') {
- return (guest.kubernetesAgentId || guest.instance || guest.node || '').trim();
- }
- return (guest.node || '').trim();
+ return resolveDiscoveryTargetForWorkload(guest)?.agentId || '';
};
export const getDiscoveryResourceIdForWorkload = (guest: WorkloadGuest): string => {
- const type = resolveWorkloadType(guest);
- if (type === 'app-container') {
- return (guest.id || '').trim();
- }
- if (type === 'pod') {
- const rawId = (guest.id || '').trim();
- const match = rawId.match(/^k8s:[^:]+:pod:(.+)$/);
- return (match?.[1] || rawId || String(guest.vmid)).trim();
- }
- if (Number.isFinite(guest.vmid) && guest.vmid > 0) {
- return String(guest.vmid);
- }
- return (guest.id || '').trim();
+ return resolveDiscoveryTargetForWorkload(guest)?.resourceId || '';
};
export const buildNodeByInstance = (nodes: Node[]): Record<string, Node> => {
diff --git a/frontend-modern/src/hooks/__tests__/useWorkloads.test.ts b/frontend-modern/src/hooks/__tests__/useWorkloads.test.ts
index b5d2c871a2..9f50d95818 100644
--- a/frontend-modern/src/hooks/__tests__/useWorkloads.test.ts
+++ b/frontend-modern/src/hooks/__tests__/useWorkloads.test.ts
@@ -177,6 +177,52 @@ describe('useWorkloads', () => {
dispose();
});
+ it('preserves canonical discovery targets for workloads instead of inferring them from platform type', async () => {
+ apiFetchJSONMock.mockResolvedValueOnce({
+ data: [
+ {
+ ...sampleResource,
+ id: 'app-container:truenas-main:nextcloud',
+ type: 'app-container',
+ name: 'nextcloud',
+ status: 'healthy',
+ sources: ['agent', 'truenas'],
+ parentName: 'truenas-main',
+ discoveryTarget: {
+ resourceType: 'app-container',
+ agentId: 'truenas-helper',
+ resourceId: 'nextcloud',
+ },
+ docker: {
+ containerId: 'nextcloud-ctr',
+ image: 'ix-nextcloud:latest',
+ },
+ },
+ ],
+ meta: { totalPages: 1 },
+ });
+
+ let dispose = () => {};
+ let result: ReturnType<UseWorkloadsModule['useWorkloads']> | undefined;
+ createRoot((d) => {
+ dispose = d;
+ const [enabled] = createSignal(true);
+ result = useWorkloads(enabled);
+ });
+
+ await flushAsync();
+ await waitForWorkloadCount(() => result!.workloads().length, 1);
+
+ expect(result!.workloads()[0]?.discoveryTarget).toEqual({
+ resourceType: 'app-container',
+ agentId: 'truenas-helper',
+ resourceId: 'nextcloud',
+ hostname: undefined,
+ });
+
+ dispose();
+ });
+
it('uses the canonical Kubernetes cluster name for pod context labels', async () => {
apiFetchJSONMock.mockResolvedValueOnce({
data: [
diff --git a/frontend-modern/src/hooks/useWorkloads.ts b/frontend-modern/src/hooks/useWorkloads.ts
index 1c909a3722..dccaeeff3f 100644
--- a/frontend-modern/src/hooks/useWorkloads.ts
+++ b/frontend-modern/src/hooks/useWorkloads.ts
@@ -10,6 +10,7 @@ import { apiFetchJSON, getOrgID } from '@/utils/apiClient';
import { normalizeOrgScope } from '@/utils/orgScope';
import { eventBus } from '@/stores/events';
import { resolvePlatformTypeFromSources } from '@/utils/sourcePlatforms';
+import { canonicalDiscoveryResourceType } from '@/utils/discoveryTarget';
import { normalizeDiskArray } from '@/utils/format';
import { isDockerManagedAppContainer, resolveWorkloadTypeFromString } from '@/utils/workloads';
import {
@@ -17,6 +18,7 @@ import {
getPreferredResourceKubernetesContext,
} from '@/utils/resourceIdentity';
import type { WorkloadGuest } from '@/types/workloads';
+import type { ResourceDiscoveryTarget } from '@/types/resource';
const WORKLOADS_URL = '/api/resources?type=vm,system-container,app-container,pod';
const WORKLOADS_PAGE_LIMIT = 200;
@@ -123,6 +125,12 @@ type APIResource = {
image?: string;
uptimeSeconds?: number;
};
+ discoveryTarget?: {
+ resourceType?: string;
+ agentId?: string;
+ resourceId?: string;
+ hostname?: string;
+ };
};
type APIListResponse = {
@@ -278,6 +286,34 @@ const resolveWorkloadsPayload = (payload: unknown): { data: APIResource[]; total
const buildWorkloadsUrl = (page: number) =>
`${WORKLOADS_URL}&page=${page}&limit=${WORKLOADS_PAGE_LIMIT}`;
+const resolveWorkloadDiscoveryTarget = (
+ resource: APIResource,
+): ResourceDiscoveryTarget | undefined => {
+ const explicit = resource.discoveryTarget;
+ if (!explicit) return undefined;
+ const resourceType = canonicalDiscoveryResourceType(explicit.resourceType);
+ const agentId = (explicit.agentId || '').trim();
+ const resourceId = (explicit.resourceId || '').trim();
+ if (!resourceType || !agentId || !resourceId) return undefined;
+ switch (resourceType) {
+ case 'agent':
+ case 'vm':
+ case 'system-container':
+ case 'app-container':
+ case 'pod':
+ case 'disk':
+ case 'ceph':
+ return {
+ resourceType,
+ agentId,
+ resourceId,
+ hostname: explicit.hostname,
+ };
+ default:
+ return undefined;
+ }
+};
+
const mapResourceToWorkload = (resource: APIResource): WorkloadGuest | null => {
const workloadType = resolveWorkloadType(resource.type);
if (!workloadType) return null;
@@ -321,6 +357,7 @@ const mapResourceToWorkload = (resource: APIResource): WorkloadGuest | null => {
const appContainerRuntimeId =
workloadType === 'app-container' ? (resource.docker?.containerId || '').trim() : '';
+ const discoveryTarget = resolveWorkloadDiscoveryTarget(resource);
// Keep workload identity on the canonical unified resource id. Platform-native
// runtime ids such as Docker container ids must stay separate so workload
@@ -435,6 +472,7 @@ const mapResourceToWorkload = (resource: APIResource): WorkloadGuest | null => {
dockerHostId: dockerManagedAppContainer ? resource.docker?.hostSourceId : undefined,
kubernetesAgentId: workloadType === 'pod' ? resource.kubernetes?.agentId : undefined,
platformType,
+ discoveryTarget,
};
};
diff --git a/frontend-modern/src/types/workloads.ts b/frontend-modern/src/types/workloads.ts
index a5d58266ca..410f3dc523 100644
--- a/frontend-modern/src/types/workloads.ts
+++ b/frontend-modern/src/types/workloads.ts
@@ -1,4 +1,5 @@
import type { VM, Container, DockerContainerUpdateStatus } from './api';
+import type { ResourceDiscoveryTarget } from './resource';
export type WorkloadType = 'vm' | 'system-container' | 'app-container' | 'pod';
export type ViewMode = 'all' | WorkloadType;
@@ -24,4 +25,7 @@ export type WorkloadGuest = (VM | Container) & {
dockerHostId?: string;
// Kubernetes agent ID (when available) — preferred for actionable operations.
kubernetesAgentId?: string;
+ // Canonical discovery ownership. API-backed platforms such as TrueNAS should
+ // only expose Discovery when the unified resource contract supplies a target.
+ discoveryTarget?: ResourceDiscoveryTarget;
};
diff --git a/frontend-modern/src/utils/__tests__/workloads.test.ts b/frontend-modern/src/utils/__tests__/workloads.test.ts
index 4072ae0ad2..507510a74b 100644
--- a/frontend-modern/src/utils/__tests__/workloads.test.ts
+++ b/frontend-modern/src/utils/__tests__/workloads.test.ts
@@ -231,20 +231,43 @@ describe('isDockerManagedAppContainer', () => {
});
describe('getDiscoveryResourceTypeForWorkload', () => {
- it('returns the canonical discovery resource type for VM workloads', () => {
- const guest = { workloadType: 'vm' as const, type: 'vm' };
+ it('returns the canonical discovery resource type for VM workloads with canonical coordinates', () => {
+ const guest = { workloadType: 'vm' as const, type: 'vm', node: 'pve1', vmid: 101 };
expect(getDiscoveryResourceTypeForWorkload(guest)).toBe('vm');
});
- it('returns the canonical discovery resource type for app-container workloads', () => {
- const guest = { workloadType: 'app-container' as const, type: 'docker' };
+ it('returns the canonical discovery resource type for Docker-managed app-container workloads', () => {
+ const guest = {
+ workloadType: 'app-container' as const,
+ type: 'app-container',
+ platformType: 'docker',
+ dockerHostId: 'docker-host-1',
+ id: 'app-container:docker-host-1:grafana',
+ };
expect(getDiscoveryResourceTypeForWorkload(guest)).toBe('app-container');
});
- it('returns the canonical discovery resource type for pod workloads', () => {
- const guest = { workloadType: 'pod' as const, type: 'k8s' };
+ it('returns the canonical discovery resource type for pod workloads with agent ownership', () => {
+ const guest = {
+ workloadType: 'pod' as const,
+ type: 'k8s',
+ kubernetesAgentId: 'k8s-agent-1',
+ id: 'k8s:cluster-a:pod:pod-uid-1',
+ };
expect(getDiscoveryResourceTypeForWorkload(guest)).toBe('pod');
});
+
+ it('returns null for API-backed TrueNAS app-containers without canonical discovery ownership', () => {
+ const guest = {
+ workloadType: 'app-container' as const,
+ type: 'app-container',
+ platformType: 'truenas',
+ id: 'app-container:truenas-main:nextcloud',
+ node: 'truenas-main',
+ instance: 'truenas-main',
+ };
+ expect(getDiscoveryResourceTypeForWorkload(guest)).toBeNull();
+ });
});
describe('getWebInterfaceTargetLabelForWorkload', () => {
diff --git a/frontend-modern/src/utils/workloads.ts b/frontend-modern/src/utils/workloads.ts
index 056a366473..2b2197c400 100644
--- a/frontend-modern/src/utils/workloads.ts
+++ b/frontend-modern/src/utils/workloads.ts
@@ -1,7 +1,9 @@
import type { WorkloadGuest, WorkloadType, ViewMode } from '@/types/workloads';
import type { ResourceType as DiscoveryResourceType } from '@/types/discovery';
+import type { ResourceDiscoveryTarget } from '@/types/resource';
import type { MetricResourceKind } from '@/utils/metricsKeys';
import { canonicalizeFrontendResourceType } from '@/utils/resourceTypeCompat';
+import { canonicalDiscoveryResourceType } from '@/utils/discoveryTarget';
/**
* Resolve a raw type string (from API or backend) to a semantic WorkloadType.
@@ -104,9 +106,96 @@ export const getCanonicalWorkloadId = (
return guest.id;
};
+export const resolveDiscoveryTargetForWorkload = (
+ guest: Pick<
+ WorkloadGuest,
+ | 'discoveryTarget'
+ | 'workloadType'
+ | 'type'
+ | 'platformType'
+ | 'containerRuntime'
+ | 'dockerHostId'
+ | 'kubernetesAgentId'
+ | 'instance'
+ | 'node'
+ | 'vmid'
+ | 'id'
+ >,
+): ResourceDiscoveryTarget | null => {
+ const explicit = guest.discoveryTarget;
+ if (explicit) {
+ const resourceType = canonicalDiscoveryResourceType(explicit.resourceType);
+ const agentId = (explicit.agentId || '').trim();
+ const resourceId = (explicit.resourceId || '').trim();
+ if (resourceType && agentId && resourceId) {
+ return {
+ resourceType: resourceType as ResourceDiscoveryTarget['resourceType'],
+ agentId,
+ resourceId,
+ hostname: explicit.hostname,
+ };
+ }
+ }
+
+ const type = resolveWorkloadType(guest);
+ if (type === 'app-container') {
+ if (!isDockerManagedAppContainer(guest)) return null;
+ const agentId = (guest.dockerHostId || '').trim();
+ const resourceId = (guest.id || '').trim();
+ return agentId && resourceId
+ ? { resourceType: 'app-container', agentId, resourceId }
+ : null;
+ }
+ if (type === 'pod') {
+ const agentId = (guest.kubernetesAgentId || guest.instance || guest.node || '').trim();
+ const rawId = (guest.id || '').trim();
+ const match = rawId.match(/^k8s:[^:]+:pod:(.+)$/);
+ const resourceId = (match?.[1] || rawId).trim();
+ return agentId && resourceId ? { resourceType: 'pod', agentId, resourceId } : null;
+ }
+ if ((type === 'vm' || type === 'system-container') && Number.isFinite(guest.vmid) && guest.vmid > 0) {
+ const agentId = (guest.node || '').trim();
+ const resourceId = String(guest.vmid);
+ return agentId ? { resourceType: type, agentId, resourceId } : null;
+ }
+ return null;
+};
+
export const getDiscoveryResourceTypeForWorkload = (
- guest: Pick<WorkloadGuest, 'workloadType' | 'type'>,
-): DiscoveryResourceType => resolveWorkloadType(guest) as DiscoveryResourceType;
+ guest: Pick<
+ WorkloadGuest,
+ | 'discoveryTarget'
+ | 'workloadType'
+ | 'type'
+ | 'platformType'
+ | 'containerRuntime'
+ | 'dockerHostId'
+ | 'kubernetesAgentId'
+ | 'instance'
+ | 'node'
+ | 'vmid'
+ | 'id'
+ >,
+): DiscoveryResourceType | null =>
+ (resolveDiscoveryTargetForWorkload(guest)?.resourceType as DiscoveryResourceType | undefined) ??
+ null;
+
+export const hasDiscoverySupportForWorkload = (
+ guest: Pick<
+ WorkloadGuest,
+ | 'discoveryTarget'
+ | 'workloadType'
+ | 'type'
+ | 'platformType'
+ | 'containerRuntime'
+ | 'dockerHostId'
+ | 'kubernetesAgentId'
+ | 'instance'
+ | 'node'
+ | 'vmid'
+ | 'id'
+ >,
+): boolean => Boolean(resolveDiscoveryTargetForWorkload(guest));
export const getWebInterfaceTargetLabelForWorkload = (
guest: Pick<WorkloadGuest, 'workloadType' | 'type'>,
diff --git a/tests/integration/tests/32-truenas-workloads-resource-deeplink.spec.ts b/tests/integration/tests/32-truenas-workloads-resource-deeplink.spec.ts
index 26d1721e2a..8f9d76de6a 100644
--- a/tests/integration/tests/32-truenas-workloads-resource-deeplink.spec.ts
+++ b/tests/integration/tests/32-truenas-workloads-resource-deeplink.spec.ts
@@ -121,6 +121,7 @@ test.describe('TrueNAS workloads resource deep links', () => {
).toContainText('nextcloud');
await expect(page.getByText('Open related infrastructure', { exact: true })).toBeVisible();
await expect(page.getByRole('button', { name: 'Overview' })).toBeVisible();
+ await expect(page.getByRole('button', { name: 'Discovery' })).toHaveCount(0);
await page.screenshot({ path: SCREENSHOT_PATH, fullPage: true });
});