import { KubernetesClient } from '../KubernetesClient.js'; import { V1PodList } from '@kubernetes/client-node'; import { Logger } from 'winston'; interface K8sObjectMeta { name: string; namespace?: string; selfLink?: string; creationTimestamp?: string; } interface ResourceUsage { cpu: string; memory: string; } export interface ContainerMetrics { name: string; usage: ResourceUsage; } export interface PodMetrics { kind: 'PodMetrics'; apiVersion: 'metrics.k8s.io/v1beta1'; metadata: K8sObjectMeta; timestamp: string; window: string; containers: ContainerMetrics[]; } export interface NodeMetrics { kind: 'NodeMetrics'; apiVersion: 'metrics.k8s.io/v1beta1'; metadata: K8sObjectMeta; timestamp: string; window: string; usage: ResourceUsage; } export interface NodeMetricsList { kind: 'NodeMetricsList'; apiVersion: 'metrics.k8s.io/v1beta1'; metadata: { selfLink?: string; resourceVersion?: string; }; items: NodeMetrics[]; } export interface PodMetricsList { kind: 'PodMetricsList'; apiVersion: 'metrics.k8s.io/v1beta1'; metadata: { selfLink?: string; resourceVersion?: string; }; items: PodMetrics[]; } export interface PrometheusTarget { url: string; serviceName: string; namespace: string; port?: number; scheme?: 'http' | 'https'; kind?: 'prometheus' | 'thanos' | 'victoriametrics' | 'unknown'; } export interface NormalizedResourceUsage { cpuCores?: number; memoryBytes?: number; } export interface NormalizedContainerMetric { name: string; usage: NormalizedResourceUsage; } export interface NormalizedPodMetric { name: string; namespace: string; uid?: string; nodeName?: string; timestamp: string; window: string; usage: NormalizedResourceUsage; containers: NormalizedContainerMetric[]; customMetrics?: NormalizedCustomMetric[]; } export interface NormalizedNodeMetric { name: string; uid?: string; timestamp: string; window: string; usage: NormalizedResourceUsage; customMetrics?: NormalizedCustomMetric[]; } export interface NormalizedCustomMetric { name: string; value: number; labels?: Record; type?: 'gauge' | 'counter' | 'histogram' | 'summary' | 'untyped'; } export interface ProblemFinding { severity: 'info' | 'warning' | 'critical'; kind: 'Cluster' | 'Node' | 'Pod' | 'Namespace'; name?: string; namespace?: string; nodeName?: string; reason: string; message: string; recommendation?: string; } export declare class MetricOperations { private k8sClient; private logger?; private static readonly METRICS_API_GROUP; private static readonly METRICS_API_VERSION; private discoveredPrometheusTargets; constructor(k8sClient: KubernetesClient, logger?: Logger); /** * Fetches metrics for all nodes. * @returns A promise that resolves to a list of node metrics. */ getNodeMetrics(): Promise; /** * Fetches metrics for a specific node. * @param nodeName The name of the node. * @returns A promise that resolves to the node's metrics. */ getNodeMetricsByName(nodeName: string): Promise; /** * Helper: List all node names in the cluster */ private listNodes; /** * Helper: Convert kubelet summary pod stats to PodMetrics format */ private convertSummaryPodToPodMetrics; /** * Helper: Fetch pod metrics from kubelet summary API for all nodes */ private getPodMetricsFromKubeletSummary; /** * Helper: Fetch pod metrics from metrics.k8s.io API (original implementation) */ private getPodMetricsFromMetricsApi; /** * Fetches metrics for all pods in a given namespace, or all namespaces if none is specified. * Always tries both metrics.k8s.io and kubelet summary API, merging results. * @param namespace Optional namespace to filter pods. * @returns A promise that resolves to a list of pod metrics. */ getPodMetrics(namespace?: string): Promise; /** * Fetches metrics for a specific pod in a given namespace. * Uses the namespace-wide getPodMetrics method and filters for the specific pod. * This approach is more reliable than direct pod metric API calls. * @param podName The name of the pod. * @param namespace The namespace of the pod. * @returns A promise that resolves to the pod's metrics. */ getPodMetricsByName(podName: string, namespace: string): Promise; /** * Discovers Prometheus targets by scanning services for specific annotations. * @param namespace Optional namespace to scan. If not provided, scans all namespaces. */ discoverPrometheusTargets(namespace?: string): Promise; /** * Queries a single Prometheus target. * @param target The Prometheus target to query. * @param promqlQuery The PromQL query string. * @returns A promise that resolves to the parsed JSON response from Prometheus. */ queryPrometheusTarget(target: PrometheusTarget, promqlQuery: string): Promise; /** * Fetches metrics from all discovered Prometheus targets for a given query. * This is a high-level method that would orchestrate discovery and querying. * @param promqlQuery The PromQL query to execute on all targets. * @returns A promise that resolves to an array of results from each target. */ getPrometheusMetrics(promqlQuery: string): Promise; private parseK8sCpuString; private parseK8sMemoryString; /** * Normalizes and merges metrics from different sources. * This is a placeholder and will need actual implementation logic. * @param nodeMetricsRaw Raw node metrics from Metrics Server. * @param podMetricsRaw Raw pod metrics from Metrics Server. * @param prometheusMetricsRaw Raw metrics from Prometheus (array of results from getPrometheusMetrics). * @param allPodsRaw Optional: Full pod list for enrichment * @returns An object containing lists of normalized node and pod metrics. */ normalizeAndMergeMetrics(nodeMetricsRaw: NodeMetricsList | null, podMetricsRaw: PodMetricsList | null, prometheusMetricsRaw: any[], // This will be an array of Prometheus query results allPodsRaw?: V1PodList | null): Promise<{ nodesMetrics: NormalizedNodeMetric[]; podsMetrics: NormalizedPodMetric[]; error?: string; }>; /** * Build a compact, LLM-friendly cluster resource summary from normalized metrics */ buildLLMSummary(nodes?: NormalizedNodeMetric[], pods?: NormalizedPodMetric[], topN?: number): any; /** * Fetches all relevant metrics (Metrics Server, Prometheus), optionally enriches with pod specs, * and returns them in a normalized and merged format. * * This is a high-level convenience method. * * @param prometheusQueries An array of PromQL queries to execute against discovered Prometheus targets. * @param fetchPodSpecs If true, fetches all pod specifications to enrich normalized pod metrics with nodeName. Defaults to true. * @param discoveryNamespace Optional namespace to restrict Prometheus discovery and pod spec fetching. * @returns A promise that resolves to an object containing lists of normalized node and pod metrics. */ getAllNormalizedMetrics(prometheusQueries: string[], fetchPodSpecs?: boolean, discoveryNamespace?: string): Promise<{ nodesMetrics: NormalizedNodeMetric[]; podsMetrics: NormalizedPodMetric[]; error?: string; }>; /** * Analyze normalized metrics and live cluster state to detect likely problems. * Returns structured findings suitable for one-shot diagnostics. */ detectProblems(normalizedNodes?: NormalizedNodeMetric[], normalizedPods?: NormalizedPodMetric[], options?: { namespace?: string; cpuSaturationThreshold?: number; memorySaturationThreshold?: number; podRestartThreshold?: number; podLimitPressureThreshold?: number; includeNodeFindings?: boolean; }): Promise<{ findings: ProblemFinding[]; counts: Record; health: 'healthy' | 'degraded' | 'critical'; }>; /** * Get metrics with specific behavior for clean API calls. * This method provides a more direct approach than getAllNormalizedMetrics for direct API access. * * @param prometheusQueries An array of PromQL queries to execute against discovered Prometheus targets. * @param fetchPodSpecs If true, fetches all pod specifications to enrich normalized pod metrics with nodeName. Defaults to true. * @param discoveryNamespace Optional namespace to restrict Prometheus discovery and pod spec fetching. * @returns A promise that resolves to an object containing lists of normalized node and pod metrics. */ getMetricsWithOptions(prometheusQueries?: string[], fetchPodSpecs?: boolean, discoveryNamespace?: string, nodeApiPath?: string, podApiPath?: string): Promise<{ normalizedNodes?: NormalizedNodeMetric[]; normalizedPods?: NormalizedPodMetric[]; error?: string; }>; /** * Parse CPU value to nanocores for consistent summing * Public method for use by tools that need raw nanocores values */ parseCpuValueToNanocores(cpuString: string): number; /** * Parse memory value to bytes for consistent summing * Public method for use by tools that need raw bytes values */ parseMemoryValueToBytes(memoryString: string): number; } export {}; //# sourceMappingURL=MetricOperations.d.ts.map