MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / validate_label_key

Function validate_label_key

crates/openshell-server/src/grpc/validation.rs:386–485  ·  view source on GitHub ↗

Validate a label selector string format. Format: "key1=value1,key2=value2" Returns `INVALID_ARGUMENT` if the selector has invalid format. Validate a label key according to Kubernetes requirements. Label keys have an optional prefix and required name, separated by `/`: - Prefix (optional): DNS subdomain format, max 253 chars - Name (required): alphanumeric + `-._`, max 63 chars, must start/end wi

(key: &str)

Source from the content-addressed store, hash-verified

384///
385/// See: <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/>
386pub(super) fn validate_label_key(key: &str) -> Result<(), Status> {
387 if key.is_empty() {
388 return Err(Status::invalid_argument("label key cannot be empty"));
389 }
390
391 if key.len() > 253 {
392 return Err(Status::invalid_argument(format!(
393 "label key exceeds 253 characters: '{key}'"
394 )));
395 }
396
397 // Split into optional prefix and required name
398 let (prefix, name) = if let Some((p, n)) = key.split_once('/') {
399 (Some(p), n)
400 } else {
401 (None, key)
402 };
403
404 // Validate name segment (required, max 63 chars)
405 if name.is_empty() {
406 return Err(Status::invalid_argument(format!(
407 "label key name segment cannot be empty: '{key}'"
408 )));
409 }
410
411 if name.len() > 63 {
412 return Err(Status::invalid_argument(format!(
413 "label key name segment exceeds 63 characters: '{key}'"
414 )));
415 }
416
417 // Name must contain only alphanumeric, hyphens, underscores, and dots
418 if !name
419 .chars()
420 .all(|c| c.is_alphanumeric() || c == '-' || c == '_' || c == '.')
421 {
422 return Err(Status::invalid_argument(format!(
423 "label key name segment contains invalid characters (must be alphanumeric, '-', '_', or '.'): '{key}'"
424 )));
425 }
426
427 // Name must start and end with alphanumeric
428 let first = name.chars().next().unwrap(); // safe: we checked !is_empty()
429 let last = name.chars().last().unwrap();
430 if !first.is_alphanumeric() {
431 return Err(Status::invalid_argument(format!(
432 "label key name segment must start with alphanumeric character: '{key}'"
433 )));
434 }
435 if !last.is_alphanumeric() {
436 return Err(Status::invalid_argument(format!(
437 "label key name segment must end with alphanumeric character: '{key}'"
438 )));
439 }
440
441 // Validate prefix if present (DNS subdomain format)
442 if let Some(prefix) = prefix {
443 if prefix.is_empty() {

Calls 3

lenMethod · 0.80
is_emptyMethod · 0.45
nextMethod · 0.45