Skip to main content

mz_cloud_resources/crd/
balancer.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 anyhow::bail;
13use k8s_openapi::{
14    api::core::v1::ResourceRequirements, apimachinery::pkg::apis::meta::v1::Condition,
15};
16use kube::{CustomResource, Resource, ResourceExt};
17use schemars::JsonSchema;
18use serde::{Deserialize, Serialize};
19
20use crate::crd::{ManagedResource, MaterializeCertSpec, new_resource_id};
21
22pub mod v1alpha1 {
23    use super::*;
24
25    #[derive(Clone, Debug)]
26    pub enum Routing<'a> {
27        Static(&'a StaticRoutingConfig),
28        Frontegg(&'a FronteggRoutingConfig),
29    }
30
31    #[derive(Clone, Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
32    #[serde(rename_all = "camelCase")]
33    pub struct StaticRoutingConfig {
34        pub environmentd_namespace: String,
35        pub environmentd_service_name: String,
36    }
37
38    #[derive(Clone, Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
39    #[serde(rename_all = "camelCase")]
40    pub struct FronteggRoutingConfig {
41        // TODO
42    }
43
44    #[derive(
45        CustomResource,
46        Clone,
47        Debug,
48        Default,
49        PartialEq,
50        Deserialize,
51        Serialize,
52        JsonSchema
53    )]
54    #[serde(rename_all = "camelCase")]
55    #[kube(
56        namespaced,
57        group = "materialize.cloud",
58        version = "v1alpha1",
59        kind = "Balancer",
60        singular = "balancer",
61        plural = "balancers",
62        status = "BalancerStatus",
63        printcolumn = r#"{"name": "ImageRef", "type": "string", "description": "Reference to the Docker image.", "jsonPath": ".spec.balancerdImageRef", "priority": 1}"#,
64        printcolumn = r#"{"name": "Ready", "type": "string", "description": "Whether the deployment is ready", "jsonPath": ".status.conditions[?(@.type==\"Ready\")].status", "priority": 1}"#
65    )]
66    pub struct BalancerSpec {
67        /// The balancerd image to run.
68        pub balancerd_image_ref: String,
69        // Resource requirements for the balancerd pod
70        pub resource_requirements: Option<ResourceRequirements>,
71        // Number of balancerd pods to create
72        pub replicas: Option<i32>,
73        // The configuration for generating an x509 certificate using cert-manager for balancerd
74        // to present to incoming connections.
75        // The dns_names and issuer_ref fields are required.
76        pub external_certificate_spec: Option<MaterializeCertSpec>,
77        // The configuration for generating an x509 certificate using cert-manager for balancerd
78        // to use to communicate with environmentd.
79        // The dns_names and issuer_ref fields are required.
80        pub internal_certificate_spec: Option<MaterializeCertSpec>,
81        // Annotations to apply to the pods
82        pub pod_annotations: Option<BTreeMap<String, String>>,
83        // Labels to apply to the pods
84        pub pod_labels: Option<BTreeMap<String, String>>,
85
86        // Configuration for statically routing traffic
87        pub static_routing: Option<StaticRoutingConfig>,
88        // Configuration for routing traffic via Frontegg
89        pub frontegg_routing: Option<FronteggRoutingConfig>,
90
91        // This can be set to override the randomly chosen resource id
92        pub resource_id: Option<String>,
93    }
94
95    impl Balancer {
96        pub fn name_prefixed(&self, suffix: &str) -> String {
97            format!("mz{}-{}", self.resource_id(), suffix)
98        }
99
100        pub fn resource_id(&self) -> &str {
101            &self.status.as_ref().unwrap().resource_id
102        }
103
104        pub fn namespace(&self) -> String {
105            self.meta().namespace.clone().unwrap()
106        }
107
108        pub fn deployment_name(&self) -> String {
109            self.name_prefixed("balancerd")
110        }
111
112        pub fn replicas(&self) -> i32 {
113            self.spec.replicas.unwrap_or(2)
114        }
115
116        pub fn app_name(&self) -> String {
117            "balancerd".to_owned()
118        }
119
120        pub fn service_name(&self) -> String {
121            self.name_prefixed("balancerd")
122        }
123
124        pub fn external_certificate_name(&self) -> String {
125            self.name_prefixed("balancerd-external")
126        }
127
128        pub fn external_certificate_secret_name(&self) -> String {
129            self.name_prefixed("balancerd-external-tls")
130        }
131
132        pub fn routing(&self) -> anyhow::Result<Routing<'_>> {
133            match (&self.spec.static_routing, &self.spec.frontegg_routing) {
134                (Some(config), None) => Ok(Routing::Static(config)),
135                (None, Some(config)) => Ok(Routing::Frontegg(config)),
136                (None, None) => bail!("no routing configuration present"),
137                _ => bail!("multiple routing configurations present"),
138            }
139        }
140
141        pub fn status(&self) -> BalancerStatus {
142            self.status.clone().unwrap_or_else(|| BalancerStatus {
143                resource_id: self
144                    .spec
145                    .resource_id
146                    .clone()
147                    .unwrap_or_else(new_resource_id),
148                conditions: vec![],
149            })
150        }
151    }
152
153    #[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema, PartialEq)]
154    #[serde(rename_all = "camelCase")]
155    pub struct BalancerStatus {
156        /// Resource identifier used as a name prefix to avoid pod name collisions.
157        pub resource_id: String,
158
159        pub conditions: Vec<Condition>,
160    }
161
162    impl ManagedResource for Balancer {
163        fn default_labels(&self) -> BTreeMap<String, String> {
164            BTreeMap::from_iter([
165                (
166                    "materialize.cloud/organization-name".to_owned(),
167                    self.name_unchecked(),
168                ),
169                (
170                    "materialize.cloud/organization-namespace".to_owned(),
171                    self.namespace(),
172                ),
173                (
174                    "materialize.cloud/mz-resource-id".to_owned(),
175                    self.resource_id().to_owned(),
176                ),
177                ("materialize.cloud/app".to_owned(), "balancerd".to_owned()),
178            ])
179        }
180    }
181}