|
| 1 | +use k8s_openapi::apimachinery::pkg::apis::meta::v1::Time; |
| 2 | +use kube::CustomResource; |
| 3 | +use schemars::JsonSchema; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +#[cfg(doc)] |
| 7 | +use crate::kvp::Annotation; |
| 8 | +use crate::versioned::versioned; |
| 9 | + |
| 10 | +#[versioned(version(name = "v1alpha1"))] |
| 11 | +pub mod versioned { |
| 12 | + #[versioned(crd( |
| 13 | + group = "autoscaling.stackable.tech", |
| 14 | + status = ScalerStatus, |
| 15 | + scale( |
| 16 | + spec_replicas_path = ".spec.replicas", |
| 17 | + status_replicas_path = ".status.replicas", |
| 18 | + label_selector_path = ".status.selector" |
| 19 | + ), |
| 20 | + namespaced |
| 21 | + ))] |
| 22 | + #[derive(Clone, Debug, PartialEq, CustomResource, Deserialize, Serialize, JsonSchema)] |
| 23 | + pub struct ScalerSpec { |
| 24 | + /// Desired replica count. |
| 25 | + /// |
| 26 | + /// Written by the horizontal pod autoscaling mechanism via the /scale subresource. |
| 27 | + /// |
| 28 | + /// NOTE: This and other replica fields)use a [`u16`] instead of a [`i32`] used by |
| 29 | + /// [`k8s_openapi`] types to force a non-negative replica count. All [`u16`]s can be |
| 30 | + /// converted losslessly to [`i32`]s where needed. |
| 31 | + /// |
| 32 | + /// Upstream issues: |
| 33 | + /// |
| 34 | + /// - https://github.com/kubernetes/kubernetes/issues/105533 |
| 35 | + /// - https://github.com/Arnavion/k8s-openapi/issues/136 |
| 36 | + pub replicas: u16, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Status of a StackableScaler. |
| 41 | +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] |
| 42 | +#[serde(rename_all = "camelCase")] |
| 43 | +pub struct ScalerStatus { |
| 44 | + /// The current total number of replicas targeted by the managed StatefulSet. |
| 45 | + /// |
| 46 | + /// Exposed via the `/scale` subresource for horizontal pod autoscaling consumption. |
| 47 | + pub replicas: u16, |
| 48 | + |
| 49 | + /// Label selector string for HPA pod counting. Written at `.status.selector`. |
| 50 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 51 | + pub selector: Option<String>, |
| 52 | + |
| 53 | + /// The current state of the scaler state machine. |
| 54 | + pub state: ScalerState, |
| 55 | + |
| 56 | + /// Timestamp indicating when the scaler state last transitioned. |
| 57 | + pub last_transition_time: Time, |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, strum::Display)] |
| 61 | +#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")] |
| 62 | +#[strum(serialize_all = "camelCase")] |
| 63 | +pub enum ScalerState { |
| 64 | + /// No scaling operation is in progress. |
| 65 | + Idle, |
| 66 | + |
| 67 | + /// Running the `pre_scale` hook (e.g. data offload). |
| 68 | + PreScaling, |
| 69 | + |
| 70 | + /// Waiting for the StatefulSet to converge to the new replica count. |
| 71 | + /// |
| 72 | + /// This stage additionally tracks the previous replica count to be able derive the direction |
| 73 | + /// of the scaling operation. |
| 74 | + Scaling { previous_replicas: u16 }, |
| 75 | + |
| 76 | + /// Running the `post_scale` hook (e.g. cluster rebalance). |
| 77 | + /// |
| 78 | + /// This stage additionally tracks the previous replica count to be able derive the direction |
| 79 | + /// of the scaling operation. |
| 80 | + PostScaling { previous_replicas: u16 }, |
| 81 | + |
| 82 | + /// A hook returned an error. |
| 83 | + /// |
| 84 | + /// The scaler stays here until the user applies the [`Annotation::autoscaling_retry`] annotation |
| 85 | + /// to trigger a reset to [`ScalerState::Idle`]. |
| 86 | + Failed { |
| 87 | + /// Which stage produced the error. |
| 88 | + failed_in: FailedInState, |
| 89 | + |
| 90 | + /// Human-readable error message from the hook. |
| 91 | + reason: String, |
| 92 | + }, |
| 93 | +} |
| 94 | + |
| 95 | +/// Which stage of a scaling operation failed. |
| 96 | +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] |
| 97 | +#[serde(rename_all = "camelCase")] |
| 98 | +pub enum FailedInState { |
| 99 | + /// The `pre_scale` hook returned an error. |
| 100 | + PreScaling, |
| 101 | + |
| 102 | + /// The StatefulSet failed to reach the desired replica count. |
| 103 | + Scaling, |
| 104 | + |
| 105 | + /// The `post_scale` hook returned an error. |
| 106 | + PostScaling, |
| 107 | +} |
0 commit comments