eksup 0.1.1-alpha

A CLI to aid in upgrading Amazon EKS clusters
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
use std::{collections::HashSet, env};

use anyhow::{bail, Result};
use aws_config::meta::region::RegionProviderChain;
use aws_sdk_autoscaling::{
  model::{AutoScalingGroup, Filter as AsgFilter},
  Client as AsgClient,
};
use aws_sdk_ec2::Client as Ec2Client;
use aws_sdk_eks::{
  model::{Addon, Cluster, FargateProfile, Nodegroup},
  Client as EksClient,
};
use aws_types::region::Region;
use kube::Client as K8sClient;
use serde::{Deserialize, Serialize};

use crate::{
  finding::{self, Findings},
  k8s, version,
};

/// Get the configuration to authn/authz with AWS that will be used across AWS clients
pub(crate) async fn get_config(region: &Option<String>) -> Result<aws_config::SdkConfig> {
  let aws_region = match region {
    Some(region) => Region::new(region.to_owned()),
    None => env::var("AWS_REGION").ok().map(Region::new).unwrap(),
  };

  let region_provider = RegionProviderChain::first_try(aws_region).or_default_provider();

  Ok(aws_config::from_env().region(region_provider).load().await)
}

/// Describe the cluster to get its full details
pub(crate) async fn get_cluster(client: &EksClient, name: &str) -> Result<Cluster> {
  let request = client.describe_cluster().name(name);
  let response = request.send().await?;

  match response.cluster {
    Some(cluster) => Ok(cluster),
    None => bail!("Cluster {name} not found"),
  }
}

/// Cluster health issue data
///
/// Nearly identical to the SDK's `ClusterIssue` but allows us to serialize/deserialize
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct ClusterHealthIssue {
  pub(crate) code: String,
  pub(crate) message: String,
  pub(crate) resource_ids: Vec<String>,
  pub(crate) remediation: finding::Remediation,
  pub(crate) fcode: finding::Code,
}

impl Findings for Vec<ClusterHealthIssue> {
  fn to_markdown_table(&self, leading_whitespace: &str) -> Option<String> {
    if self.is_empty() {
      return Some(format!(
        "{leading_whitespace}✅ - There are no reported health issues on the cluster control plane"
      ));
    }

    let mut table = String::new();
    table.push_str(&format!(
      "{leading_whitespace}|   _   | Code  | Message | Resource IDs |\n"
    ));
    table.push_str(&format!(
      "{leading_whitespace}| :---: | :---: | :------ | :----------- |\n"
    ));

    for finding in self {
      table.push_str(&format!(
        "{}| {} | `{}` | `{}` | {} |\n",
        leading_whitespace,
        finding.remediation.symbol(),
        finding.code,
        finding.message,
        finding
          .resource_ids
          .iter()
          .map(|f| format!("`{f}`"))
          .collect::<Vec<String>>()
          .join(", "),
      ))
    }

    Some(table)
  }
}

/// Check for any reported health issues on the cluster control plane
pub(crate) async fn cluster_health(cluster: &Cluster) -> Result<Vec<ClusterHealthIssue>> {
  let health = cluster.health();

  match health {
    Some(health) => {
      let issues = health
        .issues()
        .unwrap()
        .to_owned()
        .iter()
        .map(|issue| {
          let code = &issue.code().unwrap().to_owned();

          ClusterHealthIssue {
            code: code.as_str().to_string(),
            message: issue.message().unwrap().to_string(),
            resource_ids: issue.resource_ids().unwrap().to_owned(),
            remediation: finding::Remediation::Required,
            fcode: finding::Code::EKS002,
          }
        })
        .collect();

      Ok(issues)
    }
    None => Ok(vec![]),
  }
}

/// Container for the subnet IDs and their total available IPs
#[derive(Clone, Debug, Serialize, Deserialize)]
struct SubnetIPs {
  ids: Vec<String>,
  available_ips: i32,
}

/// Describe the subnets provided by ID
///
/// This will show the number of available IPs for evaluating
/// IP contention/exhaustion across the various subnets in use
/// by the control plane ENIs, the nodes, and the pods (when custom
/// networking is enabled)
async fn get_subnet_ips(client: &Ec2Client, subnet_ids: Vec<String>) -> Result<SubnetIPs> {
  let subnets = client
    .describe_subnets()
    .set_subnet_ids(Some(subnet_ids))
    .send()
    .await?
    .subnets
    .unwrap();

  let available_ips = subnets
    .iter()
    .map(|subnet| subnet.available_ip_address_count.unwrap())
    .sum();

  let ids = subnets
    .iter()
    .map(|subnet| subnet.subnet_id().unwrap().to_string())
    .collect::<Vec<String>>();

  Ok(SubnetIPs { ids, available_ips })
}

/// Subnet details that can affect upgrade behavior
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct InsufficientSubnetIps {
  pub(crate) ids: Vec<String>,
  pub(crate) available_ips: i32,
  pub(crate) remediation: finding::Remediation,
  pub(crate) code: finding::Code,
}

impl Findings for Option<InsufficientSubnetIps> {
  fn to_markdown_table(&self, leading_whitespace: &str) -> Option<String> {
    match self {
      Some(finding) => {
        let mut table = String::new();
        table.push_str(&format!(
          "{leading_whitespace}|   -   | Subnet IDs  | Available IPs |\n"
        ));
        table.push_str(&format!(
          "{leading_whitespace}| :---: | :---------- | :-----------: |\n"
        ));
        table.push_str(&format!(
          "{}| {} | {} | `{}` |\n",
          leading_whitespace,
          finding.remediation.symbol(),
          finding
            .ids
            .iter()
            .map(|f| format!("`{f}`"))
            .collect::<Vec<String>>()
            .join(", "),
          finding.available_ips,
        ));

        Some(table)
      }
      None => Some(format!(
        "{leading_whitespace}✅ - There is sufficient IP space in the subnets provided"
      )),
    }
  }
}

pub(crate) async fn control_plane_ips(
  ec2_client: &Ec2Client,
  cluster: &Cluster,
) -> Result<Option<InsufficientSubnetIps>> {
  let subnet_ids = cluster.resources_vpc_config().unwrap().subnet_ids().unwrap().to_owned();

  let subnet_ips = get_subnet_ips(ec2_client, subnet_ids).await?;
  if subnet_ips.available_ips >= 5 {
    return Ok(None);
  }

  let finding = InsufficientSubnetIps {
    ids: subnet_ips.ids,
    available_ips: subnet_ips.available_ips,
    remediation: finding::Remediation::Required,
    code: finding::Code::EKS001,
  };

  Ok(Some(finding))
}

/// Check if the subnets used by the pods will support an upgrade
///
/// This checks for the `ENIConfig` custom resource that is used to configure
/// the AWS VPC CNI for custom networking. The subnet listed for each ENIConfig
/// is queried for its relevant data used to report on the available IPs
pub(crate) async fn pod_ips(
  ec2_client: &Ec2Client,
  k8s_client: &K8sClient,
  required_ips: i32,
  recommended_ips: i32,
) -> Result<Option<InsufficientSubnetIps>> {
  let eniconfigs = k8s::get_eniconfigs(k8s_client).await?;
  if eniconfigs.is_empty() {
    return Ok(None);
  }

  let subnet_ids = eniconfigs
    .iter()
    .map(|eniconfig| eniconfig.spec.subnet.as_ref().unwrap().to_owned())
    .collect();

  let subnet_ips = get_subnet_ips(ec2_client, subnet_ids).await?;

  if subnet_ips.available_ips >= recommended_ips {
    return Ok(None);
  }

  let remediation = if subnet_ips.available_ips >= required_ips {
    finding::Remediation::Required
  } else {
    finding::Remediation::Recommended
  };
  let finding = InsufficientSubnetIps {
    ids: subnet_ips.ids,
    available_ips: subnet_ips.available_ips,
    remediation,
    code: finding::Code::AWS002,
  };

  Ok(Some(finding))
}

pub(crate) async fn get_addons(client: &EksClient, cluster_name: &str) -> Result<Vec<Addon>> {
  let addon_names = client
    .list_addons()
    .cluster_name(cluster_name)
    // TODO - paginate this
    .max_results(100)
    .send()
    .await?
    .addons
    .unwrap_or_default();

  let mut addons = Vec::new();

  for addon_name in &addon_names {
    let response = client
      .describe_addon()
      .cluster_name(cluster_name)
      .addon_name(addon_name)
      .send()
      .await?
      .addon;

    if let Some(addon) = response {
      addons.push(addon);
    }
  }

  Ok(addons)
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct AddonVersion {
  /// Latest supported version of the addon
  pub(crate) latest: String,
  /// Default version of the addon used by the service
  pub(crate) default: String,
  /// Supported versions for the given Kubernetes version
  /// This maintains the ordering of latest version to oldest
  pub(crate) supported_versions: HashSet<String>,
}

/// Get the addon version details for the given addon and Kubernetes version
///
/// Returns associated version details for a given addon that, primarily used
/// for version compatibility checks and/or upgrade recommendations
async fn get_addon_versions(client: &EksClient, name: &str, kubernetes_version: &str) -> Result<AddonVersion> {
  // Get all of the addon versions supported for the given addon and Kubernetes version
  let describe = client
    .describe_addon_versions()
    .addon_name(name)
    .kubernetes_version(kubernetes_version)
    .send()
    .await?;

  // Since we are providing an addon name, we are only concerned with the first and only item
  let addon = describe.addons().unwrap().get(0).unwrap();
  let addon_version = addon.addon_versions().unwrap();
  let latest_version = addon_version.first().unwrap().addon_version().unwrap();

  // The default version as specified by the EKS API for a given addon and Kubernetes version
  let default_version = addon
    .addon_versions()
    .unwrap()
    .iter()
    .filter(|v| v.compatibilities().unwrap().iter().any(|c| c.default_version))
    .map(|v| v.addon_version().unwrap())
    .next()
    .unwrap();

  // Get the list of ALL supported version for this addon and Kubernetes version
  // The results maintain the oder of latest version to oldest
  let supported_versions: HashSet<String> = addon
    .addon_versions()
    .unwrap()
    .iter()
    .map(|v| v.addon_version().unwrap().to_owned())
    .collect();

  Ok(AddonVersion {
    latest: latest_version.to_owned(),
    default: default_version.to_owned(),
    supported_versions,
  })
}

/// Details of the addon as viewed from an upgrade perspective
///
/// Contains the associated version information to compare the current version
/// of the addon relative to the current "desired" version, as well as
/// relative to the target Kubernetes version "desired" version. It
/// also contains any potential health issues as reported by the EKS API.
/// The intended goal is to be able to plot a path of what steps a user either
/// needs to take to upgrade the cluster, or should consider taking in terms
/// of a recommendation to update to the latest supported version.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct AddonVersionCompatibility {
  pub(crate) name: String,
  /// The current version of the add-on
  pub(crate) version: String,
  /// The default and latest add-on versions for the current Kubernetes version
  pub(crate) current_kubernetes_version: AddonVersion,
  /// The default and latest add-on versions for the target Kubernetes version
  pub(crate) target_kubernetes_version: AddonVersion,
  pub(crate) remediation: finding::Remediation,
  pub(crate) code: finding::Code,
}

impl Findings for Vec<AddonVersionCompatibility> {
  fn to_markdown_table(&self, leading_whitespace: &str) -> Option<String> {
    if self.is_empty() {
      return Some(format!(
        "{leading_whitespace}✅ - There are no reported addon version compatibility issues."
      ));
    }

    let mut table = String::new();
    table.push_str(&format!(
      "{leading_whitespace}|   -   | Name  | Version | Next Default | Next Latest |\n"
    ));
    table.push_str(&format!(
      "{leading_whitespace}| :---: | :---- | :-----: | :----------: | :---------: |\n"
    ));

    for finding in self {
      table.push_str(&format!(
        "{}| {} | `{}` | `{}` | `{}` | `{}` |\n",
        leading_whitespace,
        finding.remediation.symbol(),
        finding.name,
        finding.version,
        finding.target_kubernetes_version.default,
        finding.target_kubernetes_version.latest,
      ))
    }

    Some(table)
  }
}

/// Check for any version compatibility issues for the EKS addons enabled
pub(crate) async fn addon_version_compatibility(
  client: &EksClient,
  cluster_version: &str,
  addons: &[Addon],
) -> Result<Vec<AddonVersionCompatibility>> {
  let mut addon_versions = Vec::new();
  let target_k8s_version = format!("1.{}", version::parse_minor(cluster_version)? + 1);

  for addon in addons {
    let name = addon.addon_name().unwrap().to_owned();
    let version = addon.addon_version().unwrap().to_owned();

    let current_kubernetes_version = get_addon_versions(client, &name, cluster_version).await?;
    let target_kubernetes_version = get_addon_versions(client, &name, &target_k8s_version).await?;

    // TODO - why is this saying the if/else is the same?
    #[allow(clippy::if_same_then_else)]
    let remediation = if !target_kubernetes_version.supported_versions.contains(&version) {
      // The target Kubernetes version of addons does not support the current addon version, must update
      Some(finding::Remediation::Required)
    } else if !current_kubernetes_version.supported_versions.contains(&version) {
      // The current Kubernetes version of addons does not support the current addon version, must update
      Some(finding::Remediation::Required)
    } else if current_kubernetes_version.latest != version {
      // The current Kubernetes version of addons supports the current addon version, but it is not the latest
      Some(finding::Remediation::Recommended)
    } else {
      None
    };

    if let Some(remediation) = remediation {
      addon_versions.push(AddonVersionCompatibility {
        name,
        version,
        current_kubernetes_version,
        target_kubernetes_version,
        remediation,
        code: finding::Code::EKS005,
      })
    }
  }

  Ok(addon_versions)
}

/// Addon health issue data
///
/// Nearly identical to the SDK's `AddonIssue` but allows us to serialize/deserialize
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct AddonHealthIssue {
  pub(crate) name: String,
  pub(crate) code: String,
  pub(crate) message: String,
  pub(crate) resource_ids: Vec<String>,
  pub(crate) remediation: finding::Remediation,
  pub(crate) fcode: finding::Code,
}

impl Findings for Vec<AddonHealthIssue> {
  fn to_markdown_table(&self, leading_whitespace: &str) -> Option<String> {
    if self.is_empty() {
      return Some(format!(
        "{leading_whitespace}✅ - There are no reported addon health issues."
      ));
    }

    let mut table = String::new();
    table.push_str(&format!(
      "{leading_whitespace}|   -   | Name  | Code  | Message | Resource IDs |\n"
    ));
    table.push_str(&format!(
      "{leading_whitespace}| :---: | :---- | :---: | :------ | :----------- |\n"
    ));

    for finding in self {
      table.push_str(&format!(
        "{}| {} | `{}` | `{}` | `{}` | {} |\n",
        leading_whitespace,
        finding.remediation.symbol(),
        finding.name,
        finding.code,
        finding.message,
        finding
          .resource_ids
          .iter()
          .map(|f| format!("`{f}`"))
          .collect::<Vec<String>>()
          .join(", "),
      ))
    }

    Some(table)
  }
}

pub(crate) async fn addon_health(addons: &[Addon]) -> Result<Vec<AddonHealthIssue>> {
  let health_issues = addons
    .iter()
    .flat_map(|addon| {
      let name = addon.addon_name().unwrap();
      let health = addon.health().unwrap();

      health
        .issues()
        .unwrap()
        .iter()
        .map(|issue| {
          let code = issue.code().unwrap();

          AddonHealthIssue {
            name: name.to_owned(),
            code: code.as_str().to_string(),
            message: issue.message().unwrap().to_owned(),
            resource_ids: issue.resource_ids().unwrap().to_owned(),
            remediation: finding::Remediation::Required,
            fcode: finding::Code::EKS004,
          }
        })
        .collect::<Vec<AddonHealthIssue>>()
    })
    .collect();

  Ok(health_issues)
}

pub(crate) async fn get_eks_managed_nodegroups(client: &EksClient, cluster_name: &str) -> Result<Vec<Nodegroup>> {
  let nodegroup_names = client
    .list_nodegroups()
    .cluster_name(cluster_name)
    // TODO - paginate this
    .max_results(100)
    .send()
    .await?
    .nodegroups
    .unwrap_or_default();

  let mut nodegroups = Vec::new();

  for nodegroup_name in nodegroup_names {
    let response = client
      .describe_nodegroup()
      .cluster_name(cluster_name)
      .nodegroup_name(nodegroup_name)
      .send()
      .await?
      .nodegroup;

    if let Some(nodegroup) = response {
      nodegroups.push(nodegroup);
    }
  }

  Ok(nodegroups)
}

/// Nodegroup health issue data
///
/// Nearly similar to the SDK's `NodegroupHealth` but flattened
/// and without `Option()`s to make it a bit more ergonomic here
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct NodegroupHealthIssue {
  pub(crate) name: String,
  pub(crate) code: String,
  pub(crate) message: String,
  pub(crate) remediation: finding::Remediation,
  pub(crate) fcode: finding::Code,
}

impl Findings for Vec<NodegroupHealthIssue> {
  fn to_markdown_table(&self, leading_whitespace: &str) -> Option<String> {
    if self.is_empty() {
      return Some(format!(
        "{leading_whitespace}✅ - There are no reported nodegroup health issues."
      ));
    }

    let mut table = String::new();
    table.push_str(&format!("{leading_whitespace}|   -   | Name  | Code  | Message |\n"));
    table.push_str(&format!("{leading_whitespace}| :---: | :---- | :---: | :------ |\n"));

    for finding in self {
      table.push_str(&format!(
        "{}| {} | `{}` | `{}` | `{}` |\n",
        leading_whitespace,
        finding.remediation.symbol(),
        finding.name,
        finding.code,
        finding.message,
      ))
    }

    Some(table)
  }
}

/// Check for any reported health issues on EKS managed node groups
pub(crate) async fn eks_managed_nodegroup_health(nodegroups: &[Nodegroup]) -> Result<Vec<NodegroupHealthIssue>> {
  let health_issues = nodegroups
    .iter()
    .flat_map(|nodegroup| {
      let name = nodegroup.nodegroup_name().unwrap();
      let health = nodegroup.health().unwrap();
      let issues = health.issues().unwrap();

      issues.iter().map(|issue| {
        let code = issue.code().unwrap();
        let message = issue.message().unwrap();

        NodegroupHealthIssue {
          name: name.to_owned(),
          code: code.as_str().to_owned(),
          message: message.to_owned(),
          remediation: finding::Remediation::Required,
          fcode: finding::Code::EKS003,
        }
      })
    })
    .collect();

  Ok(health_issues)
}

pub(crate) async fn get_self_managed_nodegroups(
  client: &AsgClient,
  cluster_name: &str,
) -> Result<Vec<AutoScalingGroup>> {
  let keys = vec![
    format!("k8s.io/cluster/{cluster_name}"),
    format!("kubernetes.io/cluster/{cluster_name}"),
  ];

  let filter = AsgFilter::builder()
    .set_name(Some("tag-key".to_string()))
    .set_values(Some(keys))
    .build();

  let response = client.describe_auto_scaling_groups().filters(filter).send().await?;
  let groups = response.auto_scaling_groups().map(|groups| groups.to_vec());

  // Filter out EKS managed node groups by the EKS MNG applied tag
  match groups {
    Some(groups) => {
      let filtered = groups
        .into_iter()
        .filter(|group| {
          group
            .tags()
            .unwrap_or_default()
            .iter()
            .all(|tag| tag.key().unwrap_or_default() != "eks:nodegroup-name")
        })
        .collect();

      Ok(filtered)
    }
    None => Ok(vec![]),
  }
}

pub(crate) async fn _get_fargate_profiles(client: &EksClient, cluster_name: &str) -> Result<Vec<FargateProfile>> {
  let profile_names = client
    .list_fargate_profiles()
    .cluster_name(cluster_name)
    // TODO - paginate this
    .max_results(100)
    .send()
    .await?
    .fargate_profile_names
    .unwrap_or_default();

  let mut profiles = Vec::new();

  for profile_name in &profile_names {
    let response = client
      .describe_fargate_profile()
      .cluster_name(cluster_name)
      .fargate_profile_name(profile_name)
      .send()
      .await?
      .fargate_profile;

    if let Some(profile) = response {
      profiles.push(profile);
    }
  }

  Ok(profiles)
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct LaunchTemplate {
  /// Name of the launch template
  pub(crate) name: String,
  /// The ID of the launch template
  pub(crate) id: String,
  /// The version of the launch template currently used/specified in the autoscaling group
  pub(crate) current_version: String,
  /// The latest version of the launch template
  pub(crate) latest_version: String,
}

async fn get_launch_template(client: &Ec2Client, id: &str) -> Result<LaunchTemplate> {
  let output = client
    .describe_launch_templates()
    .set_launch_template_ids(Some(vec![id.to_string()]))
    .send()
    .await?;

  let template = output
    .launch_templates
    .unwrap()
    .into_iter()
    .map(|lt| LaunchTemplate {
      name: lt.launch_template_name.unwrap(),
      id: lt.launch_template_id.unwrap(),
      current_version: lt.default_version_number.unwrap().to_string(),
      latest_version: lt.latest_version_number.unwrap().to_string(),
    })
    .next();

  match template {
    Some(t) => Ok(t),
    None => bail!("Unable to find launch template with id: {id}"),
  }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct ManagedNodeGroupUpdate {
  /// EKS managed node group name
  pub(crate) name: String,
  /// Name of the autoscaling group associated to the EKS managed node group
  pub(crate) autoscaling_group_name: String,
  /// Launch template controlled by users that influences the autoscaling group
  ///
  /// This distinction is important because we only consider the launch templates
  /// provided by users and not provided by EKS managed node group(s)
  pub(crate) launch_template: LaunchTemplate,
  // We do not consider launch configurations because you cannot determine if any
  // updates are pending like with launch templates and because they are being deprecated
  // https://docs.aws.amazon.com/autoscaling/ec2/userguide/launch-configurations.html
  // launch_configuration_name: Option<String>,
  pub(crate) remediation: finding::Remediation,
  pub(crate) fcode: finding::Code,
}

impl Findings for Vec<ManagedNodeGroupUpdate> {
  fn to_markdown_table(&self, leading_whitespace: &str) -> Option<String> {
    if self.is_empty() {
      return Some(format!(
        "{leading_whitespace}✅ - There are no pending updates for the EKS managed nodegroup(s)"
      ));
    }

    let mut table = String::new();
    table.push_str(&format!(
      "{leading_whitespace}|   -   | MNG Name  | Launch Template ID | Current | Latest |\n"
    ));
    table.push_str(&format!(
      "{leading_whitespace}| :---: | :-------- | :----------------- | :-----: | :----: |\n"
    ));

    for finding in self {
      table.push_str(&format!(
        "{}| {} | `{}` | `{}` | `{}` | `{}` |\n",
        leading_whitespace,
        finding.remediation.symbol(),
        finding.name,
        finding.launch_template.id,
        finding.launch_template.current_version,
        finding.launch_template.latest_version,
      ))
    }

    Some(table)
  }
}

pub(crate) async fn eks_managed_nodegroup_update(
  client: &Ec2Client,
  nodegroup: &Nodegroup,
) -> Result<Vec<ManagedNodeGroupUpdate>> {
  let launch_template_spec = nodegroup.launch_template();

  // On EKS managed node groups, there are between 1 and 2 launch templates that influence the node group.
  // If the user does not specify a launch template, EKS will provide its own template.
  // If the user does specify a launch template, EKS will merge the values from that template with its own template.
  // Therefore, the launch template shown on the autoscaling group is managed by EKS and reflective of showing
  // whether there are pending changes or not (pending changes due to launch template changes). Instead, we will only
  // check the launch template field of the EKS managed node group which is the user provided template, if there is one.
  match launch_template_spec {
    Some(launch_template_spec) => {
      let launch_template_id = launch_template_spec.id().unwrap().to_owned();
      let launch_template = get_launch_template(client, &launch_template_id).await?;

      let updates = nodegroup
        .resources()
        .unwrap()
        .auto_scaling_groups()
        .unwrap()
        .iter()
        .map(|asg| ManagedNodeGroupUpdate {
          name: nodegroup.nodegroup_name().unwrap().to_owned(),
          autoscaling_group_name: asg.name().unwrap().to_owned(),
          launch_template: launch_template.to_owned(),
          remediation: finding::Remediation::Recommended,
          fcode: finding::Code::EKS006,
        })
        // Only interested in those that are not using the latest version
        .filter(|asg| asg.launch_template.current_version != asg.launch_template.latest_version)
        .collect();
      Ok(updates)
    }
    None => Ok(vec![]),
  }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct AutoscalingGroupUpdate {
  /// Autoscaling group name
  pub(crate) name: String,
  /// Launch template used by the autoscaling group
  pub(crate) launch_template: LaunchTemplate,
  // We do not consider launch configurations because you cannot determine if any
  // updates are pending like with launch templates and because they are being deprecated
  // https://docs.aws.amazon.com/autoscaling/ec2/userguide/launch-configurations.html
  // launch_configuration_name: Option<String>,
  pub(crate) remediation: finding::Remediation,
  pub(crate) fcode: finding::Code,
}

impl Findings for Vec<AutoscalingGroupUpdate> {
  fn to_markdown_table(&self, leading_whitespace: &str) -> Option<String> {
    if self.is_empty() {
      return Some(format!(
        "{leading_whitespace}✅ - There are no pending updates for the self-managed nodegroup(s)"
      ));
    }

    let mut table = String::new();
    table.push_str(&format!(
      "{leading_whitespace}|   -   | ASG Name | Launch Template ID | Current | Latest |\n"
    ));
    table.push_str(&format!(
      "{leading_whitespace}| :---: | :------- | :----------------- | :-----: | :----: |\n"
    ));

    for finding in self {
      table.push_str(&format!(
        "{}| {} | `{}` | `{}` | `{}` | `{}` |\n",
        leading_whitespace,
        finding.remediation.symbol(),
        finding.name,
        finding.launch_template.id,
        finding.launch_template.current_version,
        finding.launch_template.latest_version,
      ))
    }

    Some(table)
  }
}

/// Returns the autoscaling groups that are not using the latest launch template version
///
/// If there are pending changes, users do not necessarily need to make any changes prior to upgrading.
/// They should, however, be aware of the version currently in use and any changes that may be
/// deployed when updating the launch template for the new Kubernetes version. For example, if the
/// current launch template version is 3 and the latest version is 5, the user should be aware that
/// there may, or may not, be additional changes that were introduced in version 4 and 5 that might be
/// deployed when the launch template is updated to version 6 for the Kubernetes version upgrade. Ideally,
/// users should be on the latest version of the launch template prior to upgrading to avoid any surprises
/// or unexpected changes.
pub(crate) async fn self_managed_nodegroup_update(
  client: &Ec2Client,
  asg: &AutoScalingGroup,
) -> Result<Option<AutoscalingGroupUpdate>> {
  let name = asg.auto_scaling_group_name().unwrap().to_owned();
  let launch_template_id = asg.launch_template().unwrap().launch_template_id().unwrap().to_owned();
  let launch_template = get_launch_template(client, &launch_template_id).await?;

  // Only interested in those that are not using the latest version
  if launch_template.current_version != launch_template.latest_version {
    let update = AutoscalingGroupUpdate {
      name,
      launch_template,
      remediation: finding::Remediation::Recommended,
      fcode: finding::Code::EKS007,
    };
    Ok(Some(update))
  } else {
    Ok(None)
  }
}