Skip to content

Commit 1a30db1

Browse files
doc: Add code comments
1 parent 90013aa commit 1a30db1

8 files changed

Lines changed: 59 additions & 8 deletions

File tree

rust/operator-binary/src/controller/apply.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub enum Error {
2626

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

29+
/// Applier for the Kubernetes resource specifications produced by this controller
30+
///
31+
/// The implementation is not tied to this controller and could theoretically be moved to
32+
/// stackable_operator if `KubernetesResources` would contain all possible resource types.
2933
pub struct Applier<'a> {
3034
client: &'a Client,
3135
cluster_resources: ClusterResources,
@@ -56,6 +60,7 @@ impl<'a> Applier<'a> {
5660
}
5761
}
5862

63+
/// Applies the given Kubernetes resources and marks them as applied
5964
pub async fn apply(
6065
mut self,
6166
resources: KubernetesResources<Prepared>,

rust/operator-binary/src/controller/build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ pub mod node_config;
88
pub mod role_builder;
99
pub mod role_group_builder;
1010

11+
/// Builds Kubernetes resource specifications from the given validated cluster
12+
///
13+
/// This function cannot fail because all failing conditions were already checked in the validation
14+
/// step.
15+
/// A Kubernetes client is not required because references to other Kubernetes resources must
16+
/// already be dereferenced in a prior step and the result would be validated and added to the
17+
/// validated cluster.
1118
pub fn build(names: &ContextNames, cluster: ValidatedCluster) -> KubernetesResources<Prepared> {
1219
let mut config_maps = vec![];
1320
let mut stateful_sets = vec![];

rust/operator-binary/src/controller/build/role_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ impl<'a> RoleBuilder<'a> {
177177
.unwrap();
178178

179179
let managed_by = Label::managed_by(
180-
&self.context_names.operator_name.to_string(),
181-
&self.context_names.controller_name.to_string(),
180+
self.context_names.operator_name.as_ref(),
181+
self.context_names.controller_name.as_ref(),
182182
)
183183
.unwrap();
184-
let version = Label::version(&self.cluster.product_version.to_string()).unwrap();
184+
let version = Label::version(self.cluster.product_version.as_ref()).unwrap();
185185

186186
labels.insert(managed_by);
187187
labels.insert(version);

rust/operator-binary/src/controller/update_status.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub enum Error {
3232

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

35+
/// Updates the status of the `v1alpha1::OpenSearchCluster` according to the given applied resources
3536
pub async fn update_status(
3637
client: &Client,
3738
names: &ContextNames,

rust/operator-binary/src/controller/validate.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ pub enum Error {
6767

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

70-
// no client needed
70+
/// Validates the `v1alpha1::OpenSearchCluster` and returns a `ValidateCluster`
71+
///
72+
/// The validated values should be wrapped in fail-safe types so that illegal states are
73+
/// unrepresentable in the following steps.
74+
///
75+
/// A Kubernetes client is not required because references to other Kubernetes resources must
76+
/// already be dereferenced in a prior step.
7177
pub fn validate(
7278
context_names: &ContextNames,
7379
cluster: &v1alpha1::OpenSearchCluster,

rust/operator-binary/src/framework/builder/pod/container.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ pub enum Error {
1717
ParseEnvVarName { env_var_name: String },
1818
}
1919

20+
/// Validated environment variable name
2021
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
2122
pub struct EnvVarName(String);
2223

2324
impl EnvVarName {
25+
/// Creates an `EnvVarName` from the given string and panics if the validation failed
26+
///
27+
/// Use this only with constant names that are also tested in unit tests!
2428
pub fn from_str_unsafe(s: &str) -> Self {
2529
EnvVarName::from_str(s).expect("should be a valid environment variable name")
2630
}
@@ -36,7 +40,7 @@ impl FromStr for EnvVarName {
3640
type Err = Error;
3741

3842
fn from_str(s: &str) -> Result<Self, Self::Err> {
39-
// The length of the environment variable names seems not to be restricted.
43+
// The length of environment variable names seems not to be restricted.
4044

4145
if !s.is_empty() && s.chars().all(|c| matches!(c, ' '..='<' | '>'..='~')) {
4246
Ok(Self(s.to_owned()))
@@ -48,24 +52,35 @@ impl FromStr for EnvVarName {
4852
}
4953
}
5054

55+
/// A set of `EnvVar`s
56+
///
57+
/// The environment variable names in the set are unique.
5158
#[derive(Clone, Debug, Default, PartialEq)]
5259
pub struct EnvVarSet(BTreeMap<EnvVarName, EnvVar>);
5360

5461
impl EnvVarSet {
62+
/// Creates an empty `EnvVarSet`
5563
pub fn new() -> Self {
5664
Self::default()
5765
}
5866

67+
/// Returns a reference to the `EnvVar` with the given name
5968
pub fn get(&self, env_var_name: impl Into<EnvVarName>) -> Option<&EnvVar> {
6069
self.0.get(&env_var_name.into())
6170
}
6271

72+
/// Moves all `EnvVar`s from the given set into this one.
73+
///
74+
/// `EnvVar`s with the same name are overridden.
6375
pub fn merge(mut self, mut env_var_set: EnvVarSet) -> Self {
6476
self.0.append(&mut env_var_set.0);
6577

6678
self
6779
}
6880

81+
/// Adds the given `EnvVar`s to this set
82+
///
83+
/// `EnvVar`s with the same name are overridden.
6984
pub fn with_values<I, K, V>(self, env_vars: I) -> Self
7085
where
7186
I: IntoIterator<Item = (K, V)>,
@@ -79,6 +94,9 @@ impl EnvVarSet {
7994
})
8095
}
8196

97+
/// Adds an environment variable with the given name and string value to this set
98+
///
99+
/// An `EnvVar` with the same name is overridden.
82100
pub fn with_value(mut self, name: impl Into<EnvVarName>, value: impl Into<String>) -> Self {
83101
let name: EnvVarName = name.into();
84102

@@ -94,6 +112,9 @@ impl EnvVarSet {
94112
self
95113
}
96114

115+
/// Adds an environment variable with the given name and field path to this set
116+
///
117+
/// An `EnvVar` with the same name is overridden.
97118
pub fn with_field_path(
98119
mut self,
99120
name: impl Into<EnvVarName>,

rust/operator-binary/src/framework/role_group_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::str::FromStr;
33
use super::{ClusterName, ConfigMapName, ListenerName, RoleGroupName, RoleName, StatefulSetName};
44
use crate::framework::ServiceName;
55

6+
/// Type-safe names for role-group resources
67
pub struct ResourceNames {
78
pub cluster_name: ClusterName,
89
pub role_name: RoleName,

rust/operator-binary/src/framework/role_utils.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@ use super::{
2020
};
2121
use crate::framework::{ClusterName, ClusterRoleName};
2222

23+
/// Variant of `stackable_operator::role_utils::GenericProductSpecificCommonConfig` that implements
24+
/// `Merge`
2325
#[derive(Clone, Debug, Default, Deserialize, JsonSchema, PartialEq, Serialize)]
2426
pub struct GenericProductSpecificCommonConfig {}
2527

2628
impl Merge for GenericProductSpecificCommonConfig {
2729
fn merge(&mut self, _defaults: &Self) {}
2830
}
2931

30-
// much better to work with than RoleGroup
32+
/// Variant of `stackable_operator::role_utils::RoleGroup` that is easier to work with
33+
///
34+
/// Differences are:
35+
/// * `replicas` is non-optional.
36+
/// * `config` is flattened.
37+
/// * The `HashMap` in `env_overrides` is replaced with an `EnvVarSet`.
3138
#[derive(Clone, Debug, PartialEq)]
3239
pub struct RoleGroupConfig<ProductSpecificCommonConfig, T> {
3340
pub replicas: u16,
@@ -51,7 +58,9 @@ impl<ProductSpecificCommonConfig, T> RoleGroupConfig<ProductSpecificCommonConfig
5158
}
5259
}
5360

54-
// RoleGroup::validate_config with fixed types
61+
/// Variant of `stackable_operator::role_utils::RoleGroup::validate_config` with fixed types
62+
///
63+
/// The `role` parameter takes the `ProductSpecificCommonConfig` into account.
5564
pub fn validate_config<C, ProductSpecificCommonConfig, T, U>(
5665
role_group: &RoleGroup<T, ProductSpecificCommonConfig>,
5766
role: &Role<T, U, ProductSpecificCommonConfig>,
@@ -70,7 +79,7 @@ where
7079
fragment::validate(rolegroup_config)
7180
}
7281

73-
// also useful for operators which use the product config
82+
/// Merges and validates the `RoleGroup` with the given `role` and `default_config`
7483
pub fn with_validated_config<C, ProductSpecificCommonConfig, T, U>(
7584
role_group: &RoleGroup<T, ProductSpecificCommonConfig>,
7685
role: &Role<T, U, ProductSpecificCommonConfig>,
@@ -161,6 +170,7 @@ where
161170
merge(role_group_config, &role_config)
162171
}
163172

173+
/// Type-safe names for role resources
164174
pub struct ResourceNames {
165175
pub cluster_name: ClusterName,
166176
pub product_name: ProductName,

0 commit comments

Comments
 (0)