Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add key/value pair validation #615

Closed
wants to merge 10 commits into from
6 changes: 2 additions & 4 deletions src/builder/pod/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ use k8s_openapi::{
use tracing::warn;

use crate::{
commons::resources::ResourceRequirementsType, cpu::CpuQuantity, error::OperatorResult,
memory::MemoryQuantity,
commons::resources::ResourceRequirementsType, constants::resources::RESOURCE_DENYLIST,
cpu::CpuQuantity, error::OperatorResult, memory::MemoryQuantity,
};

const RESOURCE_DENYLIST: &[&str] = &["cpu", "memory"];

#[derive(Debug, Default)]
pub struct ResourceRequirementsBuilder<CR, CL, MR, ML> {
other: BTreeMap<String, BTreeMap<ResourceRequirementsType, Quantity>>,
Expand Down
2 changes: 0 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ use std::{
path::{Path, PathBuf},
};

pub const AUTHOR: &str = "Stackable GmbH - info@stackable.de";

/// Framework-standardized commands
///
/// If you need operator-specific commands then you can flatten [`Command`] into your own command enum. For example:
Expand Down
2 changes: 1 addition & 1 deletion src/cluster_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
LIMIT_REQUEST_RATIO_CPU, LIMIT_REQUEST_RATIO_MEMORY,
},
},
constants::labels::{APP_INSTANCE_LABEL, APP_MANAGED_BY_LABEL, APP_NAME_LABEL},
Techassi marked this conversation as resolved.
Show resolved Hide resolved
Techassi marked this conversation as resolved.
Show resolved Hide resolved
error::{Error, OperatorResult},
k8s_openapi::{
api::{
Expand All @@ -24,7 +25,6 @@ use crate::{
NamespaceResourceScope,
},
kube::{Resource, ResourceExt},
labels::{APP_INSTANCE_LABEL, APP_MANAGED_BY_LABEL, APP_NAME_LABEL},
utils::format_full_controller_name,
};

Expand Down
7 changes: 4 additions & 3 deletions src/commons/affinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use stackable_operator_derive::Fragment;

use crate::{
config::merge::{Atomic, Merge},
labels::{APP_COMPONENT_LABEL, APP_INSTANCE_LABEL, APP_NAME_LABEL},
constants::{
affinity::TOPOLOGY_KEY_HOSTNAME,
labels::{APP_COMPONENT_LABEL, APP_INSTANCE_LABEL, APP_NAME_LABEL},
Techassi marked this conversation as resolved.
Show resolved Hide resolved
Techassi marked this conversation as resolved.
Show resolved Hide resolved
},
};

pub const TOPOLOGY_KEY_HOSTNAME: &str = "kubernetes.io/hostname";

#[derive(Clone, Debug, Default, Deserialize, Fragment, JsonSchema, PartialEq, Serialize)]
#[fragment(path_overrides(fragment = "crate::config::fragment"))]
#[fragment_attrs(
Expand Down
1 change: 1 addition & 0 deletions src/constants/affinity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const TOPOLOGY_KEY_HOSTNAME: &str = "kubernetes.io/hostname";
1 change: 1 addition & 0 deletions src/constants/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const AUTHOR: &str = "Stackable GmbH - info@stackable.de";
17 changes: 17 additions & 0 deletions src/constants/labels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use const_format::concatcp;

pub const APP_KUBERNETES_LABEL_BASE: &str = "app.kubernetes.io/";

/// The name of the application e.g. "mysql"
pub const APP_NAME_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "name");
/// A unique name identifying the instance of an application e.g. "mysql-abcxzy"
pub const APP_INSTANCE_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "instance");
/// The current version of the application (e.g., a semantic version, revision hash, etc.) e.g."5.7.21"
pub const APP_VERSION_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "version");
/// The component within the architecture e.g. database
pub const APP_COMPONENT_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "component");
/// The name of a higher level application this one is part of e.g. "wordpress"
pub const APP_PART_OF_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "part-of");
/// The tool being used to manage the operation of an application e.g. helm
pub const APP_MANAGED_BY_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "managed-by");
pub const APP_ROLE_GROUP_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "role-group");
5 changes: 5 additions & 0 deletions src/constants/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod affinity;
pub mod cli;
pub mod labels;
pub mod resources;
pub mod validation;
1 change: 1 addition & 0 deletions src/constants/resources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) const RESOURCE_DENYLIST: &[&str] = &["cpu", "memory"];
18 changes: 18 additions & 0 deletions src/constants/validation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use const_format::concatcp;

pub(crate) const RFC_1123_LABEL_FMT: &str = "[a-z0-9]([-a-z0-9]*[a-z0-9])?";
pub(crate) const RFC_1123_SUBDOMAIN_FMT: &str =
concatcp!(RFC_1123_LABEL_FMT, "(\\.", RFC_1123_LABEL_FMT, ")*");
pub(crate) const RFC_1123_SUBDOMAIN_ERROR_MSG: &str = "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character";
pub(crate) const 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";

// This is a subdomain's max length in DNS (RFC 1123)
pub const RFC_1123_SUBDOMAIN_MAX_LENGTH: usize = 253;
// Minimal length reuquired by RFC 1123 is 63. Up to 255 allowed, unsupported by k8s.
pub const RFC_1123_LABEL_MAX_LENGTH: usize = 63;

pub(crate) const RFC_1035_LABEL_FMT: &str = "[a-z]([-a-z0-9]*[a-z0-9])?";
pub(crate) const RFC_1035_LABEL_ERR_MSG: &str = "a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character";

// This is a label's max length in DNS (RFC 1035)
pub const RFC_1035_LABEL_MAX_LENGTH: usize = RFC_1123_LABEL_MAX_LENGTH;
26 changes: 8 additions & 18 deletions src/labels.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use crate::utils::format_full_controller_name;
use const_format::concatcp;
use crate::{
constants::labels::{
APP_COMPONENT_LABEL, APP_INSTANCE_LABEL, APP_MANAGED_BY_LABEL, APP_NAME_LABEL,
APP_ROLE_GROUP_LABEL, APP_VERSION_LABEL,
},
utils::format_full_controller_name,
};

use kube::api::{Resource, ResourceExt};
use std::collections::BTreeMap;

Expand All @@ -8,22 +14,6 @@ use crate::builder::ObjectMetaBuilder;
#[cfg(doc)]
use crate::commons::product_image_selection::ResolvedProductImage;

const APP_KUBERNETES_LABEL_BASE: &str = "app.kubernetes.io/";

/// The name of the application e.g. "mysql"
pub const APP_NAME_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "name");
/// A unique name identifying the instance of an application e.g. "mysql-abcxzy"
pub const APP_INSTANCE_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "instance");
/// The current version of the application (e.g., a semantic version, revision hash, etc.) e.g."5.7.21"
pub const APP_VERSION_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "version");
/// The component within the architecture e.g. database
pub const APP_COMPONENT_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "component");
/// The name of a higher level application this one is part of e.g. "wordpress"
pub const APP_PART_OF_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "part-of");
/// The tool being used to manage the operation of an application e.g. helm
pub const APP_MANAGED_BY_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "managed-by");
pub const APP_ROLE_GROUP_LABEL: &str = concatcp!(APP_KUBERNETES_LABEL_BASE, "role-group");

/// Recommended labels to set on objects created by Stackable operators
///
/// See [`get_recommended_labels`] and [`ObjectMetaBuilder::with_recommended_labels`].
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod client;
pub mod cluster_resources;
pub mod commons;
pub mod config;
pub mod constants;
pub mod cpu;
pub mod crd;
pub mod error;
Expand All @@ -18,6 +19,7 @@ pub mod product_config_utils;
pub mod product_logging;
pub mod role_utils;
pub mod status;
pub mod types;
pub mod utils;
pub mod validation;
pub mod yaml;
Expand Down
Empty file added src/types/annotation.rs
Empty file.
Loading
Loading