1use std::collections::BTreeMap;
13use std::sync::Arc;
14
15use chrono::{DateTime, Utc};
16use parking_lot::RwLock;
17use serde::{Deserialize, Serialize};
18
19use fakecloud_core::multi_account::{AccountState, MultiAccountState};
20
21pub const EKS_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
22
23pub const DEFAULT_K8S_VERSION: &str = "1.31";
26
27pub type TagMap = BTreeMap<String, String>;
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct Update {
36 pub id: String,
37 pub status: String,
38 pub type_: String,
39 pub params: Vec<(String, String)>,
40 pub created_at: DateTime<Utc>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct Nodegroup {
49 pub name: String,
50 pub arn: String,
51 pub cluster_name: String,
52 pub version: String,
53 pub release_version: String,
54 pub status: String,
55 pub capacity_type: String,
56 pub ami_type: String,
57 pub node_role: String,
58 pub created_at: DateTime<Utc>,
59 pub modified_at: DateTime<Utc>,
60 pub disk_size: i64,
61 pub scaling_config: serde_json::Value,
62 pub update_config: serde_json::Value,
63 pub instance_types: serde_json::Value,
64 pub subnets: serde_json::Value,
65 pub labels: serde_json::Value,
66 pub taints: serde_json::Value,
67 pub remote_access: Option<serde_json::Value>,
69 pub launch_template: Option<serde_json::Value>,
71 pub asg_name: String,
73 pub tags: TagMap,
74 pub updates: BTreeMap<String, Update>,
77 #[serde(default)]
79 pub node_repair_config: Option<serde_json::Value>,
80 #[serde(default)]
82 pub warm_pool_config: Option<serde_json::Value>,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct Addon {
88 pub name: String,
89 pub arn: String,
90 pub cluster_name: String,
91 pub addon_version: String,
92 pub status: String,
93 pub created_at: DateTime<Utc>,
94 pub modified_at: DateTime<Utc>,
95 pub service_account_role_arn: Option<String>,
97 pub configuration_values: Option<String>,
99 pub namespace: Option<String>,
101 pub pod_identity_associations: Vec<String>,
104 pub tags: TagMap,
105 pub updates: BTreeMap<String, Update>,
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct AssociatedPolicy {
115 pub policy_arn: String,
116 pub access_scope: serde_json::Value,
117 pub associated_at: DateTime<Utc>,
118 pub modified_at: DateTime<Utc>,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
124pub struct AccessEntry {
125 pub principal_arn: String,
126 pub cluster_name: String,
127 pub arn: String,
128 pub kubernetes_groups: Vec<String>,
129 pub username: String,
130 pub type_: String,
131 pub created_at: DateTime<Utc>,
132 pub modified_at: DateTime<Utc>,
133 pub tags: TagMap,
134 #[serde(default)]
136 pub associated_policies: Vec<AssociatedPolicy>,
137}
138
139#[derive(Debug, Clone, Serialize, Deserialize)]
145pub struct IdentityProviderConfig {
146 pub name: String,
147 pub arn: String,
148 pub cluster_name: String,
149 pub issuer_url: String,
150 pub client_id: String,
151 pub username_claim: Option<String>,
152 pub username_prefix: Option<String>,
153 pub groups_claim: Option<String>,
154 pub groups_prefix: Option<String>,
155 pub required_claims: serde_json::Value,
156 pub status: String,
157 pub tags: TagMap,
158}
159
160#[derive(Debug, Clone, Serialize, Deserialize)]
164pub struct PodIdentityAssociation {
165 pub cluster_name: String,
166 pub namespace: String,
167 pub service_account: String,
168 pub role_arn: String,
169 pub association_arn: String,
170 pub association_id: String,
171 pub created_at: DateTime<Utc>,
172 pub modified_at: DateTime<Utc>,
173 pub disable_session_tags: bool,
174 pub target_role_arn: Option<String>,
176 pub external_id: Option<String>,
178 pub tags: TagMap,
179}
180
181#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct FargateProfile {
184 pub name: String,
185 pub arn: String,
186 pub cluster_name: String,
187 pub pod_execution_role_arn: String,
188 pub status: String,
189 pub created_at: DateTime<Utc>,
190 pub subnets: serde_json::Value,
191 pub selectors: serde_json::Value,
192 pub tags: TagMap,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
196pub struct Cluster {
197 pub name: String,
198 pub arn: String,
199 pub version: String,
200 pub role_arn: String,
201 pub status: String,
202 pub created_at: DateTime<Utc>,
203 pub endpoint: String,
204 pub platform_version: String,
205 pub certificate_authority_data: String,
206 pub resources_vpc_config: serde_json::Value,
208 pub kubernetes_network_config: serde_json::Value,
210 pub logging: serde_json::Value,
212 pub tags: TagMap,
213 pub updates: BTreeMap<String, Update>,
215 #[serde(default)]
218 pub connector_config: Option<serde_json::Value>,
219 #[serde(default)]
222 pub encryption_config: Option<serde_json::Value>,
223 #[serde(default = "default_access_config")]
227 pub access_config: serde_json::Value,
228 #[serde(default = "default_upgrade_policy")]
232 pub upgrade_policy: serde_json::Value,
233 #[serde(default)]
236 pub compute_config: Option<serde_json::Value>,
237 #[serde(default)]
240 pub storage_config: Option<serde_json::Value>,
241 #[serde(default)]
243 pub zonal_shift_config: Option<serde_json::Value>,
244 #[serde(default)]
246 pub remote_network_config: Option<serde_json::Value>,
247 #[serde(default)]
249 pub control_plane_scaling_config: Option<serde_json::Value>,
250 #[serde(default)]
252 pub deletion_protection: Option<bool>,
253}
254
255pub fn default_access_config() -> serde_json::Value {
258 serde_json::json!({ "authenticationMode": "CONFIG_MAP" })
259}
260
261pub fn default_upgrade_policy() -> serde_json::Value {
264 serde_json::json!({ "supportType": "EXTENDED" })
265}
266
267#[derive(Debug, Clone, Serialize, Deserialize)]
271pub struct Insight {
272 pub id: String,
273 pub name: String,
274 pub category: String,
275 pub kubernetes_version: String,
276 pub description: String,
277 pub recommendation: String,
278 pub status: String,
280 pub reason: String,
281 pub last_refresh_time: DateTime<Utc>,
282 pub last_transition_time: DateTime<Utc>,
283}
284
285#[derive(Debug, Clone, Serialize, Deserialize)]
288pub struct InsightsRefresh {
289 pub status: String,
291 pub started_at: DateTime<Utc>,
292 pub ended_at: Option<DateTime<Utc>>,
293}
294
295#[derive(Debug, Clone, Serialize, Deserialize)]
298pub struct Capability {
299 pub name: String,
300 pub arn: String,
301 pub cluster_name: String,
302 pub type_: String,
304 pub role_arn: String,
305 pub status: String,
306 pub version: String,
307 pub configuration: Option<serde_json::Value>,
309 pub tags: TagMap,
310 pub created_at: DateTime<Utc>,
311 pub modified_at: DateTime<Utc>,
312 pub delete_propagation_policy: Option<String>,
313}
314
315#[derive(Debug, Clone, Serialize, Deserialize)]
318pub struct EksAnywhereSubscription {
319 pub id: String,
320 pub arn: String,
321 pub name: String,
322 pub created_at: DateTime<Utc>,
323 pub effective_date: DateTime<Utc>,
324 pub expiration_date: DateTime<Utc>,
325 pub license_quantity: i64,
326 pub license_type: String,
328 pub term_duration: i64,
329 pub term_unit: String,
331 pub status: String,
332 pub auto_renew: bool,
333 pub tags: TagMap,
334}
335
336#[derive(Debug, Clone, Default, Serialize, Deserialize)]
337pub struct EksState {
338 pub clusters: BTreeMap<String, Cluster>,
339 pub tags: BTreeMap<String, TagMap>,
342 #[serde(default)]
347 pub nodegroups: BTreeMap<String, BTreeMap<String, Nodegroup>>,
348 #[serde(default)]
350 pub fargate_profiles: BTreeMap<String, BTreeMap<String, FargateProfile>>,
351 #[serde(default)]
353 pub addons: BTreeMap<String, BTreeMap<String, Addon>>,
354 #[serde(default)]
356 pub access_entries: BTreeMap<String, BTreeMap<String, AccessEntry>>,
357 #[serde(default)]
359 pub identity_provider_configs: BTreeMap<String, BTreeMap<String, IdentityProviderConfig>>,
360 #[serde(default)]
362 pub pod_identity_associations: BTreeMap<String, BTreeMap<String, PodIdentityAssociation>>,
363 #[serde(default)]
365 pub insights: BTreeMap<String, BTreeMap<String, Insight>>,
366 #[serde(default)]
368 pub insights_refresh: BTreeMap<String, InsightsRefresh>,
369 #[serde(default)]
371 pub capabilities: BTreeMap<String, BTreeMap<String, Capability>>,
372 #[serde(default)]
374 pub eks_anywhere_subscriptions: BTreeMap<String, EksAnywhereSubscription>,
375}
376
377impl AccountState for EksState {
378 fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
379 Self::default()
380 }
381}
382
383pub fn cluster_arn(region: &str, account_id: &str, name: &str) -> String {
385 format!("arn:aws:eks:{region}:{account_id}:cluster/{name}")
386}
387
388pub fn nodegroup_arn(
392 region: &str,
393 account_id: &str,
394 cluster: &str,
395 name: &str,
396 id: &str,
397) -> String {
398 format!("arn:aws:eks:{region}:{account_id}:nodegroup/{cluster}/{name}/{id}")
399}
400
401pub fn fargate_profile_arn(
403 region: &str,
404 account_id: &str,
405 cluster: &str,
406 name: &str,
407 id: &str,
408) -> String {
409 format!("arn:aws:eks:{region}:{account_id}:fargateprofile/{cluster}/{name}/{id}")
410}
411
412pub fn addon_arn(region: &str, account_id: &str, cluster: &str, name: &str, id: &str) -> String {
415 format!("arn:aws:eks:{region}:{account_id}:addon/{cluster}/{name}/{id}")
416}
417
418pub fn access_entry_arn(
423 region: &str,
424 account_id: &str,
425 cluster: &str,
426 principal_type: &str,
427 principal_name: &str,
428 id: &str,
429) -> String {
430 format!(
431 "arn:aws:eks:{region}:{account_id}:access-entry/{cluster}/{principal_type}/{account_id}/{principal_name}/{id}"
432 )
433}
434
435pub fn identity_provider_config_arn(
438 region: &str,
439 account_id: &str,
440 cluster: &str,
441 name: &str,
442 id: &str,
443) -> String {
444 format!("arn:aws:eks:{region}:{account_id}:identityproviderconfig/{cluster}/oidc/{name}/{id}")
445}
446
447pub fn pod_identity_association_arn(
449 region: &str,
450 account_id: &str,
451 cluster: &str,
452 id: &str,
453) -> String {
454 format!("arn:aws:eks:{region}:{account_id}:podidentityassociation/{cluster}/a-{id}")
455}
456
457pub fn capability_arn(
460 region: &str,
461 account_id: &str,
462 cluster: &str,
463 name: &str,
464 id: &str,
465) -> String {
466 format!("arn:aws:eks:{region}:{account_id}:capability/{cluster}/{name}/{id}")
467}
468
469pub fn eks_anywhere_subscription_arn(region: &str, account_id: &str, id: &str) -> String {
471 format!("arn:aws:eks:{region}:{account_id}:eks-anywhere-subscription/{id}")
472}
473
474pub type SharedEksState = Arc<RwLock<MultiAccountState<EksState>>>;
475
476#[derive(Debug, Serialize, Deserialize)]
477pub struct EksSnapshot {
478 pub schema_version: u32,
479 pub accounts: MultiAccountState<EksState>,
480}