k8_obj_core/
pod.rs

1use serde::Deserialize;
2use serde::Serialize;
3use serde_json::Value as DynamicObject;
4
5use k8_obj_metadata::Crd;
6use k8_obj_metadata::CrdNames;
7use k8_obj_metadata::DefaultHeader;
8use k8_obj_metadata::Env;
9use k8_obj_metadata::Spec;
10use k8_obj_metadata::Status;
11
12//
13// Pod Object
14
15const POD_API: Crd = Crd {
16    group: "core",
17    version: "v1",
18    names: CrdNames {
19        kind: "Pod",
20        plural: "pods",
21        singular: "pod",
22    },
23};
24
25impl Spec for PodSpec {
26    type Status = PodStatus;
27    type Header = DefaultHeader;
28
29    fn metadata() -> &'static Crd {
30        &POD_API
31    }
32}
33
34#[derive(Deserialize, Serialize, Debug, Default, Clone)]
35#[serde(rename_all = "camelCase", default)]
36pub struct PodSpec {
37    pub volumes: Vec<VolumeSpec>,
38    pub containers: Vec<ContainerSpec>,
39    pub restart_policy: Option<String>, // TODO; should be enum
40    pub service_account_name: Option<String>,
41    pub service_account: Option<String>,
42    pub node_name: Option<String>,
43    pub termination_grace_period_seconds: Option<u16>,
44    pub dns_policy: Option<String>,
45    pub security_context: Option<PodSecurityContext>,
46    pub scheduler_name: Option<String>,
47}
48
49#[derive(Deserialize, Serialize, Debug, Default, Clone)]
50#[serde(rename_all = "camelCase", default)]
51pub struct PodSecurityContext {
52    pub fs_group: Option<u32>,
53    pub run_as_group: Option<u32>,
54    pub run_as_non_root: Option<bool>,
55    pub run_as_user: Option<u32>,
56    pub sysctls: Vec<Sysctl>,
57}
58
59#[derive(Deserialize, Serialize, Debug, Default, Clone)]
60#[serde(rename_all = "camelCase")]
61pub struct Sysctl {
62    pub name: String,
63    pub value: String,
64}
65
66#[derive(Deserialize, Serialize, Default, Debug, Clone)]
67#[serde(rename_all = "camelCase", default)]
68pub struct ContainerSpec {
69    pub name: String,
70    pub args: Vec<String>,
71    pub command: Vec<String>,
72    pub ports: Vec<ContainerPortSpec>,
73    pub image: Option<String>,
74    pub image_pull_policy: Option<String>, // TODO: should be enum
75    pub volume_mounts: Vec<VolumeMount>,
76    pub env: Vec<Env>,
77    pub resources: Option<ResourceRequirements>,
78    pub termination_mssage_path: Option<String>,
79    pub termination_message_policy: Option<String>,
80    pub tty: Option<bool>,
81}
82
83#[derive(Deserialize, Serialize, Default, Debug, Clone)]
84#[serde(rename_all = "camelCase", default)]
85pub struct ResourceRequirements {
86    pub limits: DynamicObject,
87    pub requests: DynamicObject,
88}
89
90#[derive(Deserialize, Serialize, Default, Debug, Clone)]
91#[serde(rename_all = "camelCase")]
92pub struct ContainerPortSpec {
93    pub container_port: u16,
94    pub name: Option<String>,
95    pub protocol: Option<String>, // TODO: This should be enum
96}
97
98impl ContainerPortSpec {
99    pub fn new<T: Into<String>>(container_port: u16, name: T) -> Self {
100        ContainerPortSpec {
101            container_port,
102            name: Some(name.into()),
103            protocol: None,
104        }
105    }
106}
107
108#[derive(Deserialize, Serialize, Default, Debug, Clone)]
109pub struct VolumeSpec {
110    pub name: String,
111    pub secret: Option<SecretVolumeSpec>,
112    pub persistent_volume_claim: Option<PersistentVolumeClaimVolumeSource>,
113}
114
115#[derive(Deserialize, Serialize, Default, Debug, Clone)]
116#[serde(rename_all = "camelCase")]
117pub struct VolumeMount {
118    pub mount_path: String,
119    pub mount_propagation: Option<String>,
120    pub name: String,
121    pub read_only: Option<bool>,
122    pub sub_path: Option<String>,
123}
124
125#[derive(Deserialize, Serialize, Default, Debug, Clone)]
126#[serde(rename_all = "camelCase")]
127pub struct SecretVolumeSpec {
128    pub default_mode: u16,
129    pub secret_name: String,
130    pub optional: Option<bool>,
131}
132
133#[derive(Deserialize, Serialize, Default, Debug, Clone)]
134#[serde(rename_all = "camelCase")]
135pub struct PersistentVolumeClaimVolumeSource {
136    claim_name: String,
137    read_only: bool,
138}
139
140#[derive(Deserialize, Serialize, Default, Debug, Clone)]
141#[serde(rename_all = "camelCase")]
142pub struct PodStatus {
143    pub phase: String,
144    #[serde(rename = "hostIP")]
145    pub host_ip: String,
146    #[serde(rename = "podIP")]
147    pub pod_ip: Option<String>,
148    pub start_time: String,
149    pub container_statuses: Vec<ContainerStatus>,
150}
151
152impl Status for PodStatus {}
153
154#[derive(Deserialize, Serialize, Debug, Clone)]
155#[serde(rename_all = "camelCase")]
156pub struct ContainerStatus {
157    pub name: String,
158    pub state: ContainerState,
159    pub ready: bool,
160    pub restart_count: u8,
161    pub image: String,
162    #[serde(rename = "imageID")]
163    pub image_id: String,
164    #[serde(rename = "containerID")]
165    pub container_id: Option<String>,
166}
167
168#[derive(Deserialize, Serialize, Debug, Clone)]
169#[serde(rename_all = "camelCase")]
170pub struct ContainerState {
171    pub running: Option<ContainerStateRunning>,
172}
173
174#[derive(Deserialize, Serialize, Debug, Clone)]
175#[serde(rename_all = "camelCase")]
176pub struct ContainerStateRunning {
177    pub started_at: String,
178}