@chimoney.io/iaas-k8s-deployment
Version:
Kubernetes Infrastructure as a Service deployment package for streamlined multi-cloud environments
311 lines • 8.66 kB
TypeScript
/**
* Kubecost API Client
*
* This module provides functions to interact with Kubecost for cost tracking,
* usage metrics collection, and billing integration for tier-based deployments.
*/
import { Logger } from "../types/index.js";
import { PlanTier, KubecostConfig } from "../types/plans.js";
export interface KubecostAllocation {
name: string;
properties: {
cluster: string;
node: string;
container: string;
controller: string;
namespace: string;
pod: string;
providerID: string;
labels?: Record<string, string>;
};
start: string;
end: string;
minutes: number;
cpuCores: number;
cpuCoreRequestAverage: number;
cpuCoreUsageAverage: number;
cpuCoreHours: number;
cpuCost: number;
cpuCostAdjustment: number;
cpuEfficiency: number;
gpuCount: number;
gpuHours: number;
gpuCost: number;
gpuCostAdjustment: number;
networkReceiveBytes: number;
networkTransferBytes: number;
networkCost: number;
networkCostAdjustment: number;
loadBalancerCost: number;
loadBalancerCostAdjustment: number;
pvBytes: number;
pvByteHours: number;
pvCost: number;
pvs?: Record<string, any>;
ramBytes: number;
ramByteRequestAverage: number;
ramByteUsageAverage: number;
ramByteHours: number;
ramCost: number;
ramCostAdjustment: number;
ramEfficiency: number;
externalCost: number;
sharedCost: number;
totalCost: number;
totalEfficiency: number;
}
export interface KubecostAsset {
type: string;
name: string;
properties: {
category: string;
cluster: string;
node?: string;
providerID: string;
labels?: Record<string, string>;
};
labels?: Record<string, string>;
start: string;
end: string;
minutes: number;
byteHours?: number;
bytes?: number;
breakdown?: Record<string, number>;
adjustment: number;
totalCost: number;
}
export interface TierCostSummary {
tier: PlanTier;
namespace: string;
company: string;
period: {
start: string;
end: string;
};
costs: {
cpu: number;
memory: number;
storage: number;
network: number;
loadBalancer: number;
total: number;
currency: string;
};
efficiency: {
cpu: number;
memory: number;
overall: number;
};
usage: {
cpuCoreHours: number;
ramGBHours: number;
storageGBHours: number;
};
budgetStatus: {
allocated: number;
used: number;
remaining: number;
utilizationPercent: number;
};
}
export interface CostAlert {
id: string;
type: "budget" | "anomaly" | "efficiency";
severity: "info" | "warning" | "critical";
title: string;
message: string;
tier: PlanTier;
namespace: string;
company: string;
threshold: number;
current: number;
timestamp: string;
metadata?: Record<string, any>;
}
export interface KubecostQueryOptions {
window: string;
step?: string;
aggregate?: string;
accumulate?: boolean;
includeIdle?: boolean;
format?: "json" | "csv";
filter?: string;
}
/**
* GCP Billing Account configuration
*/
export interface GCPBillingConfig {
billingAccountId: string;
projectId: string;
credentialsPath?: string;
serviceAccountKey?: string;
}
/**
* GCP Billing API response structure
*/
export interface GCPBillingData {
name: string;
displayName: string;
open: boolean;
masterBillingAccount?: string;
}
export interface GCPCostData {
name: string;
displayName: string;
skuId: string;
skuDisplayName: string;
usage: {
unit: string;
amount: number;
amountInPricingUnits: number;
};
cost: {
currencyCode: string;
units: string;
nanos: number;
};
creditAdjustments: Array<{
name: string;
displayName: string;
type: string;
amount: {
currencyCode: string;
units: string;
nanos: number;
};
}>;
}
export declare class KubecostClient {
private baseUrl;
private apiKey?;
private logger;
private config;
private gcpBillingConfig?;
private googleAuth?;
constructor(baseUrl: string, config: KubecostConfig, logger: Logger, apiKey?: string, gcpBillingConfig?: GCPBillingConfig);
/**
* Initialize GCP authentication for billing API access
*/
private initializeGCPAuth;
/**
* Get cost allocation data for a specific namespace/tier
*/
getAllocationData(namespace: string, options?: KubecostQueryOptions): Promise<KubecostAllocation[]>;
/**
* Get asset cost data (storage, load balancers, etc.)
*/
getAssetData(options?: KubecostQueryOptions): Promise<KubecostAsset[]>;
/**
* Get comprehensive cost summary for a tier/namespace
*/
getTierCostSummary(tier: PlanTier, namespace: string, company: string, window?: string): Promise<TierCostSummary>;
/**
* Get cost alerts for a tier/namespace
*/
getCostAlerts(tier?: PlanTier, namespace?: string, company?: string): Promise<CostAlert[]>;
/**
* Export cost data for billing integration
*/
exportCostData(startDate: string, endDate: string, format?: "json" | "csv"): Promise<any>;
/**
* Set up cost monitoring for a namespace
*/
setupCostMonitoring(namespace: string, tier: PlanTier, company: string, budgetThresholds?: number[]): Promise<void>;
/**
* Allocate budget for a namespace based on tier
*/
allocateBudgetForNamespace(namespace: string, company: string, tier: PlanTier, billingAccountId?: string): Promise<{
namespace: string;
company: string;
tier: PlanTier;
allocatedBudget: number;
currency: string;
billingAccountId?: string;
}>;
/**
* Get the dashboard URL for a namespace
*/
getDashboardUrl(namespace: string): Promise<string>;
/**
* Make HTTP request to Kubecost API
*/
private makeRequest;
/**
* Get tier budget based on plan tier
*/
private getTierBudget;
/**
* Convert allocation data to CSV format
*/
private convertToCsv;
/**
* Get GCP billing account information
*/
getGCPBillingAccount(): Promise<GCPBillingData>;
/**
* Get detailed cost data from GCP Cloud Billing API
*/
getGCPCostData(startDate: string, endDate: string): Promise<GCPCostData[]>;
/**
* Sync GCP billing data with Kubecost
*/
syncGCPBillingWithKubecost(startDate: string, endDate: string): Promise<void>;
/**
* Reconcile GCP billing data with Kubecost allocation data
*/
private reconcileGCPWithKubecost;
/**
* Calculate total cost from Kubecost allocation data
*/
private calculateTotalKubecostCost;
/**
* Create a Kubecost alert configuration
*/
private createKubecostAlert;
/**
* Configure cost allocation labels for a namespace
*/
private configureCostAllocationLabels;
/**
* Get cost recommendations for optimization
*/
getCostRecommendations(namespace?: string): Promise<Array<{
type: "rightsizing" | "termination" | "scheduling" | "storage";
severity: "low" | "medium" | "high";
title: string;
description: string;
estimatedSavings: number;
namespace?: string;
resource?: string;
action: string;
}>>;
/**
* Get all namespaces with cost tracking labels
*/
private getAllNamespaces;
/**
* Generate cost report for billing integration
*/
generateCostReport(startDate: string, endDate: string, groupBy?: "namespace" | "tier" | "company", format?: "json" | "csv" | "pdf"): Promise<any>;
/**
* Get group key based on groupBy parameter
*/
private getGroupKey;
/**
* Convert report to CSV format
*/
private convertReportToCsv;
/**
* Convert report to PDF format (placeholder)
*/
private convertReportToPdf;
}
/**
* Create Kubecost client instance
*/
export declare function createKubecostClient(kubecostUrl: string, config: KubecostConfig, logger: Logger, apiKey?: string, gcpBillingConfig?: GCPBillingConfig): KubecostClient;
/**
* Utility function to validate Kubecost connectivity
*/
export declare function validateKubecostConnection(client: KubecostClient, logger: Logger): Promise<boolean>;
//# sourceMappingURL=kubecost-client.d.ts.map