Skip to content

Commit 58189f8

Browse files
committed
refactor: consolidate containers & logging
1 parent bd74c66 commit 58189f8

8 files changed

Lines changed: 177 additions & 136 deletions

File tree

Cargo.lock

Lines changed: 48 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
use serde::{Deserialize, Serialize};
44
use snafu::{ResultExt, Snafu};
55
use stackable_operator::{
6-
product_logging::spec::{ContainerLogConfig, ContainerLogConfigChoice, LogLevel},
7-
v2::config_overrides::JsonConfigOverrides,
6+
product_logging::spec::LogLevel,
7+
v2::{
8+
config_overrides::JsonConfigOverrides,
9+
product_logging::framework::ValidatedContainerLogConfigChoice,
10+
},
811
};
912

1013
use super::ConfigFileName;
1114
use crate::{
12-
crd::{Container, OpaConfig, OpaConfigOverrides},
15+
controller::ValidatedOpaConfig,
16+
crd::{Container, OpaConfigOverrides},
1317
opa_controller::OPA_STACKABLE_SERVICE_NAME,
1418
};
1519

@@ -29,12 +33,14 @@ pub enum Error {
2933
type Result<T, E = Error> = std::result::Result<T, E>;
3034

3135
/// Builds the OPA `config.json` from the operator defaults and the merged user `configOverrides`.
32-
pub fn build(merged_config: &OpaConfig, config_overrides: &OpaConfigOverrides) -> Result<String> {
36+
pub fn build(
37+
merged_config: &ValidatedOpaConfig,
38+
config_overrides: &OpaConfigOverrides,
39+
) -> Result<String> {
3340
let mut decision_logging_enabled = DEFAULT_DECISION_LOGGING_ENABLED;
3441

35-
if let Some(ContainerLogConfig {
36-
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
37-
}) = merged_config.logging.containers.get(&Container::Opa)
42+
if let Some(ValidatedContainerLogConfigChoice::Automatic(log_config)) =
43+
merged_config.logging.containers.get(&Container::Opa)
3844
&& let Some(config) = log_config.loggers.get("decision")
3945
{
4046
decision_logging_enabled = config.level != LogLevel::NONE;
@@ -151,8 +157,7 @@ mod tests {
151157
.values()
152158
.next()
153159
.expect("the default role group should exist");
154-
let rendered =
155-
build(&rg.config.config, &rg.config.config_overrides).expect("config.json builds");
160+
let rendered = build(&rg.config, &rg.config_overrides).expect("config.json builds");
156161
serde_json::from_str(&rendered).expect("config.json should be valid JSON")
157162
}
158163

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod tests {
9797
.next()
9898
.expect("the default role group should exist");
9999

100-
build_rolegroup_config_map(&validated, role_group_name, &rg.config, None)
100+
build_rolegroup_config_map(&validated, role_group_name, rg, None)
101101
.expect("the config map should build")
102102
}
103103

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,17 @@ use stackable_operator::{
4040
product_logging::{
4141
self,
4242
framework::{create_vector_shutdown_file_command, remove_vector_shutdown_file_command},
43-
spec::{
44-
AppenderConfig, AutomaticContainerLogConfig, ContainerLogConfig,
45-
ContainerLogConfigChoice, LogLevel,
46-
},
43+
spec::{AppenderConfig, AutomaticContainerLogConfig, LogLevel},
4744
},
4845
utils::{COMMON_BASH_TRAP_FUNCTIONS, cluster_info::KubernetesClusterInfo},
4946
v2::{
5047
builder::{
5148
meta::ownerreference_from_resource,
5249
pod::container::{EnvVarSet, new_container_builder},
5350
},
54-
product_logging::framework::{STACKABLE_LOG_DIR, vector_container},
51+
product_logging::framework::{
52+
STACKABLE_LOG_DIR, ValidatedContainerLogConfigChoice, vector_container,
53+
},
5554
types::{
5655
common::Port,
5756
kubernetes::{ContainerName, VolumeName},
@@ -61,8 +60,10 @@ use stackable_operator::{
6160

6261
use super::service::{self, APP_PORT, APP_PORT_NAME};
6362
use crate::{
64-
controller::{RoleGroupName, ValidatedCluster, ValidatedRoleGroup, build},
65-
crd::{Container, DEFAULT_SERVER_GRACEFUL_SHUTDOWN_TIMEOUT, OpaConfig, user_info_fetcher},
63+
controller::{
64+
OpaRoleGroupConfig, RoleGroupName, ValidatedCluster, ValidatedOpaConfig, build,
65+
},
66+
crd::{Container, DEFAULT_SERVER_GRACEFUL_SHUTDOWN_TIMEOUT, user_info_fetcher},
6667
operations::graceful_shutdown::add_graceful_shutdown_config,
6768
};
6869

@@ -237,14 +238,14 @@ fn http_liveness_probe(path: &str, port: IntOrString, scheme: Option<String>) ->
237238
pub fn build_server_rolegroup_daemonset(
238239
cluster: &ValidatedCluster,
239240
role_group_name: &RoleGroupName,
240-
role_group: &ValidatedRoleGroup,
241+
role_group: &OpaRoleGroupConfig,
241242
opa_bundle_builder_image: &str,
242243
user_info_fetcher_image: &str,
243244
service_account: &ServiceAccount,
244245
cluster_info: &KubernetesClusterInfo,
245246
) -> Result<DaemonSet> {
246247
let resolved_product_image = &cluster.image;
247-
let rolegroup_config = &role_group.config;
248+
let rolegroup_config = role_group;
248249
// All overrides were already merged (role group over role over defaults) in the validate step.
249250
let merged_config = &rolegroup_config.config;
250251

@@ -567,7 +568,7 @@ pub fn build_server_rolegroup_daemonset(
567568

568569
// The Vector logging config was validated up-front (see `ValidatedLogging`); a `Some` here means
569570
// the Vector agent is enabled and the aggregator discovery ConfigMap name is valid.
570-
if let Some(vector_log_config) = &role_group.logging.vector_container {
571+
if let Some(vector_log_config) = &merged_config.logging.vector_container {
571572
pb.add_container(vector_container(
572573
&container_name(&Container::Vector),
573574
resolved_product_image,
@@ -661,7 +662,7 @@ fn add_stackable_rust_cli_env_vars(
661662
}
662663

663664
fn build_opa_start_command(
664-
merged_config: &OpaConfig,
665+
merged_config: &ValidatedOpaConfig,
665666
container_name: &str,
666667
tls_enabled: bool,
667668
cli_overrides: &BTreeMap<String, String>,
@@ -671,9 +672,8 @@ fn build_opa_start_command(
671672
let mut server_log_level = DEFAULT_SERVER_LOG_LEVEL;
672673
let mut decision_log_level = DEFAULT_DECISION_LOG_LEVEL;
673674

674-
if let Some(ContainerLogConfig {
675-
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
676-
}) = merged_config.logging.containers.get(&Container::Opa)
675+
if let Some(ValidatedContainerLogConfigChoice::Automatic(log_config)) =
676+
merged_config.logging.containers.get(&Container::Opa)
677677
{
678678
if let Some(AppenderConfig {
679679
level: Some(log_level),
@@ -749,14 +749,15 @@ fn build_opa_start_command(
749749
}
750750
}
751751

752-
fn build_bundle_builder_start_command(merged_config: &OpaConfig, container_name: &str) -> String {
752+
fn build_bundle_builder_start_command(
753+
merged_config: &ValidatedOpaConfig,
754+
container_name: &str,
755+
) -> String {
753756
let mut console_logging_off = false;
754757

755758
// We need to check if the console logging is deactivated (NONE)
756759
// This will result in not using `tee` later on in the start command
757-
if let Some(ContainerLogConfig {
758-
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
759-
}) = merged_config
760+
if let Some(ValidatedContainerLogConfigChoice::Automatic(log_config)) = merged_config
760761
.logging
761762
.containers
762763
.get(&Container::BundleBuilder)
@@ -812,12 +813,11 @@ fn build_bundle_builder_start_command(merged_config: &OpaConfig, container_name:
812813
///
813814
/// Context: https://docs.stackable.tech/home/stable/concepts/logging/
814815
fn sidecar_container_log_level(
815-
merged_config: &OpaConfig,
816+
merged_config: &ValidatedOpaConfig,
816817
sidecar_container: &Container,
817818
) -> build::properties::product_logging::BundleBuilderLogLevel {
818-
if let Some(ContainerLogConfig {
819-
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
820-
}) = merged_config.logging.containers.get(sidecar_container)
819+
if let Some(ValidatedContainerLogConfigChoice::Automatic(log_config)) =
820+
merged_config.logging.containers.get(sidecar_container)
821821
&& let Some(logger) = log_config
822822
.loggers
823823
.get(AutomaticContainerLogConfig::ROOT_LOGGER)
@@ -828,11 +828,13 @@ fn sidecar_container_log_level(
828828
build::properties::product_logging::BundleBuilderLogLevel::Info
829829
}
830830

831-
fn build_prepare_start_command(merged_config: &OpaConfig, container_name: &str) -> Vec<String> {
831+
fn build_prepare_start_command(
832+
merged_config: &ValidatedOpaConfig,
833+
container_name: &str,
834+
) -> Vec<String> {
832835
let mut prepare_container_args = vec![];
833-
if let Some(ContainerLogConfig {
834-
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
835-
}) = merged_config.logging.containers.get(&Container::Prepare)
836+
if let Some(ValidatedContainerLogConfigChoice::Automatic(log_config)) =
837+
merged_config.logging.containers.get(&Container::Prepare)
836838
{
837839
prepare_container_args.push(product_logging::framework::capture_shell_output(
838840
STACKABLE_LOG_DIR,

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ use std::{collections::BTreeMap, str::FromStr};
66
// Re-exported so the rest of the controller refers to `crate::controller::RoleGroupName`.
77
pub use stackable_operator::v2::types::operator::RoleGroupName;
88
use stackable_operator::{
9-
commons::product_image_selection::ResolvedProductImage,
9+
commons::{
10+
affinity::StackableAffinity,
11+
product_image_selection::ResolvedProductImage,
12+
resources::{NoRuntimeLimits, Resources},
13+
},
1014
kube::{Resource as KubeResource, api::ObjectMeta},
1115
kvp::Labels,
16+
shared::time::Duration,
1217
v2::{
1318
HasName, HasUid, NameIsValidLabelValue,
1419
kvp::label::{recommended_labels, role_group_selector, role_selector},
@@ -25,8 +30,8 @@ use stackable_operator::{
2530

2631
use crate::{
2732
crd::{
28-
APP_NAME, OPERATOR_NAME, OpaConfig, OpaConfigOverrides, OpaRole, user_info_fetcher,
29-
v1alpha2,
33+
APP_NAME, OPERATOR_NAME, OpaConfig, OpaConfigOverrides, OpaRole, OpaStorageConfig,
34+
user_info_fetcher, v1alpha2,
3035
},
3136
opa_controller::OPA_CONTROLLER_NAME,
3237
};
@@ -49,7 +54,7 @@ pub struct ValidatedCluster {
4954
pub uid: Uid,
5055
pub image: ResolvedProductImage,
5156
pub cluster_config: ValidatedClusterConfig,
52-
pub role_group_configs: BTreeMap<OpaRole, BTreeMap<RoleGroupName, ValidatedRoleGroup>>,
57+
pub role_group_configs: BTreeMap<OpaRole, BTreeMap<RoleGroupName, OpaRoleGroupConfig>>,
5358
}
5459

5560
impl ValidatedCluster {
@@ -59,7 +64,7 @@ impl ValidatedCluster {
5964
uid: Uid,
6065
image: ResolvedProductImage,
6166
cluster_config: ValidatedClusterConfig,
62-
role_group_configs: BTreeMap<OpaRole, BTreeMap<RoleGroupName, ValidatedRoleGroup>>,
67+
role_group_configs: BTreeMap<OpaRole, BTreeMap<RoleGroupName, OpaRoleGroupConfig>>,
6368
) -> Self {
6469
let metadata = ObjectMeta {
6570
name: Some(name.to_string()),
@@ -205,19 +210,26 @@ pub struct ValidatedClusterConfig {
205210
pub listener_class: v1alpha2::CurrentlySupportedListenerClasses,
206211
}
207212

208-
/// The validated configuration of a single role group.
209-
///
210-
/// All override kinds (`config`, `configOverrides`, `envOverrides`, `cliOverrides`, `podOverrides`)
211-
/// are merged once by [`with_validated_config`](stackable_operator::v2::role_utils::with_validated_config),
212-
/// with the role group winning over the role, which wins over the operator defaults.
213-
///
214-
/// Note: `replicas` is carried by the framework type but unused here — OPA runs as a `DaemonSet`
215-
/// (one Pod per node).
216-
pub type OpaRoleGroupConfig = RoleGroupConfig<OpaConfig, GenericCommonConfig, OpaConfigOverrides>;
217-
218-
/// A single validated role group: the merged [`OpaRoleGroupConfig`] plus its validated logging
219-
/// configuration (produced together by the validate step, so the build steps never re-validate).
220-
pub struct ValidatedRoleGroup {
221-
pub config: OpaRoleGroupConfig,
213+
/// The validated, merged configuration of a single OPA role group.
214+
pub type OpaRoleGroupConfig =
215+
RoleGroupConfig<ValidatedOpaConfig, GenericCommonConfig, OpaConfigOverrides>;
216+
217+
/// A validated OPA config: the merged [`OpaConfig`].
218+
#[derive(Clone, Debug, PartialEq)]
219+
pub struct ValidatedOpaConfig {
220+
pub resources: Resources<OpaStorageConfig, NoRuntimeLimits>,
222221
pub logging: validate::ValidatedLogging,
222+
pub affinity: StackableAffinity,
223+
pub graceful_shutdown_timeout: Option<Duration>,
224+
}
225+
226+
impl ValidatedOpaConfig {
227+
pub(crate) fn from_merged(merged: OpaConfig, logging: validate::ValidatedLogging) -> Self {
228+
Self {
229+
resources: merged.resources,
230+
logging,
231+
affinity: merged.affinity,
232+
graceful_shutdown_timeout: merged.graceful_shutdown_timeout,
233+
}
234+
}
223235
}

0 commit comments

Comments
 (0)