mz_cloud_resources/crd/
console.rs1use std::collections::BTreeMap;
11
12use k8s_openapi::{
13 api::core::v1::ResourceRequirements, apimachinery::pkg::apis::meta::v1::Condition,
14};
15use kube::{CustomResource, Resource, ResourceExt};
16use schemars::JsonSchema;
17use serde::{Deserialize, Serialize};
18
19use crate::crd::{ManagedResource, MaterializeCertSpec, new_resource_id};
20use mz_server_core::listeners::AuthenticatorKind;
21
22pub mod v1alpha1 {
23 use super::*;
24
25 #[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize, JsonSchema)]
26 #[serde(rename_all = "camelCase")]
27 pub enum HttpConnectionScheme {
28 #[default]
29 Http,
30 Https,
31 }
32
33 #[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize, JsonSchema)]
34 #[serde(rename_all = "camelCase")]
35 pub struct BalancerdRef {
36 pub service_name: String,
38 pub namespace: String,
40 pub scheme: HttpConnectionScheme,
42 }
43
44 #[derive(
45 CustomResource, Clone, Debug, Default, PartialEq, Deserialize, Serialize, JsonSchema,
46 )]
47 #[serde(rename_all = "camelCase")]
48 #[kube(
49 namespaced,
50 group = "materialize.cloud",
51 version = "v1alpha1",
52 kind = "Console",
53 singular = "console",
54 plural = "consoles",
55 status = "ConsoleStatus",
56 printcolumn = r#"{"name": "ImageRef", "type": "string", "description": "Reference to the Docker image.", "jsonPath": ".spec.consoleImageRef", "priority": 1}"#,
57 printcolumn = r#"{"name": "Ready", "type": "string", "description": "Whether the deployment is ready", "jsonPath": ".status.conditions[?(@.type==\"Ready\")].status", "priority": 1}"#
58 )]
59 pub struct ConsoleSpec {
60 pub console_image_ref: String,
62 pub resource_requirements: Option<ResourceRequirements>,
64 pub replicas: Option<i32>,
66 pub external_certificate_spec: Option<MaterializeCertSpec>,
70 pub pod_annotations: Option<BTreeMap<String, String>>,
72 pub pod_labels: Option<BTreeMap<String, String>>,
74
75 pub balancerd: BalancerdRef,
77 #[serde(default)]
79 pub authenticator_kind: AuthenticatorKind,
80
81 pub resource_id: Option<String>,
83 }
84
85 impl Console {
86 pub fn name_prefixed(&self, suffix: &str) -> String {
87 format!("mz{}-{}", self.resource_id(), suffix)
88 }
89
90 pub fn resource_id(&self) -> &str {
91 &self.status.as_ref().unwrap().resource_id
92 }
93
94 pub fn namespace(&self) -> String {
95 self.meta().namespace.clone().unwrap()
96 }
97
98 pub fn deployment_name(&self) -> String {
99 self.name_prefixed("console")
100 }
101
102 pub fn replicas(&self) -> i32 {
103 self.spec.replicas.unwrap_or(2)
104 }
105
106 pub fn app_name(&self) -> String {
107 "console".to_owned()
108 }
109
110 pub fn service_name(&self) -> String {
111 self.name_prefixed("console")
112 }
113
114 pub fn configmap_name(&self) -> String {
115 self.name_prefixed("console")
116 }
117
118 pub fn external_certificate_name(&self) -> String {
119 self.name_prefixed("console-external")
120 }
121
122 pub fn external_certificate_secret_name(&self) -> String {
123 self.name_prefixed("console-external-tls")
124 }
125
126 pub fn status(&self) -> ConsoleStatus {
127 self.status.clone().unwrap_or_else(|| ConsoleStatus {
128 resource_id: self
129 .spec
130 .resource_id
131 .clone()
132 .unwrap_or_else(new_resource_id),
133 conditions: vec![],
134 })
135 }
136 }
137
138 #[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema, PartialEq)]
139 #[serde(rename_all = "camelCase")]
140 pub struct ConsoleStatus {
141 pub resource_id: String,
143
144 pub conditions: Vec<Condition>,
145 }
146
147 impl ManagedResource for Console {
148 fn default_labels(&self) -> BTreeMap<String, String> {
149 BTreeMap::from_iter([
150 (
151 "materialize.cloud/organization-name".to_owned(),
152 self.name_unchecked(),
153 ),
154 (
155 "materialize.cloud/organization-namespace".to_owned(),
156 self.namespace(),
157 ),
158 (
159 "materialize.cloud/mz-resource-id".to_owned(),
160 self.resource_id().to_owned(),
161 ),
162 ("materialize.cloud/app".to_owned(), "console".to_owned()),
163 ])
164 }
165 }
166}