use anyhow::Result;
use aws_sdk_autoscaling::Client as AsgClient;
use aws_sdk_ec2::Client as Ec2Client;
use aws_sdk_eks::{types::Cluster, Client as EksClient};
use kube::Client as K8sClient;
use serde::{Deserialize, Serialize};
use crate::eks::{checks, resources};
#[derive(Debug, Serialize, Deserialize)]
pub struct ClusterFindings {
pub cluster_health: Vec<checks::ClusterHealthIssue>,
}
pub async fn get_cluster_findings(cluster: &Cluster) -> Result<ClusterFindings> {
let cluster_health = checks::cluster_health(cluster).await?;
Ok(ClusterFindings { cluster_health })
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SubnetFindings {
pub control_plane_ips: Vec<checks::InsufficientSubnetIps>,
pub pod_ips: Vec<checks::InsufficientSubnetIps>,
}
pub async fn get_subnet_findings(
ec2_client: &Ec2Client,
k8s_client: &K8sClient,
cluster: &Cluster,
) -> Result<SubnetFindings> {
let control_plane_ips = checks::control_plane_ips(ec2_client, cluster).await?;
let pod_ips = checks::pod_ips(ec2_client, k8s_client, 16, 256).await?;
Ok(SubnetFindings {
control_plane_ips,
pod_ips,
})
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AddonFindings {
pub version_compatibility: Vec<checks::AddonVersionCompatibility>,
pub health: Vec<checks::AddonHealthIssue>,
}
pub async fn get_addon_findings(
eks_client: &EksClient,
cluster_name: &str,
cluster_version: &str,
) -> Result<AddonFindings> {
let addons = resources::get_addons(eks_client, cluster_name).await?;
let version_compatibility = checks::addon_version_compatibility(eks_client, cluster_version, &addons).await?;
let health = checks::addon_health(&addons).await?;
Ok(AddonFindings {
version_compatibility,
health,
})
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DataPlaneFindings {
pub eks_managed_nodegroup_health: Vec<checks::NodegroupHealthIssue>,
pub eks_managed_nodegroup_update: Vec<checks::ManagedNodeGroupUpdate>,
pub self_managed_nodegroup_update: Vec<checks::AutoscalingGroupUpdate>,
pub eks_managed_nodegroups: Vec<String>,
pub self_managed_nodegroups: Vec<String>,
pub fargate_profiles: Vec<String>,
}
pub async fn get_data_plane_findings(
asg_client: &AsgClient,
ec2_client: &Ec2Client,
eks_client: &EksClient,
cluster: &Cluster,
) -> Result<DataPlaneFindings> {
let cluster_name = cluster.name().unwrap_or_default();
let eks_mngs = resources::get_eks_managed_nodegroups(eks_client, cluster_name).await?;
let self_mngs = resources::get_self_managed_nodegroups(asg_client, cluster_name).await?;
let fargate_profiles = resources::get_fargate_profiles(eks_client, cluster_name).await?;
let eks_managed_nodegroup_health = checks::eks_managed_nodegroup_health(&eks_mngs).await?;
let mut eks_managed_nodegroup_update = Vec::new();
for eks_mng in &eks_mngs {
let update = checks::eks_managed_nodegroup_update(ec2_client, eks_mng).await?;
eks_managed_nodegroup_update.push(update);
}
let mut self_managed_nodegroup_update = Vec::new();
for self_mng in &self_mngs {
let update = checks::self_managed_nodegroup_update(ec2_client, self_mng).await?;
self_managed_nodegroup_update.push(update);
}
Ok(DataPlaneFindings {
eks_managed_nodegroup_health,
eks_managed_nodegroup_update: eks_managed_nodegroup_update
.into_iter()
.flatten()
.collect::<Vec<checks::ManagedNodeGroupUpdate>>(),
self_managed_nodegroup_update: self_managed_nodegroup_update
.into_iter()
.flatten()
.collect::<Vec<checks::AutoscalingGroupUpdate>>(),
eks_managed_nodegroups: eks_mngs
.iter()
.map(|mng| mng.nodegroup_name().unwrap_or_default().to_owned())
.collect(),
self_managed_nodegroups: self_mngs
.iter()
.map(|asg| asg.auto_scaling_group_name().unwrap_or_default().to_owned())
.collect(),
fargate_profiles: fargate_profiles
.iter()
.map(|fp| fp.fargate_profile_name().unwrap_or_default().to_owned())
.collect(),
})
}