Skip to content

Commit

Permalink
chore: bump crd dependencies version
Browse files Browse the repository at this point in the history
Signed-off-by: iGxnon <[email protected]>
  • Loading branch information
iGxnon committed Oct 27, 2023
1 parent 059ce22 commit d7d1fbc
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 149 deletions.
73 changes: 38 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions crd-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ keywords = ["operator", "API"]

[dependencies]
anyhow = "1.0.75"
garde = { version = "0.16.1", default-features = false, features = ["derive", "pattern", "url"] }
k8s-openapi = { version = "0.20.0", features = ["v1_28", "schemars"] }
kube = { version = "0.86.0", features = ["runtime", "derive"] }
regex = { version = "1", default-features = false, features = ["unicode-perl"] } # garde did not enable regex unicode-perl feature by default, but we need it
schemars = "0.8.6"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.97"
serde_json = "1"
tokio = { version = "1.0", features = ["time"] }
tracing = "0.1.37"

[dev-dependencies]
garde = { version = "0.11.2", default-features = false, features = ["derive", "pattern"] }
serde_yaml = "0.9.25"
# Some false positive deps...
[package.metadata.cargo-machete]
ignored = ["regex", "serde_json"]
79 changes: 33 additions & 46 deletions crd-api/src/v1alpha1/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
#![allow(clippy::str_to_string)]
#![allow(clippy::missing_docs_in_private_items)]

#[cfg(test)]
use garde::Validate;
use k8s_openapi::api::core::v1::{Affinity, Container, PersistentVolumeClaim};
use k8s_openapi::serde::{Deserialize, Serialize};
use kube::CustomResource;
use schemars::JsonSchema;
use std::collections::HashMap;
use std::net::IpAddr;

/// Xline cluster specification
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[cfg_attr(test, derive(Validate))]
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema, Validate)]
#[kube(
group = "xlineoperator.xline.cloud",
version = "v1alpha1",
Expand All @@ -29,63 +28,59 @@ use std::collections::HashMap;
printcolumn = r#"{"name":"Backup Cron", "type":"string", "description":"The cron spec defining the interval a backup CronJob is run", "jsonPath":".spec.backup.cron"}"#,
printcolumn = r#"{"name":"Age", "type":"date", "description":"The cluster age", "jsonPath":".metadata.creationTimestamp"}"#
)]
#[schemars(rename_all = "camelCase")]
#[garde(allow_unvalidated)]
pub struct ClusterSpec {
/// Size of the xline cluster, less than 3 is not allowed
#[cfg_attr(test, garde(range(min = 3)))]
#[garde(range(min = 3))]
#[schemars(range(min = 3))]
pub size: i32,
pub size: usize,
/// Xline container specification
#[cfg_attr(test, garde(skip))]
pub container: Container,
/// The affinity of the xline node
#[cfg_attr(test, garde(skip))]
#[serde(skip_serializing_if = "Option::is_none")]
pub affinity: Option<Affinity>,
/// Backup specification
#[cfg_attr(test, garde(custom(option_backup_dive)))]
#[garde(dive)]
#[serde(skip_serializing_if = "Option::is_none")]
pub backup: Option<BackupSpec>,
/// The data PVC, if it is not specified, then use emptyDir instead
#[cfg_attr(test, garde(skip))]
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<PersistentVolumeClaim>,
/// Some user defined persistent volume claim templates
#[cfg_attr(test, garde(skip))]
#[serde(skip_serializing_if = "Option::is_none")]
pub pvcs: Option<Vec<PersistentVolumeClaim>>,
}

/// Xline cluster backup specification
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[cfg_attr(test, derive(Validate))]
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, Validate)]
pub struct BackupSpec {
/// Cron Spec
#[cfg_attr(test, garde(pattern(r"^(?:\*|[0-5]?\d)(?:[-/,]?(?:\*|[0-5]?\d))*(?: +(?:\*|1?[0-9]|2[0-3])(?:[-/,]?(?:\*|1?[0-9]|2[0-3]))*){4}$")))]
#[garde(pattern(r"^(?:\*|[0-5]?\d)(?:[-/,]?(?:\*|[0-5]?\d))*(?: +(?:\*|1?[0-9]|2[0-3])(?:[-/,]?(?:\*|1?[0-9]|2[0-3]))*){4}$"))]
#[schemars(regex(
pattern = r"^(?:\*|[0-5]?\d)(?:[-/,]?(?:\*|[0-5]?\d))*(?: +(?:\*|1?[0-9]|2[0-3])(?:[-/,]?(?:\*|1?[0-9]|2[0-3]))*){4}$"
))]
pub cron: String,
/// Backup storage type
#[cfg_attr(test, garde(dive))]
#[garde(dive)]
#[serde(flatten)]
pub storage: StorageSpec,
}

/// Xline cluster backup storage specification
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[cfg_attr(test, derive(Validate))]
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, Validate)]
#[serde(untagged)]
pub enum StorageSpec {
/// S3 backup type
S3 {
/// S3 backup specification
#[cfg_attr(test, garde(dive))]
#[garde(dive)]
s3: S3Spec,
},
/// Persistent volume backup type
Pvc {
/// Persistent volume claim
#[cfg_attr(test, garde(skip))]
#[garde(skip)]
pvc: PersistentVolumeClaim,
},
}
Expand All @@ -100,32 +95,24 @@ impl StorageSpec {
}

/// Xline cluster backup S3 specification
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[cfg_attr(test, derive(Validate))]
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, Validate)]
pub struct S3Spec {
/// S3 bucket name to use for backup
#[cfg_attr(test, garde(pattern(r"^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$")))]
#[garde(pattern(r"^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$"))]
#[schemars(regex(pattern = r"^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$"))]
pub bucket: String,
}

/// Xline cluster status
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default, Validate)]
#[garde(context(ClusterSpec as ctx))]
pub struct ClusterStatus {
/// The available nodes' number in the cluster
pub available: i32,
#[garde(range(max = ctx.size))]
pub available: usize,
/// The members registry
pub members: HashMap<String, String>,
}

#[cfg(test)]
#[allow(clippy::trivially_copy_pass_by_ref)] // required bt garde
fn option_backup_dive(value: &Option<BackupSpec>, _cx: &()) -> garde::Result {
if let Some(spec) = value.as_ref() {
spec.validate(&())
.map_err(|e| garde::Error::new(e.to_string()))?;
}
Ok(())
#[garde(skip)]
pub members: HashMap<String, IpAddr>,
}

#[cfg(test)]
Expand Down Expand Up @@ -168,10 +155,10 @@ mod test {
pvcs: None,
data: None,
};
assert_eq!(
Validate::validate(&bad_size, &()).unwrap_err().flatten()[0].0,
"value.size"
);
assert!(Validate::validate(&bad_size, &())
.unwrap_err()
.to_string()
.contains("size"));
}

#[test]
Expand All @@ -189,10 +176,10 @@ mod test {
pvcs: None,
data: None,
};
assert_eq!(
Validate::validate(&bad_cron, &()).unwrap_err().flatten()[0].0,
"value.backup"
);
assert!(Validate::validate(&bad_cron, &())
.unwrap_err()
.to_string()
.contains("backup.cron"));
}

#[test]
Expand All @@ -212,9 +199,9 @@ mod test {
pvcs: None,
data: None,
};
assert_eq!(
Validate::validate(&bad_bucket, &()).unwrap_err().flatten()[0].0,
"value.backup"
);
assert!(Validate::validate(&bad_bucket, &())
.unwrap_err()
.to_string()
.contains("backup.storage.s3.bucket"))
}
}
Loading

0 comments on commit d7d1fbc

Please sign in to comment.