use std::collections::BTreeMap;
use std::sync::Arc;
use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use fakecloud_core::multi_account::{AccountState, MultiAccountState};
pub const EKS_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
pub const DEFAULT_K8S_VERSION: &str = "1.31";
pub type TagMap = BTreeMap<String, String>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Update {
pub id: String,
pub status: String,
pub type_: String,
pub params: Vec<(String, String)>,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Nodegroup {
pub name: String,
pub arn: String,
pub cluster_name: String,
pub version: String,
pub release_version: String,
pub status: String,
pub capacity_type: String,
pub ami_type: String,
pub node_role: String,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
pub disk_size: i64,
pub scaling_config: serde_json::Value,
pub update_config: serde_json::Value,
pub instance_types: serde_json::Value,
pub subnets: serde_json::Value,
pub labels: serde_json::Value,
pub taints: serde_json::Value,
pub remote_access: Option<serde_json::Value>,
pub launch_template: Option<serde_json::Value>,
pub asg_name: String,
pub tags: TagMap,
pub updates: BTreeMap<String, Update>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Addon {
pub name: String,
pub arn: String,
pub cluster_name: String,
pub addon_version: String,
pub status: String,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
pub service_account_role_arn: Option<String>,
pub configuration_values: Option<String>,
pub namespace: Option<String>,
pub pod_identity_associations: Vec<String>,
pub tags: TagMap,
pub updates: BTreeMap<String, Update>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssociatedPolicy {
pub policy_arn: String,
pub access_scope: serde_json::Value,
pub associated_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessEntry {
pub principal_arn: String,
pub cluster_name: String,
pub arn: String,
pub kubernetes_groups: Vec<String>,
pub username: String,
pub type_: String,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
pub tags: TagMap,
#[serde(default)]
pub associated_policies: Vec<AssociatedPolicy>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdentityProviderConfig {
pub name: String,
pub arn: String,
pub cluster_name: String,
pub issuer_url: String,
pub client_id: String,
pub username_claim: Option<String>,
pub username_prefix: Option<String>,
pub groups_claim: Option<String>,
pub groups_prefix: Option<String>,
pub required_claims: serde_json::Value,
pub status: String,
pub tags: TagMap,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PodIdentityAssociation {
pub cluster_name: String,
pub namespace: String,
pub service_account: String,
pub role_arn: String,
pub association_arn: String,
pub association_id: String,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
pub disable_session_tags: bool,
pub target_role_arn: Option<String>,
pub external_id: Option<String>,
pub tags: TagMap,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FargateProfile {
pub name: String,
pub arn: String,
pub cluster_name: String,
pub pod_execution_role_arn: String,
pub status: String,
pub created_at: DateTime<Utc>,
pub subnets: serde_json::Value,
pub selectors: serde_json::Value,
pub tags: TagMap,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cluster {
pub name: String,
pub arn: String,
pub version: String,
pub role_arn: String,
pub status: String,
pub created_at: DateTime<Utc>,
pub endpoint: String,
pub platform_version: String,
pub certificate_authority_data: String,
pub resources_vpc_config: serde_json::Value,
pub kubernetes_network_config: serde_json::Value,
pub logging: serde_json::Value,
pub tags: TagMap,
pub updates: BTreeMap<String, Update>,
#[serde(default)]
pub connector_config: Option<serde_json::Value>,
#[serde(default)]
pub encryption_config: Option<serde_json::Value>,
#[serde(default = "default_access_config")]
pub access_config: serde_json::Value,
#[serde(default = "default_upgrade_policy")]
pub upgrade_policy: serde_json::Value,
}
pub fn default_access_config() -> serde_json::Value {
serde_json::json!({ "authenticationMode": "CONFIG_MAP" })
}
pub fn default_upgrade_policy() -> serde_json::Value {
serde_json::json!({ "supportType": "EXTENDED" })
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Insight {
pub id: String,
pub name: String,
pub category: String,
pub kubernetes_version: String,
pub description: String,
pub recommendation: String,
pub status: String,
pub reason: String,
pub last_refresh_time: DateTime<Utc>,
pub last_transition_time: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InsightsRefresh {
pub status: String,
pub started_at: DateTime<Utc>,
pub ended_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Capability {
pub name: String,
pub arn: String,
pub cluster_name: String,
pub type_: String,
pub role_arn: String,
pub status: String,
pub version: String,
pub configuration: Option<serde_json::Value>,
pub tags: TagMap,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
pub delete_propagation_policy: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EksAnywhereSubscription {
pub id: String,
pub arn: String,
pub name: String,
pub created_at: DateTime<Utc>,
pub effective_date: DateTime<Utc>,
pub expiration_date: DateTime<Utc>,
pub license_quantity: i64,
pub license_type: String,
pub term_duration: i64,
pub term_unit: String,
pub status: String,
pub auto_renew: bool,
pub tags: TagMap,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EksState {
pub clusters: BTreeMap<String, Cluster>,
pub tags: BTreeMap<String, TagMap>,
#[serde(default)]
pub nodegroups: BTreeMap<String, BTreeMap<String, Nodegroup>>,
#[serde(default)]
pub fargate_profiles: BTreeMap<String, BTreeMap<String, FargateProfile>>,
#[serde(default)]
pub addons: BTreeMap<String, BTreeMap<String, Addon>>,
#[serde(default)]
pub access_entries: BTreeMap<String, BTreeMap<String, AccessEntry>>,
#[serde(default)]
pub identity_provider_configs: BTreeMap<String, BTreeMap<String, IdentityProviderConfig>>,
#[serde(default)]
pub pod_identity_associations: BTreeMap<String, BTreeMap<String, PodIdentityAssociation>>,
#[serde(default)]
pub insights: BTreeMap<String, BTreeMap<String, Insight>>,
#[serde(default)]
pub insights_refresh: BTreeMap<String, InsightsRefresh>,
#[serde(default)]
pub capabilities: BTreeMap<String, BTreeMap<String, Capability>>,
#[serde(default)]
pub eks_anywhere_subscriptions: BTreeMap<String, EksAnywhereSubscription>,
}
impl AccountState for EksState {
fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
Self::default()
}
}
pub fn cluster_arn(region: &str, account_id: &str, name: &str) -> String {
format!("arn:aws:eks:{region}:{account_id}:cluster/{name}")
}
pub fn nodegroup_arn(
region: &str,
account_id: &str,
cluster: &str,
name: &str,
id: &str,
) -> String {
format!("arn:aws:eks:{region}:{account_id}:nodegroup/{cluster}/{name}/{id}")
}
pub fn fargate_profile_arn(
region: &str,
account_id: &str,
cluster: &str,
name: &str,
id: &str,
) -> String {
format!("arn:aws:eks:{region}:{account_id}:fargateprofile/{cluster}/{name}/{id}")
}
pub fn addon_arn(region: &str, account_id: &str, cluster: &str, name: &str, id: &str) -> String {
format!("arn:aws:eks:{region}:{account_id}:addon/{cluster}/{name}/{id}")
}
pub fn access_entry_arn(
region: &str,
account_id: &str,
cluster: &str,
principal_type: &str,
principal_name: &str,
id: &str,
) -> String {
format!(
"arn:aws:eks:{region}:{account_id}:access-entry/{cluster}/{principal_type}/{account_id}/{principal_name}/{id}"
)
}
pub fn identity_provider_config_arn(
region: &str,
account_id: &str,
cluster: &str,
name: &str,
id: &str,
) -> String {
format!("arn:aws:eks:{region}:{account_id}:identityproviderconfig/{cluster}/oidc/{name}/{id}")
}
pub fn pod_identity_association_arn(
region: &str,
account_id: &str,
cluster: &str,
id: &str,
) -> String {
format!("arn:aws:eks:{region}:{account_id}:podidentityassociation/{cluster}/a-{id}")
}
pub fn capability_arn(
region: &str,
account_id: &str,
cluster: &str,
name: &str,
id: &str,
) -> String {
format!("arn:aws:eks:{region}:{account_id}:capability/{cluster}/{name}/{id}")
}
pub fn eks_anywhere_subscription_arn(region: &str, account_id: &str, id: &str) -> String {
format!("arn:aws:eks:{region}:{account_id}:eks-anywhere-subscription/{id}")
}
pub type SharedEksState = Arc<RwLock<MultiAccountState<EksState>>>;
#[derive(Debug, Serialize, Deserialize)]
pub struct EksSnapshot {
pub schema_version: u32,
pub accounts: MultiAccountState<EksState>,
}