Skip to content

Commit 8c9b1b3

Browse files
committed
fix: separate between official and kubernetes-native RFC 1123 definition
1 parent ef89fdb commit 8c9b1b3

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

crates/stackable-operator/src/builder/pod/container.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ mod tests {
441441
resources::ResourceRequirementsBuilder,
442442
},
443443
commons::resources::ResourceRequirementsType,
444-
validation::RFC_1123_LABEL_FMT,
444+
validation::LOWERCASE_RFC_1123_LABEL_FMT,
445445
};
446446

447447
#[test]
@@ -604,11 +604,11 @@ mod tests {
604604
assert!(ContainerBuilder::new("name-with-hyphen").is_ok());
605605
assert_container_builder_err(
606606
ContainerBuilder::new("ends-with-hyphen-"),
607-
&format!(r#"regex used for validation is "{RFC_1123_LABEL_FMT}""#),
607+
&format!(r#"regex used for validation is "{LOWERCASE_RFC_1123_LABEL_FMT}""#),
608608
);
609609
assert_container_builder_err(
610610
ContainerBuilder::new("-starts-with-hyphen"),
611-
&format!(r#"regex used for validation is "{RFC_1123_LABEL_FMT}""#),
611+
&format!(r#"regex used for validation is "{LOWERCASE_RFC_1123_LABEL_FMT}""#),
612612
);
613613
}
614614

@@ -623,7 +623,7 @@ mod tests {
623623
assert_container_builder_err(
624624
ContainerBuilder::new("name_name"),
625625
&format!(
626-
r#"(e.g. "example-label", or "1-label-1", regex used for validation is "{RFC_1123_LABEL_FMT}""#
626+
r#"(e.g. "example-label", or "1-label-1", regex used for validation is "{LOWERCASE_RFC_1123_LABEL_FMT}""#
627627
),
628628
);
629629
}

crates/stackable-operator/src/validation.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ use snafu::Snafu;
1717

1818
/// Minimal length required by RFC 1123 is 63. Up to 255 allowed, unsupported by k8s.
1919
const RFC_1123_LABEL_MAX_LENGTH: usize = 63;
20-
pub const RFC_1123_LABEL_FMT: &str = "[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])?";
21-
const RFC_1123_LABEL_ERROR_MSG: &str = "a RFC 1123 label must consist of alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character";
20+
// This is a modified RFC 1123 format according to the Kubernetes specification, see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names
21+
pub const LOWERCASE_RFC_1123_LABEL_FMT: &str = "[a-z0-9]([-a-z0-9]*[a-z0-9])?";
22+
const LOWERCASE_RFC_1123_LABEL_ERROR_MSG: &str = "a lowercase RFC 1123 label must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character";
23+
24+
// This is a RFC 1123 format, see https://www.rfc-editor.org/rfc/rfc1123
25+
const RFC_1123_LABEL_FMT: &str = "[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])?";
2226

2327
/// This is a subdomain's max length in DNS (RFC 1123)
2428
const RFC_1123_SUBDOMAIN_MAX_LENGTH: usize = 253;
2529
const RFC_1123_SUBDOMAIN_FMT: &str =
2630
concatcp!(RFC_1123_LABEL_FMT, "(\\.", RFC_1123_LABEL_FMT, ")*");
31+
const RFC_1123_SUBDOMAIN_ERROR_MSG: &str = "a RFC 1123 subdomain must consist of alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character";
2732

2833
const DOMAIN_MAX_LENGTH: usize = RFC_1123_SUBDOMAIN_MAX_LENGTH;
2934
/// Same as [`RFC_1123_SUBDOMAIN_FMT`], but allows a trailing dot
@@ -54,8 +59,14 @@ pub(crate) static DOMAIN_REGEX: LazyLock<Regex> = LazyLock::new(|| {
5459
Regex::new(&format!("^{DOMAIN_FMT}$")).expect("failed to compile domain regex")
5560
});
5661

57-
static RFC_1123_LABEL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
58-
Regex::new(&format!("^{RFC_1123_LABEL_FMT}$")).expect("failed to compile RFC 1123 label regex")
62+
static LOWERCASE_RFC_1123_LABEL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
63+
Regex::new(&format!("^{LOWERCASE_RFC_1123_LABEL_FMT}$"))
64+
.expect("failed to compile RFC 1123 label regex")
65+
});
66+
67+
static RFC_1123_SUBDOMAIN_REGEX: LazyLock<Regex> = LazyLock::new(|| {
68+
Regex::new(&format!("^{RFC_1123_SUBDOMAIN_FMT}$"))
69+
.expect("failed to compile RFC 1123 subdomain regex")
5970
});
6071

6172
static RFC_1035_LABEL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
@@ -205,13 +216,26 @@ pub fn is_rfc_1123_label(value: &str) -> Result {
205216
validate_str_length(value, RFC_1123_LABEL_MAX_LENGTH),
206217
validate_str_regex(
207218
value,
208-
&RFC_1123_LABEL_REGEX,
209-
RFC_1123_LABEL_ERROR_MSG,
219+
&LOWERCASE_RFC_1123_LABEL_REGEX,
220+
LOWERCASE_RFC_1123_LABEL_ERROR_MSG,
210221
&["example-label", "1-label-1"],
211222
),
212223
])
213224
}
214225

226+
/// Tests for a string that conforms to the definition of a subdomain in DNS (RFC 1123).
227+
pub fn is_rfc_1123_subdomain(value: &str) -> Result {
228+
validate_all([
229+
validate_str_length(value, RFC_1123_SUBDOMAIN_MAX_LENGTH),
230+
validate_str_regex(
231+
value,
232+
&RFC_1123_SUBDOMAIN_REGEX,
233+
RFC_1123_SUBDOMAIN_ERROR_MSG,
234+
&["example.com"],
235+
),
236+
])
237+
}
238+
215239
/// Tests for a string that conforms to the definition of a label in DNS (RFC 1035).
216240
pub fn is_rfc_1035_label(value: &str) -> Result {
217241
validate_all([
@@ -277,26 +301,6 @@ mod tests {
277301

278302
use super::*;
279303

280-
const RFC_1123_SUBDOMAIN_ERROR_MSG: &str = "a RFC 1123 subdomain must consist of alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character";
281-
282-
static RFC_1123_SUBDOMAIN_REGEX: LazyLock<Regex> = LazyLock::new(|| {
283-
Regex::new(&format!("^{RFC_1123_SUBDOMAIN_FMT}$"))
284-
.expect("failed to compile RFC 1123 subdomain regex")
285-
});
286-
287-
/// Tests for a string that conforms to the definition of a subdomain in DNS (RFC 1123).
288-
fn is_rfc_1123_subdomain(value: &str) -> Result {
289-
validate_all([
290-
validate_str_length(value, RFC_1123_SUBDOMAIN_MAX_LENGTH),
291-
validate_str_regex(
292-
value,
293-
&RFC_1123_SUBDOMAIN_REGEX,
294-
RFC_1123_SUBDOMAIN_ERROR_MSG,
295-
&["example.com"],
296-
),
297-
])
298-
}
299-
300304
#[rstest]
301305
#[case("")]
302306
#[case("-")]

0 commit comments

Comments
 (0)