mz_cloud_resources/crd/
console.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::collections::BTreeMap;
11
12use k8s_openapi::{
13    api::core::v1::ResourceRequirements, apimachinery::pkg::apis::meta::v1::Condition,
14};
15use kube::CustomResource;
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        /// The service name for the balancerd service to connect to
37        pub service_name: String,
38        /// The namespace the balancerd service runs in
39        pub namespace: String,
40        /// Whether to use HTTP or HTTPS to communicate with Materialize.
41        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        /// The console image to run.
61        pub console_image_ref: String,
62        // Resource requirements for the console pod
63        pub resource_requirements: Option<ResourceRequirements>,
64        // Number of console pods to create
65        pub replicas: Option<i32>,
66        // The configuration for generating an x509 certificate using cert-manager for console
67        // to present to incoming connections.
68        // The dns_names and issuer_ref fields are required.
69        pub external_certificate_spec: Option<MaterializeCertSpec>,
70        // Annotations to apply to the pods
71        pub pod_annotations: Option<BTreeMap<String, String>>,
72        // Labels to apply to the pods
73        pub pod_labels: Option<BTreeMap<String, String>>,
74
75        // Connection information for the balancerd service to use
76        pub balancerd: BalancerdRef,
77        /// How to authenticate with Materialize.
78        #[serde(default)]
79        pub authenticator_kind: AuthenticatorKind,
80
81        // This can be set to override the randomly chosen resource id
82        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 deployment_name(&self) -> String {
95            self.name_prefixed("console")
96        }
97
98        pub fn replicas(&self) -> i32 {
99            self.spec.replicas.unwrap_or(2)
100        }
101
102        pub fn app_name(&self) -> String {
103            "console".to_owned()
104        }
105
106        pub fn service_name(&self) -> String {
107            self.name_prefixed("console")
108        }
109
110        pub fn configmap_name(&self) -> String {
111            self.name_prefixed("console")
112        }
113
114        pub fn external_certificate_name(&self) -> String {
115            self.name_prefixed("console-external")
116        }
117
118        pub fn external_certificate_secret_name(&self) -> String {
119            self.name_prefixed("console-external-tls")
120        }
121
122        pub fn status(&self) -> ConsoleStatus {
123            self.status.clone().unwrap_or_else(|| ConsoleStatus {
124                resource_id: self
125                    .spec
126                    .resource_id
127                    .clone()
128                    .unwrap_or_else(new_resource_id),
129                conditions: vec![],
130            })
131        }
132    }
133
134    #[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema, PartialEq)]
135    #[serde(rename_all = "camelCase")]
136    pub struct ConsoleStatus {
137        /// Resource identifier used as a name prefix to avoid pod name collisions.
138        pub resource_id: String,
139
140        pub conditions: Vec<Condition>,
141    }
142
143    impl ManagedResource for Console {
144        fn default_labels(&self) -> BTreeMap<String, String> {
145            BTreeMap::from_iter([
146                (
147                    "materialize.cloud/mz-resource-id".to_owned(),
148                    self.resource_id().to_owned(),
149                ),
150                ("materialize.cloud/app".to_owned(), "console".to_owned()),
151            ])
152        }
153    }
154}