Validate a label value according to Kubernetes requirements. Label values: - Can be empty (Kubernetes allows empty values) - Max 63 characters - If non-empty, must contain only alphanumeric, hyphens, underscores, and dots - If non-empty, must start and end with alphanumeric character See:
(value: &str)
| 494 | /// |
| 495 | /// See: <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/> |
| 496 | pub(super) fn validate_label_value(value: &str) -> Result<(), Status> { |
| 497 | // Empty values are allowed in Kubernetes |
| 498 | if value.is_empty() { |
| 499 | return Ok(()); |
| 500 | } |
| 501 | |
| 502 | if value.len() > 63 { |
| 503 | return Err(Status::invalid_argument(format!( |
| 504 | "label value exceeds 63 characters: '{value}'" |
| 505 | ))); |
| 506 | } |
| 507 | |
| 508 | // Must contain only alphanumeric, hyphens, underscores, and dots |
| 509 | if !value |
| 510 | .chars() |
| 511 | .all(|c| c.is_alphanumeric() || c == '-' || c == '_' || c == '.') |
| 512 | { |
| 513 | return Err(Status::invalid_argument(format!( |
| 514 | "label value contains invalid characters (must be alphanumeric, '-', '_', or '.'): '{value}'" |
| 515 | ))); |
| 516 | } |
| 517 | |
| 518 | // Must start and end with alphanumeric |
| 519 | let first = value.chars().next().unwrap(); // safe: we checked !is_empty() |
| 520 | let last = value.chars().last().unwrap(); |
| 521 | if !first.is_alphanumeric() { |
| 522 | return Err(Status::invalid_argument(format!( |
| 523 | "label value must start with alphanumeric character: '{value}'" |
| 524 | ))); |
| 525 | } |
| 526 | if !last.is_alphanumeric() { |
| 527 | return Err(Status::invalid_argument(format!( |
| 528 | "label value must end with alphanumeric character: '{value}'" |
| 529 | ))); |
| 530 | } |
| 531 | |
| 532 | Ok(()) |
| 533 | } |
| 534 | |
| 535 | /// Validate a label selector string format. |
| 536 | /// |