Skip to main content

What-If Simulator Module

Overview​

The What-If Simulator is a critical safety layer that allows you to test policy changes before applying them in production. It replays historical traffic through a simulated policy engine and predicts the exact impact of changes.

Think of it as: A time machine for your network policies.

Why You Need This​

❌ Without Simulator:

You: "Let me block 8.8.8.8"
*applies policy*
💥 DNS breaks for 12 pods
💥 2 critical services fail health checks
💥 Production outage

✅ With Simulator:

$ paqtra simulate --block 8.8.8.8

Impact Analysis:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
❌ 12 pods depend on 8.8.8.8 for DNS
❌ 2 services will fail health checks
⚠️ Risk Score: HIGH (8/10)

Recommendation:
Configure kube-dns instead
OR allow specific pods only

Safe to Apply: NO

Architecture​

Historical Flows (eBPF + Hubble)
↓
[Load & Parse]
↓
Apply Scenario (modify policies)
↓
[Simulation Engine]
↓
Replay each flow
↓
[Impact Analyzer]
↓
[Risk Scorer]
↓
Detailed Report

Core Components​

1. Simulation Engine (engine.rs)​

Evaluates flows against modified policies:

pub struct SimulationEngine {
policies: Vec<PolicyDecision>,
simulated_policies: Vec<PolicyDecision>,
trace: Vec<String>,
}

impl SimulationEngine {
pub fn apply_scenario(&mut self, scenario: &SimulationScenario) -> Result<()>
pub fn evaluate_flow(&self, flow: &HistoricalFlow) -> PolicyVerdict
}

Scenarios Supported:

  • Add new policy
  • Remove existing policy
  • Modify policy
  • Block specific traffic
  • Allow specific traffic
  • Block external IP
  • Apply default-deny

2. Impact Analyzer (analyzer.rs)​

Finds affected resources:

pub struct ImpactAnalyzer {
pub fn analyze(&self, flow_results: &[FlowSimulationResult]) -> Result<ImpactAnalysis>
pub fn find_affected_resources(&self, ...) -> Result<AffectedResources>
}

Analyzes:

  • Services that will break
  • Broken dependencies
  • Affected namespaces
  • Blocked endpoints
  • Critical vs optional impacts

3. Risk Scorer (scorer.rs)​

Calculates risk scores (0-10):

pub struct RiskScorer {
pub fn assess_risk(&self, impact, affected, scenario) -> RiskAssessment
}

Risk Levels:

  • Low (0-2): Safe to apply
  • Medium (3-5): Review recommended
  • High (6-8): Dangerous, test in staging
  • Critical (9-10): DO NOT APPLY

Usage Examples​

Example 1: Test Blocking External IP​

use paqtra::modules::simulator::*;

#[tokio::main]
async fn main() -> Result<()> {
let config = SimulatorConfig::default();
let ebpf_reader = CiliumMapReader::new()?;
let k8s_client = K8sClient::new().await?;

let mut simulator = Simulator::new(config, ebpf_reader, k8s_client);

// Load historical flows
simulator.load_history().await?;

// Test scenario
let scenario = SimulationScenario::BlockExternalIP {
ip: "8.8.8.8".parse()?,
};

let result = simulator.simulate(scenario).await?;

// Print results
println!("Impact Analysis:");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!("Total flows analyzed: {}", result.impact.total_flows);
println!("Flows that would be blocked: {}", result.impact.blocked_flows);
println!("Affected services: {}", result.affected.services.len());

println!("\nRisk Assessment:");
println!("Level: {} (Score: {}/10)", result.risk.level.to_string(), result.risk.score);
println!("Safe to apply: {}", result.risk.safe_to_apply);

if !result.risk.safe_to_apply {
println!("\nReasons:");
for reason in &result.risk.unsafe_reasons {
println!(" • {}", reason);
}
}

println!("\nRecommendations:");
for rec in &result.recommendations {
println!(" • {}", rec);
}

Ok(())
}

Output:

Impact Analysis:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Total flows analyzed: 1000
Flows that would be blocked: 142
Affected services: 3

Risk Assessment:
Level: High (Score: 8/10)
Safe to apply: false

Reasons:
• Critical services affected: dns-service, auth-service
• More than 50% of flows would be blocked (58%)
• 2 critical dependencies would be broken

Recommendations:
• ⚠️ HIGH RISK: Do not apply this change in production
• Critical services affected: dns-service, auth-service. Consider gradual rollout.
• Verify DNS and external dependencies before blocking IPs

Confidence: 87%

Example 2: Test Default-Deny Policy​

let scenario = SimulationScenario::DefaultDeny {
namespace: "production".to_string(),
};

let result = simulator.simulate(scenario).await?;

println!("Impact on namespace 'production':");
println!("Services affected: {:?}", result.impact.impacted_services);
println!("Critical services: {:?}", result.impact.critical_services);

Example 3: Test Adding Allow Rule​

use std::collections::HashMap;

let scenario = SimulationScenario::AllowTraffic {
from_labels: HashMap::from([
("app".to_string(), "web".to_string()),
]),
to_labels: HashMap::from([
("app".to_string(), "api".to_string()),
]),
port: 8080,
protocol: "TCP".to_string(),
};

let result = simulator.simulate(scenario).await?;

if result.risk.safe_to_apply {
println!("✅ Safe to apply this allow rule");
} else {
println!("⚠️ Review security implications");
}

Example 4: Test Policy YAML​

let policy_yaml = r#"
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: block-external-dns
namespace: production
spec:
endpointSelector:
matchLabels:
app: web
egress:
- toFQDNs:
- matchPattern: "*.google.com"
"#;

let scenario = SimulationScenario::AddPolicy {
policy_yaml: policy_yaml.to_string(),
namespace: "production".to_string(),
};

let result = simulator.simulate(scenario).await?;

Simulation Scenarios​

SimulationScenario Types​

pub enum SimulationScenario {
// Add a new policy
AddPolicy {
policy_yaml: String,
namespace: String,
},

// Remove existing policy
RemovePolicy {
policy_name: String,
namespace: String,
},

// Modify existing policy
ModifyPolicy {
policy_name: String,
namespace: String,
new_yaml: String,
},

// Block specific traffic
BlockTraffic {
from_labels: HashMap<String, String>,
to_labels: HashMap<String, String>,
port: Option<u16>,
protocol: Option<String>,
},

// Allow specific traffic
AllowTraffic {
from_labels: HashMap<String, String>,
to_labels: HashMap<String, String>,
port: u16,
protocol: String,
},

// Block external IP
BlockExternalIP {
ip: IpAddr,
},

// Apply default-deny
DefaultDeny {
namespace: String,
},
}

Simulation Result​

Complete Result Structure​

pub struct SimulationResult {
pub scenario: SimulationScenario,
pub impact: ImpactAnalysis,
pub risk: RiskAssessment,
pub affected: AffectedResources,
pub recommendations: Vec<String>,
pub confidence: f32,
pub details: SimulationDetails,
}

Impact Analysis​

pub struct ImpactAnalysis {
pub total_flows: usize,
pub blocked_flows: usize,
pub allowed_flows: usize,
pub changed_flows: usize,
pub impacted_services: Vec<String>,
pub critical_services: Vec<String>,
}

Risk Assessment​

pub struct RiskAssessment {
pub level: RiskLevel, // Low/Medium/High/Critical
pub score: u8, // 0-10
pub factors: Vec<RiskFactor>,
pub safe_to_apply: bool,
pub unsafe_reasons: Vec<String>,
}

Affected Resources​

pub struct AffectedResources {
pub namespaces: Vec<String>,
pub pod_identities: Vec<u32>,
pub services: Vec<ServiceImpact>,
pub endpoints: Vec<EndpointImpact>,
pub broken_dependencies: Vec<Dependency>,
}

Risk Scoring System​

Risk Factors​

1. Service Availability (weight: high)

  • Critical services affected = +2 per service
  • Fully blocked services = +3 per service

2. Data Path (weight: high)

  • 50% flows blocked = +8

  • 25-50% flows blocked = +5
  • 10-25% flows blocked = +3
  • <10% flows blocked = +1

3. Broken Dependencies (weight: critical)

  • Critical dependency broken = +2 per dependency
  • Important dependency broken = +1 per dependency

4. Security (weight: medium)

  • Allowing new traffic = +2
  • Blocking external = 0 (safe)

5. Scope (weight: low)

  • 3 namespaces affected = +3

Risk Calculation Example​

Scenario: Block 8.8.8.8

Factors:
• 2 critical services affected: +4
• 58% flows blocked: +8
• 1 critical dependency (DNS): +2
• Blocking external: +0

Total Score: 14 → Capped at 10 → CRITICAL

Safe to Apply: NO

Configuration​

pub struct SimulatorConfig {
pub enabled: bool,
pub replay_flow_count: usize, // Default: 1000
pub history_window_secs: u64, // Default: 3600 (1 hour)
pub track_dependencies: bool, // Default: true
pub risk_scoring: bool, // Default: true
pub min_confidence: f32, // Default: 0.7
}

Tuning Guide:

  • Small cluster (<100 pods):

    • replay_flow_count: 500
    • history_window_secs: 1800 (30 min)
  • Medium cluster (100-500 pods):

    • replay_flow_count: 1000 (default)
    • history_window_secs: 3600 (1 hour)
  • Large cluster (>500 pods):

    • replay_flow_count: 2000
    • history_window_secs: 7200 (2 hours)

Integration Examples​

With Self-Healer​

// Self-healer proposes a fix
let fix = healer.diagnose_dns_issue().await?;

// Simulate the fix before applying
let scenario = SimulationScenario::AddPolicy {
policy_yaml: fix.yaml,
namespace: fix.namespace,
};

let simulation = simulator.simulate(scenario).await?;

// Only apply if safe
if simulation.risk.safe_to_apply && simulation.confidence > 0.8 {
healer.apply_fix(fix).await?;
} else {
println!("Fix too risky, requires manual review");
}

With AutoPolicy​

// AutoPolicy learned a policy
let learned_policies = autopolicy.generate_policies()?;

// Simulate each learned policy
for policy in learned_policies {
let scenario = SimulationScenario::AddPolicy {
policy_yaml: policy.yaml.clone(),
namespace: policy.namespace.clone(),
};

let simulation = simulator.simulate(scenario).await?;

if simulation.risk.score <= 3 {
println!("✅ Safe to apply: {}", policy.name);
autopolicy.apply_policy(&policy).await?;
} else {
println!("⚠️ Needs review: {} (risk: {})", policy.name, simulation.risk.score);
}
}

With RootCause​

// RootCause suggests a fix
let analysis = rootcause.analyze_drops().await?;

for drop_analysis in analysis {
if let SuggestedFix::AddPolicyRule { yaml, .. } = &drop_analysis.suggested_fix {
// Simulate the suggested fix
let scenario = SimulationScenario::AddPolicy {
policy_yaml: yaml.clone(),
namespace: drop_analysis.event.namespace.clone().unwrap_or_default(),
};

let simulation = simulator.simulate(scenario).await?;

println!("Fix for drop: {}", drop_analysis.likely_cause);
println!(" Risk: {}", simulation.risk.level.to_string());
println!(" Safe: {}", simulation.risk.safe_to_apply);
}
}

CLI Integration (Future)​

# Simulate blocking an IP
paqtra simulate block-ip 8.8.8.8

# Simulate a policy file
paqtra simulate apply-policy new-policy.yaml

# Simulate default-deny
paqtra simulate default-deny --namespace prod

# Compare multiple scenarios
paqtra simulate compare \
--scenario1 block-ip:8.8.8.8 \
--scenario2 allow-dns \
--scenario3 default-deny

Best Practices​

1. Always Simulate Before Production​

// ❌ BAD: Apply directly
k8s_client.apply_policy(policy).await?;

// ✅ GOOD: Simulate first
let simulation = simulator.simulate(scenario).await?;
if simulation.risk.safe_to_apply {
k8s_client.apply_policy(policy).await?;
}

2. Collect Enough History​

// Load at least 1 hour of traffic
simulator.config.history_window_secs = 3600;
simulator.load_history().await?;

let stats = simulator.stats();
if stats.flow_history_size < 500 {
eprintln!("⚠️ Low flow count, results may be unreliable");
}

3. Review High-Risk Changes​

let result = simulator.simulate(scenario).await?;

if result.risk.score >= 6 {
println!("⚠️ HIGH RISK - Manual review required");
println!("\nImpact:");
for service in &result.impact.critical_services {
println!(" • Critical service: {}", service);
}

println!("\nRisk Factors:");
for factor in &result.risk.factors {
println!(" • {} (severity: {})", factor.description, factor.severity);
}

return Ok(()); // Don't apply
}

4. Use Confidence Scores​

if result.confidence < 0.7 {
println!("⚠️ Low confidence ({:.0}%)", result.confidence * 100.0);
println!("Recommendation: Collect more traffic history");
}

5. Test in Stages​

# Stage 1: Test in dev
paqtra simulate --cluster dev apply-policy.yaml

# Stage 2: Test in staging
paqtra simulate --cluster staging apply-policy.yaml

# Stage 3: Apply to production (if both passed)
kubectl apply -f policy.yaml

Troubleshooting​

Low Confidence Scores​

Problem: Confidence is 45%

Solutions:

  • Increase history_window_secs to collect more flows
  • Wait for more traffic to occur
  • Verify eBPF maps are populating correctly

No Historical Flows​

Problem: flow_history_size = 0

Solutions:

// Check eBPF map access
let conntrack = ebpf_reader.read_conntrack_map()?;
println!("Conntrack entries: {}", conntrack.len());

// Ensure Hubble is enabled
// kubectl get pods -n kube-system | grep hubble

Inaccurate Predictions​

Problem: Simulation said safe, but production broke

Causes:

  1. Stale flow data - Increase history window
  2. Missing identities - IPCache not yet integrated
  3. External dependencies - Not captured in conntrack
  4. Batch jobs - Run during low-traffic period

Performance​

Memory Usage​

  • Base: ~5 MB
  • Per 1000 flows: +2 MB
  • Typical (1000 flows): ~7 MB

CPU Usage​

  • Simulation time: 50-200ms for 1000 flows
  • Memory-bound, not CPU-bound

Scalability​

  • Tested up to 10,000 flows
  • Linear scaling with flow count
  • Recommend batching for >5000 flows

Testing​

Run simulator tests:

cargo test simulator

Tests include:

  • ✓ Simulator creation
  • ✓ Engine policy evaluation
  • ✓ Risk scoring (low/high)
  • ✓ Impact analysis
  • ✓ Dependency criticality
  • ✓ Confidence adjustment

Total: 13 tests passing

Future Enhancements​

v1.1 - Enhanced Simulation​

  • Real CiliumNetworkPolicy YAML parsing
  • IPCache integration for identity resolution
  • Historical policy state tracking

v1.2 - Advanced Analysis​

  • ML-based impact prediction
  • Anomaly detection in simulations
  • Cost impact estimation

v1.3 - Multi-Scenario​

  • Batch scenario comparison
  • Optimal policy suggestion
  • Automatic rollback triggers

v1.4 - Integration​

  • GitOps integration (dry-run in CI/CD)
  • Slack/Teams notifications
  • Grafana dashboard
  • Self-Healer: Simulates fixes before applying
  • AutoPolicy: Validates learned policies
  • RootCause: Tests suggested fixes
  • Traffic Replay: Provides realistic flow data

Status​

✅ Completed (v1.0)

  • Core simulation engine
  • Impact analysis
  • Risk scoring (0-10)
  • Confidence calculation
  • 7 scenario types
  • Tests passing

⏳ In Progress

  • YAML policy parsing
  • IPCache resolution
  • CLI interface

📋 Planned

  • ML-based predictions
  • Multi-cluster simulation
  • Cost impact analysis

Last Updated: 2026-02-05 Module Status: Production Ready Test Coverage: 13/13 passing