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
//!
//! # Fetch SPU Groups
//!
//! Public API to fetch SPU Group metadata from the SC
//!
use kf_protocol::api::Request;
use kf_protocol::derive::Decode;
use kf_protocol::derive::Encode;
use k8_metadata::spg::SpuGroupSpec;
use k8_metadata::spg::SpuGroupStatus;
use k8_metadata::spg::SpuGroupStatusResolution;
use k8_metadata::spg::SpuTemplate;
use k8_metadata::metadata::TemplateSpec;
use k8_metadata::metadata::K8Obj;
use k8_metadata::spg::StorageConfig;

use crate::ScApiKey;
use crate::FlvResponseMessage;

use super::spu::FlvSpuGroupResolution;

/// Fetch SPU Groups by type
#[derive(Decode, Encode, Default, Debug)]
pub struct FlvFetchSpuGroupsRequest {}


impl Request for FlvFetchSpuGroupsRequest {
    const API_KEY: u16 = ScApiKey::FlvFetchSpuGroups as u16;
    const DEFAULT_API_VERSION: i16 = 1;
    type Response = FlvFetchSpuGroupsResponse;
}


#[derive(Encode, Decode, Default, Debug)]
pub struct FlvFetchSpuGroupsResponse {
    pub error: FlvResponseMessage,
    /// Each spu in the response.
    pub spu_groups: Vec<FlvFetchSpuGroup>,
}

#[derive(Encode, Decode, Default, Debug)]
pub struct FlvFetchSpuGroup {

    pub name: String,

    /// The number of replicas for the spu group
    pub replicas: u16,

    // The base spu id for the spu group
    pub min_id: i32,

    /// Rack label, optional parameter used by replica assignment algorithm.
    pub rack: Option<String>,

    /// storage size
    pub size: String,     

    /// Status resolution
    pub resolution: FlvSpuGroupResolution,

    /// Reason for Status resolution (if applies)
    pub reason: Option<String>,
}

impl Into<(String,SpuGroupSpec,SpuGroupStatus)> for FlvFetchSpuGroup {

    fn into(self) -> (String,SpuGroupSpec,SpuGroupStatus) {

        (
            self.name,
            SpuGroupSpec {
                replicas: self.replicas,
                min_id: Some(self.min_id),
                template: TemplateSpec {
                    spec: SpuTemplate {
                        rack: self.rack,
                        storage: Some(StorageConfig {
                            size: Some(self.size),
                            ..Default::default()
                        }),
                        ..Default::default()
                    },
                    ..Default::default()
                }

            },
            SpuGroupStatus {
                resolution: self.resolution.into(),
                ..Default::default()
            }
        )
    }
}


impl From<K8Obj<SpuGroupSpec>> for FlvFetchSpuGroup {

    fn from(item: K8Obj<SpuGroupSpec>) -> Self {

        let (name,spec,status) = (item.metadata.name,item.spec,item.status);
        let min_id = spec.min_id();
        let (replicas,template) = (spec.replicas,spec.template.spec);
        let (rack,storage) = (template.rack,template.storage.unwrap_or_default());
        Self {
            name,
            replicas,
            min_id,
            rack,
            size: storage.size(),
            resolution: status.resolution.into(),
            reason: None,
        }
    }
  
}

impl From<SpuGroupStatusResolution> for FlvSpuGroupResolution {
    
    fn from(res: SpuGroupStatusResolution) -> Self {
        
        match res {
            SpuGroupStatusResolution::Init => FlvSpuGroupResolution::Init,
            SpuGroupStatusResolution::Invalid => FlvSpuGroupResolution::Invalid,
            SpuGroupStatusResolution::Reserved => FlvSpuGroupResolution::Reserved,
        }
    }
}


impl Into<SpuGroupStatusResolution> for FlvSpuGroupResolution {
    fn into(self) -> SpuGroupStatusResolution  {
        match self {
            Self::Init => SpuGroupStatusResolution::Init,
            Self::Invalid => SpuGroupStatusResolution::Invalid,
            Self::Reserved => SpuGroupStatusResolution::Reserved,
        }
    }
}