feat: native Headlamp integration, TrueNAS API, docs, and CI for v0.2.0

Native Headlamp integrations:
- registerResourceTableColumnsProcessor: add Protocol/Pool/Server columns to
  native StorageClass table and Protocol/Volume Handle to PV table
- registerDetailsViewSection: inject TNS-CSI section into PV detail pages
- registerDetailsViewSection: inject driver role/status into tns-csi Pod pages
- registerDetailsViewHeaderAction: Benchmark shortcut on StorageClass detail
- registerAppBarAction: driver health badge (N/Nc M/Mn, color-coded)
- Trim sidebar from 6 → 4 entries (Overview, Snapshots, Metrics, Benchmark)

TrueNAS API integration:
- src/api/truenas.ts: ConfigStore-backed settings, WebSocket JSON-RPC client
  for pool.query (auth.login_with_api_key + pool.query)
- src/components/TnsCsiSettings.tsx: API key + server override settings UI
  with connection test button
- TnsCsiDataContext: fetch real pool stats (size/allocated/free/status)
- OverviewPage: three-tier pool capacity display (real data → error → metrics
  fallback)

Documentation:
- README, CHANGELOG, CONTRIBUTING, SECURITY
- docs/: architecture, deployment (Helm), getting-started, user-guide,
  troubleshooting

CI:
- .github/workflows/ci.yaml: lint + type-check + test on PR/push
- .github/workflows/release.yaml: workflow_dispatch versioned release

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
2026-02-18 16:37:56 -05:00
parent f2f3c3a87e
commit f1feb5c2f7
30 changed files with 3540 additions and 44 deletions
+6
View File
@@ -20,6 +20,12 @@ vi.mock('@kinvolk/headlamp-plugin/lib', () => ({
},
},
},
ConfigStore: class {
get() { return {}; }
set() {}
update() {}
useConfig() { return () => ({}); }
},
}));
import { TnsCsiDataProvider, useTnsCsiContext } from './TnsCsiDataContext';
+55 -4
View File
@@ -22,6 +22,7 @@ import {
VolumeSnapshot,
VolumeSnapshotClass,
} from './k8s';
import { fetchTruenasPoolStats, getTnsCsiConfig, PoolStats } from './truenas';
// ---------------------------------------------------------------------------
// Context shape
@@ -46,6 +47,10 @@ export interface TnsCsiContextValue {
volumeSnapshotClasses: VolumeSnapshotClass[];
snapshotCrdAvailable: boolean;
// TrueNAS pool capacity (only populated when API key is configured)
poolStats: PoolStats[];
poolStatsError: string | null;
// Loading / error state
loading: boolean;
error: string | null;
@@ -88,6 +93,8 @@ export function TnsCsiDataProvider({ children }: { children: React.ReactNode })
const [asyncLoading, setAsyncLoading] = useState(true);
const [asyncError, setAsyncError] = useState<string | null>(null);
const [refreshKey, setRefreshKey] = useState(0);
const [poolStats, setPoolStats] = useState<PoolStats[]>([]);
const [poolStatsError, setPoolStatsError] = useState<string | null>(null);
const refresh = useCallback(() => {
setRefreshKey(k => k + 1);
@@ -161,7 +168,32 @@ export function TnsCsiDataProvider({ children }: { children: React.ReactNode })
setVolumeSnapshots([]);
}
}
} catch (err: unknown) {
// TrueNAS pool stats (only when API key is configured)
const config = getTnsCsiConfig();
if (config.truenasApiKey.trim()) {
// Determine server: explicit override → first SC server param → fail gracefully
const server = config.truenasServerOverride.trim();
if (server) {
try {
const pools = await fetchTruenasPoolStats(server, config.truenasApiKey.trim());
if (!cancelled) {
setPoolStats(pools);
setPoolStatsError(null);
}
} catch (err: unknown) {
if (!cancelled) {
setPoolStats([]);
setPoolStatsError(err instanceof Error ? err.message : String(err));
}
}
}
} else {
if (!cancelled) {
setPoolStats([]);
setPoolStatsError(null);
}
}
} catch (err: unknown) {
if (!cancelled) {
setAsyncError(err instanceof Error ? err.message : String(err));
}
@@ -178,19 +210,34 @@ export function TnsCsiDataProvider({ children }: { children: React.ReactNode })
// Derived / filtered values — memoized to avoid recomputation on every render
// ---------------------------------------------------------------------------
// Headlamp useList() returns KubeObject class instances that store raw Kubernetes
// JSON under `.jsonData`. Direct property access only works for fields that have
// explicit getter definitions in the class (e.g. provisioner, reclaimPolicy).
// Fields like `parameters`, `spec`, `status` must be read from `.jsonData`.
// We extract jsonData here so our plain-object type helpers work correctly.
const extractJsonData = (items: unknown[]): unknown[] =>
items.map(item =>
item && typeof item === 'object' && 'jsonData' in item
? (item as { jsonData: unknown }).jsonData
: item
);
const storageClasses = useMemo(() => {
if (!allStorageClasses) return [];
return filterTnsCsiStorageClasses(allStorageClasses as unknown[]);
return filterTnsCsiStorageClasses(extractJsonData(allStorageClasses as unknown[]));
}, [allStorageClasses]);
const persistentVolumes = useMemo(() => {
if (!allPvs) return [];
return filterTnsCsiPersistentVolumes(allPvs as unknown[]);
return filterTnsCsiPersistentVolumes(extractJsonData(allPvs as unknown[]));
}, [allPvs]);
const persistentVolumeClaims = useMemo(() => {
if (!allPvcs || persistentVolumes.length === 0) return [];
return filterTnsCsiPVCs(allPvcs as TnsCsiPersistentVolumeClaim[], persistentVolumes);
return filterTnsCsiPVCs(
extractJsonData(allPvcs as unknown[]) as TnsCsiPersistentVolumeClaim[],
persistentVolumes
);
}, [allPvcs, persistentVolumes]);
// ---------------------------------------------------------------------------
@@ -224,6 +271,8 @@ export function TnsCsiDataProvider({ children }: { children: React.ReactNode })
volumeSnapshots,
volumeSnapshotClasses,
snapshotCrdAvailable,
poolStats,
poolStatsError,
loading,
error,
refresh,
@@ -239,6 +288,8 @@ export function TnsCsiDataProvider({ children }: { children: React.ReactNode })
volumeSnapshots,
volumeSnapshotClasses,
snapshotCrdAvailable,
poolStats,
poolStatsError,
loading,
error,
refresh,
+176
View File
@@ -0,0 +1,176 @@
/**
* TrueNAS API client for the headlamp-tns-csi-plugin.
*
* Uses the TrueNAS WebSocket JSON-RPC 2.0 API to fetch pool-level capacity
* information (pool.query). Requires a TrueNAS API key configured by the user
* in plugin settings.
*
* The WebSocket connects directly from the browser to the TrueNAS server.
* The server address comes from the StorageClass parameters (already in context).
*
* All operations are read-only (pool.query only).
*/
import { ConfigStore } from '@kinvolk/headlamp-plugin/lib';
// ---------------------------------------------------------------------------
// Config store — persists across sessions via Headlamp Redux store
// ---------------------------------------------------------------------------
export interface TnsCsiConfig {
truenasApiKey: string;
/** Override server address (defaults to StorageClass parameter 'server') */
truenasServerOverride: string;
}
const DEFAULT_CONFIG: TnsCsiConfig = {
truenasApiKey: '',
truenasServerOverride: '',
};
const configStore = new ConfigStore<TnsCsiConfig>('headlamp-tns-csi-plugin');
export function getTnsCsiConfig(): TnsCsiConfig {
return { ...DEFAULT_CONFIG, ...configStore.get() };
}
export function setTnsCsiConfig(partial: Partial<TnsCsiConfig>): void {
configStore.update(partial);
}
export function useTnsCsiConfig(): () => TnsCsiConfig {
return configStore.useConfig();
}
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface PoolStats {
name: string;
/** Total pool capacity in bytes */
size: number;
/** Allocated (used) bytes */
allocated: number;
/** Free bytes */
free: number;
/** Pool health status string */
status: string;
}
// ---------------------------------------------------------------------------
// TrueNAS WebSocket JSON-RPC client
// ---------------------------------------------------------------------------
/**
* Opens a WebSocket to TrueNAS, authenticates with the API key, calls
* pool.query, collects results, and closes the connection.
*
* @param server - TrueNAS host/IP (no protocol prefix)
* @param apiKey - TrueNAS API key
* @returns Array of pool stats
*/
export function fetchTruenasPoolStats(
server: string,
apiKey: string
): Promise<PoolStats[]> {
return new Promise((resolve, reject) => {
// TrueNAS WebSocket endpoint — supports both SCALE and CORE
const url = `wss://${server}/api/current`;
let ws: WebSocket;
const timeout = setTimeout(() => {
ws?.close();
reject(new Error('TrueNAS connection timed out (10s)'));
}, 10_000);
try {
ws = new WebSocket(url);
} catch (err) {
clearTimeout(timeout);
reject(new Error(`Failed to open WebSocket to ${server}: ${String(err)}`));
return;
}
let msgId = 1;
// State machine: connect → authenticate → query → done
type Phase = 'connecting' | 'authenticating' | 'querying' | 'done';
let phase: Phase = 'connecting';
ws.onopen = () => {
phase = 'authenticating';
ws.send(JSON.stringify({
id: msgId++,
msg: 'method',
method: 'auth.login_with_api_key',
params: [apiKey],
}));
};
ws.onmessage = (event: MessageEvent) => {
let msg: Record<string, unknown>;
try {
msg = JSON.parse(event.data as string) as Record<string, unknown>;
} catch {
return;
}
if (phase === 'authenticating') {
const result = msg['result'];
if (result !== true) {
clearTimeout(timeout);
ws.close();
reject(new Error('TrueNAS authentication failed — check your API key'));
return;
}
phase = 'querying';
ws.send(JSON.stringify({
id: msgId++,
msg: 'method',
method: 'pool.query',
params: [],
}));
return;
}
if (phase === 'querying') {
const result = msg['result'];
if (!Array.isArray(result)) {
clearTimeout(timeout);
ws.close();
reject(new Error('pool.query returned unexpected result'));
return;
}
phase = 'done';
clearTimeout(timeout);
ws.close();
const pools: PoolStats[] = result.map((pool: unknown) => {
const p = pool as Record<string, unknown>;
return {
name: String(p['name'] ?? ''),
size: Number(p['size'] ?? 0),
allocated: Number(p['allocated'] ?? 0),
free: Number(p['free'] ?? 0),
status: String(p['status'] ?? 'UNKNOWN'),
};
});
resolve(pools);
}
};
ws.onerror = () => {
if (phase !== 'done') {
clearTimeout(timeout);
reject(new Error(`WebSocket error connecting to ${server} — check the server address and that TrueNAS is reachable`));
}
};
ws.onclose = (event: CloseEvent) => {
if (phase !== 'done') {
clearTimeout(timeout);
reject(new Error(`WebSocket closed unexpectedly (code ${event.code}) while ${phase}`));
}
};
});
}