Module k8s_optimize

Module k8s_optimize 

Source
Expand description

Kubernetes Resource Optimization Analyzer

A native Rust analyzer for detecting over-provisioned and under-provisioned Kubernetes workloads. Helps reduce cloud costs by right-sizing resource requests and limits.

§Features

§Phase 1: Static Analysis

  • Static analysis of Kubernetes manifests (no cluster access required)
  • Terraform HCL support - Parse kubernetes_* provider resources
  • Pattern-based detection of over/under-provisioning
  • Workload type classification for smarter recommendations
  • Support for Deployments, StatefulSets, DaemonSets, Jobs, CronJobs
  • Helm chart and Kustomize directory support
  • Multiple output formats (table, JSON)

§Phase 2: Live Cluster Analysis

  • Kubernetes API integration - Connect to real clusters via kubeconfig
  • metrics-server support - Real-time CPU/memory usage data
  • Prometheus integration - Historical metrics (P50, P95, P99, max)
  • Data-driven recommendations based on actual usage
  • Waste percentage calculations with confidence levels

§Example

use syncable_cli::analyzer::k8s_optimize::{lint, K8sOptimizeConfig, OptimizationResult};
use std::path::Path;

// Static analysis (no cluster needed)
let config = K8sOptimizeConfig::default();
let result = lint(Path::new("./k8s/"), &config);

// Or using the backward-compatible analyze() function:
let result = analyze(Path::new("./k8s/"), &config);

// Live cluster analysis (requires kubeconfig)
use syncable_cli::analyzer::k8s_optimize::live_analyzer::{LiveAnalyzer, LiveAnalyzerConfig};
let live_config = LiveAnalyzerConfig::default();
let analyzer = LiveAnalyzer::new(live_config).await?;
let live_result = analyzer.analyze().await?;

§Optimization Rules

The analyzer checks for these common issues (K8S-OPT-001 through K8S-OPT-010):

§Over-Provisioning Detection

  • K8S-OPT-005: CPU request > 1 core for non-batch workload
  • K8S-OPT-006: Memory request > 2Gi for non-database workload
  • K8S-OPT-007: Excessive CPU limit-to-request ratio (> 10x)
  • K8S-OPT-008: Excessive memory limit-to-request ratio (> 4x)

§Under-Provisioning Detection

  • K8S-OPT-001: No CPU request defined
  • K8S-OPT-002: No memory request defined
  • K8S-OPT-003: No CPU limit defined
  • K8S-OPT-004: No memory limit defined

§Best Practices

  • K8S-OPT-009: Requests equal to limits (no bursting allowed)
  • K8S-OPT-010: Unbalanced resource allocation for workload type

Re-exports§

pub use config::K8sOptimizeConfig;
pub use types::AnalysisMetadata;
pub use types::AnalysisMode;
pub use types::ChartValidation;
pub use types::CloudProvider;
pub use types::CostBreakdown;
pub use types::CostEstimation;
pub use types::CostSavings;
pub use types::FixApplicationResult;
pub use types::FixImpact;
pub use types::FixResourceValues;
pub use types::FixRisk;
pub use types::FixSource;
pub use types::FixStatus;
pub use types::HelmIssue;
pub use types::HelmValidationReport;
pub use types::HelmValidationSummary;
pub use types::LiveClusterSummary;
pub use types::LiveFix;
pub use types::OptimizationIssue;
pub use types::OptimizationResult;
pub use types::OptimizationSummary;
pub use types::PreciseFix;
pub use types::ResourceOptimizationReport;
pub use types::ResourceOptimizationSummary;
pub use types::ResourceRecommendation;
pub use types::ResourceSpec;
pub use types::ResourceUsage;
pub use types::ResourceWarning;
pub use types::RuleCode;
pub use types::SecurityFinding;
pub use types::SecurityReport;
pub use types::SecuritySummary;
pub use types::Severity;
pub use types::TrendAnalysis;
pub use types::TrendDirection;
pub use types::UnifiedMetadata;
pub use types::UnifiedReport;
pub use types::UnifiedSummary;
pub use types::WasteMetrics;
pub use types::WorkloadCost;
pub use types::WorkloadTrend;
pub use types::WorkloadType;
pub use formatter::OutputFormat;
pub use formatter::format_result;
pub use formatter::format_result_to_string;
pub use static_analyzer::analyze as lint;
pub use static_analyzer::analyze_content as lint_content;
pub use static_analyzer::analyze_file as lint_file;
pub use static_analyzer::analyze;
pub use static_analyzer::analyze_content;
pub use static_analyzer::analyze_file;
pub use parser::TerraformContainer;
pub use parser::TerraformK8sResource;
pub use parser::TfResourceSpec;
pub use parser::bytes_to_memory_string;
pub use parser::cpu_limit_to_request_ratio;
pub use parser::detect_workload_type;
pub use parser::extract_container_image;
pub use parser::extract_container_name;
pub use parser::extract_resources;
pub use parser::memory_limit_to_request_ratio;
pub use parser::millicores_to_cpu_string;
pub use parser::parse_cpu_to_millicores;
pub use parser::parse_memory_to_bytes;
pub use parser::parse_terraform_k8s_resources;
pub use rules::ContainerContext;
pub use rules::OptimizationRule;
pub use rules::RuleContext;
pub use rules::all_rules;
pub use rules::codes as rule_codes;
pub use rules::generate_recommendations;
pub use rules::rule_description;
pub use pragma::IGNORE_ANNOTATION_PREFIX;
pub use pragma::extract_annotations;
pub use pragma::get_ignore_reason;
pub use pragma::get_ignored_rules;
pub use pragma::should_ignore_rule;
pub use live_analyzer::DataSource;
pub use live_analyzer::LiveAnalysisResult;
pub use live_analyzer::LiveAnalyzer;
pub use live_analyzer::LiveAnalyzerConfig;
pub use live_analyzer::LiveRecommendation;
pub use metrics_client::MetricsClient;
pub use metrics_client::PodMetrics;
pub use metrics_client::PodResources;
pub use metrics_client::ResourceComparison;
pub use prometheus_client::ContainerHistory;
pub use prometheus_client::HistoricalRecommendation;
pub use prometheus_client::PrometheusAuth;
pub use prometheus_client::PrometheusClient;
pub use cost_calculator::calculate_from_live;
pub use cost_calculator::calculate_from_static;
pub use fix_applicator::apply_fixes;
pub use fix_applicator::locate_resources_from_static;
pub use fix_applicator::locate_resources_in_file;

Modules§

config
Configuration for the optimizer. Configuration for Kubernetes resource optimization analysis.
cost_calculator
Cost calculation and estimation. Cost Calculator for Kubernetes Resource Waste
fix_applicator
Fix application to manifest files. Precise Fix Locator and Applicator
formatter
Output formatting (table, JSON, YAML). Output formatting for optimization results.
live_analyzer
Live cluster analyzer. Live Cluster Analyzer for Kubernetes resource optimization.
metrics_client
Kubernetes metrics-server client. Kubernetes Metrics Client for live cluster resource usage.
parser
Parsing utilities (YAML, Terraform, Helm). Parsing utilities for Kubernetes resource analysis.
pragma
Annotation-based rule ignoring (pragma). Annotation-based rule ignoring for k8s-optimize.
prometheus_client
Prometheus client for historical metrics. Prometheus Client for historical Kubernetes metrics.
recommender
Recommendation generation (now in rules/). Resource recommendation generation.
rules
Individual optimization rules (K8S-OPT-001 through K8S-OPT-010). Individual optimization rules for Kubernetes resources.
static_analyzer
Static analysis of Kubernetes manifests. Static analysis of Kubernetes manifests for resource optimization.
terraform_parser
Terraform parser (now in parser/terraform.rs, re-exported for compatibility). Terraform HCL parser for Kubernetes resources.
trend_analyzer
Trend analysis. Trend Analyzer for Kubernetes Resource Waste
types
Core data types. Core types for Kubernetes resource optimization analysis.