-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathframework.rs
More file actions
194 lines (172 loc) · 5.69 KB
/
Copy pathframework.rs
File metadata and controls
194 lines (172 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Type-safe wrappers that cannot throw errors
// The point is, to move the validation "upwards".
// The contents of this module will be moved to operator-rs when stabilized.
use std::{fmt::Display, str::FromStr};
use kvp::label::MAX_LABEL_VALUE_LENGTH;
use snafu::{ResultExt, Snafu, ensure};
use stackable_operator::kvp::LabelValue;
use strum::{EnumDiscriminants, IntoStaticStr};
pub mod builder;
pub mod cluster_resources;
pub mod kvp;
pub mod listener;
pub mod role_group_utils;
pub mod role_utils;
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
pub enum Error {
#[snafu(display("maximum length exceeded"))]
LengthExceeded { length: usize, max_length: usize },
#[snafu(display("object name not RFC 1123 compliant"))]
InvalidObjectName {
source: stackable_operator::validation::Errors,
},
#[snafu(display("failed to use as label"))]
InvalidLabelValue {
source: stackable_operator::kvp::LabelValueError,
},
}
/// Maximum length of a DNS subdomain name as defined in RFC 1123.
/// The object names of most types, e.g. ConfigMap and StatefulSet, must not exceed this limit.
/// However, there are kinds that allow longer object names, e.g. ClusterRole.
#[allow(dead_code)]
pub const MAX_OBJECT_NAME_LENGTH: usize = 253;
/// Has a name that can be used as a DNS subdomain name as defined in RFC 1123.
/// Most resource types, e.g. a Pod, require such a compliant name.
pub trait HasObjectName {
fn to_object_name(&self) -> String;
}
/// Has a namespace
pub trait HasNamespace {
fn to_namespace(&self) -> String;
}
/// Has a Kubernetes UID
pub trait HasUid {
fn to_uid(&self) -> String;
}
/// Is a valid label value as defined in RFC 1123.
pub trait IsLabelValue {
fn to_label_value(&self) -> String;
}
/// Restricted string type with attributes like maximum length.
macro_rules! attributed_string_type {
($name:ident, $description:literal $(, $attribute:tt)*) => {
#[doc = $description]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct $name(String);
impl Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for $name {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
$(attributed_string_type!(@from_str $name, s, $attribute);)*
Ok(Self(s.to_owned()))
}
}
$(attributed_string_type!(@trait_impl $name, $attribute);)*
};
(@from_str $name:ident, $s:expr, (max_length = $max_length:expr)) => {
let length = $s.len() as usize;
ensure!(
length <= $name::MAX_LENGTH,
LengthExceededSnafu {
length,
max_length: $name::MAX_LENGTH,
}
);
};
(@from_str $name:ident, $s:expr, is_object_name) => {
stackable_operator::validation::is_rfc_1123_subdomain($s).context(InvalidObjectNameSnafu)?;
};
(@from_str $name:ident, $s:expr, is_valid_label_value) => {
LabelValue::from_str($s).context(InvalidLabelValueSnafu)?;
};
(@trait_impl $name:ident, (max_length = $max_length:expr)) => {
impl $name {
// type arithmetic would be better
pub const MAX_LENGTH: usize = $max_length;
}
};
(@trait_impl $name:ident, is_object_name) => {
impl HasObjectName for $name {
fn to_object_name(&self) -> String {
self.0.clone()
}
}
};
(@trait_impl $name:ident, is_valid_label_value) => {
impl IsLabelValue for $name {
fn to_label_value(&self) -> String {
self.0.clone()
}
}
};
}
attributed_string_type! {
ProductName,
"The name of a product, e.g. \"opensearch\"",
// A suffix is added to produce a label value. An according compile-time check ensures that
// max_length cannot be set higher.
(max_length = 54),
is_valid_label_value
}
attributed_string_type! {
ProductVersion,
"The version of a product, e.g. \"3.0.0\"",
(max_length = MAX_LABEL_VALUE_LENGTH),
is_valid_label_value
}
attributed_string_type! {
ClusterName,
"The name of a cluster/stacklet, e.g. \"my-opensearch-cluster\"",
// Suffixes are added to produce a resource names. According compile-time check ensures that
// max_length cannot be set higher.
(max_length = 24),
is_object_name,
is_valid_label_value
}
attributed_string_type! {
ControllerName,
"The name of a controller in an operator, e.g. \"opensearchcluster\"",
(max_length = MAX_LABEL_VALUE_LENGTH),
is_valid_label_value
}
attributed_string_type! {
OperatorName,
"The name of an operator, e.g. \"opensearch.stackable.tech\"",
(max_length = MAX_LABEL_VALUE_LENGTH),
is_valid_label_value
}
attributed_string_type! {
RoleGroupName,
"The name of a role-group name, e.g. \"cluster-manager\"",
(max_length = 16),
is_object_name,
is_valid_label_value
}
attributed_string_type! {
RoleName,
"The name of a role name, e.g. \"nodes\"",
(max_length = 10),
is_object_name,
is_valid_label_value
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use crate::framework::ProductName;
#[test]
fn test_object_name_constraints() {
assert!(ProductName::from_str("valid-role-group-name").is_ok());
assert!(ProductName::from_str("invalid-character: /").is_err());
assert!(
ProductName::from_str(
"too-long-123456789012345678901234567890123456789012345678901234567890"
)
.is_err()
);
}
}