7997eb29fa
The plugin now fetches audit data from the Polaris dashboard service via the Kubernetes service proxy instead of reading from a ConfigMap. This works with the standard Polaris dashboard deployment without requiring additional configuration. - Replace ConfigMap.useGet with ApiProxy.request to /results.json - Compute score from result counts (pass/total) since the API response doesn't include a pre-computed score - Update error messages for service proxy context - Update CLAUDE.md to reflect new data source Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
184 lines
4.5 KiB
TypeScript
184 lines
4.5 KiB
TypeScript
import { ApiProxy } from '@kinvolk/headlamp-plugin/lib';
|
|
import React from 'react';
|
|
|
|
// --- Polaris AuditData schema (matches pkg/validator/output.go) ---
|
|
|
|
type Severity = 'ignore' | 'warning' | 'danger';
|
|
|
|
interface ResultMessage {
|
|
ID: string;
|
|
Message: string;
|
|
Details: string[];
|
|
Success: boolean;
|
|
Severity: Severity;
|
|
Category: string;
|
|
}
|
|
|
|
type ResultSet = Record<string, ResultMessage>;
|
|
|
|
interface ContainerResult {
|
|
Name: string;
|
|
Results: ResultSet;
|
|
}
|
|
|
|
interface PodResult {
|
|
Name: string;
|
|
Results: ResultSet;
|
|
ContainerResults: ContainerResult[];
|
|
}
|
|
|
|
export interface Result {
|
|
Name: string;
|
|
Namespace: string;
|
|
Kind: string;
|
|
Results: ResultSet;
|
|
PodResult?: PodResult;
|
|
CreatedTime: string;
|
|
}
|
|
|
|
interface ClusterInfo {
|
|
Version: string;
|
|
Nodes: number;
|
|
Pods: number;
|
|
Namespaces: number;
|
|
Controllers: number;
|
|
}
|
|
|
|
export interface AuditData {
|
|
PolarisOutputVersion: string;
|
|
AuditTime: string;
|
|
SourceType: string;
|
|
SourceName: string;
|
|
DisplayName: string;
|
|
ClusterInfo: ClusterInfo;
|
|
Results: Result[];
|
|
}
|
|
|
|
// --- Result counting ---
|
|
|
|
export interface ResultCounts {
|
|
total: number;
|
|
pass: number;
|
|
warning: number;
|
|
danger: number;
|
|
}
|
|
|
|
function countResultSet(rs: ResultSet, counts: ResultCounts): void {
|
|
for (const key of Object.keys(rs)) {
|
|
const msg = rs[key];
|
|
counts.total++;
|
|
if (msg.Success) {
|
|
counts.pass++;
|
|
} else if (msg.Severity === 'warning') {
|
|
counts.warning++;
|
|
} else if (msg.Severity === 'danger') {
|
|
counts.danger++;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function countResults(data: AuditData): ResultCounts {
|
|
const counts: ResultCounts = { total: 0, pass: 0, warning: 0, danger: 0 };
|
|
for (const result of data.Results) {
|
|
countResultSet(result.Results, counts);
|
|
if (result.PodResult) {
|
|
countResultSet(result.PodResult.Results, counts);
|
|
for (const container of result.PodResult.ContainerResults) {
|
|
countResultSet(container.Results, counts);
|
|
}
|
|
}
|
|
}
|
|
return counts;
|
|
}
|
|
|
|
// --- Settings ---
|
|
|
|
const STORAGE_KEY = 'polaris-plugin-refresh-interval';
|
|
const DEFAULT_INTERVAL_SECONDS = 300; // 5 minutes
|
|
|
|
export function getRefreshInterval(): number {
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
if (stored !== null) {
|
|
const parsed = parseInt(stored, 10);
|
|
if (!isNaN(parsed) && parsed > 0) {
|
|
return parsed;
|
|
}
|
|
}
|
|
return DEFAULT_INTERVAL_SECONDS;
|
|
}
|
|
|
|
export function setRefreshInterval(seconds: number): void {
|
|
localStorage.setItem(STORAGE_KEY, String(seconds));
|
|
}
|
|
|
|
// --- Score computation ---
|
|
|
|
export function computeScore(counts: ResultCounts): number {
|
|
if (counts.total === 0) return 0;
|
|
return Math.round((counts.pass / counts.total) * 100);
|
|
}
|
|
|
|
// --- Data fetching hook ---
|
|
|
|
const POLARIS_API_PATH =
|
|
'/api/v1/namespaces/polaris/services/polaris-dashboard:80/proxy/results.json';
|
|
|
|
interface PolarisDataState {
|
|
data: AuditData | null;
|
|
loading: boolean;
|
|
error: string | null;
|
|
}
|
|
|
|
export function usePolarisData(refreshIntervalSeconds: number): PolarisDataState {
|
|
const [data, setData] = React.useState<AuditData | null>(null);
|
|
const [loading, setLoading] = React.useState(true);
|
|
const [error, setError] = React.useState<string | null>(null);
|
|
const [tick, setTick] = React.useState(0);
|
|
|
|
React.useEffect(() => {
|
|
let cancelled = false;
|
|
|
|
async function fetchData() {
|
|
try {
|
|
const result: AuditData = await ApiProxy.request(POLARIS_API_PATH);
|
|
if (!cancelled) {
|
|
setData(result);
|
|
setError(null);
|
|
setLoading(false);
|
|
}
|
|
} catch (err: unknown) {
|
|
if (cancelled) return;
|
|
const status = (err as { status?: number }).status;
|
|
if (status === 403) {
|
|
setError(
|
|
'Access denied (403). Check that your RBAC permissions allow proxying to the Polaris service.'
|
|
);
|
|
} else if (status === 404 || status === 503) {
|
|
setError(
|
|
'Polaris dashboard not reachable. Ensure Polaris is installed in the polaris namespace.'
|
|
);
|
|
} else {
|
|
setError(`Failed to fetch Polaris data: ${String(err)}`);
|
|
}
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
fetchData();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [tick]);
|
|
|
|
// Periodic refresh
|
|
React.useEffect(() => {
|
|
if (refreshIntervalSeconds <= 0) return;
|
|
const intervalId = window.setInterval(() => {
|
|
setTick(t => t + 1);
|
|
}, refreshIntervalSeconds * 1000);
|
|
return () => window.clearInterval(intervalId);
|
|
}, [refreshIntervalSeconds]);
|
|
|
|
return { data, loading, error };
|
|
}
|