apiforge 0.2.4

Production-grade API release automation CLI. From merged code to healthy pods in production — one command.
Documentation
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
use crate::error::{K8sError, Result};
use crate::utils::{with_retry, RetryConfig, RetryableError};
use k8s_openapi::api::apps::v1::Deployment;
use k8s_openapi::api::core::v1::{Namespace, Pod};
use kube::api::{Api, ListParams, Patch, PatchParams};
use kube::config::{KubeConfigOptions, Kubeconfig};
use kube::{Client, Config};
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;

/// Wrapper for K8s errors that implements RetryableError
#[derive(Debug)]
struct K8sRetryableError(K8sError);

impl std::fmt::Display for K8sRetryableError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(core::format_args!("{}", self.0))
    }
}

impl RetryableError for K8sRetryableError {
    fn is_retryable(&self) -> bool {
        match &self.0 {
            // API errors may be transient
            K8sError::KubeApi(msg) => {
                msg.contains("connection")
                    || msg.contains("timeout")
                    || msg.contains("503")
                    || msg.contains("504")
                    || msg.contains("429")
                    || msg.contains("ETIMEDOUT")
                    || msg.contains("temporarily unavailable")
            }
            // Cluster unreachable is transient
            K8sError::ClusterUnreachable(msg) => {
                msg.contains("connection") || msg.contains("timeout") || msg.contains("refused")
            }
            // Rollout timeout is transient in nature
            K8sError::RolloutTimeout(_) => false,
            // These are permanent errors
            K8sError::KubeconfigInvalid => false,
            K8sError::ContextNotFound(_) => false,
            K8sError::DeploymentNotFound(_, _) => false,
            K8sError::NamespaceNotFound(_) => false,
            K8sError::RolloutFailed(_) => false,
            K8sError::ManifestError(_) => false,
            K8sError::PermissionDenied(_) => false,
        }
    }
}

impl From<K8sRetryableError> for crate::error::ApiForgError {
    fn from(e: K8sRetryableError) -> Self {
        crate::error::ApiForgError::Kubernetes(e.0)
    }
}

pub struct K8sClient {
    client: Arc<Client>,
    context: String,
    retry_config: RetryConfig,
}

#[derive(Debug, Clone)]
pub struct RolloutStatus {
    pub ready: bool,
    pub ready_replicas: i32,
    pub desired_replicas: i32,
    pub updated_replicas: i32,
    pub available_replicas: i32,
    pub message: String,
}

impl K8sClient {
    pub async fn new(context: &str) -> Result<Self> {
        let kubeconfig = Kubeconfig::read().map_err(|_e| K8sError::KubeconfigInvalid)?;

        let config = Config::from_custom_kubeconfig(
            kubeconfig,
            &KubeConfigOptions {
                context: Some(context.to_string()),
                ..Default::default()
            },
        )
        .await
        .map_err(|_e| K8sError::ContextNotFound(context.to_string()))?;

        let client = Arc::new(
            Client::try_from(config).map_err(|e| K8sError::ClusterUnreachable(e.to_string()))?,
        );

        Ok(Self {
            client,
            context: context.to_string(),
            retry_config: RetryConfig::default(),
        })
    }

    pub async fn new_in_cluster() -> Result<Self> {
        let client = Arc::new(
            Client::try_default()
                .await
                .map_err(|e| K8sError::ClusterUnreachable(e.to_string()))?,
        );

        Ok(Self {
            client,
            context: "in-cluster".to_string(),
            retry_config: RetryConfig::default(),
        })
    }

    pub async fn verify_connection(&self) -> Result<()> {
        let _: Api<Namespace> = Api::all(self.client.as_ref().clone());
        Ok(())
    }

    pub async fn namespace_exists(&self, namespace: &str) -> Result<bool> {
        let client = self.client.clone();
        let namespace = namespace.to_string();
        let retry_config = self.retry_config.clone();

        let result = with_retry(&retry_config, "K8s namespace_exists", || {
            let client = client.clone();
            let namespace = namespace.clone();
            async move {
                let namespaces: Api<Namespace> = Api::all(client.as_ref().clone());
                match namespaces.get(&namespace).await {
                    Ok(_) => Ok(true),
                    Err(kube::Error::Api(err)) if err.code == 404 => Ok(false),
                    Err(e) => Err(K8sRetryableError(K8sError::KubeApi(e.to_string()))),
                }
            }
        })
        .await?;

        Ok(result)
    }

    pub async fn get_deployment(&self, namespace: &str, name: &str) -> Result<Deployment> {
        let client = self.client.clone();
        let namespace = namespace.to_string();
        let name = name.to_string();
        let retry_config = self.retry_config.clone();

        let result = with_retry(&retry_config, "K8s get_deployment", || {
            let client = client.clone();
            let namespace = namespace.clone();
            let name = name.clone();
            async move {
                let deployments: Api<Deployment> =
                    Api::namespaced(client.as_ref().clone(), &namespace);
                deployments.get(&name).await.map_err(|e| match e {
                    kube::Error::Api(err) if err.code == 404 => K8sRetryableError(
                        K8sError::DeploymentNotFound(name.clone(), namespace.clone()),
                    ),
                    _ => K8sRetryableError(K8sError::KubeApi(e.to_string())),
                })
            }
        })
        .await?;

        Ok(result)
    }

    /// Update the image of a specific container in a deployment
    /// `container` can be a container name or index (e.g., "app", "0", "sidecar")
    pub async fn update_deployment_image(
        &self,
        namespace: &str,
        deployment_name: &str,
        container: &str,
        new_image: &str,
    ) -> Result<()> {
        // Resolve container name (could be a name or an index)
        let container_name = self
            .resolve_container_name(namespace, deployment_name, container)
            .await?;

        let client = self.client.clone();
        let namespace = namespace.to_string();
        let deployment_name = deployment_name.to_string();
        let new_image = new_image.to_string();
        let retry_config = self.retry_config.clone();

        with_retry(&retry_config, "K8s update_deployment_image", || {
            let client = client.clone();
            let namespace = namespace.clone();
            let deployment_name = deployment_name.clone();
            let container_name = container_name.clone();
            let new_image = new_image.clone();
            async move {
                let deployments: Api<Deployment> =
                    Api::namespaced(client.as_ref().clone(), &namespace);

                let patch = serde_json::json!({
                    "spec": {
                        "template": {
                            "spec": {
                                "containers": [{
                                    "name": container_name,
                                    "image": new_image
                                }]
                            }
                        }
                    }
                });

                let patch_params = PatchParams::apply("apiforge");
                deployments
                    .patch(&deployment_name, &patch_params, &Patch::Strategic(patch))
                    .await
                    .map_err(|e| {
                        K8sRetryableError(K8sError::KubeApi(format!(
                            "Failed to patch deployment: {}",
                            e
                        )))
                    })?;

                Ok::<(), K8sRetryableError>(())
            }
        })
        .await?;

        Ok(())
    }

    /// Resolve a container identifier to a container name
    /// Accepts either a container name or a numeric index
    async fn resolve_container_name(
        &self,
        namespace: &str,
        deployment_name: &str,
        container: &str,
    ) -> Result<String> {
        let deployment = self.get_deployment(namespace, deployment_name).await?;
        let containers = deployment
            .spec
            .as_ref()
            .and_then(|s| s.template.spec.as_ref())
            .map(|s| &s.containers)
            .ok_or_else(|| K8sError::ManifestError("No containers in deployment".to_string()))?;

        // First try to parse as index
        if let Ok(index) = container.parse::<usize>() {
            return containers
                .get(index)
                .map(|c| c.name.clone())
                .ok_or_else(|| {
                    K8sError::ManifestError(format!("Container index {} not found", index)).into()
                });
        }

        // Otherwise treat as container name - verify it exists
        if containers.iter().any(|c| c.name == container) {
            Ok(container.to_string())
        } else {
            Err(K8sError::ManifestError(format!(
                "Container '{}' not found. Available containers: {}",
                container,
                containers
                    .iter()
                    .map(|c| c.name.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            ))
            .into())
        }
    }

    pub async fn get_rollout_status(
        &self,
        namespace: &str,
        deployment_name: &str,
    ) -> Result<RolloutStatus> {
        let deployment = self.get_deployment(namespace, deployment_name).await?;

        let spec_replicas = deployment
            .spec
            .as_ref()
            .and_then(|s| s.replicas)
            .unwrap_or(1);

        let status = deployment.status.as_ref();

        let ready_replicas = status.and_then(|s| s.ready_replicas).unwrap_or(0);
        let updated_replicas = status.and_then(|s| s.updated_replicas).unwrap_or(0);
        let available_replicas = status.and_then(|s| s.available_replicas).unwrap_or(0);

        let ready = ready_replicas >= spec_replicas
            && updated_replicas >= spec_replicas
            && available_replicas >= spec_replicas;

        let message = if ready {
            format!(
                "Deployment {} successfully rolled out ({}/{} replicas ready)",
                deployment_name, ready_replicas, spec_replicas
            )
        } else {
            format!(
                "Rolling out: {}/{} ready, {}/{} updated",
                ready_replicas, spec_replicas, updated_replicas, spec_replicas
            )
        };

        Ok(RolloutStatus {
            ready,
            ready_replicas,
            desired_replicas: spec_replicas,
            updated_replicas,
            available_replicas,
            message,
        })
    }

    pub async fn wait_for_rollout<F>(
        &self,
        namespace: &str,
        deployment_name: &str,
        timeout_seconds: u64,
        on_progress: F,
    ) -> Result<RolloutStatus>
    where
        F: Fn(&RolloutStatus),
    {
        let start = std::time::Instant::now();
        let timeout = Duration::from_secs(timeout_seconds);
        let poll_interval = Duration::from_secs(2);

        loop {
            let status = self.get_rollout_status(namespace, deployment_name).await?;
            on_progress(&status);

            if status.ready {
                return Ok(status);
            }

            if start.elapsed() >= timeout {
                return Err(K8sError::RolloutTimeout(timeout_seconds).into());
            }

            sleep(poll_interval).await;
        }
    }

    pub async fn get_pods_for_deployment(
        &self,
        namespace: &str,
        deployment_name: &str,
    ) -> Result<Vec<Pod>> {
        let deployment = self.get_deployment(namespace, deployment_name).await?;

        let match_labels = deployment
            .spec
            .as_ref()
            .and_then(|s| s.selector.match_labels.clone())
            .unwrap_or_default();

        let label_selector = match_labels
            .iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect::<Vec<_>>()
            .join(",");

        let client = self.client.clone();
        let namespace = namespace.to_string();
        let retry_config = self.retry_config.clone();

        let result = with_retry(&retry_config, "K8s get_pods_for_deployment", || {
            let client = client.clone();
            let namespace = namespace.clone();
            let label_selector = label_selector.clone();
            async move {
                let pods: Api<Pod> = Api::namespaced(client.as_ref().clone(), &namespace);
                let list_params = ListParams::default().labels(&label_selector);

                let pod_list = pods
                    .list(&list_params)
                    .await
                    .map_err(|e| K8sRetryableError(K8sError::KubeApi(e.to_string())))?;

                Ok::<Vec<Pod>, K8sRetryableError>(pod_list.items)
            }
        })
        .await?;

        Ok(result)
    }

    pub async fn restart_deployment(&self, namespace: &str, deployment_name: &str) -> Result<()> {
        let client = self.client.clone();
        let namespace = namespace.to_string();
        let deployment_name = deployment_name.to_string();
        let retry_config = self.retry_config.clone();

        with_retry(&retry_config, "K8s restart_deployment", || {
            let client = client.clone();
            let namespace = namespace.clone();
            let deployment_name = deployment_name.clone();
            async move {
                let deployments: Api<Deployment> = Api::namespaced(client.as_ref().clone(), &namespace);

                let patch = serde_json::json!({
                    "spec": {
                        "template": {
                            "metadata": {
                                "annotations": {
                                    "kubectl.kubernetes.io/restartedAt": chrono::Utc::now().to_rfc3339()
                                }
                            }
                        }
                    }
                });

                let patch_params = PatchParams::apply("apiforge");
                deployments
                    .patch(&deployment_name, &patch_params, &Patch::Strategic(patch))
                    .await
                    .map_err(|e| K8sRetryableError(K8sError::KubeApi(format!("Failed to restart deployment: {}", e))))?;

                Ok::<(), K8sRetryableError>(())
            }
        }).await?;

        Ok(())
    }

    pub async fn scale_deployment(
        &self,
        namespace: &str,
        deployment_name: &str,
        replicas: i32,
    ) -> Result<()> {
        let client = self.client.clone();
        let namespace = namespace.to_string();
        let deployment_name = deployment_name.to_string();
        let retry_config = self.retry_config.clone();

        with_retry(&retry_config, "K8s scale_deployment", || {
            let client = client.clone();
            let namespace = namespace.clone();
            let deployment_name = deployment_name.clone();
            async move {
                let deployments: Api<Deployment> =
                    Api::namespaced(client.as_ref().clone(), &namespace);

                let patch = serde_json::json!({
                    "spec": {
                        "replicas": replicas
                    }
                });

                let patch_params = PatchParams::apply("apiforge");
                deployments
                    .patch(&deployment_name, &patch_params, &Patch::Strategic(patch))
                    .await
                    .map_err(|e| {
                        K8sRetryableError(K8sError::KubeApi(format!(
                            "Failed to scale deployment: {}",
                            e
                        )))
                    })?;

                Ok::<(), K8sRetryableError>(())
            }
        })
        .await?;

        Ok(())
    }

    pub fn context(&self) -> &str {
        &self.context
    }

    /// Rollback a deployment to a previous revision.
    /// If `revision` is None, rolls back to the previous revision.
    /// If `revision` is Some(n), rolls back to revision n.
    pub async fn rollback_deployment(
        &self,
        namespace: &str,
        deployment_name: &str,
        revision: Option<i64>,
    ) -> Result<()> {
        // Get the deployment to verify it exists and get current state
        let deployment = self.get_deployment(namespace, deployment_name).await?;

        let client = self.client.clone();
        let namespace_str = namespace.to_string();
        let deployment_name_str = deployment_name.to_string();
        let retry_config = self.retry_config.clone();

        // Get the ReplicaSets to find the revision to rollback to
        use k8s_openapi::api::apps::v1::ReplicaSet;

        // Get selector labels from deployment
        let match_labels = deployment
            .spec
            .as_ref()
            .and_then(|s| s.selector.match_labels.clone())
            .unwrap_or_default();

        let label_selector = match_labels
            .iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect::<Vec<_>>()
            .join(",");

        // Fetch and sort replicasets
        let rs_list = with_retry(&retry_config, "K8s list_replicasets", || {
            let client = client.clone();
            let namespace_str = namespace_str.clone();
            let label_selector = label_selector.clone();
            async move {
                let replicasets: Api<ReplicaSet> =
                    Api::namespaced(client.as_ref().clone(), &namespace_str);
                let list_params = ListParams::default().labels(&label_selector);
                replicasets.list(&list_params).await.map_err(|e| {
                    K8sRetryableError(K8sError::KubeApi(format!(
                        "Failed to list ReplicaSets: {}",
                        e
                    )))
                })
            }
        })
        .await?;

        // Sort ReplicaSets by revision annotation (descending)
        let mut replica_sets: Vec<_> = rs_list.items.into_iter().collect();
        replica_sets.sort_by(|a, b| {
            let rev_a = a
                .metadata
                .annotations
                .as_ref()
                .and_then(|ann| ann.get("deployment.kubernetes.io/revision"))
                .and_then(|v| v.parse::<i64>().ok())
                .unwrap_or(0);
            let rev_b = b
                .metadata
                .annotations
                .as_ref()
                .and_then(|ann| ann.get("deployment.kubernetes.io/revision"))
                .and_then(|v| v.parse::<i64>().ok())
                .unwrap_or(0);
            rev_b.cmp(&rev_a) // Descending order
        });

        // Find the target revision
        let target_rs = if let Some(target_rev) = revision {
            // Find specific revision
            replica_sets.iter().find(|rs| {
                rs.metadata
                    .annotations
                    .as_ref()
                    .and_then(|ann| ann.get("deployment.kubernetes.io/revision"))
                    .and_then(|v| v.parse::<i64>().ok())
                    == Some(target_rev)
            })
        } else {
            // Get the second most recent revision (previous)
            replica_sets.get(1)
        };

        let target_rs = target_rs.ok_or_else(|| {
            K8sError::RolloutFailed("No previous revision found to rollback to".to_string())
        })?;

        // Extract the pod template spec from the target ReplicaSet
        let target_template = target_rs
            .spec
            .as_ref()
            .map(|s| s.template.clone())
            .ok_or_else(|| {
                K8sError::RolloutFailed("Target ReplicaSet has no template".to_string())
            })?;

        // Patch the deployment with the previous pod template
        // This mimics `kubectl rollout undo`
        let patch = serde_json::json!({
            "spec": {
                "template": target_template
            }
        });

        with_retry(&retry_config, "K8s rollback_deployment", || {
            let client = client.clone();
            let namespace_str = namespace_str.clone();
            let deployment_name_str = deployment_name_str.clone();
            let patch = patch.clone();
            async move {
                let deployments: Api<Deployment> =
                    Api::namespaced(client.as_ref().clone(), &namespace_str);
                let patch_params = PatchParams::apply("apiforge-rollback");
                deployments
                    .patch(
                        &deployment_name_str,
                        &patch_params,
                        &Patch::Strategic(patch),
                    )
                    .await
                    .map_err(|e| {
                        K8sRetryableError(K8sError::KubeApi(format!(
                            "Failed to rollback deployment: {}",
                            e
                        )))
                    })?;
                Ok::<(), K8sRetryableError>(())
            }
        })
        .await?;

        tracing::info!(
            "Rolled back deployment {} to previous revision",
            deployment_name
        );

        Ok(())
    }

    /// Get the current revision number of a deployment
    pub async fn get_deployment_revision(
        &self,
        namespace: &str,
        deployment_name: &str,
    ) -> Result<Option<i64>> {
        let deployment = self.get_deployment(namespace, deployment_name).await?;

        Ok(deployment
            .metadata
            .annotations
            .as_ref()
            .and_then(|ann| ann.get("deployment.kubernetes.io/revision"))
            .and_then(|v| v.parse::<i64>().ok()))
    }

    /// List available revisions for a deployment
    pub async fn list_deployment_revisions(
        &self,
        namespace: &str,
        deployment_name: &str,
    ) -> Result<Vec<i64>> {
        use k8s_openapi::api::apps::v1::ReplicaSet;

        let deployment = self.get_deployment(namespace, deployment_name).await?;

        let client = self.client.clone();
        let namespace = namespace.to_string();
        let retry_config = self.retry_config.clone();

        // Get selector labels from deployment
        let match_labels = deployment
            .spec
            .as_ref()
            .and_then(|s| s.selector.match_labels.clone())
            .unwrap_or_default();

        let label_selector = match_labels
            .iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect::<Vec<_>>()
            .join(",");

        let rs_list = with_retry(&retry_config, "K8s list_deployment_revisions", || {
            let client = client.clone();
            let namespace = namespace.clone();
            let label_selector = label_selector.clone();
            async move {
                let replicasets: Api<ReplicaSet> =
                    Api::namespaced(client.as_ref().clone(), &namespace);
                let list_params = ListParams::default().labels(&label_selector);
                replicasets.list(&list_params).await.map_err(|e| {
                    K8sRetryableError(K8sError::KubeApi(format!(
                        "Failed to list ReplicaSets: {}",
                        e
                    )))
                })
            }
        })
        .await?;

        let mut revisions: Vec<i64> = rs_list
            .items
            .iter()
            .filter_map(|rs| {
                rs.metadata
                    .annotations
                    .as_ref()
                    .and_then(|ann| ann.get("deployment.kubernetes.io/revision"))
                    .and_then(|v| v.parse::<i64>().ok())
            })
            .collect();

        revisions.sort_by(|a, b| b.cmp(a)); // Descending
        Ok(revisions)
    }
}