Skip to content

Commit 951b386

Browse files
Add types for DaemonSetName and DeploymentName
1 parent 365ccb5 commit 951b386

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

crates/stackable-operator/src/v2/role_group_utils.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::str::FromStr;
33
use sha2::{Digest, Sha256};
44

55
use super::types::{
6-
kubernetes::{ConfigMapName, ListenerName, ServiceName, StatefulSetName},
6+
kubernetes::{
7+
ConfigMapName, DaemonSetName, DeploymentName, ListenerName, ServiceName, StatefulSetName,
8+
},
79
operator::{ClusterName, RoleGroupName, RoleName},
810
};
911
use crate::attributed_string_type;
@@ -145,6 +147,31 @@ impl ResourceNames {
145147
.expect("should be a valid ConfigMap name")
146148
}
147149

150+
pub fn daemon_set_name(&self) -> DaemonSetName {
151+
// compile-time checks
152+
const _: () = assert!(
153+
QualifiedRoleGroupName::MAX_LENGTH <= DaemonSetName::MAX_LENGTH,
154+
"The string `<qualified_role_group_name>` must not exceed the limit of DaemonSet names."
155+
);
156+
let _ = QualifiedRoleGroupName::IS_RFC_1123_SUBDOMAIN_NAME;
157+
158+
DaemonSetName::from_str(self.qualified_role_group_name().as_ref())
159+
.expect("should be a valid DaemonSet name")
160+
}
161+
162+
pub fn deployment_name(&self) -> DeploymentName {
163+
// compile-time checks
164+
const _: () = assert!(
165+
QualifiedRoleGroupName::MAX_LENGTH <= DeploymentName::MAX_LENGTH,
166+
"The string `<qualified_role_group_name>` must not exceed the limit of Deployment \
167+
names."
168+
);
169+
let _ = QualifiedRoleGroupName::IS_RFC_1123_LABEL_NAME;
170+
171+
DeploymentName::from_str(self.qualified_role_group_name().as_ref())
172+
.expect("should be a valid Deployment name")
173+
}
174+
148175
pub fn stateful_set_name(&self) -> StatefulSetName {
149176
// compile-time checks
150177
const _: () = assert!(
@@ -209,7 +236,10 @@ mod tests {
209236
use super::{ClusterName, RoleGroupName, RoleName};
210237
use crate::v2::{
211238
role_group_utils::{QualifiedRoleGroupName, ResourceNames},
212-
types::kubernetes::{ConfigMapName, ListenerName, ServiceName, StatefulSetName},
239+
types::kubernetes::{
240+
ConfigMapName, DaemonSetName, DeploymentName, ListenerName, ServiceName,
241+
StatefulSetName,
242+
},
213243
};
214244

215245
#[test]
@@ -230,6 +260,14 @@ mod tests {
230260
ConfigMapName::from_str_unsafe("test-cluster-data-nodes-ssd-storage"),
231261
resource_names.role_group_config_map()
232262
);
263+
assert_eq!(
264+
DaemonSetName::from_str_unsafe("test-cluster-data-nodes-ssd-storage"),
265+
resource_names.daemon_set_name()
266+
);
267+
assert_eq!(
268+
DeploymentName::from_str_unsafe("test-cluster-data-nodes-ssd-storage"),
269+
resource_names.deployment_name()
270+
);
233271
assert_eq!(
234272
StatefulSetName::from_str_unsafe("test-cluster-data-nodes-ssd-storage"),
235273
resource_names.stateful_set_name()

crates/stackable-operator/src/v2/types/kubernetes.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ attributed_string_type! {
4141
is_rfc_1123_dns_subdomain_name
4242
}
4343

44+
attributed_string_type! {
45+
DaemonSetName,
46+
"The name of a DaemonSet",
47+
"secret-operator-csi-node-driver",
48+
is_rfc_1123_dns_subdomain_name
49+
}
50+
51+
attributed_string_type! {
52+
DeploymentName,
53+
"The name of a Deployment",
54+
"nginx",
55+
// The name of a Deployment must be a lowercase RFC 1123 DNS subdomain name. But the name is
56+
// also used in Pod names and hostnames. Therefore, Kubernetes recommends to use only RFC 1123
57+
// label names which are a subset of subdomain names (63 characters instead of 253) to avoid
58+
// surprising behavior.
59+
is_rfc_1123_label_name
60+
}
61+
4462
attributed_string_type! {
4563
Hostname,
4664
"A hostname",
@@ -163,9 +181,10 @@ attributed_string_type! {
163181
#[cfg(test)]
164182
mod tests {
165183
use super::{
166-
ClusterRoleName, ConfigMapKey, ConfigMapName, ContainerName, Hostname, ListenerClassName,
167-
ListenerName, NamespaceName, PersistentVolumeClaimName, RoleBindingName, SecretClassName,
168-
SecretKey, SecretName, ServiceAccountName, ServiceName, StatefulSetName, Uid, VolumeName,
184+
ClusterRoleName, ConfigMapKey, ConfigMapName, ContainerName, DaemonSetName, DeploymentName,
185+
Hostname, ListenerClassName, ListenerName, NamespaceName, PersistentVolumeClaimName,
186+
RoleBindingName, SecretClassName, SecretKey, SecretName, ServiceAccountName, ServiceName,
187+
StatefulSetName, Uid, VolumeName,
169188
};
170189

171190
#[test]
@@ -174,6 +193,8 @@ mod tests {
174193
ConfigMapKey::test_example();
175194
ContainerName::test_example();
176195
ClusterRoleName::test_example();
196+
DaemonSetName::test_example();
197+
DeploymentName::test_example();
177198
Hostname::test_example();
178199
ListenerName::test_example();
179200
ListenerClassName::test_example();

0 commit comments

Comments
 (0)