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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! kubectl-style YAML diff engine for Kubernetes resources
//!
//! This module provides functionality to compare Kubernetes resources by:
//! 1. Normalizing resources (removing auto-generated fields)
//! 2. Converting to YAML with consistent formatting
//! 3. Generating unified diffs (like kubectl diff)
use crate::Result;
use kube::api::DynamicObject;
use serde_json::Value;
use similar::{ChangeTag, TextDiff};
/// Diff engine for comparing Kubernetes resources
pub struct DiffEngine;
impl DiffEngine {
/// Normalize a resource for comparison by removing auto-generated fields
///
/// This removes fields that Kubernetes generates automatically and should
/// not be considered when comparing desired vs live state:
/// - metadata.resourceVersion
/// - metadata.uid
/// - metadata.generation
/// - metadata.creationTimestamp
/// - metadata.managedFields
/// - metadata.selfLink
/// - status (entire section)
pub fn normalize(resource: &mut Value) {
// Remove auto-generated metadata fields
if let Some(metadata) = resource.get_mut("metadata").and_then(|m| m.as_object_mut()) {
metadata.remove("resourceVersion");
metadata.remove("uid");
metadata.remove("generation");
metadata.remove("creationTimestamp");
metadata.remove("managedFields");
metadata.remove("selfLink");
}
// Remove entire status section
if let Some(obj) = resource.as_object_mut() {
obj.remove("status");
}
}
/// Convert a resource to normalized YAML string for comparison
///
/// This normalizes the resource and converts it to YAML with consistent
/// formatting for reliable comparison.
pub fn to_comparable_yaml(resource: &Value) -> Result<String> {
let mut normalized = resource.clone();
Self::normalize(&mut normalized);
// Convert to YAML with consistent formatting
serde_norway::to_string(&normalized)
.map_err(|e| crate::NylError::Config(format!("Failed to serialize resource to YAML: {}", e)))
}
/// Compare two resources and return a unified diff (kubectl-style)
///
/// This generates a unified diff similar to `kubectl diff` by:
/// 1. Normalizing both resources
/// 2. Converting to YAML
/// 3. Running a line-by-line unified diff algorithm
///
/// Returns empty string if resources are equivalent.
pub fn diff_yaml(desired: &Value, live: &Value) -> Result<String> {
let desired_yaml = Self::to_comparable_yaml(desired)?;
let live_yaml = Self::to_comparable_yaml(live)?;
// Generate unified diff using similar crate
let diff = TextDiff::from_lines(&live_yaml, &desired_yaml);
// Build unified diff output
let mut output = String::new();
for hunk in diff.unified_diff().iter_hunks() {
// Add hunk header (e.g., @@ -1,4 +1,4 @@)
use std::fmt::Write;
write!(&mut output, "{}", hunk.header()).unwrap();
// Add diff lines
for change in hunk.iter_changes() {
match change.tag() {
ChangeTag::Delete => {
output.push('-');
output.push_str(change.value());
}
ChangeTag::Insert => {
output.push('+');
output.push_str(change.value());
}
ChangeTag::Equal => {
output.push(' ');
output.push_str(change.value());
}
}
}
}
Ok(output)
}
/// Check if two resources are equivalent after normalization
///
/// This is a simple equality check after normalization. Use this for
/// determining if resources differ, then use diff_yaml() to show what changed.
pub fn are_equivalent(desired: &Value, live: &Value) -> Result<bool> {
let desired_yaml = Self::to_comparable_yaml(desired)?;
let live_yaml = Self::to_comparable_yaml(live)?;
Ok(desired_yaml == live_yaml)
}
/// Normalize a resource with optional server-side normalization
///
/// If a client is provided, sends the resource to the server with dry-run
/// to apply server-side defaults. Otherwise, just clones the resource.
/// In both cases, applies client-side normalization to remove metadata fields.
pub async fn normalize_with_server(
resource: &Value,
client: Option<&dyn crate::kubernetes::KubeClient>,
) -> Result<Value> {
let normalized = if let Some(client) = client {
// Convert to DynamicObject for server-side normalization
let dynamic: DynamicObject = serde_json::from_value(resource.clone())?;
// Get server-normalized version using dry-run apply
let server_normalized = client.get_normalized_resource(&dynamic, "nyl-diff").await?;
// Convert back to Value
serde_json::to_value(&server_normalized)?
} else {
// No client provided, just clone
resource.clone()
};
// Apply client-side normalization to remove metadata fields
let mut result = normalized;
Self::normalize(&mut result);
Ok(result)
}
/// Generate a diff with server-side normalization
///
/// Uses server-side apply dry-run to normalize the desired resource,
/// then compares against the live resource (which already has server defaults).
pub async fn diff_yaml_with_server(
desired: &Value,
live: &Value,
client: &dyn crate::kubernetes::KubeClient,
) -> Result<String> {
// Normalize desired with server-side defaults
let desired_normalized = Self::normalize_with_server(desired, Some(client)).await?;
// Normalize live without server call (it already has server defaults)
let live_normalized = Self::normalize_with_server(live, None).await?;
// Convert to YAML and generate unified diff
let desired_yaml = serde_norway::to_string(&desired_normalized)
.map_err(|e| crate::NylError::Config(format!("Failed to serialize resource to YAML: {}", e)))?;
let live_yaml = serde_norway::to_string(&live_normalized)
.map_err(|e| crate::NylError::Config(format!("Failed to serialize resource to YAML: {}", e)))?;
// Generate unified diff
let diff = TextDiff::from_lines(&live_yaml, &desired_yaml);
let mut output = String::new();
for hunk in diff.unified_diff().iter_hunks() {
use std::fmt::Write;
write!(&mut output, "{}", hunk.header()).unwrap();
for change in hunk.iter_changes() {
match change.tag() {
ChangeTag::Delete => {
output.push('-');
output.push_str(change.value());
}
ChangeTag::Insert => {
output.push('+');
output.push_str(change.value());
}
ChangeTag::Equal => {
output.push(' ');
output.push_str(change.value());
}
}
}
}
Ok(output)
}
/// Check if two resources are equivalent with server-side normalization
///
/// Uses server-side apply dry-run to normalize the desired resource,
/// then compares against the live resource.
pub async fn are_equivalent_with_server(
desired: &Value,
live: &Value,
client: &dyn crate::kubernetes::KubeClient,
) -> Result<bool> {
// Normalize desired with server-side defaults
let desired_normalized = Self::normalize_with_server(desired, Some(client)).await?;
// Normalize live without server call
let live_normalized = Self::normalize_with_server(live, None).await?;
// Convert to YAML for comparison
let desired_yaml = serde_norway::to_string(&desired_normalized)
.map_err(|e| crate::NylError::Config(format!("Failed to serialize resource to YAML: {}", e)))?;
let live_yaml = serde_norway::to_string(&live_normalized)
.map_err(|e| crate::NylError::Config(format!("Failed to serialize resource to YAML: {}", e)))?;
Ok(desired_yaml == live_yaml)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_normalize_removes_metadata_fields() {
let mut resource = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test-config",
"namespace": "default",
"resourceVersion": "12345",
"uid": "abc-123",
"generation": 1,
"creationTimestamp": "2024-01-01T00:00:00Z",
"managedFields": [],
"selfLink": "/api/v1/namespaces/default/configmaps/test-config"
},
"data": {
"key": "value"
}
});
DiffEngine::normalize(&mut resource);
let metadata = resource["metadata"].as_object().unwrap();
assert!(!metadata.contains_key("resourceVersion"));
assert!(!metadata.contains_key("uid"));
assert!(!metadata.contains_key("generation"));
assert!(!metadata.contains_key("creationTimestamp"));
assert!(!metadata.contains_key("managedFields"));
assert!(!metadata.contains_key("selfLink"));
// Should preserve name and namespace
assert_eq!(metadata.get("name").unwrap(), "test-config");
assert_eq!(metadata.get("namespace").unwrap(), "default");
}
#[test]
fn test_normalize_removes_status() {
let mut resource = json!({
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "test-deploy"
},
"spec": {
"replicas": 3
},
"status": {
"availableReplicas": 3,
"conditions": []
}
});
DiffEngine::normalize(&mut resource);
assert!(!resource.as_object().unwrap().contains_key("status"));
// Should preserve spec
assert!(resource.as_object().unwrap().contains_key("spec"));
}
#[test]
fn test_to_comparable_yaml() {
let resource = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"resourceVersion": "12345"
},
"data": {
"key": "value"
}
});
let yaml = DiffEngine::to_comparable_yaml(&resource).unwrap();
// Should not contain resourceVersion
assert!(!yaml.contains("resourceVersion"));
// Should contain name and data
assert!(yaml.contains("name: test"));
assert!(yaml.contains("key: value"));
}
#[test]
fn test_are_equivalent_identical_resources() {
let resource1 = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test"
},
"data": {
"key": "value"
}
});
let resource2 = resource1.clone();
assert!(DiffEngine::are_equivalent(&resource1, &resource2).unwrap());
}
#[test]
fn test_are_equivalent_ignores_auto_generated_fields() {
let resource1 = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"resourceVersion": "12345"
},
"data": {
"key": "value"
}
});
let resource2 = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"resourceVersion": "67890"
},
"data": {
"key": "value"
}
});
// Should be equivalent despite different resourceVersion
assert!(DiffEngine::are_equivalent(&resource1, &resource2).unwrap());
}
#[test]
fn test_are_equivalent_different_data() {
let resource1 = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test"
},
"data": {
"key": "value1"
}
});
let resource2 = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test"
},
"data": {
"key": "value2"
}
});
// Should NOT be equivalent
assert!(!DiffEngine::are_equivalent(&resource1, &resource2).unwrap());
}
#[test]
fn test_diff_yaml_shows_changes() {
let live = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test"
},
"data": {
"key": "old-value"
}
});
let desired = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test"
},
"data": {
"key": "new-value"
}
});
let diff = DiffEngine::diff_yaml(&desired, &live).unwrap();
// Should contain diff markers
assert!(diff.contains('-') || diff.contains('+'));
// Should show the change
assert!(diff.contains("old-value") || diff.contains("new-value"));
}
#[test]
fn test_diff_yaml_empty_for_equivalent() {
let resource1 = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"resourceVersion": "12345"
},
"data": {
"key": "value"
}
});
let resource2 = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"resourceVersion": "67890"
},
"data": {
"key": "value"
}
});
let diff = DiffEngine::diff_yaml(&resource1, &resource2).unwrap();
// Should be empty for equivalent resources
assert_eq!(diff, "");
}
#[tokio::test]
async fn test_normalize_with_server_no_client() {
let resource = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"resourceVersion": "12345"
},
"data": {
"key": "value"
}
});
let normalized = DiffEngine::normalize_with_server(&resource, None).await.unwrap();
// Should not contain resourceVersion (client-side normalization applied)
assert!(!normalized["metadata"]
.as_object()
.unwrap()
.contains_key("resourceVersion"));
// Should contain name and data
assert_eq!(normalized["metadata"]["name"], "test");
assert_eq!(normalized["data"]["key"], "value");
}
#[tokio::test]
async fn test_normalize_with_server_with_mock_client() {
use crate::kubernetes::MockKubeClient;
let client = MockKubeClient::new();
let resource = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default",
"resourceVersion": "12345"
},
"data": {
"key": "value"
}
});
let normalized = DiffEngine::normalize_with_server(&resource, Some(&client))
.await
.unwrap();
// Should not contain resourceVersion (client-side normalization applied)
assert!(!normalized["metadata"]
.as_object()
.unwrap()
.contains_key("resourceVersion"));
// Should contain name and data
assert_eq!(normalized["metadata"]["name"], "test");
assert_eq!(normalized["data"]["key"], "value");
}
#[tokio::test]
async fn test_are_equivalent_with_server() {
use crate::kubernetes::MockKubeClient;
let client = MockKubeClient::new();
let desired = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default"
},
"data": {
"key": "value"
}
});
let live = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default",
"resourceVersion": "12345",
"uid": "abc-123"
},
"data": {
"key": "value"
}
});
// Should be equivalent despite different metadata
assert!(DiffEngine::are_equivalent_with_server(&desired, &live, &client)
.await
.unwrap());
}
#[tokio::test]
async fn test_are_equivalent_with_server_different_data() {
use crate::kubernetes::MockKubeClient;
let client = MockKubeClient::new();
let desired = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default"
},
"data": {
"key": "new-value"
}
});
let live = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default"
},
"data": {
"key": "old-value"
}
});
// Should NOT be equivalent
assert!(!DiffEngine::are_equivalent_with_server(&desired, &live, &client)
.await
.unwrap());
}
#[tokio::test]
async fn test_diff_yaml_with_server_shows_changes() {
use crate::kubernetes::MockKubeClient;
let client = MockKubeClient::new();
let desired = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default"
},
"data": {
"key": "new-value"
}
});
let live = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default",
"resourceVersion": "12345"
},
"data": {
"key": "old-value"
}
});
let diff = DiffEngine::diff_yaml_with_server(&desired, &live, &client)
.await
.unwrap();
// Should contain diff markers and the actual change
assert!(diff.contains("new-value") || diff.contains("old-value"));
assert!(diff.contains('-') || diff.contains('+'));
}
#[tokio::test]
async fn test_diff_yaml_with_server_ignores_server_defaults() {
use crate::kubernetes::MockKubeClient;
let client = MockKubeClient::new();
let desired = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default"
},
"data": {
"key": "value"
}
});
let live = json!({
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "test",
"namespace": "default",
"resourceVersion": "12345",
"uid": "abc-123",
"generation": 1
},
"data": {
"key": "value"
}
});
let diff = DiffEngine::diff_yaml_with_server(&desired, &live, &client)
.await
.unwrap();
// Should be empty (no differences in actual data)
assert_eq!(diff, "");
}
}