From 07cb79e17b0a9e184ee355b2bbee07f545d49221 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 3 Feb 2025 16:40:01 +0100 Subject: [PATCH 1/5] chore: Add rustfmt config, apply formatting to code --- crates/k8s-version/src/api_version.rs | 12 ++-- crates/k8s-version/src/level.rs | 10 ++- crates/k8s-version/src/version.rs | 10 ++- crates/stackable-certs/src/keys/ecdsa.rs | 5 +- crates/stackable-certs/src/keys/rsa.rs | 4 +- crates/stackable-certs/src/lib.rs | 5 +- .../src/builder/configmap.rs | 7 +- crates/stackable-operator/src/builder/meta.rs | 3 +- crates/stackable-operator/src/builder/pdb.rs | 3 +- .../src/builder/pod/container.rs | 5 +- .../stackable-operator/src/builder/pod/mod.rs | 12 ++-- .../src/builder/pod/security.rs | 7 +- .../src/builder/pod/volume.rs | 7 +- crates/stackable-operator/src/cli.rs | 7 +- crates/stackable-operator/src/client.rs | 10 ++- .../src/cluster_resources.rs | 38 +++++----- .../src/commons/affinity.rs | 3 +- .../src/commons/listener.rs | 7 +- .../src/commons/networking.rs | 4 +- crates/stackable-operator/src/commons/opa.rs | 3 +- .../src/commons/product_image_selection.rs | 4 +- .../src/commons/resources.rs | 44 +++++++----- .../stackable-operator/src/commons/s3/crd.rs | 5 +- .../src/commons/s3/helpers.rs | 18 ++--- .../stackable-operator/src/commons/s3/mod.rs | 11 ++- .../src/commons/secret_class.rs | 3 +- .../stackable-operator/src/config/fragment.rs | 10 ++- crates/stackable-operator/src/config/merge.rs | 13 ++-- crates/stackable-operator/src/config/mod.rs | 5 +- crates/stackable-operator/src/cpu.rs | 3 +- crates/stackable-operator/src/helm/mod.rs | 1 - .../src/kvp/annotation/mod.rs | 58 +++++++-------- crates/stackable-operator/src/kvp/key.rs | 3 +- .../stackable-operator/src/kvp/label/mod.rs | 70 +++++++++---------- .../src/kvp/label/selector.rs | 6 +- .../stackable-operator/src/kvp/label/value.rs | 3 +- crates/stackable-operator/src/kvp/mod.rs | 2 +- .../src/logging/k8s_events.rs | 3 +- crates/stackable-operator/src/memory.rs | 10 +-- crates/stackable-operator/src/namespace.rs | 3 +- crates/stackable-operator/src/pod_utils.rs | 3 +- .../src/product_config_utils.rs | 9 +-- .../src/product_logging/framework.rs | 13 ++-- .../src/product_logging/spec.rs | 14 ++-- crates/stackable-operator/src/role_utils.rs | 18 ++--- .../src/status/condition/daemonset.rs | 9 +-- .../src/status/condition/operations.rs | 13 ++-- .../src/status/condition/statefulset.rs | 9 +-- .../stackable-operator/src/time/duration.rs | 3 +- crates/stackable-operator/src/utils/mod.rs | 1 - crates/stackable-operator/src/utils/option.rs | 3 +- crates/stackable-operator/src/validation.rs | 3 +- .../src/instrumentation/axum/mod.rs | 2 +- crates/stackable-telemetry/src/tracing/mod.rs | 2 +- .../src/codegen/changes.rs | 3 +- .../tests/k8s/fail/crd.rs | 1 - crates/stackable-webhook/src/lib.rs | 2 +- .../src/servers/conversion.rs | 3 +- rustfmt.toml | 4 ++ 59 files changed, 286 insertions(+), 266 deletions(-) create mode 100644 rustfmt.toml diff --git a/crates/k8s-version/src/api_version.rs b/crates/k8s-version/src/api_version.rs index 527f7a6cc..f3157647e 100644 --- a/crates/k8s-version/src/api_version.rs +++ b/crates/k8s-version/src/api_version.rs @@ -1,9 +1,8 @@ use std::{cmp::Ordering, fmt::Display, str::FromStr}; -use snafu::{ResultExt, Snafu}; - #[cfg(feature = "darling")] use darling::FromMeta; +use snafu::{ResultExt, Snafu}; use crate::{Group, ParseGroupError, ParseVersionError, Version}; @@ -102,13 +101,12 @@ impl ApiVersion { #[cfg(test)] mod test { - use super::*; - use crate::Level; - - use rstest::rstest; - #[cfg(feature = "darling")] use quote::quote; + use rstest::rstest; + + use super::*; + use crate::Level; #[cfg(feature = "darling")] fn parse_meta(tokens: proc_macro2::TokenStream) -> ::std::result::Result { diff --git a/crates/k8s-version/src/level.rs b/crates/k8s-version/src/level.rs index 823ca63f0..15a49b02b 100644 --- a/crates/k8s-version/src/level.rs +++ b/crates/k8s-version/src/level.rs @@ -7,11 +7,10 @@ use std::{ sync::LazyLock, }; -use regex::Regex; -use snafu::{OptionExt, ResultExt, Snafu}; - #[cfg(feature = "darling")] use darling::FromMeta; +use regex::Regex; +use snafu::{OptionExt, ResultExt, Snafu}; static LEVEL_REGEX: LazyLock = LazyLock::new(|| { Regex::new(r"^(?P[a-z]+)(?P\d+)$").expect("failed to compile level regex") @@ -158,14 +157,13 @@ impl FromMeta for Level { #[cfg(test)] mod test { + #[cfg(feature = "darling")] + use quote::quote; use rstest::rstest; use rstest_reuse::*; use super::*; - #[cfg(feature = "darling")] - use quote::quote; - #[cfg(feature = "darling")] fn parse_meta(tokens: proc_macro2::TokenStream) -> ::std::result::Result { let attribute: syn::Attribute = syn::parse_quote!(#[#tokens]); diff --git a/crates/k8s-version/src/version.rs b/crates/k8s-version/src/version.rs index ec8d6aa6f..6f6b16d55 100644 --- a/crates/k8s-version/src/version.rs +++ b/crates/k8s-version/src/version.rs @@ -1,10 +1,9 @@ use std::{cmp::Ordering, fmt::Display, num::ParseIntError, str::FromStr, sync::LazyLock}; -use regex::Regex; -use snafu::{OptionExt, ResultExt, Snafu}; - #[cfg(feature = "darling")] use darling::FromMeta; +use regex::Regex; +use snafu::{OptionExt, ResultExt, Snafu}; use crate::{Level, ParseLevelError}; @@ -117,14 +116,13 @@ impl Version { #[cfg(test)] mod test { + #[cfg(feature = "darling")] + use quote::quote; use rstest::rstest; use rstest_reuse::{apply, template}; use super::*; - #[cfg(feature = "darling")] - use quote::quote; - #[cfg(feature = "darling")] fn parse_meta(tokens: proc_macro2::TokenStream) -> ::std::result::Result { let attribute: syn::Attribute = syn::parse_quote!(#[#tokens]); diff --git a/crates/stackable-certs/src/keys/ecdsa.rs b/crates/stackable-certs/src/keys/ecdsa.rs index 167fca3d1..c5ee0439b 100644 --- a/crates/stackable-certs/src/keys/ecdsa.rs +++ b/crates/stackable-certs/src/keys/ecdsa.rs @@ -40,12 +40,11 @@ impl SigningKey { } impl CertificateKeypair for SigningKey { - type SigningKey = p256::ecdsa::SigningKey; + type Error = Error; type Signature = ecdsa::der::Signature; + type SigningKey = p256::ecdsa::SigningKey; type VerifyingKey = p256::ecdsa::VerifyingKey; - type Error = Error; - fn signing_key(&self) -> &Self::SigningKey { &self.0 } diff --git a/crates/stackable-certs/src/keys/rsa.rs b/crates/stackable-certs/src/keys/rsa.rs index e23cf0009..dc70ccd0e 100644 --- a/crates/stackable-certs/src/keys/rsa.rs +++ b/crates/stackable-certs/src/keys/rsa.rs @@ -60,10 +60,10 @@ impl SigningKey { } impl CertificateKeypair for SigningKey { - type SigningKey = rsa::pkcs1v15::SigningKey; + type Error = Error; type Signature = rsa::pkcs1v15::Signature; + type SigningKey = rsa::pkcs1v15::SigningKey; type VerifyingKey = rsa::pkcs1v15::VerifyingKey; - type Error = Error; fn signing_key(&self) -> &Self::SigningKey { &self.0 diff --git a/crates/stackable-certs/src/lib.rs b/crates/stackable-certs/src/lib.rs index 1db8d78d9..cb4324453 100644 --- a/crates/stackable-certs/src/lib.rs +++ b/crates/stackable-certs/src/lib.rs @@ -21,6 +21,8 @@ #[cfg(feature = "rustls")] use std::ops::Deref; +use snafu::Snafu; +use x509_cert::{spki::EncodePublicKey, Certificate}; #[cfg(feature = "rustls")] use { p256::pkcs8::EncodePrivateKey, @@ -29,9 +31,6 @@ use { x509_cert::der::Encode, }; -use snafu::Snafu; -use x509_cert::{spki::EncodePublicKey, Certificate}; - use crate::keys::CertificateKeypair; pub mod ca; diff --git a/crates/stackable-operator/src/builder/configmap.rs b/crates/stackable-operator/src/builder/configmap.rs index 095fda8fa..0dfe1b334 100644 --- a/crates/stackable-operator/src/builder/configmap.rs +++ b/crates/stackable-operator/src/builder/configmap.rs @@ -1,6 +1,7 @@ +use std::collections::BTreeMap; + use k8s_openapi::{api::core::v1::ConfigMap, apimachinery::pkg::apis::meta::v1::ObjectMeta}; use snafu::{OptionExt, Snafu}; -use std::collections::BTreeMap; type Result = std::result::Result; @@ -64,10 +65,10 @@ impl ConfigMapBuilder { #[cfg(test)] mod tests { - use crate::builder::{configmap::ConfigMapBuilder, meta::ObjectMetaBuilder}; - use std::collections::BTreeMap; + use crate::builder::{configmap::ConfigMapBuilder, meta::ObjectMetaBuilder}; + #[test] fn configmap_builder() { let mut data = BTreeMap::new(); diff --git a/crates/stackable-operator/src/builder/meta.rs b/crates/stackable-operator/src/builder/meta.rs index fbf23e793..ed1c3b20c 100644 --- a/crates/stackable-operator/src/builder/meta.rs +++ b/crates/stackable-operator/src/builder/meta.rs @@ -311,9 +311,10 @@ impl OwnerReferenceBuilder { #[cfg(test)] mod tests { + use k8s_openapi::api::core::v1::Pod; + use super::*; use crate::builder::meta::ObjectMetaBuilder; - use k8s_openapi::api::core::v1::Pod; #[test] fn objectmeta_builder() { diff --git a/crates/stackable-operator/src/builder/pdb.rs b/crates/stackable-operator/src/builder/pdb.rs index d6ff52778..89bb94891 100644 --- a/crates/stackable-operator/src/builder/pdb.rs +++ b/crates/stackable-operator/src/builder/pdb.rs @@ -209,10 +209,9 @@ mod tests { use schemars::JsonSchema; use serde::{Deserialize, Serialize}; + use super::*; use crate::builder::meta::{ObjectMetaBuilder, OwnerReferenceBuilder}; - use super::PodDisruptionBudgetBuilder; - #[test] pub fn normal_build() { #[allow(deprecated)] diff --git a/crates/stackable-operator/src/builder/pod/container.rs b/crates/stackable-operator/src/builder/pod/container.rs index 49b847b13..7997ecc60 100644 --- a/crates/stackable-operator/src/builder/pod/container.rs +++ b/crates/stackable-operator/src/builder/pod/container.rs @@ -1,8 +1,5 @@ use std::fmt; -#[cfg(doc)] -use {k8s_openapi::api::core::v1::PodSpec, std::collections::BTreeMap}; - use indexmap::IndexMap; use k8s_openapi::api::core::v1::{ ConfigMapKeySelector, Container, ContainerPort, EnvVar, EnvVarSource, Lifecycle, @@ -11,6 +8,8 @@ use k8s_openapi::api::core::v1::{ }; use snafu::{ResultExt as _, Snafu}; use tracing::instrument; +#[cfg(doc)] +use {k8s_openapi::api::core::v1::PodSpec, std::collections::BTreeMap}; use crate::{ commons::product_image_selection::ResolvedProductImage, diff --git a/crates/stackable-operator/src/builder/pod/mod.rs b/crates/stackable-operator/src/builder/pod/mod.rs index 54b34bd3a..2140066dc 100644 --- a/crates/stackable-operator/src/builder/pod/mod.rs +++ b/crates/stackable-operator/src/builder/pod/mod.rs @@ -12,9 +12,11 @@ use k8s_openapi::{ use snafu::{OptionExt, ResultExt, Snafu}; use tracing::{instrument, warn}; -use crate::kvp::Labels; use crate::{ - builder::meta::ObjectMetaBuilder, + builder::{ + meta::ObjectMetaBuilder, + pod::volume::{ListenerOperatorVolumeSourceBuilder, ListenerReference, VolumeBuilder}, + }, commons::{ affinity::StackableAffinity, product_image_selection::ResolvedProductImage, @@ -23,11 +25,10 @@ use crate::{ LIMIT_REQUEST_RATIO_CPU, LIMIT_REQUEST_RATIO_MEMORY, }, }, + kvp::Labels, time::Duration, }; -use self::volume::{ListenerOperatorVolumeSourceBuilder, ListenerReference, VolumeBuilder}; - pub mod container; pub mod resources; pub mod security; @@ -633,6 +634,7 @@ mod tests { }; use rstest::*; + use super::*; use crate::builder::{ meta::ObjectMetaBuilder, pod::{ @@ -641,8 +643,6 @@ mod tests { }, }; - use super::*; - // A simple [`Container`] with a name and image. #[fixture] fn dummy_container() -> Container { diff --git a/crates/stackable-operator/src/builder/pod/security.rs b/crates/stackable-operator/src/builder/pod/security.rs index 95e4be8f5..72426cf05 100644 --- a/crates/stackable-operator/src/builder/pod/security.rs +++ b/crates/stackable-operator/src/builder/pod/security.rs @@ -70,6 +70,7 @@ impl SecurityContextBuilder { sc.level = Some(level.into()); self } + pub fn se_linux_role(&mut self, role: impl Into) -> &mut Self { let sc = self .security_context @@ -201,6 +202,7 @@ impl PodSecurityContextBuilder { )); self } + pub fn se_linux_role(&mut self, role: &str) -> &mut Self { self.pod_security_context.se_linux_options = Some(self.pod_security_context.se_linux_options.clone().map_or( @@ -215,6 +217,7 @@ impl PodSecurityContextBuilder { )); self } + pub fn se_linux_type(&mut self, type_: &str) -> &mut Self { self.pod_security_context.se_linux_options = Some(self.pod_security_context.se_linux_options.clone().map_or( @@ -229,6 +232,7 @@ impl PodSecurityContextBuilder { )); self } + pub fn se_linux_user(&mut self, user: &str) -> &mut Self { self.pod_security_context.se_linux_options = Some(self.pod_security_context.se_linux_options.clone().map_or( @@ -335,9 +339,10 @@ impl PodSecurityContextBuilder { #[cfg(test)] mod tests { - use super::*; use k8s_openapi::api::core::v1::{PodSecurityContext, SELinuxOptions, SeccompProfile, Sysctl}; + use super::*; + #[test] fn security_context_builder() { let mut builder = PodSecurityContextBuilder::new(); diff --git a/crates/stackable-operator/src/builder/pod/volume.rs b/crates/stackable-operator/src/builder/pod/volume.rs index bb15c2c09..66180d0ac 100644 --- a/crates/stackable-operator/src/builder/pod/volume.rs +++ b/crates/stackable-operator/src/builder/pod/volume.rs @@ -8,7 +8,6 @@ use k8s_openapi::{ }, apimachinery::pkg::api::resource::Quantity, }; - use snafu::{ResultExt, Snafu}; use tracing::warn; @@ -568,10 +567,12 @@ impl ListenerOperatorVolumeSourceBuilder { #[cfg(test)] mod tests { - use super::*; - use k8s_openapi::apimachinery::pkg::api::resource::Quantity; use std::collections::BTreeMap; + use k8s_openapi::apimachinery::pkg::api::resource::Quantity; + + use super::*; + #[test] fn builder() { let mut volume_builder = VolumeBuilder::new("name"); diff --git a/crates/stackable-operator/src/cli.rs b/crates/stackable-operator/src/cli.rs index 8bfeb26c9..2a53184c1 100644 --- a/crates/stackable-operator/src/cli.rs +++ b/crates/stackable-operator/src/cli.rs @@ -284,13 +284,14 @@ fn resolve_path<'a>( #[cfg(test)] mod tests { - use super::*; + use std::{env, fs::File}; + use clap::Parser; use rstest::*; - use std::env; - use std::fs::File; use tempfile::tempdir; + use super::*; + const USER_PROVIDED_PATH: &str = "user_provided_path_properties.yaml"; const DEPLOY_FILE_PATH: &str = "deploy_config_spec_properties.yaml"; const DEFAULT_FILE_PATH: &str = "default_file_path_properties.yaml"; diff --git a/crates/stackable-operator/src/client.rs b/crates/stackable-operator/src/client.rs index de2b5f870..594c06af2 100644 --- a/crates/stackable-operator/src/client.rs +++ b/crates/stackable-operator/src/client.rs @@ -579,12 +579,14 @@ where (K, K::Scope): GetApiImpl, { type Namespace = <(K, K::Scope) as GetApiImpl>::Namespace; + fn get_api(client: kube::Client, ns: &Self::Namespace) -> kube::Api where Self::DynamicType: Default, { <(K, K::Scope) as GetApiImpl>::get_api(client, ns) } + fn get_namespace(&self) -> &Self::Namespace { <(K, K::Scope) as GetApiImpl>::get_namespace(self) } @@ -605,14 +607,16 @@ impl GetApiImpl for (K, NamespaceResourceScope) where K: Resource, { - type Resource = K; type Namespace = str; + type Resource = K; + fn get_api(client: kube::Client, ns: &Self::Namespace) -> kube::Api where ::DynamicType: Default, { Api::namespaced(client, ns) } + fn get_namespace(res: &Self::Resource) -> &Self::Namespace { res.meta().namespace.as_deref().unwrap_or_default() } @@ -622,14 +626,16 @@ impl GetApiImpl for (K, ClusterResourceScope) where K: Resource, { - type Resource = K; type Namespace = (); + type Resource = K; + fn get_api(client: kube::Client, (): &Self::Namespace) -> kube::Api where ::DynamicType: Default, { Api::all(client) } + fn get_namespace(_res: &Self::Resource) -> &Self::Namespace { &() } diff --git a/crates/stackable-operator/src/cluster_resources.rs b/crates/stackable-operator/src/cluster_resources.rs index 8cce2b099..dd4fad75c 100644 --- a/crates/stackable-operator/src/cluster_resources.rs +++ b/crates/stackable-operator/src/cluster_resources.rs @@ -1,20 +1,8 @@ //! A structure containing the cluster resources. -use crate::{ - client::{Client, GetApi}, - commons::{ - cluster_operation::ClusterOperation, - listener::Listener, - resources::{ - ComputeResource, ResourceRequirementsExt, ResourceRequirementsType, - LIMIT_REQUEST_RATIO_CPU, LIMIT_REQUEST_RATIO_MEMORY, - }, - }, - kvp::{ - consts::{K8S_APP_INSTANCE_KEY, K8S_APP_MANAGED_BY_KEY, K8S_APP_NAME_KEY}, - Label, LabelError, Labels, - }, - utils::format_full_controller_name, +use std::{ + collections::{BTreeMap, HashSet}, + fmt::Debug, }; use k8s_openapi::{ @@ -33,10 +21,6 @@ use k8s_openapi::{ use kube::{core::ErrorResponse, Resource, ResourceExt}; use serde::{de::DeserializeOwned, Serialize}; use snafu::{OptionExt, ResultExt, Snafu}; -use std::{ - collections::{BTreeMap, HashSet}, - fmt::Debug, -}; use strum::Display; use tracing::{debug, info, warn}; @@ -45,6 +29,22 @@ use crate::k8s_openapi::api::{ apps::v1::Deployment, core::v1::{NodeSelector, Pod}, }; +use crate::{ + client::{Client, GetApi}, + commons::{ + cluster_operation::ClusterOperation, + listener::Listener, + resources::{ + ComputeResource, ResourceRequirementsExt, ResourceRequirementsType, + LIMIT_REQUEST_RATIO_CPU, LIMIT_REQUEST_RATIO_MEMORY, + }, + }, + kvp::{ + consts::{K8S_APP_INSTANCE_KEY, K8S_APP_MANAGED_BY_KEY, K8S_APP_NAME_KEY}, + Label, LabelError, Labels, + }, + utils::format_full_controller_name, +}; type Result = std::result::Result; diff --git a/crates/stackable-operator/src/commons/affinity.rs b/crates/stackable-operator/src/commons/affinity.rs index 071c14e70..188b366a9 100644 --- a/crates/stackable-operator/src/commons/affinity.rs +++ b/crates/stackable-operator/src/commons/affinity.rs @@ -138,9 +138,8 @@ mod tests { apimachinery::pkg::apis::meta::v1::LabelSelectorRequirement, }; - use crate::config::fragment; - use super::*; + use crate::config::fragment; #[test] fn merge_new_attributes() { diff --git a/crates/stackable-operator/src/commons/listener.rs b/crates/stackable-operator/src/commons/listener.rs index abcda4293..b088e3950 100644 --- a/crates/stackable-operator/src/commons/listener.rs +++ b/crates/stackable-operator/src/commons/listener.rs @@ -27,14 +27,13 @@ use std::collections::BTreeMap; -use kube::CustomResource; -use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; - #[cfg(doc)] use k8s_openapi::api::core::v1::{ Node, PersistentVolume, PersistentVolumeClaim, Pod, Service, Volume, }; +use kube::CustomResource; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; #[cfg(doc)] use crate::builder::pod::volume::ListenerOperatorVolumeSourceBuilder; diff --git a/crates/stackable-operator/src/commons/networking.rs b/crates/stackable-operator/src/commons/networking.rs index 534cd3bb1..1dd1079d9 100644 --- a/crates/stackable-operator/src/commons/networking.rs +++ b/crates/stackable-operator/src/commons/networking.rs @@ -177,10 +177,10 @@ impl Deref for KerberosRealmName { #[cfg(test)] mod tests { - use super::*; - use rstest::rstest; + use super::*; + #[rstest] #[case("foo")] #[case("foo.bar")] diff --git a/crates/stackable-operator/src/commons/opa.rs b/crates/stackable-operator/src/commons/opa.rs index cedcec45e..1adb11f8c 100644 --- a/crates/stackable-operator/src/commons/opa.rs +++ b/crates/stackable-operator/src/commons/opa.rs @@ -45,7 +45,6 @@ //! ``` use std::sync::LazyLock; -use crate::client::{Client, GetApi}; use k8s_openapi::{api::core::v1::ConfigMap, NamespaceResourceScope}; use kube::{Resource, ResourceExt}; use regex::Regex; @@ -53,6 +52,8 @@ use schemars::{self, JsonSchema}; use serde::{Deserialize, Serialize}; use snafu::{OptionExt, ResultExt, Snafu}; +use crate::client::{Client, GetApi}; + static DOT_REGEX: LazyLock = LazyLock::new(|| Regex::new("\\.").expect("failed to compile OPA dot regex")); diff --git a/crates/stackable-operator/src/commons/product_image_selection.rs b/crates/stackable-operator/src/commons/product_image_selection.rs index 66a3fa916..6fe5ba674 100644 --- a/crates/stackable-operator/src/commons/product_image_selection.rs +++ b/crates/stackable-operator/src/commons/product_image_selection.rs @@ -178,10 +178,10 @@ impl ProductImage { #[cfg(test)] mod tests { - use super::*; - use rstest::rstest; + use super::*; + #[rstest] #[case::stackable_version_without_stackable_version_stable_version( "superset", diff --git a/crates/stackable-operator/src/commons/resources.rs b/crates/stackable-operator/src/commons/resources.rs index fbaaf9f1e..e25bdf856 100644 --- a/crates/stackable-operator/src/commons/resources.rs +++ b/crates/stackable-operator/src/commons/resources.rs @@ -68,6 +68,24 @@ //! } //! ``` +use std::{collections::BTreeMap, fmt::Debug}; + +use educe::Educe; +use k8s_openapi::{ + api::core::v1::{ + Container, PersistentVolumeClaim, PersistentVolumeClaimSpec, PodSpec, ResourceRequirements, + VolumeResourceRequirements, + }, + apimachinery::pkg::{ + api::resource::Quantity, + apis::meta::v1::{LabelSelector, ObjectMeta}, + }, +}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use snafu::{ResultExt, Snafu}; +use strum::Display; + use crate::{ config::{ fragment::{Fragment, FromFragment}, @@ -76,18 +94,6 @@ use crate::{ cpu::CpuQuantity, memory::MemoryQuantity, }; -use educe::Educe; -use k8s_openapi::api::core::v1::{ - Container, PersistentVolumeClaim, PersistentVolumeClaimSpec, PodSpec, ResourceRequirements, - VolumeResourceRequirements, -}; -use k8s_openapi::apimachinery::pkg::api::resource::Quantity; -use k8s_openapi::apimachinery::pkg::apis::meta::v1::{LabelSelector, ObjectMeta}; -use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; -use snafu::{ResultExt, Snafu}; -use std::{collections::BTreeMap, fmt::Debug}; -use strum::Display; pub const LIMIT_REQUEST_RATIO_CPU: f32 = 5.0; pub const LIMIT_REQUEST_RATIO_MEMORY: f32 = 1.0; @@ -522,17 +528,19 @@ impl ResourceRequirementsExt for PodSpec { #[cfg(test)] mod tests { - use crate::builder::pod::resources::ResourceRequirementsBuilder; - use crate::commons::resources::{PvcConfig, PvcConfigFragment, Resources, ResourcesFragment}; - use crate::config::{ - fragment::{self, Fragment}, - merge::Merge, - }; use k8s_openapi::api::core::v1::{Container, PersistentVolumeClaim, ResourceRequirements}; use rstest::rstest; use serde::{Deserialize, Serialize}; use super::*; + use crate::{ + builder::pod::resources::ResourceRequirementsBuilder, + commons::resources::{PvcConfig, PvcConfigFragment, Resources, ResourcesFragment}, + config::{ + fragment::{self, Fragment}, + merge::Merge, + }, + }; #[derive(Clone, Debug, Default, Fragment)] #[fragment(path_overrides(fragment = "crate::config::fragment"))] diff --git a/crates/stackable-operator/src/commons/s3/crd.rs b/crates/stackable-operator/src/commons/s3/crd.rs index 6905a396b..d1609ccb9 100644 --- a/crates/stackable-operator/src/commons/s3/crd.rs +++ b/crates/stackable-operator/src/commons/s3/crd.rs @@ -3,11 +3,10 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::commons::{ - networking::HostName, secret_class::SecretClassVolume, tls_verification::TlsClientDetails, + networking::HostName, s3::S3ConnectionInlineOrReference, secret_class::SecretClassVolume, + tls_verification::TlsClientDetails, }; -use super::S3ConnectionInlineOrReference; - /// S3 bucket specification containing the bucket name and an inlined or referenced connection specification. /// Learn more on the [S3 concept documentation](DOCS_BASE_URL_PLACEHOLDER/concepts/s3). #[derive(Clone, CustomResource, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] diff --git a/crates/stackable-operator/src/commons/s3/helpers.rs b/crates/stackable-operator/src/commons/s3/helpers.rs index c43068734..654bee025 100644 --- a/crates/stackable-operator/src/commons/s3/helpers.rs +++ b/crates/stackable-operator/src/commons/s3/helpers.rs @@ -7,13 +7,14 @@ use url::Url; use crate::{ builder::pod::{container::ContainerBuilder, volume::VolumeMountBuilder, PodBuilder}, client::Client, - commons::authentication::SECRET_BASE_PATH, -}; - -use super::{ - AddS3CredentialVolumesSnafu, AddS3TlsClientDetailsVolumesSnafu, AddVolumeMountsSnafu, - AddVolumesSnafu, ParseS3EndpointSnafu, RetrieveS3ConnectionSnafu, S3Bucket, S3BucketSpec, - S3Connection, S3ConnectionSpec, S3Error, SetS3EndpointSchemeSnafu, + commons::{ + authentication::SECRET_BASE_PATH, + s3::{ + AddS3CredentialVolumesSnafu, AddS3TlsClientDetailsVolumesSnafu, AddVolumeMountsSnafu, + AddVolumesSnafu, ParseS3EndpointSnafu, RetrieveS3ConnectionSnafu, S3Bucket, + S3BucketSpec, S3Connection, S3ConnectionSpec, S3Error, SetS3EndpointSchemeSnafu, + }, + }, }; #[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] @@ -185,13 +186,12 @@ impl S3BucketInlineOrReference { mod tests { use std::collections::BTreeMap; + use super::*; use crate::commons::{ secret_class::SecretClassVolume, tls_verification::{CaCert, Tls, TlsClientDetails, TlsServerVerification, TlsVerification}, }; - use super::*; - // We cant test the correct resolve, as we can't mock the k8s API. #[test] fn http_endpoint() { diff --git a/crates/stackable-operator/src/commons/s3/mod.rs b/crates/stackable-operator/src/commons/s3/mod.rs index ecd6b87d9..8ed68d810 100644 --- a/crates/stackable-operator/src/commons/s3/mod.rs +++ b/crates/stackable-operator/src/commons/s3/mod.rs @@ -3,13 +3,12 @@ mod helpers; pub use crd::*; pub use helpers::*; - use snafu::Snafu; use url::Url; -use crate::builder::{self}; - -use super::{secret_class::SecretClassVolumeError, tls_verification::TlsClientDetailsError}; +use crate::commons::{ + secret_class::SecretClassVolumeError, tls_verification::TlsClientDetailsError, +}; #[derive(Debug, Snafu)] pub enum S3Error { @@ -35,10 +34,10 @@ pub enum S3Error { AddS3TlsClientDetailsVolumes { source: TlsClientDetailsError }, #[snafu(display("failed to add required volumes"))] - AddVolumes { source: builder::pod::Error }, + AddVolumes { source: crate::builder::pod::Error }, #[snafu(display("failed to add required volumeMounts"))] AddVolumeMounts { - source: builder::pod::container::Error, + source: crate::builder::pod::container::Error, }, } diff --git a/crates/stackable-operator/src/commons/secret_class.rs b/crates/stackable-operator/src/commons/secret_class.rs index 71a2195ac..2ed8ca7de 100644 --- a/crates/stackable-operator/src/commons/secret_class.rs +++ b/crates/stackable-operator/src/commons/secret_class.rs @@ -96,9 +96,10 @@ pub struct SecretClassVolumeScope { #[cfg(test)] mod tests { - use super::*; use std::collections::BTreeMap; + use super::*; + #[test] fn volume_to_csi_volume_source() { let secret_class_volume_source = SecretClassVolume { diff --git a/crates/stackable-operator/src/config/fragment.rs b/crates/stackable-operator/src/config/fragment.rs index 3548a852a..b5bb72af0 100644 --- a/crates/stackable-operator/src/config/fragment.rs +++ b/crates/stackable-operator/src/config/fragment.rs @@ -8,18 +8,16 @@ use std::{ hash::Hash, }; -use super::merge::Atomic; +use k8s_openapi::api::core::v1::PodTemplateSpec; +use snafu::Snafu; +pub use stackable_operator_derive::Fragment; +use super::merge::Atomic; #[cfg(doc)] use super::merge::Merge; #[cfg(doc)] use crate::role_utils::{Role, RoleGroup}; -use k8s_openapi::api::core::v1::PodTemplateSpec; -use snafu::Snafu; - -pub use stackable_operator_derive::Fragment; - /// Contains context used for generating validation errors /// /// Constructed internally in [`validate`] diff --git a/crates/stackable-operator/src/config/merge.rs b/crates/stackable-operator/src/config/merge.rs index ebced0fa0..4e451c793 100644 --- a/crates/stackable-operator/src/config/merge.rs +++ b/crates/stackable-operator/src/config/merge.rs @@ -1,15 +1,15 @@ //! Automatically merges objects *deeply*, especially fragments. -use k8s_openapi::{ - api::core::v1::{NodeAffinity, PodAffinity, PodAntiAffinity, PodTemplateSpec}, - apimachinery::pkg::{api::resource::Quantity, apis::meta::v1::LabelSelector}, - DeepMerge, -}; use std::{ collections::{btree_map, hash_map, BTreeMap, HashMap}, hash::Hash, }; +use k8s_openapi::{ + api::core::v1::{NodeAffinity, PodAffinity, PodAntiAffinity, PodTemplateSpec}, + apimachinery::pkg::{api::resource::Quantity, apis::meta::v1::LabelSelector}, + DeepMerge, +}; pub use stackable_operator_derive::Merge; use crate::time::Duration; @@ -162,9 +162,10 @@ impl Merge for Option { #[cfg(test)] mod tests { - use k8s_openapi::api::core::v1::{PodSpec, PodTemplateSpec}; use std::collections::{BTreeMap, HashMap}; + use k8s_openapi::api::core::v1::{PodSpec, PodTemplateSpec}; + use super::{merge, Merge}; #[derive(Debug, PartialEq, Eq, Clone)] diff --git a/crates/stackable-operator/src/config/mod.rs b/crates/stackable-operator/src/config/mod.rs index a349804be..3cbb464e9 100644 --- a/crates/stackable-operator/src/config/mod.rs +++ b/crates/stackable-operator/src/config/mod.rs @@ -165,9 +165,10 @@ pub mod fragment; pub mod merge; -#[cfg(doc)] -use crate::role_utils::{Role, RoleGroup}; #[cfg(doc)] use fragment::{validate, Fragment, FromFragment}; #[cfg(doc)] use merge::{Atomic, Merge}; + +#[cfg(doc)] +use crate::role_utils::{Role, RoleGroup}; diff --git a/crates/stackable-operator/src/cpu.rs b/crates/stackable-operator/src/cpu.rs index 45bb421c9..59f299225 100644 --- a/crates/stackable-operator/src/cpu.rs +++ b/crates/stackable-operator/src/cpu.rs @@ -229,9 +229,10 @@ impl Sum for CpuQuantity { #[cfg(test)] mod tests { - use super::*; use rstest::*; + use super::*; + #[rstest] #[case("1", 1000)] #[case("1000m", 1000)] diff --git a/crates/stackable-operator/src/helm/mod.rs b/crates/stackable-operator/src/helm/mod.rs index f987c8c71..5c3d551ea 100644 --- a/crates/stackable-operator/src/helm/mod.rs +++ b/crates/stackable-operator/src/helm/mod.rs @@ -78,7 +78,6 @@ mod tests { use std::path::PathBuf; use rstest::rstest; - use stackable_shared::yaml::{serialize as ser, SerializeOptions}; use super::*; diff --git a/crates/stackable-operator/src/kvp/annotation/mod.rs b/crates/stackable-operator/src/kvp/annotation/mod.rs index a64f327e0..c73d64c44 100644 --- a/crates/stackable-operator/src/kvp/annotation/mod.rs +++ b/crates/stackable-operator/src/kvp/annotation/mod.rs @@ -243,34 +243,6 @@ impl From for BTreeMap { } impl Annotations { - /// Creates a new empty list of [`Annotations`]. - pub fn new() -> Self { - Self::default() - } - - /// Creates a new list of [`Annotations`] from `pairs`. - pub fn new_with(pairs: BTreeSet>) -> Self { - Self(KeyValuePairs::new_with(pairs)) - } - - /// Tries to insert a new annotation by first parsing `annotation` as an - /// [`Annotation`] and then inserting it into the list. This function will - /// overwrite any existing annotation already present. - pub fn parse_insert( - &mut self, - annotation: impl TryInto, - ) -> Result<(), AnnotationError> { - self.0.insert(annotation.try_into()?.0); - Ok(()) - } - - /// Inserts a new [`Annotation`]. This function will overwrite any existing - /// annotation already present. - pub fn insert(&mut self, annotation: Annotation) -> &mut Self { - self.0.insert(annotation.0); - self - } - // This forwards / delegates associated functions to the inner field. In // this case self.0 which is of type KeyValuePairs. So calling // Annotations::len() will be delegated to KeyValuePair::len() without @@ -305,11 +277,39 @@ impl Annotations { pub fn iter(&self) -> impl Iterator> + '_; } } + + /// Creates a new empty list of [`Annotations`]. + pub fn new() -> Self { + Self::default() + } + + /// Creates a new list of [`Annotations`] from `pairs`. + pub fn new_with(pairs: BTreeSet>) -> Self { + Self(KeyValuePairs::new_with(pairs)) + } + + /// Tries to insert a new annotation by first parsing `annotation` as an + /// [`Annotation`] and then inserting it into the list. This function will + /// overwrite any existing annotation already present. + pub fn parse_insert( + &mut self, + annotation: impl TryInto, + ) -> Result<(), AnnotationError> { + self.0.insert(annotation.try_into()?.0); + Ok(()) + } + + /// Inserts a new [`Annotation`]. This function will overwrite any existing + /// annotation already present. + pub fn insert(&mut self, annotation: Annotation) -> &mut Self { + self.0.insert(annotation.0); + self + } } impl IntoIterator for Annotations { - type Item = KeyValuePair; type IntoIter = as IntoIterator>::IntoIter; + type Item = KeyValuePair; /// Returns a consuming [`Iterator`] over [`Annotations`] moving every [`Annotation`] out. /// The [`Annotations`] cannot be used again after calling this. diff --git a/crates/stackable-operator/src/kvp/key.rs b/crates/stackable-operator/src/kvp/key.rs index a7a6e0790..6275689bd 100644 --- a/crates/stackable-operator/src/kvp/key.rs +++ b/crates/stackable-operator/src/kvp/key.rs @@ -340,9 +340,8 @@ where mod test { use rstest::rstest; - use crate::kvp::Label; - use super::*; + use crate::kvp::Label; #[test] fn key_with_prefix() { diff --git a/crates/stackable-operator/src/kvp/label/mod.rs b/crates/stackable-operator/src/kvp/label/mod.rs index ae38c12d5..5aa74ca5a 100644 --- a/crates/stackable-operator/src/kvp/label/mod.rs +++ b/crates/stackable-operator/src/kvp/label/mod.rs @@ -232,6 +232,40 @@ impl From for BTreeMap { } impl Labels { + // This forwards / delegates associated functions to the inner field. In + // this case self.0 which is of type KeyValuePairs. So calling + // Labels::len() will be delegated to KeyValuePair::len() without the + // need to write boilerplate code. + delegate! { + to self.0 { + /// Tries to insert a new [`Label`]. It ensures there are no duplicate + /// entries. Trying to insert duplicated data returns an error. If no such + /// check is required, use [`Labels::insert`] instead. + pub fn try_insert(&mut self, #[newtype] label: Label) -> Result<(), LabelsError>; + + /// Extends `self` with `other`. + pub fn extend(&mut self, #[newtype] other: Self); + + /// Returns the number of labels. + pub fn len(&self) -> usize; + + /// Returns if the set of labels is empty. + pub fn is_empty(&self) -> bool; + + /// Returns if the set of labels contains the provided `label`. Failure to + /// parse/validate the [`KeyValuePair`] will return `false`. + pub fn contains(&self, label: impl TryInto>) -> bool; + + /// Returns if the set of labels contains a label with the provided `key`. + /// Failure to parse/validate the [`Key`] will return `false`. + pub fn contains_key(&self, key: impl TryInto) -> bool; + + /// Returns an [`Iterator`] over [`Labels`] yielding a reference to every [`Label`] contained within. + pub fn iter(&self) -> impl Iterator> + '_; + + } + } + /// Creates a new empty list of [`Labels`]. pub fn new() -> Self { Self::default() @@ -355,45 +389,11 @@ impl Labels { Ok(labels) } - - // This forwards / delegates associated functions to the inner field. In - // this case self.0 which is of type KeyValuePairs. So calling - // Labels::len() will be delegated to KeyValuePair::len() without the - // need to write boilerplate code. - delegate! { - to self.0 { - /// Tries to insert a new [`Label`]. It ensures there are no duplicate - /// entries. Trying to insert duplicated data returns an error. If no such - /// check is required, use [`Labels::insert`] instead. - pub fn try_insert(&mut self, #[newtype] label: Label) -> Result<(), LabelsError>; - - /// Extends `self` with `other`. - pub fn extend(&mut self, #[newtype] other: Self); - - /// Returns the number of labels. - pub fn len(&self) -> usize; - - /// Returns if the set of labels is empty. - pub fn is_empty(&self) -> bool; - - /// Returns if the set of labels contains the provided `label`. Failure to - /// parse/validate the [`KeyValuePair`] will return `false`. - pub fn contains(&self, label: impl TryInto>) -> bool; - - /// Returns if the set of labels contains a label with the provided `key`. - /// Failure to parse/validate the [`Key`] will return `false`. - pub fn contains_key(&self, key: impl TryInto) -> bool; - - /// Returns an [`Iterator`] over [`Labels`] yielding a reference to every [`Label`] contained within. - pub fn iter(&self) -> impl Iterator> + '_; - - } - } } impl IntoIterator for Labels { - type Item = KeyValuePair; type IntoIter = as IntoIterator>::IntoIter; + type Item = KeyValuePair; /// Returns a consuming [`Iterator`] over [`Labels`] moving every [`Label`] out. /// The [`Labels`] cannot be used again after calling this. diff --git a/crates/stackable-operator/src/kvp/label/selector.rs b/crates/stackable-operator/src/kvp/label/selector.rs index a8f99d89a..9f9c3f806 100644 --- a/crates/stackable-operator/src/kvp/label/selector.rs +++ b/crates/stackable-operator/src/kvp/label/selector.rs @@ -111,10 +111,12 @@ impl LabelSelectorExt for LabelSelector { #[cfg(test)] mod tests { - use super::*; - use k8s_openapi::apimachinery::pkg::apis::meta::v1::{LabelSelector, LabelSelectorRequirement}; use std::collections::BTreeMap; + use k8s_openapi::apimachinery::pkg::apis::meta::v1::{LabelSelector, LabelSelectorRequirement}; + + use super::*; + #[test] fn label_selector() { let mut match_labels = BTreeMap::new(); diff --git a/crates/stackable-operator/src/kvp/label/value.rs b/crates/stackable-operator/src/kvp/label/value.rs index 2f8610e04..e47ef2db4 100644 --- a/crates/stackable-operator/src/kvp/label/value.rs +++ b/crates/stackable-operator/src/kvp/label/value.rs @@ -89,9 +89,10 @@ impl Display for LabelValue { #[cfg(test)] mod test { - use super::*; use rstest::rstest; + use super::*; + #[rstest] #[case("a".repeat(64), LabelValueError::ValueTooLong { length: 64 })] #[case("foo-", LabelValueError::ValueInvalid)] diff --git a/crates/stackable-operator/src/kvp/mod.rs b/crates/stackable-operator/src/kvp/mod.rs index 5a233c603..45c9379bb 100644 --- a/crates/stackable-operator/src/kvp/mod.rs +++ b/crates/stackable-operator/src/kvp/mod.rs @@ -342,9 +342,9 @@ impl IntoIterator for KeyValuePairs where T: Value, { - type Item = KeyValuePair; type IntoIter = std::iter::Map, fn((Key, T)) -> Self::Item>; + type Item = KeyValuePair; /// Returns a consuming [`Iterator`] over [`KeyValuePairs`] moving every [`KeyValuePair`] out. /// The [`KeyValuePairs`] cannot be used again after calling this. diff --git a/crates/stackable-operator/src/logging/k8s_events.rs b/crates/stackable-operator/src/logging/k8s_events.rs index f282f599b..a4f3f392c 100644 --- a/crates/stackable-operator/src/logging/k8s_events.rs +++ b/crates/stackable-operator/src/logging/k8s_events.rs @@ -92,9 +92,8 @@ mod message { #[cfg(test)] mod tests { - use crate::logging::k8s_events::message::find_start_of_char; - use super::truncate_with_ellipsis; + use crate::logging::k8s_events::message::find_start_of_char; #[test] fn truncate_should_be_noop_if_string_fits() { diff --git a/crates/stackable-operator/src/memory.rs b/crates/stackable-operator/src/memory.rs index 8dfef7bee..2019fd791 100644 --- a/crates/stackable-operator/src/memory.rs +++ b/crates/stackable-operator/src/memory.rs @@ -8,10 +8,6 @@ //! //! For details on Kubernetes quantities see: -use k8s_openapi::apimachinery::pkg::api::resource::Quantity; -use serde::{de::Visitor, Deserialize, Serialize}; -use snafu::{OptionExt, ResultExt, Snafu}; - use std::{ fmt::Display, iter::Sum, @@ -19,6 +15,10 @@ use std::{ str::FromStr, }; +use k8s_openapi::apimachinery::pkg::api::resource::Quantity; +use serde::{de::Visitor, Deserialize, Serialize}; +use snafu::{OptionExt, ResultExt, Snafu}; + pub type Result = std::result::Result; #[derive(Debug, PartialEq, Snafu)] @@ -516,9 +516,9 @@ impl From<&MemoryQuantity> for Quantity { #[cfg(test)] mod tests { use k8s_openapi::apimachinery::pkg::api::resource::Quantity; + use rstest::rstest; use super::*; - use rstest::rstest; #[rstest] #[case("256Ki", MemoryQuantity { value: 256.0, unit: BinaryMultiple::Kibi })] diff --git a/crates/stackable-operator/src/namespace.rs b/crates/stackable-operator/src/namespace.rs index f2fdf3275..ccca8dc13 100644 --- a/crates/stackable-operator/src/namespace.rs +++ b/crates/stackable-operator/src/namespace.rs @@ -1,8 +1,9 @@ //! This module provides helpers and constants to deal with namespaces -use crate::client::Client; use k8s_openapi::NamespaceResourceScope; use kube::{Api, Resource}; +use crate::client::Client; + #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] pub enum WatchNamespace { All, diff --git a/crates/stackable-operator/src/pod_utils.rs b/crates/stackable-operator/src/pod_utils.rs index ab9b8e675..19c1fcc1d 100644 --- a/crates/stackable-operator/src/pod_utils.rs +++ b/crates/stackable-operator/src/pod_utils.rs @@ -4,8 +4,7 @@ Not necessarily these exact functions, but things in here will probably stick ar shape or form. */ -use kube::api::Resource; -use kube::runtime::reflector::ObjectRef; +use kube::{api::Resource, runtime::reflector::ObjectRef}; /// Returns a name that is suitable for directly passing to a log macro. /// diff --git a/crates/stackable-operator/src/product_config_utils.rs b/crates/stackable-operator/src/product_config_utils.rs index 240957751..2063c709a 100644 --- a/crates/stackable-operator/src/product_config_utils.rs +++ b/crates/stackable-operator/src/product_config_utils.rs @@ -530,12 +530,13 @@ mod tests { }; } - use super::*; - use crate::role_utils::{GenericProductSpecificCommonConfig, Role, RoleGroup}; + use std::{collections::HashMap, str::FromStr}; + use k8s_openapi::api::core::v1::PodTemplateSpec; use rstest::*; - use std::collections::HashMap; - use std::str::FromStr; + + use super::*; + use crate::role_utils::{GenericProductSpecificCommonConfig, Role, RoleGroup}; const ROLE_GROUP: &str = "role_group"; diff --git a/crates/stackable-operator/src/product_logging/framework.rs b/crates/stackable-operator/src/product_logging/framework.rs index f5ee29022..4d946609b 100644 --- a/crates/stackable-operator/src/product_logging/framework.rs +++ b/crates/stackable-operator/src/product_logging/framework.rs @@ -13,13 +13,12 @@ use crate::{ }, kube::Resource, memory::{BinaryMultiple, MemoryQuantity}, + product_logging::spec::{ + AutomaticContainerLogConfig, ContainerLogConfig, ContainerLogConfigChoice, LogLevel, + }, role_utils::RoleGroupRef, }; -use super::spec::{ - AutomaticContainerLogConfig, ContainerLogConfig, ContainerLogConfigChoice, LogLevel, -}; - /// Config directory used in the Vector log agent container const STACKABLE_CONFIG_DIR: &str = "/stackable/config"; /// Directory in the Vector container which contains a subdirectory for every container which @@ -1525,10 +1524,12 @@ pub fn remove_vector_shutdown_file_command(stackable_log_dir: &str) -> String { #[cfg(test)] mod tests { + use std::collections::BTreeMap; + + use rstest::rstest; + use super::*; use crate::product_logging::spec::{AppenderConfig, LoggerConfig}; - use rstest::rstest; - use std::collections::BTreeMap; #[rstest] #[case("0Mi", &[])] diff --git a/crates/stackable-operator/src/product_logging/spec.rs b/crates/stackable-operator/src/product_logging/spec.rs index 29a904a82..48a1646dd 100644 --- a/crates/stackable-operator/src/product_logging/spec.rs +++ b/crates/stackable-operator/src/product_logging/spec.rs @@ -2,16 +2,15 @@ use std::{borrow::Cow, collections::BTreeMap, fmt::Display}; -use crate::config::{ - fragment::{self, Fragment, FromFragment}, - merge::Atomic, - merge::Merge, -}; - use educe::Educe; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use crate::config::{ + fragment::{self, Fragment, FromFragment}, + merge::{Atomic, Merge}, +}; + /// Logging configuration /// /// The type parameter `T` should be an enum listing all containers: @@ -451,8 +450,6 @@ pub fn default_container_log_config() -> ContainerLogConfigFragment { mod tests { use std::collections::BTreeMap; - use crate::config::{fragment, merge}; - use super::{ AppenderConfig, AppenderConfigFragment, AutomaticContainerLogConfig, AutomaticContainerLogConfigFragment, ConfigMapLogConfig, ConfigMapLogConfigFragment, @@ -460,6 +457,7 @@ mod tests { ContainerLogConfigFragment, CustomContainerLogConfig, CustomContainerLogConfigFragment, LogLevel, }; + use crate::config::{fragment, merge}; #[test] fn serialize_container_log_config() { diff --git a/crates/stackable-operator/src/role_utils.rs b/crates/stackable-operator/src/role_utils.rs index 068705cd4..7e41e5d54 100644 --- a/crates/stackable-operator/src/role_utils.rs +++ b/crates/stackable-operator/src/role_utils.rs @@ -85,6 +85,14 @@ use std::{ fmt::{Debug, Display}, }; +use educe::Educe; +use k8s_openapi::api::core::v1::PodTemplateSpec; +use kube::{runtime::reflector::ObjectRef, Resource}; +use regex::Regex; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use snafu::{OptionExt, ResultExt, Snafu}; + use crate::{ commons::pdb::PdbConfig, config::{ @@ -94,13 +102,6 @@ use crate::{ product_config_utils::Configuration, utils::crds::raw_object_schema, }; -use educe::Educe; -use k8s_openapi::api::core::v1::PodTemplateSpec; -use kube::{runtime::reflector::ObjectRef, Resource}; -use regex::Regex; -use schemars::JsonSchema; -use serde::{Deserialize, Serialize}; -use snafu::{OptionExt, ResultExt, Snafu}; #[derive(Debug, Snafu)] pub enum Error { @@ -486,9 +487,8 @@ impl Display for RoleGroupRef { mod tests { use std::collections::HashSet; - use crate::role_utils::JavaCommonConfig; - use super::*; + use crate::role_utils::JavaCommonConfig; #[test] fn test_merge_java_common_config() { diff --git a/crates/stackable-operator/src/status/condition/daemonset.rs b/crates/stackable-operator/src/status/condition/daemonset.rs index ac4e5234d..a9fc46f79 100644 --- a/crates/stackable-operator/src/status/condition/daemonset.rs +++ b/crates/stackable-operator/src/status/condition/daemonset.rs @@ -1,12 +1,13 @@ +use std::cmp; + +use k8s_openapi::api::apps::v1::DaemonSet; +use kube::ResourceExt; + use crate::status::condition::{ ClusterCondition, ClusterConditionSet, ClusterConditionStatus, ClusterConditionType, ConditionBuilder, }; -use k8s_openapi::api::apps::v1::DaemonSet; -use kube::ResourceExt; -use std::cmp; - /// Default implementation to build [`ClusterCondition`]s for /// `DaemonSet` resources. /// diff --git a/crates/stackable-operator/src/status/condition/operations.rs b/crates/stackable-operator/src/status/condition/operations.rs index d90e0e0e3..22d57c288 100644 --- a/crates/stackable-operator/src/status/condition/operations.rs +++ b/crates/stackable-operator/src/status/condition/operations.rs @@ -1,7 +1,9 @@ -use crate::commons::cluster_operation::ClusterOperation; -use crate::status::condition::{ - ClusterCondition, ClusterConditionSet, ClusterConditionStatus, ClusterConditionType, - ConditionBuilder, +use crate::{ + commons::cluster_operation::ClusterOperation, + status::condition::{ + ClusterCondition, ClusterConditionSet, ClusterConditionStatus, ClusterConditionType, + ConditionBuilder, + }, }; /// Default implementation to build [`ClusterCondition`]s for @@ -87,9 +89,10 @@ impl<'a> ClusterOperationsConditionBuilder<'a> { #[cfg(test)] mod tests { - use super::*; use rstest::*; + use super::*; + #[rstest] #[case::not_paused_not_stopped( false, diff --git a/crates/stackable-operator/src/status/condition/statefulset.rs b/crates/stackable-operator/src/status/condition/statefulset.rs index baa79b41d..b75308289 100644 --- a/crates/stackable-operator/src/status/condition/statefulset.rs +++ b/crates/stackable-operator/src/status/condition/statefulset.rs @@ -1,12 +1,13 @@ +use std::cmp; + +use k8s_openapi::api::apps::v1::StatefulSet; +use kube::ResourceExt; + use crate::status::condition::{ ClusterCondition, ClusterConditionSet, ClusterConditionStatus, ClusterConditionType, ConditionBuilder, }; -use k8s_openapi::api::apps::v1::StatefulSet; -use kube::ResourceExt; -use std::cmp; - /// Default implementation to build [`ClusterCondition`]s for /// `StatefulSet` resources. /// diff --git a/crates/stackable-operator/src/time/duration.rs b/crates/stackable-operator/src/time/duration.rs index d7053bd73..8f70d93b9 100644 --- a/crates/stackable-operator/src/time/duration.rs +++ b/crates/stackable-operator/src/time/duration.rs @@ -421,10 +421,11 @@ impl DurationUnit { #[cfg(test)] mod test { - use super::*; use rstest::rstest; use serde::{Deserialize, Serialize}; + use super::*; + #[test] fn const_from() { assert_eq!(Duration::from_secs(42).as_secs(), 42); diff --git a/crates/stackable-operator/src/utils/mod.rs b/crates/stackable-operator/src/utils/mod.rs index e400072d5..12b469b11 100644 --- a/crates/stackable-operator/src/utils/mod.rs +++ b/crates/stackable-operator/src/utils/mod.rs @@ -15,7 +15,6 @@ pub use self::bash::COMMON_BASH_TRAP_FUNCTIONS; since = "0.61.1" )] pub use self::logging::print_startup_string; - pub use self::{option::OptionExt, url::UrlExt}; /// Returns the fully qualified controller name, which should be used when a single controller needs to be referred to uniquely. diff --git a/crates/stackable-operator/src/utils/option.rs b/crates/stackable-operator/src/utils/option.rs index e851644b1..b8c56520b 100644 --- a/crates/stackable-operator/src/utils/option.rs +++ b/crates/stackable-operator/src/utils/option.rs @@ -1,7 +1,6 @@ -use std::{borrow::Cow, ops::Deref}; - #[cfg(doc)] use std::path::PathBuf; +use std::{borrow::Cow, ops::Deref}; /// Extension methods for [`Option`]. pub trait OptionExt { diff --git a/crates/stackable-operator/src/validation.rs b/crates/stackable-operator/src/validation.rs index 68ad7b8ad..524d4f7d9 100644 --- a/crates/stackable-operator/src/validation.rs +++ b/crates/stackable-operator/src/validation.rs @@ -308,9 +308,10 @@ pub fn validate_namespace_name(name: &str, prefix: bool) -> Result { #[cfg(test)] mod tests { - use super::*; use rstest::rstest; + use super::*; + #[rstest] #[case("")] #[case("A")] diff --git a/crates/stackable-telemetry/src/instrumentation/axum/mod.rs b/crates/stackable-telemetry/src/instrumentation/axum/mod.rs index 5b88c437b..d4ea09639 100644 --- a/crates/stackable-telemetry/src/instrumentation/axum/mod.rs +++ b/crates/stackable-telemetry/src/instrumentation/axum/mod.rs @@ -121,9 +121,9 @@ where S::Error: std::error::Error + 'static, S::Future: Send + 'static, { - type Response = S::Response; type Error = S::Error; type Future = ResponseFuture; + type Response = S::Response; fn poll_ready( &mut self, diff --git a/crates/stackable-telemetry/src/tracing/mod.rs b/crates/stackable-telemetry/src/tracing/mod.rs index 7af9206c1..a400d96d6 100644 --- a/crates/stackable-telemetry/src/tracing/mod.rs +++ b/crates/stackable-telemetry/src/tracing/mod.rs @@ -19,7 +19,7 @@ use tracing::subscriber::SetGlobalDefaultError; use tracing_appender::rolling::{InitError, RollingFileAppender, Rotation}; use tracing_subscriber::{filter::Directive, layer::SubscriberExt, EnvFilter, Layer, Registry}; -use settings::*; +use crate::tracing::settings::*; pub mod settings; diff --git a/crates/stackable-versioned-macros/src/codegen/changes.rs b/crates/stackable-versioned-macros/src/codegen/changes.rs index 8aa0e8858..1a3c0bdf3 100644 --- a/crates/stackable-versioned-macros/src/codegen/changes.rs +++ b/crates/stackable-versioned-macros/src/codegen/changes.rs @@ -204,9 +204,10 @@ impl ChangesetExt for BTreeMap { #[cfg(test)] mod test { - use super::*; use rstest::rstest; + use super::*; + #[rstest] #[case(0, (None, Some(&"test1")))] #[case(1, (None, Some(&"test3")))] diff --git a/crates/stackable-versioned-macros/tests/k8s/fail/crd.rs b/crates/stackable-versioned-macros/tests/k8s/fail/crd.rs index 38acfe8c8..e88cbd7e6 100644 --- a/crates/stackable-versioned-macros/tests/k8s/fail/crd.rs +++ b/crates/stackable-versioned-macros/tests/k8s/fail/crd.rs @@ -1,6 +1,5 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; - use stackable_versioned_macros::versioned; #[allow(deprecated)] diff --git a/crates/stackable-webhook/src/lib.rs b/crates/stackable-webhook/src/lib.rs index bc8763822..e7b023282 100644 --- a/crates/stackable-webhook/src/lib.rs +++ b/crates/stackable-webhook/src/lib.rs @@ -29,8 +29,8 @@ use snafu::{ResultExt, Snafu}; use stackable_telemetry::AxumTraceLayer; use tokio::signal::unix::{signal, SignalKind}; use tower::ServiceBuilder; -// use tower_http::trace::TraceLayer; +// use tower_http::trace::TraceLayer; use crate::tls::TlsServer; pub mod constants; diff --git a/crates/stackable-webhook/src/servers/conversion.rs b/crates/stackable-webhook/src/servers/conversion.rs index b612a77bf..e8a823744 100644 --- a/crates/stackable-webhook/src/servers/conversion.rs +++ b/crates/stackable-webhook/src/servers/conversion.rs @@ -1,13 +1,12 @@ use std::fmt::Debug; use axum::{extract::State, routing::post, Json, Router}; -use tracing::instrument; - // Re-export this type because users of the conversion webhook server require // this type to write the handler function. Instead of importing this type from // kube directly, consumers can use this type instead. This also eliminates // keeping the kube dependency version in sync between here and the operator. pub use kube::core::conversion::ConversionReview; +use tracing::instrument; use crate::{options::Options, StatefulWebhookHandler, WebhookHandler, WebhookServer}; diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 000000000..b9884b78e --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,4 @@ +imports_granularity = "Crate" +group_imports = "StdExternalCrate" +reorder_impl_items = true +use_field_init_shorthand = true From bd05bda494b37d3cb54dd4b8b1fbbeda1a529180 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 3 Feb 2025 16:40:12 +0100 Subject: [PATCH 2/5] chore: Adjust pre-commit config file Pre-commit now uses a local hook to validate formatting of Rust code. --- .pre-commit-config.yaml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 691eb8d85..137a08e6f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,8 +15,6 @@ repos: - repo: https://github.com/doublify/pre-commit-rust rev: eeee35a89e69d5772bdee97db1a6a898467b686e # 1.0 hooks: - - id: fmt - args: ["--all", "--", "--check"] - id: clippy args: ["--all-targets", "--", "-D", "warnings"] - repo: https://github.com/adrienverge/yamllint @@ -53,3 +51,10 @@ repos: entry: .scripts/verify_crate_versions.sh stages: [pre-commit, pre-merge-commit, manual] pass_filenames: false + + - id: rustfmt + name: rustfmt + language: system + entry: cargo +nightly-2025-01-15 fmt --all -- --check + stages: [pre-commit] + pass_filenames: false From 474c347b0b99deb4cd71a79d2abb15f08d503fdc Mon Sep 17 00:00:00 2001 From: Techassi Date: Tue, 4 Feb 2025 08:45:52 +0100 Subject: [PATCH 3/5] ci: Use nightly toolchain for pre-commit workflow --- .github/workflows/pr_pre-commit.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr_pre-commit.yaml b/.github/workflows/pr_pre-commit.yaml index 6f2ceb8e0..73590cf69 100644 --- a/.github/workflows/pr_pre-commit.yaml +++ b/.github/workflows/pr_pre-commit.yaml @@ -6,7 +6,7 @@ on: env: CARGO_TERM_COLOR: always - RUST_TOOLCHAIN_VERSION: "1.82.0" + RUST_TOOLCHAIN_VERSION: "nightly-2025-01-15" HADOLINT_VERSION: "v1.17.6" jobs: From 759b2a4b11137f3759b68592f4b0771560aa16c8 Mon Sep 17 00:00:00 2001 From: Techassi Date: Tue, 4 Feb 2025 08:46:21 +0100 Subject: [PATCH 4/5] test: Fix stderr output after test code changes --- .../stackable-versioned-macros/tests/k8s/fail/crd.stderr | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/stackable-versioned-macros/tests/k8s/fail/crd.stderr b/crates/stackable-versioned-macros/tests/k8s/fail/crd.stderr index 33d671a9a..cbd2faa7e 100644 --- a/crates/stackable-versioned-macros/tests/k8s/fail/crd.stderr +++ b/crates/stackable-versioned-macros/tests/k8s/fail/crd.stderr @@ -1,11 +1,11 @@ error: struct name needs to include the `Spec` suffix if Kubernetes features are enabled via `#[versioned(k8s())]` - --> tests/k8s/fail/crd.rs:15:16 + --> tests/k8s/fail/crd.rs:14:16 | -15 | pub struct Foo { +14 | pub struct Foo { | ^^^ error[E0433]: failed to resolve: use of undeclared type `Foo` - --> tests/k8s/fail/crd.rs:24:22 + --> tests/k8s/fail/crd.rs:23:22 | -24 | let merged_crd = Foo::merged_crd("v1").unwrap(); +23 | let merged_crd = Foo::merged_crd("v1").unwrap(); | ^^^ use of undeclared type `Foo` From daa30a74928049a735a3a8a6c2379b6541671801 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 10 Feb 2025 11:27:57 +0100 Subject: [PATCH 5/5] chore: Apply suggestion Co-authored-by: Sebastian Bernauer --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 137a08e6f..ed3c7b4f9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -55,6 +55,7 @@ repos: - id: rustfmt name: rustfmt language: system + # Pinning to a specific rustc version, so that we get consistent formatting entry: cargo +nightly-2025-01-15 fmt --all -- --check stages: [pre-commit] pass_filenames: false