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

refactor: Replace lazy_static with std::sync::LazyLock #840

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: '0'
CARGO_PROFILE_DEV_DEBUG: '0'
RUST_TOOLCHAIN_VERSION: "1.79.0"
Techassi marked this conversation as resolved.
Show resolved Hide resolved
RUST_TOOLCHAIN_VERSION: "1.80.1"
RUSTFLAGS: "-D warnings"
RUSTDOCFLAGS: "-D warnings"
RUST_LOG: "info"
Expand All @@ -37,7 +37,7 @@ jobs:
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3
with:
key: udeps
- run: cargo install --locked cargo-udeps@0.1.47
- run: cargo install --locked cargo-udeps@0.1.50
- run: cargo udeps --all-targets

run_cargodeny:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr_pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

env:
CARGO_TERM_COLOR: always
RUST_TOOLCHAIN_VERSION: "1.79.0"
RUST_TOOLCHAIN_VERSION: "1.80.1"

jobs:
pre-commit:
Expand Down
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ json-patch = "2.0.0"
k8s-openapi = { version = "0.22.0", default-features = false, features = ["schemars", "v1_30"] }
# We use rustls instead of openssl for easier portablitly, e.g. so that we can build stackablectl without the need to vendor (build from source) openssl
kube = { version = "0.93.1", default-features = false, features = ["client", "jsonpatch", "runtime", "derive", "rustls-tls"] }
lazy_static = "1.5.0"
opentelemetry = "0.23.0"
opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio"] }
opentelemetry-appender-tracing = "0.4.0"
Expand Down
8 changes: 8 additions & 0 deletions crates/k8s-version/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Changed

- Replace `lazy_static` with `std::cell::LazyCell` ([#827], [#835], [#840]).

[#827]: https://github.com/stackabletech/operator-rs/pull/827
[#835]: https://github.com/stackabletech/operator-rs/pull/835
[#840]: https://github.com/stackabletech/operator-rs/pull/840

## [0.1.1] - 2024-07-10

### Changed
Expand Down
1 change: 0 additions & 1 deletion crates/k8s-version/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ darling = ["dep:darling"]

[dependencies]
darling = { workspace = true, optional = true }
lazy_static.workspace = true
regex.workspace = true
snafu.workspace = true

Expand Down
12 changes: 5 additions & 7 deletions crates/k8s-version/src/group.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::{fmt, ops::Deref, str::FromStr};
use std::{fmt, ops::Deref, str::FromStr, sync::LazyLock};

use lazy_static::lazy_static;
use regex::Regex;
use snafu::{ensure, Snafu};

const MAX_GROUP_LENGTH: usize = 253;

lazy_static! {
static ref API_GROUP_REGEX: Regex =
Regex::new(r"^(?:(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9])\.?)+$")
.expect("failed to compile API group regex");
}
static API_GROUP_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(?:(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9])\.?)+$")
.expect("failed to compile API group regex")
});

/// Error variants which can be encountered when creating a new [`Group`] from
/// unparsed input.
Expand Down
9 changes: 4 additions & 5 deletions crates/k8s-version/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ use std::{
num::ParseIntError,
ops::{Add, AddAssign, Sub, SubAssign},
str::FromStr,
sync::LazyLock,
};

use lazy_static::lazy_static;
use regex::Regex;
use snafu::{OptionExt, ResultExt, Snafu};

#[cfg(feature = "darling")]
use darling::FromMeta;

lazy_static! {
static ref LEVEL_REGEX: Regex = Regex::new(r"^(?P<identifier>[a-z]+)(?P<version>\d+)$")
.expect("failed to compile level regex");
}
static LEVEL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(?P<identifier>[a-z]+)(?P<version>\d+)$").expect("failed to compile level regex")
});

/// Error variants which can be encountered when creating a new [`Level`] from
/// unparsed input.
Expand Down
12 changes: 5 additions & 7 deletions crates/k8s-version/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{cmp::Ordering, fmt::Display, num::ParseIntError, str::FromStr};
use std::{cmp::Ordering, fmt::Display, num::ParseIntError, str::FromStr, sync::LazyLock};

use lazy_static::lazy_static;
use regex::Regex;
use snafu::{OptionExt, ResultExt, Snafu};

Expand All @@ -9,11 +8,10 @@ use darling::FromMeta;

use crate::{Level, ParseLevelError};

lazy_static! {
static ref VERSION_REGEX: Regex =
Regex::new(r"^v(?P<major>\d+)(?P<level>[a-z0-9][a-z0-9-]{0,60}[a-z0-9])?$")
.expect("failed to compile version regex");
}
static VERSION_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^v(?P<major>\d+)(?P<level>[a-z0-9][a-z0-9-]{0,60}[a-z0-9])?$")
.expect("failed to compile version regex")
});

/// Error variants which can be encountered when creating a new [`Version`] from
/// unparsed input.
Expand Down
7 changes: 5 additions & 2 deletions crates/stackable-operator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- BREAKING: Replace `lazy_static` with `std::cell::LazyCell` (the original implementation was done in [#827] and reverted in [#835]) ([#840]).

### Added

- `iter::reverse_if` helper ([#838]).
Expand All @@ -18,7 +20,7 @@ All notable changes to this project will be documented in this file.

### Changed

- Reverted [#827], in order to restore Rust 1.79 compatibility for now ([#835]).
- Reverted [#827], in order to restore Rust 1.79 compatibility for now ([#835]), re-opened in ([#840]).

### Fixed

Expand All @@ -32,7 +34,7 @@ All notable changes to this project will be documented in this file.

### Changed

- BREAKING: Replace `lazy_static` with `std::cell::LazyCell` ([#827]).
- BREAKING: Replace `lazy_static` with `std::cell::LazyCell` ([#827], [#835], [#840]).
- BREAKING: Convert `podOverrides` and `affinity` fields to take any arbitrary
YAML input, rather than using the underlying schema. With this change, one of
the larger CRDs, like the Druid CRD went down in size from `2.4MB` to `288K`
Expand All @@ -44,6 +46,7 @@ All notable changes to this project will be documented in this file.

[#821]: https://github.com/stackabletech/operator-rs/pull/821
[#827]: https://github.com/stackabletech/operator-rs/pull/827
[#840]: https://github.com/stackabletech/operator-rs/pull/840

## [0.71.0] - 2024-07-29

Expand Down
1 change: 0 additions & 1 deletion crates/stackable-operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ futures.workspace = true
json-patch.workspace = true
k8s-openapi.workspace = true
kube.workspace = true
lazy_static.workspace = true
opentelemetry_sdk.workspace = true
opentelemetry-jaeger.workspace = true
product-config.workspace = true
Expand Down
12 changes: 6 additions & 6 deletions crates/stackable-operator/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,15 @@ impl Client {

/// There are four different patch strategies:
/// 1) Apply (<https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply>)
/// Starting from Kubernetes v1.18, you can enable the Server Side Apply feature so that the control plane tracks managed fields for all newly created objects.
/// Starting from Kubernetes v1.18, you can enable the Server Side Apply feature so that the control plane tracks managed fields for all newly created objects.
/// 2) Json (<https://tools.ietf.org/html/rfc6902>):
/// This is supported on crate feature jsonpatch only
/// This is supported on crate feature jsonpatch only
/// 3) Merge (<https://tools.ietf.org/html/rfc7386>):
/// For example, if you want to update a list you have to specify the complete list and update everything
/// For example, if you want to update a list you have to specify the complete list and update everything
/// 4) Strategic (not for CustomResource)
/// With a strategic merge patch, a list is either replaced or merged depending on its patch strategy.
/// The patch strategy is specified by the value of the patchStrategy key in a field tag in the Kubernetes source code.
/// For example, the Containers field of PodSpec struct has a patchStrategy of merge.
/// With a strategic merge patch, a list is either replaced or merged depending on its patch strategy.
/// The patch strategy is specified by the value of the patchStrategy key in a field tag in the Kubernetes source code.
/// For example, the Containers field of PodSpec struct has a patchStrategy of merge.
async fn patch_status<T, S>(
&self,
resource: &T,
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-operator/src/commons/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub enum KubernetesTrafficPolicy {
/// 1. It uses a cluster-level policy object (ListenerClass) to define how exactly the exposure works
/// 2. It has a consistent API for reading back the exposed address(es) of the service
/// 3. The Pod must mount a Volume referring to the Listener, which also allows
/// ["sticky" scheduling](DOCS_BASE_URL_PLACEHOLDER/listener-operator/listener#_sticky_scheduling).
/// ["sticky" scheduling](DOCS_BASE_URL_PLACEHOLDER/listener-operator/listener#_sticky_scheduling).
///
/// Learn more in the [Listener documentation](DOCS_BASE_URL_PLACEHOLDER/listener-operator/listener).
#[derive(
Expand Down
15 changes: 9 additions & 6 deletions crates/stackable-operator/src/commons/opa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,23 @@
//! assert_eq!(opa_config.document_url(&cluster, Some("allow"), OpaApiVersion::V1), "v1/data/test/allow".to_string());
//! assert_eq!(opa_config.full_document_url(&cluster, "http://localhost:8081", None, OpaApiVersion::V1), "http://localhost:8081/v1/data/test".to_string());
//! ```
use std::sync::LazyLock;

use crate::client::{Client, GetApi};
use k8s_openapi::{api::core::v1::ConfigMap, NamespaceResourceScope};
use kube::{Resource, ResourceExt};
use lazy_static::lazy_static;
use regex::Regex;
use schemars::{self, JsonSchema};
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};

static DOT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new("\\.").expect("failed to compile OPA dot regex"));

/// To remove leading slashes from OPA package name (if present)
static LEADING_SLASH_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new("(/*)(.*)").expect("failed to compile OPA leasing slash regex"));

type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Snafu)]
Expand All @@ -72,11 +80,6 @@ pub enum Error {
},
}

lazy_static! {
static ref DOT_REGEX: Regex = Regex::new("\\.").unwrap();
/// To remove leading slashes from OPA package name (if present)
static ref LEADING_SLASH_REGEX: Regex = Regex::new("(/*)(.*)").unwrap();
}
/// Indicates the OPA API version. This is required to choose the correct
/// path when constructing the OPA urls to query.
pub enum OpaApiVersion {
Expand Down
10 changes: 6 additions & 4 deletions crates/stackable-operator/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ impl Display for CpuQuantity {
impl FromStr for CpuQuantity {
type Err = Error;

/// Only two formats can be parsed
/// Only two formats can be parsed:
///
/// - {usize}m
/// - {f32}
/// For the float, only milli-precision is supported.
/// Using more precise values will trigger an error, and using any other
/// unit than 'm' or None will also trigger an error.
///
/// For the float, only milli-precision is supported. Using more precise
/// values will trigger an error, and using any other unit than 'm' or None
/// will also trigger an error.
fn from_str(q: &str) -> Result<Self> {
let start_of_unit = q.find(|c: char| c != '.' && !c.is_numeric());
if let Some(start_of_unit) = start_of_unit {
Expand Down
19 changes: 11 additions & 8 deletions crates/stackable-operator/src/kvp/key.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use std::{fmt::Display, ops::Deref, str::FromStr};
use std::{fmt::Display, ops::Deref, str::FromStr, sync::LazyLock};

use lazy_static::lazy_static;
use regex::Regex;
use snafu::{ensure, ResultExt, Snafu};

const KEY_PREFIX_MAX_LEN: usize = 253;
const KEY_NAME_MAX_LEN: usize = 63;

lazy_static! {
static ref KEY_PREFIX_REGEX: Regex =
Regex::new(r"^[a-zA-Z](\.?[a-zA-Z0-9-])*\.[a-zA-Z]{2,}\.?$").unwrap();
static ref KEY_NAME_REGEX: Regex =
Regex::new(r"^[a-z0-9A-Z]([a-z0-9A-Z-_.]*[a-z0-9A-Z]+)?$").unwrap();
}
// Lazily initialized regular expressions
static KEY_PREFIX_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[a-zA-Z](\.?[a-zA-Z0-9-])*\.[a-zA-Z]{2,}\.?$")
.expect("failed to compile key prefix regex")
});

static KEY_NAME_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[a-z0-9A-Z]([a-z0-9A-Z-_.]*[a-z0-9A-Z]+)?$")
.expect("failed to compile key name regex")
});

/// The error type for key parsing/validation operations.
///
Expand Down
12 changes: 6 additions & 6 deletions crates/stackable-operator/src/kvp/label/value.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::{fmt::Display, ops::Deref, str::FromStr};
use std::{fmt::Display, ops::Deref, str::FromStr, sync::LazyLock};

use lazy_static::lazy_static;
use regex::Regex;
use snafu::{ensure, Snafu};

use crate::kvp::Value;

const LABEL_VALUE_MAX_LEN: usize = 63;

lazy_static! {
static ref LABEL_VALUE_REGEX: Regex =
Regex::new(r"^[a-z0-9A-Z]([a-z0-9A-Z-_.]*[a-z0-9A-Z]+)?$").unwrap();
}
// Lazily initialized regular expressions
static LABEL_VALUE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[a-z0-9A-Z]([a-z0-9A-Z-_.]*[a-z0-9A-Z]+)?$")
.expect("failed to compile value regex")
});

/// The error type for label value parse/validation operations.
#[derive(Debug, PartialEq, Snafu)]
Expand Down
23 changes: 14 additions & 9 deletions crates/stackable-operator/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Utilities for converting Kubernetes quantities to Java heap settings.
//! Since Java heap sizes are a subset of Kubernetes quantities, the conversion
//! might lose precision or fail completely.
//! In addition:
//! might lose precision or fail completely. In addition:
//!
//! - decimal quantities are not supported ("2G" is invalid)
//! - units are case sensitive ("2gi" is invalid)
//! - exponential notation is not supported.
Expand Down Expand Up @@ -121,12 +121,15 @@ impl Display for BinaryMultiple {
}

/// Convert a (memory) [`Quantity`] to Java heap settings.
///
/// Quantities are usually passed on to container resources while Java heap
/// sizes need to be scaled accordingly.
/// This implements a very simple heuristic to ensure that:
/// sizes need to be scaled accordingly. This implements a very simple heuristic
/// to ensure that:
///
/// - the quantity unit has been mapped to a java supported heap unit. Java only
/// supports up to Gibibytes while K8S quantities can be expressed in Exbibytes.
/// - the heap size has a non-zero value.
///
/// Fails if it can't enforce the above restrictions.
#[deprecated(
since = "0.33.0",
Expand All @@ -148,15 +151,17 @@ pub fn to_java_heap(q: &Quantity, factor: f32) -> Result<String> {
}

/// Convert a (memory) [`Quantity`] to a raw Java heap value of the desired `target_unit`.
///
/// Quantities are usually passed on to container resources while Java heap
/// sizes need to be scaled accordingly.
/// The raw heap value is converted to the specified `target_unit` (this conversion
/// is done even if specified a unit greater that Gibibytes. It is not recommended to scale
/// to anything bigger than Gibibytes.
/// This implements a very simple heuristic to ensure that:
/// sizes need to be scaled accordingly. The raw heap value is converted to the
/// specified `target_unit` (this conversion is done even if specified a unit
/// greater that Gibibytes. It is not recommended to scale to anything bigger
/// than Gibibytes. This implements a very simple heuristic to ensure that:
///
/// - the quantity unit has been mapped to a java supported heap unit. Java only
/// supports up to Gibibytes while K8S quantities can be expressed in Exbibytes.
/// - the heap size has a non-zero value.
///
/// Fails if it can't enforce the above restrictions.
#[deprecated(
since = "0.33.0",
Expand Down
Loading
Loading