|
| 1 | +//! This modules provides resource types used to interact with [listener-operator](https://docs.stackable.tech/listener-operator/stable/index.html) |
| 2 | +
|
| 3 | +use std::collections::BTreeMap; |
| 4 | + |
| 5 | +use kube::CustomResource; |
| 6 | +use schemars::JsonSchema; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | + |
| 9 | +#[cfg(doc)] |
| 10 | +use k8s_openapi::api::core::v1::{Node, Pod, Service}; |
| 11 | + |
| 12 | +/// Defines a policy for how [`Listener`]s should be exposed. |
| 13 | +#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, JsonSchema)] |
| 14 | +#[kube( |
| 15 | + group = "listeners.stackable.tech", |
| 16 | + version = "v1alpha1", |
| 17 | + kind = "ListenerClass" |
| 18 | +)] |
| 19 | +#[serde(rename_all = "camelCase")] |
| 20 | +pub struct ListenerClassSpec { |
| 21 | + pub service_type: ServiceType, |
| 22 | + /// Annotations that should be added to the [`Service`] object. |
| 23 | + #[serde(default)] |
| 24 | + pub service_annotations: BTreeMap<String, String>, |
| 25 | +} |
| 26 | + |
| 27 | +/// The method used to access the services. |
| 28 | +#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, PartialEq, Eq)] |
| 29 | +pub enum ServiceType { |
| 30 | + /// Reserve a port on each node. |
| 31 | + NodePort, |
| 32 | + /// Provision a dedicated load balancer. |
| 33 | + LoadBalancer, |
| 34 | +} |
| 35 | + |
| 36 | +/// Exposes a set of pods to the outside world. |
| 37 | +/// |
| 38 | +/// Essentially a Stackable extension of a Kubernetes [`Service`]. Compared to [`Service`], [`Listener`] changes two things: |
| 39 | +/// 1. It uses a cluster-level policy object ([`ListenerClass`]) to define how exactly the exposure works |
| 40 | +/// 2. It has a consistent API for reading back the exposed address(es) of the service |
| 41 | +#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, JsonSchema, Default)] |
| 42 | +#[kube( |
| 43 | + group = "listeners.stackable.tech", |
| 44 | + version = "v1alpha1", |
| 45 | + kind = "Listener", |
| 46 | + namespaced, |
| 47 | + status = "ListenerStatus" |
| 48 | +)] |
| 49 | +#[serde(rename_all = "camelCase")] |
| 50 | +pub struct ListenerSpec { |
| 51 | + /// The name of the [`ListenerClass`]. |
| 52 | + pub class_name: Option<String>, |
| 53 | + /// Labels that the [`Pod`]s must share in order to be exposed. |
| 54 | + pub pod_selector: Option<BTreeMap<String, String>>, |
| 55 | + /// Ports that should be exposed. |
| 56 | + pub ports: Option<Vec<ListenerPort>>, |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)] |
| 60 | +#[serde(rename_all = "camelCase")] |
| 61 | +pub struct ListenerPort { |
| 62 | + /// The name of the port. |
| 63 | + /// |
| 64 | + /// The name of each port *must* be unique within a single [`Listener`]. |
| 65 | + pub name: String, |
| 66 | + /// The port number. |
| 67 | + pub port: i32, |
| 68 | + /// The layer-4 protocol (`TCP` or `UDP`). |
| 69 | + pub protocol: Option<String>, |
| 70 | +} |
| 71 | + |
| 72 | +/// Informs users about how to reach the [`Listener`]. |
| 73 | +#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)] |
| 74 | +#[serde(rename_all = "camelCase")] |
| 75 | +pub struct ListenerStatus { |
| 76 | + /// The backing Kubernetes [`Service`]. |
| 77 | + pub service_name: Option<String>, |
| 78 | + /// All addresses that the [`Listener`] is currently reachable from. |
| 79 | + pub ingress_addresses: Option<Vec<ListenerIngress>>, |
| 80 | + /// Port mappings for accessing the [`Listener`] on each [`Node`] that the [`Pod`]s are currently running on. |
| 81 | + /// |
| 82 | + /// This is only intended for internal use by listener-operator itself. This will be left unset if using a [`ListenerClass`] that does |
| 83 | + /// not require [`Node`]-local access. |
| 84 | + pub node_ports: Option<BTreeMap<String, i32>>, |
| 85 | +} |
| 86 | + |
| 87 | +/// One address that a [`Listener`] is accessible from. |
| 88 | +#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)] |
| 89 | +#[serde(rename_all = "camelCase")] |
| 90 | +pub struct ListenerIngress { |
| 91 | + /// The hostname or IP address to the [`Listener`]. |
| 92 | + pub address: String, |
| 93 | + /// Port mapping table. |
| 94 | + pub ports: BTreeMap<String, i32>, |
| 95 | +} |
0 commit comments