1use std::sync::Arc;
11
12use k8s_openapi::{
13 api::{
14 apps::v1::{Deployment, DeploymentSpec},
15 core::v1::{
16 Affinity, Capabilities, ConfigMap, ConfigMapVolumeSource, Container, ContainerPort,
17 EnvVar, HTTPGetAction, KeyToPath, PodSecurityContext, PodSpec, PodTemplateSpec, Probe,
18 ResourceRequirements, SeccompProfile, SecretVolumeSource, SecurityContext, Service,
19 ServicePort, ServiceSpec, Toleration, Volume, VolumeMount,
20 },
21 networking::v1::{
22 IPBlock, NetworkPolicy, NetworkPolicyIngressRule, NetworkPolicyPeer, NetworkPolicyPort,
23 NetworkPolicySpec,
24 },
25 },
26 apimachinery::pkg::{
27 apis::meta::v1::{Condition, LabelSelector, Time},
28 util::intstr::IntOrString,
29 },
30 jiff::Timestamp,
31};
32use kube::{
33 Api, Client, Resource, ResourceExt,
34 api::{DeleteParams, ObjectMeta, PostParams},
35 runtime::{
36 conditions::is_deployment_completed,
37 controller::Action,
38 reflector::{ObjectRef, Store},
39 wait::await_condition,
40 },
41};
42use maplit::btreemap;
43use serde::Serialize;
44use tracing::{trace, warn};
45
46use crate::{
47 Error,
48 k8s::{apply_resource, make_reflector, replace_resource},
49 tls::{DefaultCertificateSpecs, create_certificate, issuer_ref_defined},
50};
51use mz_cloud_resources::crd::{
52 ManagedResource,
53 console::v1alpha1::{Console, HttpConnectionScheme},
54 generated::cert_manager::certificates::{Certificate, CertificatePrivateKeyAlgorithm},
55};
56use mz_orchestrator_kubernetes::KubernetesImagePullPolicy;
57use mz_ore::{cli::KeyValueArg, instrument};
58use mz_server_core::listeners::AuthenticatorKind;
59
60pub struct Config {
61 pub enable_security_context: bool,
62 pub enable_prometheus_scrape_annotations: bool,
63
64 pub image_pull_policy: KubernetesImagePullPolicy,
65 pub scheduler_name: Option<String>,
66 pub console_node_selector: Vec<KeyValueArg<String, String>>,
67 pub console_affinity: Option<Affinity>,
68 pub console_tolerations: Option<Vec<Toleration>>,
69 pub console_default_resources: Option<ResourceRequirements>,
70 pub network_policies_ingress_enabled: bool,
71 pub network_policies_ingress_cidrs: Vec<String>,
72
73 pub default_certificate_specs: DefaultCertificateSpecs,
74
75 pub console_http_port: u16,
76 pub balancerd_http_port: u16,
77}
78
79#[derive(Serialize)]
80struct AppConfig {
81 version: String,
82 auth: AppConfigAuth,
83}
84
85#[derive(Serialize)]
86struct AppConfigAuth {
87 mode: AuthenticatorKind,
88}
89
90pub struct Context {
91 config: Config,
92 deployments: Store<Deployment>,
93}
94
95impl Context {
96 pub async fn new(config: Config, client: Client) -> Self {
97 Self {
98 config,
99 deployments: make_reflector(client).await,
100 }
101 }
102
103 async fn sync_deployment_status(
104 &self,
105 client: &Client,
106 console: &Console,
107 ) -> Result<(), kube::Error> {
108 let namespace = console.namespace();
109 let console_api: Api<Console> = Api::namespaced(client.clone(), &namespace);
110
111 let Some(deployment) = self
112 .deployments
113 .get(&ObjectRef::new(&console.deployment_name()).within(&namespace))
114 else {
115 return Ok(());
116 };
117
118 let Some(deployment_conditions) = &deployment
119 .status
120 .as_ref()
121 .and_then(|status| status.conditions.as_ref())
122 else {
123 return Ok(());
126 };
127
128 let ready = deployment_conditions
129 .iter()
130 .any(|condition| condition.type_ == "Available" && condition.status == "True");
131 let ready_str = if ready { "True" } else { "False" };
132
133 let mut status = console.status.clone().unwrap();
134 if status
135 .conditions
136 .iter()
137 .any(|condition| condition.type_ == "Ready" && condition.status == ready_str)
138 {
139 return Ok(());
143 }
144
145 status.conditions = vec![Condition {
146 type_: "Ready".to_string(),
147 status: ready_str.to_string(),
148 last_transition_time: Time(Timestamp::now()),
149 message: format!(
150 "console deployment is{} ready",
151 if ready { "" } else { " not" }
152 ),
153 observed_generation: None,
154 reason: "DeploymentStatus".to_string(),
155 }];
156 let mut new_console = console.clone();
157 new_console.status = Some(status);
158
159 console_api
160 .replace_status(
161 &console.name_unchecked(),
162 &PostParams::default(),
163 &new_console,
164 )
165 .await?;
166
167 Ok(())
168 }
169
170 fn create_network_policies(&self, console: &Console) -> Vec<NetworkPolicy> {
171 let mut network_policies = Vec::new();
172 if self.config.network_policies_ingress_enabled {
173 let console_label_selector = LabelSelector {
174 match_labels: Some(
175 console
176 .default_labels()
177 .into_iter()
178 .chain([("materialize.cloud/app".to_owned(), console.app_name())])
179 .collect(),
180 ),
181 ..Default::default()
182 };
183 network_policies.extend([NetworkPolicy {
184 metadata: console.managed_resource_meta(console.name_prefixed("console-ingress")),
185 spec: Some(NetworkPolicySpec {
186 ingress: Some(vec![NetworkPolicyIngressRule {
187 from: Some(
188 self.config
189 .network_policies_ingress_cidrs
190 .iter()
191 .map(|cidr| NetworkPolicyPeer {
192 ip_block: Some(IPBlock {
193 cidr: cidr.to_owned(),
194 except: None,
195 }),
196 ..Default::default()
197 })
198 .collect(),
199 ),
200 ports: Some(vec![NetworkPolicyPort {
201 port: Some(IntOrString::Int(self.config.console_http_port.into())),
202 protocol: Some("TCP".to_string()),
203 ..Default::default()
204 }]),
205 ..Default::default()
206 }]),
207 pod_selector: Some(console_label_selector),
208 policy_types: Some(vec!["Ingress".to_owned()]),
209 ..Default::default()
210 }),
211 }]);
212 }
213 network_policies
214 }
215
216 fn create_console_external_certificate(&self, console: &Console) -> Option<Certificate> {
217 create_certificate(
218 self.config
219 .default_certificate_specs
220 .console_external
221 .clone(),
222 console,
223 console.spec.external_certificate_spec.clone(),
224 console.external_certificate_name(),
225 console.external_certificate_secret_name(),
226 None,
227 CertificatePrivateKeyAlgorithm::Rsa,
228 Some(4096),
229 )
230 }
231
232 fn create_console_app_configmap_object(&self, console: &Console) -> ConfigMap {
233 let version: String = console
234 .spec
235 .console_image_ref
236 .rsplitn(2, ':')
237 .next()
238 .expect("at least one chunk, even if empty")
239 .to_owned();
240 let app_config_json = serde_json::to_string(&AppConfig {
241 version,
242 auth: AppConfigAuth {
243 mode: console.spec.authenticator_kind,
244 },
245 })
246 .expect("known valid");
247 ConfigMap {
248 binary_data: None,
249 data: Some(btreemap! {
250 "app-config.json".to_owned() => app_config_json,
251 }),
252 immutable: None,
253 metadata: console.managed_resource_meta(console.configmap_name()),
254 }
255 }
256
257 fn create_console_deployment_object(&self, console: &Console) -> Deployment {
258 let mut pod_template_labels = console.default_labels();
259 pod_template_labels.insert(
260 "materialize.cloud/name".to_owned(),
261 console.deployment_name(),
262 );
263 pod_template_labels.insert("app".to_owned(), "console".to_string());
264 pod_template_labels.insert("materialize.cloud/app".to_owned(), console.app_name());
265
266 let ports = vec![ContainerPort {
267 container_port: self.config.console_http_port.into(),
268 name: Some("http".into()),
269 protocol: Some("TCP".into()),
270 ..Default::default()
271 }];
272
273 let scheme = match console.spec.balancerd.scheme {
274 HttpConnectionScheme::Http => "http",
275 HttpConnectionScheme::Https => "https",
276 };
277 let mut env = vec![EnvVar {
278 name: "MZ_ENDPOINT".to_string(),
279 value: Some(format!(
280 "{}://{}.{}.svc.cluster.local:{}",
281 scheme,
282 console.spec.balancerd.service_name,
283 console.spec.balancerd.namespace,
284 self.config.balancerd_http_port,
285 )),
286 ..Default::default()
287 }];
288 let mut volumes = vec![Volume {
289 name: "app-config".to_string(),
290 config_map: Some(ConfigMapVolumeSource {
291 name: console.configmap_name(),
292 default_mode: Some(256),
293 optional: Some(false),
294 items: Some(vec![KeyToPath {
295 key: "app-config.json".to_string(),
296 path: "app-config.json".to_string(),
297 ..Default::default()
298 }]),
299 }),
300 ..Default::default()
301 }];
302 let mut volume_mounts = vec![VolumeMount {
303 name: "app-config".to_string(),
304 mount_path: "/usr/share/nginx/html/app-config".to_string(),
305 ..Default::default()
306 }];
307
308 let scheme = if issuer_ref_defined(
309 &self.config.default_certificate_specs.console_external,
310 &console.spec.external_certificate_spec,
311 ) {
312 volumes.push(Volume {
313 name: "external-certificate".to_owned(),
314 secret: Some(SecretVolumeSource {
315 default_mode: Some(0o400),
316 secret_name: Some(console.external_certificate_secret_name()),
317 items: None,
318 optional: Some(false),
319 }),
320 ..Default::default()
321 });
322 volume_mounts.push(VolumeMount {
323 name: "external-certificate".to_owned(),
324 mount_path: "/nginx/tls".to_owned(),
325 read_only: Some(true),
326 ..Default::default()
327 });
328 env.push(EnvVar {
329 name: "MZ_NGINX_LISTENER_CONFIG".to_string(),
330 value: Some(format!(
331 "listen {} ssl;
332ssl_certificate /nginx/tls/tls.crt;
333ssl_certificate_key /nginx/tls/tls.key;",
334 self.config.console_http_port
335 )),
336 ..Default::default()
337 });
338 Some("HTTPS".to_owned())
339 } else {
340 env.push(EnvVar {
341 name: "MZ_NGINX_LISTENER_CONFIG".to_string(),
342 value: Some(format!("listen {};", self.config.console_http_port)),
343 ..Default::default()
344 });
345 Some("HTTP".to_owned())
346 };
347
348 let probe = Probe {
349 http_get: Some(HTTPGetAction {
350 path: Some("/".to_string()),
351 port: IntOrString::Int(self.config.console_http_port.into()),
352 scheme,
353 ..Default::default()
354 }),
355 ..Default::default()
356 };
357
358 let security_context = if self.config.enable_security_context {
359 Some(SecurityContext {
363 run_as_non_root: Some(true),
364 capabilities: Some(Capabilities {
365 drop: Some(vec!["ALL".to_string()]),
366 ..Default::default()
367 }),
368 seccomp_profile: Some(SeccompProfile {
369 type_: "RuntimeDefault".to_string(),
370 ..Default::default()
371 }),
372 allow_privilege_escalation: Some(false),
373 ..Default::default()
374 })
375 } else {
376 None
377 };
378
379 let container = Container {
380 name: "console".to_owned(),
381 image: Some(console.spec.console_image_ref.clone()),
382 image_pull_policy: Some(self.config.image_pull_policy.to_string()),
383 ports: Some(ports),
384 env: Some(env),
385 startup_probe: Some(Probe {
386 period_seconds: Some(1),
387 failure_threshold: Some(10),
388 ..probe.clone()
389 }),
390 readiness_probe: Some(Probe {
391 period_seconds: Some(30),
392 failure_threshold: Some(1),
393 ..probe.clone()
394 }),
395 liveness_probe: Some(Probe {
396 period_seconds: Some(30),
397 ..probe.clone()
398 }),
399 resources: console
400 .spec
401 .resource_requirements
402 .clone()
403 .or_else(|| self.config.console_default_resources.clone()),
404 security_context,
405 volume_mounts: Some(volume_mounts),
406 ..Default::default()
407 };
408
409 let deployment_spec = DeploymentSpec {
410 replicas: Some(console.replicas()),
411 selector: LabelSelector {
412 match_labels: Some(pod_template_labels.clone()),
413 ..Default::default()
414 },
415 template: PodTemplateSpec {
416 metadata: Some(ObjectMeta {
419 labels: Some(pod_template_labels),
420 ..Default::default()
421 }),
422 spec: Some(PodSpec {
423 containers: vec![container],
424 node_selector: Some(
425 self.config
426 .console_node_selector
427 .iter()
428 .map(|selector| (selector.key.clone(), selector.value.clone()))
429 .collect(),
430 ),
431 affinity: self.config.console_affinity.clone(),
432 tolerations: self.config.console_tolerations.clone(),
433 scheduler_name: self.config.scheduler_name.clone(),
434 volumes: Some(volumes),
435 security_context: Some(PodSecurityContext {
436 fs_group: Some(101),
437 ..Default::default()
438 }),
439 ..Default::default()
440 }),
441 },
442 ..Default::default()
443 };
444
445 Deployment {
446 metadata: ObjectMeta {
447 ..console.managed_resource_meta(console.deployment_name())
448 },
449 spec: Some(deployment_spec),
450 status: None,
451 }
452 }
453
454 fn create_console_service_object(&self, console: &Console) -> Service {
455 let selector =
456 btreemap! {"materialize.cloud/name".to_string() => console.deployment_name()};
457
458 let ports = vec![ServicePort {
459 name: Some("http".to_string()),
460 protocol: Some("TCP".to_string()),
461 port: self.config.console_http_port.into(),
462 target_port: Some(IntOrString::Int(self.config.console_http_port.into())),
463 ..Default::default()
464 }];
465
466 let spec = ServiceSpec {
467 type_: Some("ClusterIP".to_string()),
468 cluster_ip: Some("None".to_string()),
469 selector: Some(selector),
470 ports: Some(ports),
471 ..Default::default()
472 };
473
474 Service {
475 metadata: console.managed_resource_meta(console.service_name()),
476 spec: Some(spec),
477 status: None,
478 }
479 }
480
481 async fn fix_deployment(
484 &self,
485 deployment_api: &Api<Deployment>,
486 new_deployment: &Deployment,
487 ) -> Result<(), Error> {
488 let Some(mut existing_deployment) = self
489 .deployments
490 .get(
491 &ObjectRef::new(&new_deployment.name_unchecked())
492 .within(&new_deployment.namespace().unwrap()),
493 )
494 .map(Arc::unwrap_or_clone)
495 else {
496 return Ok(());
497 };
498
499 if existing_deployment.spec.as_ref().unwrap().selector
500 == new_deployment.spec.as_ref().unwrap().selector
501 {
502 return Ok(());
503 }
504
505 warn!("found existing deployment with old label selector, fixing");
506
507 existing_deployment
510 .spec
511 .as_mut()
512 .unwrap()
513 .template
514 .metadata
515 .as_mut()
516 .unwrap()
517 .labels = new_deployment
518 .spec
519 .as_ref()
520 .unwrap()
521 .template
522 .metadata
523 .as_ref()
524 .unwrap()
525 .labels
526 .clone();
527
528 replace_resource(deployment_api, &existing_deployment).await?;
532 await_condition(
533 deployment_api.clone(),
534 &existing_deployment.name_unchecked(),
535 |deployment: Option<&Deployment>| {
536 let observed_generation = deployment
537 .and_then(|deployment| deployment.status.as_ref())
538 .and_then(|status| status.observed_generation)
539 .unwrap_or(0);
540 let current_generation = deployment
541 .and_then(|deployment| deployment.meta().generation)
542 .unwrap_or(0);
543 let previous_generation = existing_deployment.meta().generation.unwrap_or(0);
544 observed_generation == current_generation
545 && current_generation > previous_generation
546 },
547 )
548 .await
549 .map_err(|e| anyhow::anyhow!(e))?;
550 await_condition(
551 deployment_api.clone(),
552 &existing_deployment.name_unchecked(),
553 is_deployment_completed(),
554 )
555 .await
556 .map_err(|e| anyhow::anyhow!(e))?;
557
558 match kube::runtime::wait::delete::delete_and_finalize(
561 deployment_api.clone(),
562 &existing_deployment.name_unchecked(),
563 &DeleteParams::orphan(),
564 )
565 .await
566 {
567 Ok(_) => {}
568 Err(kube::runtime::wait::delete::Error::Delete(kube::Error::Api(e)))
569 if e.code == 404 =>
570 {
571 }
573 Err(e) => return Err(anyhow::anyhow!(e).into()),
574 }
575
576 Ok(())
582 }
583}
584
585#[async_trait::async_trait]
586impl k8s_controller::Context for Context {
587 type Resource = Console;
588 type Error = Error;
589
590 #[instrument(fields())]
591 async fn apply(
592 &self,
593 client: Client,
594 console: &Self::Resource,
595 ) -> Result<Option<Action>, Self::Error> {
596 if console.status.is_none() {
597 let console_api: Api<Console> =
598 Api::namespaced(client.clone(), &console.meta().namespace.clone().unwrap());
599 let mut new_console = console.clone();
600 new_console.status = Some(console.status());
601 console_api
602 .replace_status(
603 &console.name_unchecked(),
604 &PostParams::default(),
605 &new_console,
606 )
607 .await?;
608 return Ok(None);
611 }
612
613 let namespace = console.namespace();
614 let network_policy_api: Api<NetworkPolicy> = Api::namespaced(client.clone(), &namespace);
615 let configmap_api: Api<ConfigMap> = Api::namespaced(client.clone(), &namespace);
616 let deployment_api: Api<Deployment> = Api::namespaced(client.clone(), &namespace);
617 let service_api: Api<Service> = Api::namespaced(client.clone(), &namespace);
618 let certificate_api: Api<Certificate> = Api::namespaced(client.clone(), &namespace);
619
620 trace!("creating new network policies");
621 let network_policies = self.create_network_policies(console);
622 for network_policy in &network_policies {
623 apply_resource(&network_policy_api, network_policy).await?;
624 }
625
626 trace!("creating new console configmap");
627 let console_configmap = self.create_console_app_configmap_object(console);
628 apply_resource(&configmap_api, &console_configmap).await?;
629
630 trace!("creating new console deployment");
631 let console_deployment = self.create_console_deployment_object(console);
632 self.fix_deployment(&deployment_api, &console_deployment)
633 .await?;
634 apply_resource(&deployment_api, &console_deployment).await?;
635
636 trace!("creating new console service");
637 let console_service = self.create_console_service_object(console);
638 apply_resource(&service_api, &console_service).await?;
639
640 let console_external_certificate = self.create_console_external_certificate(console);
641 if let Some(certificate) = &console_external_certificate {
642 trace!("creating new console external certificate");
643 apply_resource(&certificate_api, certificate).await?;
644 }
645
646 self.sync_deployment_status(&client, console).await?;
647
648 Ok(None)
649 }
650}