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
#![doc = include_str!("../README.md")]

pub use builder::SessionLivedBackendBuilder;
use k8s_openapi::{
    api::core::v1::{
        Container, Event, LocalObjectReference, Pod, PodSpec, Service, ServicePort, ServiceSpec,
        Volume,
    },
    apimachinery::pkg::apis::meta::v1::{MicroTime, OwnerReference},
    chrono::Utc,
};
use kube::{
    api::{Patch, PatchParams, PostParams},
    Api, Client, CustomResource, Resource,
};
use kube::{core::ObjectMeta, ResourceExt};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
    collections::BTreeMap,
    fmt::{Debug, Display},
    str::FromStr,
};

mod builder;
pub mod event_stream;

pub const SPAWNER_GROUP: &str = "spawner.dev";
const LABEL_RUN: &str = "run";
const SIDECAR_PORT: u16 = 9090;
const SIDECAR: &str = "spawner-sidecar";
const APPLICATION: &str = "spawner-app";
const TCP: &str = "TCP";

#[derive(CustomResource, Clone, Debug, Deserialize, Serialize, JsonSchema)]
#[kube(
    group = "spawner.dev",
    version = "v1",
    kind = "SessionLivedBackend",
    shortname = "slab",
    status = "SessionLivedBackendStatus",
    namespaced
)]
#[serde(rename_all = "camelCase")]
pub struct SessionLivedBackendSpec {
    pub template: BackendPodSpec,
    pub grace_period_seconds: Option<u32>,
    pub http_port: Option<u16>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema, Default)]
#[serde(rename_all = "camelCase")]
pub struct BackendPodSpec {
    containers: Vec<Container>,
    image_pull_secrets: Option<Vec<LocalObjectReference>>,
    volumes: Option<Vec<Volume>>,
}

impl Into<PodSpec> for BackendPodSpec {
    fn into(self) -> PodSpec {
        PodSpec {
            containers: self.containers,
            image_pull_secrets: self.image_pull_secrets,
            volumes: self.volumes,
            ..Default::default()
        }
    }
}

#[derive(Debug)]
pub enum SessionLivedBackendEvent {
    /// The `SessionLivedBackend` object exists.
    Submitted,

    /// The pod and service backing a `SessionLivedBackend` exist.
    Constructed,

    /// The pod that backs a `SessionLivedBackend` has been assigned to a node.
    Scheduled,

    /// The pod that backs a `SessionLivedBackend` is running.
    Running,

    /// The pod that backs a `SessionLivedBackend` is accepting new connections.
    Ready,

    /// The `SessionLivedBackend` has been marked as swept, meaning that it can be deleted.
    Swept,
}

impl Default for SessionLivedBackendEvent {
    fn default() -> Self {
        SessionLivedBackendEvent::Submitted
    }
}

impl Display for SessionLivedBackendEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(&self, f)
    }
}

impl SessionLivedBackendEvent {
    pub fn message(&self) -> String {
        match self {
            SessionLivedBackendEvent::Submitted => {
                "SessionLivedBackend object created.".to_string()
            }
            SessionLivedBackendEvent::Constructed => {
                "Backing resources created by Spawner.".to_string()
            }
            SessionLivedBackendEvent::Scheduled => {
                "Backing pod was scheduled by Kubernetes.".to_string()
            }
            SessionLivedBackendEvent::Running => "Pod was observed running.".to_string(),
            SessionLivedBackendEvent::Ready => {
                "Pod was observed listening on TCP port.".to_string()
            }
            SessionLivedBackendEvent::Swept => {
                "SessionLivedBackend was found idle and swept.".to_string()
            }
        }
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema, Default)]
#[serde(rename_all = "camelCase")]
pub struct SessionLivedBackendStatus {
    /// Set to `true` by `spawner` once the backing resources (pod and service) have been created.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub submitted: Option<bool>,

    /// Set to `true` by `spawner` once the backing pod has been assigned to a node.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scheduled: Option<bool>,

    /// Set to `true` by `spawner` once the backing pod is observed in the `Running` state.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub running: Option<bool>,

    /// Set to `true` by `sweeper` once the `sidecar` proxy issues an event with `ready`
    /// set to `true`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ready: Option<bool>,

    /// Set to `true` by `sweeper` once the pod is observed to be inactive (per `sidecar`
    /// events) for at least its grace period. This marks the `SessionLivedBackend` for
    /// deletion by `spawner`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub swept: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_name: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub ip: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub port: Option<u16>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Copy)]
pub enum ImagePullPolicy {
    Always,
    Never,
    IfNotPresent,
}

impl ToString for ImagePullPolicy {
    fn to_string(&self) -> String {
        match self {
            ImagePullPolicy::Always => "Always",
            ImagePullPolicy::Never => "Never",
            ImagePullPolicy::IfNotPresent => "IfNotPresent",
        }
        .to_string()
    }
}

#[derive(thiserror::Error, Debug)]
#[error("Unknown ImagePullPolicy: {0}.")]
pub struct BadPolicyName(String);

impl FromStr for ImagePullPolicy {
    type Err = BadPolicyName;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Always" => Ok(ImagePullPolicy::Always),
            "Never" => Ok(ImagePullPolicy::Never),
            "IfNotPresent" => Ok(ImagePullPolicy::IfNotPresent),
            _ => Err(BadPolicyName(s.to_string())),
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("MissingObjectKey: {0}")]
    MissingObjectKey(&'static str),

    #[error("Failure from Kubernetes: {0}")]
    KubernetesFailure(#[source] kube::Error),
}

impl SessionLivedBackend {
    pub fn is_submitted(&self) -> bool {
        self.status
            .as_ref()
            .map(|d| d.submitted.unwrap_or_default())
            .unwrap_or_default()
    }

    pub fn is_scheduled(&self) -> bool {
        self.status
            .as_ref()
            .map(|d| d.scheduled.unwrap_or_default())
            .unwrap_or_default()
    }

    pub fn is_running(&self) -> bool {
        self.status
            .as_ref()
            .map(|d| d.running.unwrap_or_default())
            .unwrap_or_default()
    }

    pub fn is_ready(&self) -> bool {
        self.status
            .as_ref()
            .map(|d| d.ready.unwrap_or_default())
            .unwrap_or_default()
    }

    pub fn is_swept(&self) -> bool {
        self.status
            .as_ref()
            .map(|d| d.swept.unwrap_or_default())
            .unwrap_or_default()
    }

    fn metadata_reference(&self) -> Result<OwnerReference, Error> {
        let meta = &self.metadata;

        Ok(OwnerReference {
            api_version: SessionLivedBackend::api_version(&()).to_string(),
            kind: SessionLivedBackend::kind(&()).to_string(),
            controller: Some(true),
            name: meta
                .name
                .as_ref()
                .ok_or(Error::MissingObjectKey("metadata.name"))?
                .to_string(),
            uid: meta
                .uid
                .as_ref()
                .ok_or(Error::MissingObjectKey("metadata.uid"))?
                .to_string(),
            ..OwnerReference::default()
        })
    }

    fn run_label(&self) -> BTreeMap<String, String> {
        vec![(LABEL_RUN.to_string(), self.name())]
            .into_iter()
            .collect()
    }

    pub fn pod(
        &self,
        sidecar_image: &str,
        image_pull_secret: &Option<String>,
    ) -> Result<Pod, Error> {
        let name = self.name();
        let mut args = vec![format!("--serve-port={}", SIDECAR_PORT)];
        if let Some(port) = self.spec.http_port {
            args.push(format!("--upstream-port={}", port))
        };

        let mut template: PodSpec = self.spec.template.clone().into();
        template.containers.insert(
            0,
            Container {
                name: SIDECAR.to_string(),
                image: Some(sidecar_image.to_string()),
                args: Some(args),
                ..Container::default()
            },
        );

        if let Some(image_pull_secret) = image_pull_secret {
            let secret_ref = LocalObjectReference {
                name: Some(image_pull_secret.to_string()),
            };

            template.image_pull_secrets = match template.image_pull_secrets {
                Some(mut v) => {
                    v.push(secret_ref);
                    Some(v)
                }
                None => Some(vec![secret_ref]),
            }
        }

        Ok(Pod {
            metadata: ObjectMeta {
                name: Some(name),
                labels: Some(self.run_label()),
                owner_references: Some(vec![self.metadata_reference()?]),
                ..ObjectMeta::default()
            },
            spec: Some(template),
            ..Pod::default()
        })
    }

    pub fn service(&self) -> Result<Service, Error> {
        let name = self.name();

        Ok(Service {
            metadata: ObjectMeta {
                name: Some(name.to_string()),
                owner_references: Some(vec![self.metadata_reference()?]),
                ..ObjectMeta::default()
            },
            spec: Some(ServiceSpec {
                selector: Some(self.run_label()),
                ports: Some(vec![ServicePort {
                    name: Some(APPLICATION.to_string()),
                    protocol: Some(TCP.to_string()),
                    port: SIDECAR_PORT as i32,
                    ..ServicePort::default()
                }]),
                ..ServiceSpec::default()
            }),
            ..Service::default()
        })
    }

    pub async fn log_event(
        &self,
        client: Client,
        new_state: SessionLivedBackendEvent,
    ) -> Result<(), Error> {
        tracing::info!(name=%self.name(), %new_state, "SessionLivedBackend state change.");
        let namespace = self
            .namespace()
            .expect("SessionLivedBackend is a namespaced resource, but didn't have a namespace.");
        let event_api = Api::<Event>::namespaced(client.clone(), &namespace);

        event_api
            .create(
                &PostParams::default(),
                &Event {
                    involved_object: self.object_ref(&()),
                    action: Some(new_state.to_string()),
                    message: Some(new_state.message()),
                    reason: Some(new_state.to_string()),
                    type_: Some("Normal".to_string()),
                    event_time: Some(MicroTime(Utc::now())),
                    reporting_component: Some("spawner.dev/sessionlivedbackend".to_string()),
                    reporting_instance: Some(self.name()),
                    metadata: ObjectMeta {
                        namespace: self.namespace(),
                        generate_name: Some(format!("{}-", self.name())),
                        ..Default::default()
                    },

                    ..Default::default()
                },
            )
            .await
            .map_err(Error::KubernetesFailure)?;

        Ok(())
    }

    pub async fn set_spawner_group(
        &self,
        client: Client,
        spawner_group: &str,
    ) -> Result<(), Error> {
        let namespace = self
            .namespace()
            .expect("SessionLivedBackend is a namespaced resource, but didn't have a namespace.");
        let slab_api = Api::<SessionLivedBackend>::namespaced(client.clone(), &namespace);

        slab_api
            .patch(
                &self.name(),
                &PatchParams::default(),
                &Patch::Merge(json!({
                    "metadata": {
                        "labels": {
                            "spawnerGroup": spawner_group
                        }
                    }
                })),
            )
            .await
            .map_err(Error::KubernetesFailure)?;

        Ok(())
    }

    pub async fn update_status(
        &self,
        client: Client,
        new_status: SessionLivedBackendStatus,
    ) -> Result<(), Error> {
        let namespace = self
            .namespace()
            .expect("SessionLivedBackend is a namespaced resource, but didn't have a namespace.");
        let slab_api = Api::<SessionLivedBackend>::namespaced(client.clone(), &namespace);

        slab_api
            .patch_status(
                &self.name(),
                &PatchParams::default(),
                &Patch::Merge(json!({ "status": new_status })),
            )
            .await
            .map_err(Error::KubernetesFailure)?;

        Ok(())
    }
}