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}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct Addon {
82 pub name: String,
83 pub arn: String,
84 pub cluster_name: String,
85 pub addon_version: String,
86 pub status: String,
87 pub created_at: DateTime<Utc>,
88 pub modified_at: DateTime<Utc>,
89 pub service_account_role_arn: Option<String>,
91 pub configuration_values: Option<String>,
93 pub namespace: Option<String>,
95 pub pod_identity_associations: Vec<String>,
98 pub tags: TagMap,
99 pub updates: BTreeMap<String, Update>,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct AssociatedPolicy {
109 pub policy_arn: String,
110 pub access_scope: serde_json::Value,
111 pub associated_at: DateTime<Utc>,
112 pub modified_at: DateTime<Utc>,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct AccessEntry {
119 pub principal_arn: String,
120 pub cluster_name: String,
121 pub arn: String,
122 pub kubernetes_groups: Vec<String>,
123 pub username: String,
124 pub type_: String,
125 pub created_at: DateTime<Utc>,
126 pub modified_at: DateTime<Utc>,
127 pub tags: TagMap,
128 #[serde(default)]
130 pub associated_policies: Vec<AssociatedPolicy>,
131}
132
133#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct IdentityProviderConfig {
140 pub name: String,
141 pub arn: String,
142 pub cluster_name: String,
143 pub issuer_url: String,
144 pub client_id: String,
145 pub username_claim: Option<String>,
146 pub username_prefix: Option<String>,
147 pub groups_claim: Option<String>,
148 pub groups_prefix: Option<String>,
149 pub required_claims: serde_json::Value,
150 pub status: String,
151 pub tags: TagMap,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct PodIdentityAssociation {
159 pub cluster_name: String,
160 pub namespace: String,
161 pub service_account: String,
162 pub role_arn: String,
163 pub association_arn: String,
164 pub association_id: String,
165 pub created_at: DateTime<Utc>,
166 pub modified_at: DateTime<Utc>,
167 pub disable_session_tags: bool,
168 pub target_role_arn: Option<String>,
170 pub external_id: Option<String>,
172 pub tags: TagMap,
173}
174
175#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct FargateProfile {
178 pub name: String,
179 pub arn: String,
180 pub cluster_name: String,
181 pub pod_execution_role_arn: String,
182 pub status: String,
183 pub created_at: DateTime<Utc>,
184 pub subnets: serde_json::Value,
185 pub selectors: serde_json::Value,
186 pub tags: TagMap,
187}
188
189#[derive(Debug, Clone, Serialize, Deserialize)]
190pub struct Cluster {
191 pub name: String,
192 pub arn: String,
193 pub version: String,
194 pub role_arn: String,
195 pub status: String,
196 pub created_at: DateTime<Utc>,
197 pub endpoint: String,
198 pub platform_version: String,
199 pub certificate_authority_data: String,
200 pub resources_vpc_config: serde_json::Value,
202 pub kubernetes_network_config: serde_json::Value,
204 pub logging: serde_json::Value,
206 pub tags: TagMap,
207 pub updates: BTreeMap<String, Update>,
209 #[serde(default)]
212 pub connector_config: Option<serde_json::Value>,
213 #[serde(default)]
216 pub encryption_config: Option<serde_json::Value>,
217 #[serde(default = "default_access_config")]
221 pub access_config: serde_json::Value,
222 #[serde(default = "default_upgrade_policy")]
226 pub upgrade_policy: serde_json::Value,
227 #[serde(default)]
230 pub compute_config: Option<serde_json::Value>,
231 #[serde(default)]
234 pub storage_config: Option<serde_json::Value>,
235 #[serde(default)]
237 pub zonal_shift_config: Option<serde_json::Value>,
238 #[serde(default)]
240 pub remote_network_config: Option<serde_json::Value>,
241 #[serde(default)]
243 pub control_plane_scaling_config: Option<serde_json::Value>,
244 #[serde(default)]
246 pub deletion_protection: Option<bool>,
247}
248
249pub fn default_access_config() -> serde_json::Value {
252 serde_json::json!({ "authenticationMode": "CONFIG_MAP" })
253}
254
255pub fn default_upgrade_policy() -> serde_json::Value {
258 serde_json::json!({ "supportType": "EXTENDED" })
259}
260
261#[derive(Debug, Clone, Serialize, Deserialize)]
265pub struct Insight {
266 pub id: String,
267 pub name: String,
268 pub category: String,
269 pub kubernetes_version: String,
270 pub description: String,
271 pub recommendation: String,
272 pub status: String,
274 pub reason: String,
275 pub last_refresh_time: DateTime<Utc>,
276 pub last_transition_time: DateTime<Utc>,
277}
278
279#[derive(Debug, Clone, Serialize, Deserialize)]
282pub struct InsightsRefresh {
283 pub status: String,
285 pub started_at: DateTime<Utc>,
286 pub ended_at: Option<DateTime<Utc>>,
287}
288
289#[derive(Debug, Clone, Serialize, Deserialize)]
292pub struct Capability {
293 pub name: String,
294 pub arn: String,
295 pub cluster_name: String,
296 pub type_: String,
298 pub role_arn: String,
299 pub status: String,
300 pub version: String,
301 pub configuration: Option<serde_json::Value>,
303 pub tags: TagMap,
304 pub created_at: DateTime<Utc>,
305 pub modified_at: DateTime<Utc>,
306 pub delete_propagation_policy: Option<String>,
307}
308
309#[derive(Debug, Clone, Serialize, Deserialize)]
312pub struct EksAnywhereSubscription {
313 pub id: String,
314 pub arn: String,
315 pub name: String,
316 pub created_at: DateTime<Utc>,
317 pub effective_date: DateTime<Utc>,
318 pub expiration_date: DateTime<Utc>,
319 pub license_quantity: i64,
320 pub license_type: String,
322 pub term_duration: i64,
323 pub term_unit: String,
325 pub status: String,
326 pub auto_renew: bool,
327 pub tags: TagMap,
328}
329
330#[derive(Debug, Clone, Default, Serialize, Deserialize)]
331pub struct EksState {
332 pub clusters: BTreeMap<String, Cluster>,
333 pub tags: BTreeMap<String, TagMap>,
336 #[serde(default)]
341 pub nodegroups: BTreeMap<String, BTreeMap<String, Nodegroup>>,
342 #[serde(default)]
344 pub fargate_profiles: BTreeMap<String, BTreeMap<String, FargateProfile>>,
345 #[serde(default)]
347 pub addons: BTreeMap<String, BTreeMap<String, Addon>>,
348 #[serde(default)]
350 pub access_entries: BTreeMap<String, BTreeMap<String, AccessEntry>>,
351 #[serde(default)]
353 pub identity_provider_configs: BTreeMap<String, BTreeMap<String, IdentityProviderConfig>>,
354 #[serde(default)]
356 pub pod_identity_associations: BTreeMap<String, BTreeMap<String, PodIdentityAssociation>>,
357 #[serde(default)]
359 pub insights: BTreeMap<String, BTreeMap<String, Insight>>,
360 #[serde(default)]
362 pub insights_refresh: BTreeMap<String, InsightsRefresh>,
363 #[serde(default)]
365 pub capabilities: BTreeMap<String, BTreeMap<String, Capability>>,
366 #[serde(default)]
368 pub eks_anywhere_subscriptions: BTreeMap<String, EksAnywhereSubscription>,
369}
370
371impl AccountState for EksState {
372 fn new_for_account(_account_id: &str, _region: &str, _endpoint: &str) -> Self {
373 Self::default()
374 }
375}
376
377pub fn cluster_arn(region: &str, account_id: &str, name: &str) -> String {
379 format!("arn:aws:eks:{region}:{account_id}:cluster/{name}")
380}
381
382pub fn nodegroup_arn(
386 region: &str,
387 account_id: &str,
388 cluster: &str,
389 name: &str,
390 id: &str,
391) -> String {
392 format!("arn:aws:eks:{region}:{account_id}:nodegroup/{cluster}/{name}/{id}")
393}
394
395pub fn fargate_profile_arn(
397 region: &str,
398 account_id: &str,
399 cluster: &str,
400 name: &str,
401 id: &str,
402) -> String {
403 format!("arn:aws:eks:{region}:{account_id}:fargateprofile/{cluster}/{name}/{id}")
404}
405
406pub fn addon_arn(region: &str, account_id: &str, cluster: &str, name: &str, id: &str) -> String {
409 format!("arn:aws:eks:{region}:{account_id}:addon/{cluster}/{name}/{id}")
410}
411
412pub fn access_entry_arn(
417 region: &str,
418 account_id: &str,
419 cluster: &str,
420 principal_type: &str,
421 principal_name: &str,
422 id: &str,
423) -> String {
424 format!(
425 "arn:aws:eks:{region}:{account_id}:access-entry/{cluster}/{principal_type}/{account_id}/{principal_name}/{id}"
426 )
427}
428
429pub fn identity_provider_config_arn(
432 region: &str,
433 account_id: &str,
434 cluster: &str,
435 name: &str,
436 id: &str,
437) -> String {
438 format!("arn:aws:eks:{region}:{account_id}:identityproviderconfig/{cluster}/oidc/{name}/{id}")
439}
440
441pub fn pod_identity_association_arn(
443 region: &str,
444 account_id: &str,
445 cluster: &str,
446 id: &str,
447) -> String {
448 format!("arn:aws:eks:{region}:{account_id}:podidentityassociation/{cluster}/a-{id}")
449}
450
451pub fn capability_arn(
454 region: &str,
455 account_id: &str,
456 cluster: &str,
457 name: &str,
458 id: &str,
459) -> String {
460 format!("arn:aws:eks:{region}:{account_id}:capability/{cluster}/{name}/{id}")
461}
462
463pub fn eks_anywhere_subscription_arn(region: &str, account_id: &str, id: &str) -> String {
465 format!("arn:aws:eks:{region}:{account_id}:eks-anywhere-subscription/{id}")
466}
467
468pub type SharedEksState = Arc<RwLock<MultiAccountState<EksState>>>;
469
470#[derive(Debug, Serialize, Deserialize)]
471pub struct EksSnapshot {
472 pub schema_version: u32,
473 pub accounts: MultiAccountState<EksState>,
474}