syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! K8s Drift tool - Detect configuration drift between manifests and live cluster
//!
//! Compares declared Kubernetes manifests against the live cluster state to identify
//! resource drift, especially in CPU/memory limits and requests.
//!
//! Output is optimized for AI agent decision-making with:
//! - Clear drift detection results
//! - Resource-specific differences
//! - Remediation suggestions

use rig::completion::ToolDefinition;
use rig::tool::Tool;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::path::PathBuf;

use crate::analyzer::k8s_optimize::{K8sOptimizeConfig, analyze};

/// Arguments for the k8s-drift tool
#[derive(Debug, Deserialize)]
pub struct K8sDriftArgs {
    /// Path to K8s manifest file or directory (relative to project root)
    pub path: String,

    /// Kubernetes cluster context name (from kubeconfig)
    #[serde(default)]
    pub cluster: Option<String>,

    /// Filter by namespace
    #[serde(default)]
    pub namespace: Option<String>,

    /// Only check resource fields (requests/limits)
    #[serde(default)]
    pub resources_only: bool,

    /// Include all fields in diff, not just resource-related
    #[serde(default)]
    pub full_diff: bool,

    /// Output format: "summary", "detailed", "remediation"
    #[serde(default)]
    pub output_format: Option<String>,
}

/// Error type for k8s-drift tool
#[derive(Debug, thiserror::Error)]
#[error("K8s drift error: {0}")]
pub struct K8sDriftError(String);

/// Represents a single drift item
#[derive(Debug, Clone, Serialize)]
pub struct DriftItem {
    pub resource_kind: String,
    pub resource_name: String,
    pub namespace: String,
    pub container: Option<String>,
    pub field: String,
    pub declared_value: Option<String>,
    pub actual_value: Option<String>,
    pub drift_type: DriftType,
    pub severity: DriftSeverity,
}

/// Type of drift detected
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DriftType {
    /// Value differs between manifest and cluster
    ValueChanged,
    /// Field exists in manifest but not in cluster
    MissingInCluster,
    /// Field exists in cluster but not in manifest
    ExtraInCluster,
    /// Resource exists in manifest but not in cluster
    ResourceMissing,
    /// Resource exists in cluster but not in manifest
    ResourceExtra,
}

/// Severity of the drift
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum DriftSeverity {
    Critical,
    High,
    Medium,
    Low,
    Info,
}

/// Tool for detecting Kubernetes configuration drift
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct K8sDriftTool {
    project_root: PathBuf,
}

impl K8sDriftTool {
    /// Create a new K8sDriftTool with the given project root.
    pub fn new(project_root: PathBuf) -> Self {
        Self { project_root }
    }

    /// Analyze manifests and detect drift (static analysis placeholder).
    ///
    /// In production, this would connect to the cluster and compare.
    /// For now, it analyzes manifests and prepares a drift detection structure.
    fn analyze_drift(&self, args: &K8sDriftArgs) -> Result<Vec<DriftItem>, K8sDriftError> {
        let path = &args.path;
        let full_path = if std::path::Path::new(path).is_absolute() {
            PathBuf::from(path)
        } else {
            self.project_root.join(path)
        };

        if !full_path.exists() {
            return Err(K8sDriftError(format!(
                "Path not found: {}",
                full_path.display()
            )));
        }

        // Run static analysis to understand what's declared
        let config = K8sOptimizeConfig::default();
        let result = analyze(&full_path, &config);

        // Without live cluster connection, we can only report what we'd check
        // This is a placeholder - full implementation requires kube-rs integration
        let mut drift_items: Vec<DriftItem> = Vec::new();

        // If no cluster specified, return info about what would be checked
        if args.cluster.is_none() {
            // Add informational items about what resources exist in manifests
            for rec in &result.recommendations {
                // These aren't real drifts, but they indicate what we'd compare
                drift_items.push(DriftItem {
                    resource_kind: rec.resource_kind.clone(),
                    resource_name: rec.resource_name.clone(),
                    namespace: rec
                        .namespace
                        .clone()
                        .unwrap_or_else(|| "default".to_string()),
                    container: Some(rec.container.clone()),
                    field: "resources".to_string(),
                    declared_value: Some(format!(
                        "cpu_req={}, mem_req={}",
                        rec.current.cpu_request.as_deref().unwrap_or("none"),
                        rec.current.memory_request.as_deref().unwrap_or("none")
                    )),
                    actual_value: None, // Would be populated with cluster data
                    drift_type: DriftType::ValueChanged,
                    severity: DriftSeverity::Info,
                });
            }
        }

        Ok(drift_items)
    }

    /// Format drift results for agent consumption.
    fn format_for_agent(
        &self,
        drift_items: &[DriftItem],
        args: &K8sDriftArgs,
    ) -> serde_json::Value {
        let cluster_connected = args.cluster.is_some();

        // Group by severity
        let critical_count = drift_items
            .iter()
            .filter(|d| matches!(d.severity, DriftSeverity::Critical))
            .count();
        let high_count = drift_items
            .iter()
            .filter(|d| matches!(d.severity, DriftSeverity::High))
            .count();
        let medium_count = drift_items
            .iter()
            .filter(|d| matches!(d.severity, DriftSeverity::Medium))
            .count();
        let low_count = drift_items
            .iter()
            .filter(|d| matches!(d.severity, DriftSeverity::Low))
            .count();
        let info_count = drift_items
            .iter()
            .filter(|d| matches!(d.severity, DriftSeverity::Info))
            .count();

        let mut response = json!({
            "summary": {
                "total_drifts": drift_items.len(),
                "critical": critical_count,
                "high": high_count,
                "medium": medium_count,
                "low": low_count,
                "info": info_count,
                "cluster_connected": cluster_connected,
                "path_analyzed": args.path,
            },
        });

        if cluster_connected {
            response["drifts"] = json!(drift_items.iter().map(|d| {
                json!({
                    "resource": format!("{}/{}", d.resource_kind, d.resource_name),
                    "namespace": d.namespace,
                    "container": d.container,
                    "field": d.field,
                    "drift_type": d.drift_type,
                    "severity": d.severity,
                    "declared": d.declared_value,
                    "actual": d.actual_value,
                    "remediation": match d.drift_type {
                        DriftType::ValueChanged => "Update manifest or apply kubectl to sync",
                        DriftType::MissingInCluster => "Apply manifest with kubectl apply",
                        DriftType::ExtraInCluster => "Remove from cluster or add to manifest",
                        DriftType::ResourceMissing => "Deploy resource with kubectl apply",
                        DriftType::ResourceExtra => "Consider adding to version control",
                    },
                })
            }).collect::<Vec<_>>());
        } else {
            // Without cluster connection, provide guidance
            response["status"] = json!("no_cluster_connection");
            response["message"] = json!(
                "No cluster context specified. To detect actual drift, provide a cluster name. \
                 Currently showing resources that would be checked."
            );
            response["resources_to_check"] = json!(
                drift_items
                    .iter()
                    .map(|d| {
                        json!({
                            "resource": format!("{}/{}", d.resource_kind, d.resource_name),
                            "namespace": d.namespace,
                            "container": d.container,
                            "declared_resources": d.declared_value,
                        })
                    })
                    .collect::<Vec<_>>()
            );
            response["next_steps"] = json!([
                "Specify 'cluster' parameter with your kubeconfig context name",
                "Run: kubectl config get-contexts to see available contexts",
                "Example: k8s_drift with cluster='my-cluster-context'",
            ]);
        }

        // Add remediation commands if drifts found
        if cluster_connected && !drift_items.is_empty() {
            let mut commands: Vec<String> = Vec::new();

            // Generate kubectl commands for remediation
            for drift in drift_items
                .iter()
                .filter(|d| matches!(d.severity, DriftSeverity::Critical | DriftSeverity::High))
            {
                match drift.drift_type {
                    DriftType::ValueChanged | DriftType::MissingInCluster => {
                        commands.push(format!(
                            "kubectl apply -f {} -n {}",
                            args.path, drift.namespace
                        ));
                    }
                    DriftType::ResourceMissing => {
                        commands.push(format!(
                            "kubectl apply -f {} -n {}",
                            args.path, drift.namespace
                        ));
                    }
                    _ => {}
                }
            }

            if !commands.is_empty() {
                // Deduplicate commands
                commands.sort();
                commands.dedup();
                response["remediation_commands"] = json!(commands);
            }
        }

        response
    }
}

impl Tool for K8sDriftTool {
    const NAME: &'static str = "k8s_drift";

    type Args = K8sDriftArgs;
    type Output = String;
    type Error = K8sDriftError;

    async fn definition(&self, _prompt: String) -> ToolDefinition {
        ToolDefinition {
            name: Self::NAME.to_string(),
            description: r#"Detect configuration drift between Kubernetes manifests and live cluster.

**IMPORTANT: Only use this tool when the user EXPLICITLY asks about:**
- Drift detection between manifests and cluster
- What's different between declared and actual state
- GitOps compliance or sync status
- Whether manifests match what's running

**DO NOT use this tool for:**
- General Kubernetes linting (use kubelint)
- Resource optimization (use k8s_optimize)
- Cost analysis (use k8s_costs)
- Any task where user didn't ask about drift/sync/compliance

## What It Does
Compares manifest files against live cluster state (when cluster is connected) to find differences in resource configurations.

## Returns (analysis only - does NOT apply changes)
- Summary of drift counts by severity
- Per-resource drift information
- Suggested remediation commands
- Does NOT automatically sync or modify anything"#.to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "description": "Path to K8s manifest file or directory (required)"
                    },
                    "cluster": {
                        "type": "string",
                        "description": "Kubernetes cluster context name (from kubeconfig). Required for actual drift detection."
                    },
                    "namespace": {
                        "type": "string",
                        "description": "Filter drift detection to specific namespace"
                    },
                    "resources_only": {
                        "type": "boolean",
                        "description": "Only check resource requests/limits fields (default: false)"
                    },
                    "full_diff": {
                        "type": "boolean",
                        "description": "Include all fields in comparison, not just resources (default: false)"
                    },
                    "output_format": {
                        "type": "string",
                        "description": "Output format: 'summary', 'detailed', 'remediation'",
                        "enum": ["summary", "detailed", "remediation"]
                    }
                },
                "required": ["path"]
            }),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        let drift_items = self.analyze_drift(&args)?;
        let output = self.format_for_agent(&drift_items, &args);
        Ok(serde_json::to_string_pretty(&output).unwrap_or_else(|_| "{}".to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tool_name() {
        assert_eq!(K8sDriftTool::NAME, "k8s_drift");
    }

    #[test]
    fn test_drift_type_serialization() {
        let drift = DriftItem {
            resource_kind: "Deployment".to_string(),
            resource_name: "my-app".to_string(),
            namespace: "default".to_string(),
            container: Some("app".to_string()),
            field: "resources.limits.cpu".to_string(),
            declared_value: Some("500m".to_string()),
            actual_value: Some("1000m".to_string()),
            drift_type: DriftType::ValueChanged,
            severity: DriftSeverity::High,
        };

        let json = serde_json::to_string(&drift).unwrap();
        assert!(json.contains("value_changed"));
        assert!(json.contains("high"));
    }

    #[tokio::test]
    async fn test_definition() {
        let tool = K8sDriftTool::new(PathBuf::from("."));
        let def = tool.definition("".to_string()).await;

        assert_eq!(def.name, "k8s_drift");
        assert!(def.description.contains("drift"));
    }

    #[tokio::test]
    async fn test_no_cluster_output() {
        let tool = K8sDriftTool::new(PathBuf::from("."));

        // Without cluster, should return guidance
        let args = K8sDriftArgs {
            path: ".".to_string(),
            cluster: None,
            namespace: None,
            resources_only: false,
            full_diff: false,
            output_format: None,
        };

        let result = tool.call(args).await.unwrap();
        let json: serde_json::Value = serde_json::from_str(&result).unwrap();

        assert_eq!(json["status"], "no_cluster_connection");
        assert!(json["next_steps"].is_array());
    }
}