controller 0.59.0

Tembo Operator for Postgres
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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
use crate::{
    apis::coredb_types::CoreDB,
    snapshots::{
        volumesnapshotcontents_crd::{
            VolumeSnapshotContent, VolumeSnapshotContentDeletionPolicy,
            VolumeSnapshotContentSource, VolumeSnapshotContentSpec,
            VolumeSnapshotContentVolumeSnapshotRef,
        },
        volumesnapshots_crd::{VolumeSnapshot, VolumeSnapshotSource, VolumeSnapshotSpec},
    },
    Context,
};
use chrono::{DateTime, Utc};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use kube::{
    api::{ListParams, Patch, PatchParams},
    client::Client,
    runtime::controller::Action,
    Api, ResourceExt,
};
use std::sync::Arc;
use tracing::{debug, error, info, warn};

// Main function to reconcile the VolumeSnapshotContent and VolumeSnapshot
pub async fn reconcile_volume_snapshot_restore(
    cdb: &CoreDB,
    ctx: Arc<Context>,
) -> Result<VolumeSnapshot, Action> {
    let client = ctx.client.clone();

    // Lookup the VolumeSnapshot from the original instance
    let ogvs = lookup_volume_snapshot(cdb, &client).await?;

    // Lookup the VolumeSnapshotContent of the original instance
    let ogvsc = lookup_volume_snapshot_content(cdb, &client, ogvs).await?;

    // Generate the VolumeSnapshotContent and VolumeSnapshot for the restore
    let vsc = generate_volume_snapshot_content(cdb, &ogvsc)?;
    let vs = generate_volume_snapshot(cdb, &vsc)?;

    // Apply the VolumeSnapshotContent and VolumeSnapshot
    apply_volume_snapshot_content(cdb, &client, &vsc).await?;
    apply_volume_snapshot(cdb, &client, &vs).await?;

    // We need to wait for the snapshot to become ready before we can proceed
    is_snapshot_ready(cdb, &client, &vs).await?;

    // Patch the VolumeSnapshotContent with the UID of the VolumeSnapshot
    patch_volume_snapshot_content(cdb, &client, &vsc, &vs).await?;

    Ok(vs)
}

async fn patch_volume_snapshot_content(
    cdb: &CoreDB,
    client: &Client,
    vsc: &VolumeSnapshotContent,
    vs: &VolumeSnapshot,
) -> Result<(), Action> {
    let name = cdb.name_any();
    let vsc_name = vsc.metadata.name.as_ref().ok_or_else(|| {
        error!(
            "VolumeSnapshotContent name is empty for instance: {}.",
            cdb.name_any()
        );
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;

    // Name for the VolumeSnapshot
    let vs_name = vs.metadata.name.as_ref().ok_or_else(|| {
        error!(
            "VolumeSnapshot name is empty for instance: {}.",
            cdb.name_any()
        );
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;

    // Namespace for the VolumeSnapshot
    let vs_ns = vs.metadata.namespace.as_ref().ok_or_else(|| {
        error!(
            "VolumeSnapshot namespace is empty for instance: {}.",
            cdb.name_any()
        );
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;

    // Look up the VolumeSnapshot to get the UID
    let vs_api: Api<VolumeSnapshot> = Api::namespaced(client.clone(), vs_ns);
    let lp = ListParams::default().fields(&format!("metadata.name={}", vs_name));
    let res = vs_api.list(&lp).await.map_err(|e| {
        warn!(
            "Error listing VolumeSnapshots {} in namespace {} for instance {}: {}",
            vsc_name,
            vs_ns,
            cdb.name_any(),
            e,
        );
        Action::requeue(tokio::time::Duration::from_secs(30))
    })?;

    // Check if the VolumeSnapshot (vs) has a UID, requeue if it doesn't
    let vs_uid = match res.items.first().and_then(|vs| vs.metadata.uid.as_ref()) {
        Some(uid) => uid.to_string(),
        None => {
            error!(
                "VolumeSnapshot UID is empty for snapshot {} in namespace {}: {}.",
                vs_name,
                vs_ns,
                cdb.name_any()
            );
            return Err(Action::requeue(tokio::time::Duration::from_secs(10)));
        }
    };

    // Update the VolumeSnapshotContent with the UID of the VolumeSnapshot
    let mut new_vsc = vsc.clone();
    new_vsc.spec.volume_snapshot_ref = VolumeSnapshotContentVolumeSnapshotRef {
        api_version: Some("snapshot.storage.k8s.io/v1".to_string()),
        kind: Some("VolumeSnapshot".to_string()),
        name: vs.metadata.name.clone(),
        namespace: vs.metadata.namespace.clone(),
        uid: Some(vs_uid.to_string()),
        ..VolumeSnapshotContentVolumeSnapshotRef::default()
    };

    // Apply VolumeSnapshotContent (All Namespaces)
    let vsc_api: Api<VolumeSnapshotContent> = Api::all(client.clone());
    debug!("Patching VolumeSnapshotContent for instance: {}", name);
    let ps = PatchParams::apply("cntrlr").force();

    match vsc_api.patch(vsc_name, &ps, &Patch::Apply(&new_vsc)).await {
        Ok(_) => debug!("VolumeSnapshotContent patched successfully for {}.", name),
        Err(e) => {
            error!(
                "Failed to patch VolumeSnapshotContent for instance {}: {}",
                name, e
            );
            return Err(Action::requeue(tokio::time::Duration::from_secs(300)));
        }
    }

    Ok(())
}

async fn is_snapshot_ready(
    cdb: &CoreDB,
    client: &Client,
    vs: &VolumeSnapshot,
) -> Result<(), Action> {
    let name = vs.metadata.name.as_ref().ok_or_else(|| {
        error!(
            "VolumeSnapshot name is empty for instance: {}.",
            cdb.name_any()
        );
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;
    let namespace = vs.metadata.namespace.as_ref().ok_or_else(|| {
        error!(
            "VolumeSnapshot namespace is empty for instance: {}.",
            cdb.name_any()
        );
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;

    let vs_api: Api<VolumeSnapshot> = Api::namespaced(client.clone(), namespace);
    let lp = ListParams::default().fields(&format!("metadata.name={}", name));

    // Fetch the VolumeSnapshot to check its status
    let res = vs_api.list(&lp).await.map_err(|e| {
        warn!(
            "Error listing VolumeSnapshots {} for instance {}: {}",
            name,
            cdb.name_any(),
            e,
        );
        Action::requeue(tokio::time::Duration::from_secs(30))
    })?;

    // Check if the first VolumeSnapshot is ready
    if let Some(status) = res.items.first().and_then(|vs| vs.status.as_ref()) {
        if status.ready_to_use.unwrap_or(false) {
            info!(
                "VolumeSnapshot {} is ready for instance {}.",
                name,
                cdb.name_any()
            );
            Ok(())
        } else {
            warn!(
                "VolumeSnapshot {} is not ready yet for instance {}.",
                name,
                cdb.name_any()
            );
            Err(Action::requeue(tokio::time::Duration::from_secs(10)))
        }
    } else {
        error!(
            "VolumeSnapshot {} not found for instance {}",
            name,
            cdb.name_any()
        );
        Err(Action::requeue(tokio::time::Duration::from_secs(30)))
    }
}

async fn apply_volume_snapshot(
    cdb: &CoreDB,
    client: &Client,
    volume_snapshot: &VolumeSnapshot,
) -> Result<(), Action> {
    let name = cdb.name_any();
    let vs_name = volume_snapshot.metadata.name.as_ref().ok_or_else(|| {
        error!(
            "VolumeSnapshot name is empty for instance: {}.",
            cdb.name_any()
        );
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;

    // Namespace for the VolumeSnapshot
    let namespace = volume_snapshot
        .metadata
        .namespace
        .as_deref()
        .ok_or_else(|| {
            error!(
                "VolumeSnapshot namespace is empty for instance: {}.",
                cdb.name_any()
            );
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?;

    // Apply VolumeSnapshot (Namespaced)
    let vs_api: Api<VolumeSnapshot> = Api::namespaced(client.clone(), namespace);
    debug!("Patching VolumeSnapshot for instance: {}", name);
    let ps = PatchParams::apply("cntrlr").force();

    match vs_api
        .patch(vs_name, &ps, &Patch::Apply(volume_snapshot))
        .await
    {
        Ok(_) => debug!("VolumeSnapshot created successfully for {}.", name),
        Err(e) => {
            error!(
                "Failed to create VolumeSnapshot for instance {}: {}",
                name, e
            );
            return Err(Action::requeue(tokio::time::Duration::from_secs(300)));
        }
    }

    Ok(())
}

async fn apply_volume_snapshot_content(
    cdb: &CoreDB,
    client: &Client,
    volume_snapshot_content: &VolumeSnapshotContent,
) -> Result<(), Action> {
    let name = cdb.name_any();
    let vsc_name = volume_snapshot_content
        .metadata
        .name
        .as_ref()
        .ok_or_else(|| {
            error!(
                "VolumeSnapshot name is empty for instance: {}.",
                cdb.name_any()
            );
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?;

    // Apply VolumeSnapshotContent (All Namespaces)
    let vs_api: Api<VolumeSnapshotContent> = Api::all(client.clone());
    debug!("Patching VolumeSnapshotContent for instance: {}", name);
    let ps = PatchParams::apply("cntrlr").force();

    match vs_api
        .patch(vsc_name, &ps, &Patch::Apply(volume_snapshot_content))
        .await
    {
        Ok(_) => debug!("VolumeSnapshotContent created successfully for {}.", name),
        Err(e) => {
            error!(
                "Failed to create VolumeSnapshotContent for instance {}: {}",
                name, e
            );
            return Err(Action::requeue(tokio::time::Duration::from_secs(300)));
        }
    }

    Ok(())
}

// generate_volume_snapshot_content function generates the VolumeSnapshotContent object
// to map the VolumeSnapshot for the restore
fn generate_volume_snapshot_content(
    cdb: &CoreDB,
    snapshot_content: &VolumeSnapshotContent,
) -> Result<VolumeSnapshotContent, Action> {
    let name = cdb.name_any();
    let namespace = cdb.namespace().ok_or_else(|| {
        error!("CoreDB namespace is empty for instance: {}.", name);
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;
    let snapshot_handle = snapshot_content
        .status
        .as_ref()
        .and_then(|status| status.snapshot_handle.as_ref())
        .ok_or_else(|| {
            error!("Snapshot Handle is empty for instance {}", name);
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?
        .to_string();
    let driver = &snapshot_content.spec.driver;
    let volume_snapshot_class_name = snapshot_content
        .spec
        .volume_snapshot_class_name
        .as_ref()
        .ok_or_else(|| {
            error!("VolumeSnapshotClass name is empty for instance {}", name);
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?;
    let snapshot = format!("{}-restore-vs", name);

    let vsc = VolumeSnapshotContent {
        metadata: ObjectMeta {
            name: Some(format!("{}-restore-vsc", name)),
            namespace: Some(namespace.clone()),
            ..ObjectMeta::default()
        },
        spec: VolumeSnapshotContentSpec {
            deletion_policy: VolumeSnapshotContentDeletionPolicy::Retain,
            driver: driver.to_string(),
            source: VolumeSnapshotContentSource {
                snapshot_handle: Some(snapshot_handle),
                ..VolumeSnapshotContentSource::default()
            },
            volume_snapshot_class_name: Some(volume_snapshot_class_name.to_string()),
            volume_snapshot_ref: VolumeSnapshotContentVolumeSnapshotRef {
                api_version: Some("snapshot.storage.k8s.io/v1".to_string()),
                kind: Some("VolumeSnapshot".to_string()),
                name: Some(snapshot),
                namespace: Some(namespace.clone()),
                ..VolumeSnapshotContentVolumeSnapshotRef::default()
            },
            ..VolumeSnapshotContentSpec::default()
        },
        status: None,
    };

    Ok(vsc)
}

// generate_volume_snapshot function generates the VolumeSnapshot object and ties
// it back to the VolumeSnapshotContent
fn generate_volume_snapshot(
    cdb: &CoreDB,
    snapshot_content: &VolumeSnapshotContent,
) -> Result<VolumeSnapshot, Action> {
    let name = cdb.name_any();
    let namespace = cdb.namespace().ok_or_else(|| {
        error!("CoreDB namespace is empty for instance {}", name);
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;
    let volume_snapshot_content_name =
        snapshot_content.metadata.name.as_ref().ok_or_else(|| {
            error!("VolumeSnapshotContent name is empty for instance {}", name);
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?;
    let volume_snapshot_class_name = snapshot_content
        .spec
        .volume_snapshot_class_name
        .as_ref()
        .ok_or_else(|| {
            error!("VolumeSnapshotClass name is empty for instance {}", name);
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?;

    let vs = VolumeSnapshot {
        metadata: ObjectMeta {
            name: Some(format!("{}-restore-vs", name)),
            namespace: Some(namespace),
            ..ObjectMeta::default()
        },
        spec: VolumeSnapshotSpec {
            source: VolumeSnapshotSource {
                volume_snapshot_content_name: Some(volume_snapshot_content_name.to_string()),
                ..VolumeSnapshotSource::default()
            },
            volume_snapshot_class_name: Some(volume_snapshot_class_name.to_string()),
        },
        status: None,
    };
    Ok(vs)
}

// lookup_volume_snapshot function looks up the VolumeSnapshot object from the
// original instance you are restoring from
async fn lookup_volume_snapshot(cdb: &CoreDB, client: &Client) -> Result<VolumeSnapshot, Action> {
    // name will be the name of the original instance
    let og_instance_name = cdb
        .spec
        .restore
        .as_ref()
        .map(|r| r.server_name.clone())
        .ok_or_else(|| {
            error!(
                "CoreDB restore server_name is empty for instance {}",
                cdb.name_any()
            );
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?;

    debug!(
        "Looking up VolumeSnapshots for original instance: {}",
        og_instance_name
    );

    // todo: This is a temporary fix to get the VolumeSnapshot from the same namespace as the
    // instance you are attempting to restore from.  We need to figure out a better way of
    // doing this in case someone wants to name a namespace differently than the instance name.
    // At Tembo we assume that the namespace and the name of the instance name are the same.
    let volume_snapshot_api: Api<VolumeSnapshot> =
        Api::namespaced(client.clone(), &og_instance_name);

    let label_selector = format!("cnpg.io/cluster={}", og_instance_name);
    let lp = ListParams::default().labels(&label_selector);
    let result = volume_snapshot_api.list(&lp).await.map_err(|e| {
        error!(
            "Error listing VolumeSnapshots for instance {}: {}",
            og_instance_name, e
        );
        Action::requeue(tokio::time::Duration::from_secs(300))
    })?;

    debug!(
        "Found {} VolumeSnapshots for instance {}",
        result.items.len(),
        og_instance_name
    );

    // Set recovery_target_time if it's set in the CoreDB spec as DateTime<Utc>
    let recovery_target_time: Option<DateTime<Utc>> = cdb
        .spec
        .restore
        .as_ref()
        .and_then(|r| r.recovery_target_time.as_deref())
        .and_then(|time_str| DateTime::parse_from_rfc3339(time_str).ok())
        .map(|dt| dt.with_timezone(&Utc));

    debug!("Recovery target time: {:?}", recovery_target_time);

    // Filter snapshots that are ready to use and sort them by creation timestamp in descending order
    let snapshots: Vec<VolumeSnapshot> = result
        .into_iter()
        .filter(|vs| {
            vs.status
                .as_ref()
                .and_then(|s| s.ready_to_use)
                .unwrap_or(false)
        })
        .filter(|vs| {
            vs.metadata
                .annotations
                .as_ref()
                .and_then(|ann| ann.get("cnpg.io/instanceRole"))
                .map(|role| role == "primary")
                .unwrap_or(false)
        })
        .collect();

    debug!(
        "Filtered {} VolumeSnapshots for primary role and readiness",
        snapshots.len()
    );

    match find_closest_snapshot(snapshots, recovery_target_time) {
        Some(snapshot) => {
            debug!(
                "Selected VolumeSnapshot: {}",
                snapshot
                    .metadata
                    .name
                    .as_deref()
                    .map_or("unknown", |name| name)
            );
            Ok(snapshot)
        }
        None => {
            error!(
                "No suitable VolumeSnapshot found for instance {}",
                og_instance_name
            );
            Err(Action::requeue(tokio::time::Duration::from_secs(300)))
        }
    }
}

fn find_closest_snapshot(
    snapshots: Vec<VolumeSnapshot>,
    recovery_target_time: Option<DateTime<Utc>>,
) -> Option<VolumeSnapshot> {
    let filtered_snapshots: Vec<_> = snapshots
        .into_iter()
        .filter_map(|snapshot| {
            let end_time_str = snapshot
                .metadata
                .annotations
                .as_ref()?
                .get("cnpg.io/snapshotEndTime")?;
            let end_time = DateTime::parse_from_rfc3339(end_time_str)
                .ok()?
                .with_timezone(&Utc);
            Some((snapshot, end_time))
        })
        .collect();

    debug!(
        "Filtered snapshots based on end time: {:?}",
        filtered_snapshots
    );

    match recovery_target_time {
        Some(target_time) => {
            debug!("Recovery target time is specified: {:?}", target_time);
            let closest_snapshot = filtered_snapshots
                .into_iter()
                .filter(|(_, end_time)| *end_time <= target_time)
                .min_by_key(|(_, end_time)| (target_time - *end_time).num_seconds().abs())
                .map(|(snapshot, _)| snapshot);

            debug!("Selected closest snapshot: {:?}", closest_snapshot);
            closest_snapshot
        }
        None => {
            debug!("No recovery target time specified, selecting the latest snapshot.");
            let latest_snapshot = filtered_snapshots
                .into_iter()
                .max_by_key(|(_, end_time)| *end_time)
                .map(|(snapshot, _)| snapshot);

            debug!("Selected latest snapshot: {:?}", latest_snapshot);
            latest_snapshot
        }
    }
}

async fn lookup_volume_snapshot_content(
    cdb: &CoreDB,
    client: &Client,
    snapshot: VolumeSnapshot,
) -> Result<VolumeSnapshotContent, Action> {
    // The name of the VolumeSnapshotContext is in the status.boundVolumeSnapshotContentName field
    // in the VolumeSnapshot
    let name = snapshot
        .status
        .as_ref()
        .and_then(|s| s.bound_volume_snapshot_content_name.clone())
        .ok_or_else(|| {
            error!("Snapshot status is empty for instance {}", cdb.name_any());
            Action::requeue(tokio::time::Duration::from_secs(300))
        })?;

    // Lookup the VolumeSnapshotContent object, since it's not namespaced we will
    // need to filter on all objects in the cluster
    let volume_snapshot_content_api: Api<VolumeSnapshotContent> = Api::all(client.clone());
    match volume_snapshot_content_api.get(&name).await {
        Ok(vsc) => Ok(vsc),
        Err(e) => {
            error!(
                "Failed to get VolumeSnapshotContent for instance {}: {}",
                cdb.name_any(),
                e
            );
            Err(Action::requeue(tokio::time::Duration::from_secs(300)))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        apis::coredb_types::CoreDB,
        snapshots::volumesnapshotcontents_crd::{
            VolumeSnapshotContent, VolumeSnapshotContentSource, VolumeSnapshotContentSpec,
            VolumeSnapshotContentStatus,
        },
    };
    use chrono::DateTime;
    use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
    use std::collections::BTreeMap;

    #[test]
    fn test_generate_volume_snapshot_content() {
        let cdb_yaml = r#"
        apiVersion: coredb.io/v1alpha1
        kind: CoreDB
        metadata:
          name: test
          namespace: default
        spec:
          backup:
            destinationPath: s3://tembo-backup/sample-standard-backup
            encryption: ""
            retentionPolicy: "30"
            schedule: 17 9 * * *
            endpointURL: http://minio:9000
            volumeSnapshot:
              enabled: true
              snapshotClass: "csi-vsc"
          image: quay.io/tembo/tembo-pg-cnpg:15.3.0-5-48d489e
          port: 5432
          replicas: 1
          resources:
            limits:
              cpu: "1"
              memory: 0.5Gi
          serviceAccountTemplate:
            metadata:
              annotations:
                eks.amazonaws.com/role-arn: arn:aws:iam::012345678901:role/aws-iam-role-iam
          sharedirStorage: 1Gi
          stop: false
          storage: 1Gi
          storageClass: "gp3-enc"
          uid: 999
        "#;
        let cdb: CoreDB = serde_yaml::from_str(cdb_yaml).expect("Failed to parse YAML");

        let snapshot_content = VolumeSnapshotContent {
            metadata: ObjectMeta {
                name: Some("test-snapshot-content".to_string()),
                namespace: cdb.namespace(),
                ..ObjectMeta::default()
            },
            spec: VolumeSnapshotContentSpec {
                source: VolumeSnapshotContentSource {
                    volume_handle: Some("test-volume-handle".to_string()),
                    ..VolumeSnapshotContentSource::default()
                },
                driver: "test-driver".to_string(),
                volume_snapshot_class_name: Some("test-class".to_string()),
                ..VolumeSnapshotContentSpec::default()
            },
            status: Some(VolumeSnapshotContentStatus {
                creation_time: Some(1708542600948000000),
                ready_to_use: Some(true),
                restore_size: Some(10737418240),
                snapshot_handle: Some("snap-01234567abcdef890".to_string()),
                ..VolumeSnapshotContentStatus::default()
            }),
        };

        let result = generate_volume_snapshot_content(&cdb, &snapshot_content).unwrap();

        assert_eq!(result.spec.driver, "test-driver");
        assert_eq!(
            result.spec.source.snapshot_handle,
            Some("snap-01234567abcdef890".to_string())
        );
        assert_eq!(
            result.spec.volume_snapshot_class_name,
            Some("test-class".to_string())
        );
    }

    #[test]
    fn test_generate_volume_snapshot() {
        let cdb_yaml = r#"
        apiVersion: coredb.io/v1alpha1
        kind: CoreDB
        metadata:
          name: test
          namespace: default
        spec:
          backup:
            destinationPath: s3://tembo-backup/sample-standard-backup
            encryption: ""
            retentionPolicy: "30"
            schedule: 17 9 * * *
            endpointURL: http://minio:9000
            volumeSnapshot:
              enabled: true
              snapshotClass: "csi-vsc"
          image: quay.io/tembo/tembo-pg-cnpg:15.3.0-5-48d489e
          port: 5432
          replicas: 1
          resources:
            limits:
              cpu: "1"
              memory: 0.5Gi
          serviceAccountTemplate:
            metadata:
              annotations:
                eks.amazonaws.com/role-arn: arn:aws:iam::012345678901:role/aws-iam-role-iam
          sharedirStorage: 1Gi
          stop: false
          storage: 1Gi
          storageClass: "gp3-enc"
          uid: 999
        "#;
        let cdb: CoreDB = serde_yaml::from_str(cdb_yaml).expect("Failed to parse YAML");

        let snapshot_content = VolumeSnapshotContent {
            metadata: ObjectMeta {
                name: Some("test-snapshot-content".to_string()),
                namespace: Some("default".to_string()), // Ensure namespace matches CoreDB for the test's purpose
                ..ObjectMeta::default()
            },
            spec: VolumeSnapshotContentSpec {
                source: VolumeSnapshotContentSource {
                    volume_handle: Some("test-volume-handle".to_string()), // This might not be relevant for this test
                    ..VolumeSnapshotContentSource::default()
                },
                driver: "test-driver".to_string(), // Not directly relevant for this test
                volume_snapshot_class_name: Some("test-class".to_string()),
                ..VolumeSnapshotContentSpec::default()
            },
            status: None,
        };

        // Execute the function under test
        let result = generate_volume_snapshot(&cdb, &snapshot_content).unwrap();

        // Assertions
        assert_eq!(
            result.metadata.name.unwrap(),
            format!("{}-restore-vs", cdb.name_any())
        );
        assert_eq!(
            result.spec.source.volume_snapshot_content_name,
            Some("test-snapshot-content".to_string())
        );
        assert_eq!(
            result.spec.volume_snapshot_class_name,
            Some("test-class".to_string())
        );
        // The namespace of the generated VolumeSnapshot should match the namespace of the CoreDB
        assert_eq!(result.metadata.namespace.unwrap(), "default");
    }

    fn create_volume_snapshot(name: &str, snapshot_end_time: &str) -> VolumeSnapshot {
        let mut annotations = BTreeMap::new();
        annotations.insert(
            "cnpg.io/snapshotEndTime".to_string(),
            snapshot_end_time.to_string(),
        );

        VolumeSnapshot {
            metadata: ObjectMeta {
                name: Some(name.to_string()),
                annotations: Some(annotations),
                ..ObjectMeta::default()
            },
            spec: VolumeSnapshotSpec {
                ..VolumeSnapshotSpec::default()
            },
            status: None,
        }
    }

    #[test]
    fn test_find_closest_snapshot_pitr() {
        let recovery_target_time_str = "2024-03-06T00:00:00Z";
        let recovery_target_time = DateTime::parse_from_rfc3339(recovery_target_time_str)
            .unwrap()
            .with_timezone(&Utc);

        let snapshots = vec![
            create_volume_snapshot("snapshot1", "2024-03-05T23:50:00Z"),
            create_volume_snapshot("snapshot2", "2024-03-05T22:00:00Z"),
            create_volume_snapshot("snapshot3", "2024-03-05T23:55:00Z"),
            create_volume_snapshot("snapshot4", "2024-03-05T21:00:00Z"),
            create_volume_snapshot("snapshot5", "2024-03-06T00:01:00Z"),
        ];

        let closest_snapshot =
            find_closest_snapshot(snapshots, Some(recovery_target_time)).unwrap();
        assert_eq!(closest_snapshot.metadata.name.unwrap(), "snapshot3");
    }

    #[test]
    fn test_find_latest_snapshot_when_target_time_empty() {
        let snapshots = vec![
            create_volume_snapshot("snapshot1", "2024-03-05T20:00:00Z"),
            create_volume_snapshot("snapshot2", "2024-03-05T22:00:00Z"),
            create_volume_snapshot("snapshot3", "2024-03-05T23:00:00Z"),
            create_volume_snapshot("snapshot4", "2024-03-05T21:00:00Z"),
            create_volume_snapshot("snapshot5", "2024-03-06T00:01:00Z"),
        ];

        let latest_snapshot = find_closest_snapshot(snapshots, None).unwrap();
        assert_eq!(latest_snapshot.metadata.name.unwrap(), "snapshot5");
    }
}