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
use async_trait::async_trait;
use k8s_openapi::api::apps::v1::Deployment;
use kube::{
    api::{Patch, PatchParams},
    Api,
};
use serde::de::DeserializeOwned;
use serde_json::json;
use std::fmt::Debug;

pub trait Scalable {}

#[async_trait]
pub trait Scale {
    type Resource;
    async fn replicas(&self, name: &str, replicas: i32) -> Result<Self::Resource, kube::Error>;
}

#[async_trait]
impl<S> Scale for Api<S>
where
    S: Scalable + Clone + DeserializeOwned + Debug,
{
    type Resource = S;

    async fn replicas(&self, name: &str, replicas: i32) -> Result<Self::Resource, kube::Error> {
        let pp = PatchParams::default();
        self.patch(
            name,
            &pp,
            &Patch::Strategic(json!({"spec":{"replicas": replicas}})),
        )
        .await
    }
}

impl Scalable for Deployment {}