-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvalidate.rs
More file actions
124 lines (107 loc) · 3.88 KB
/
Copy pathvalidate.rs
File metadata and controls
124 lines (107 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! The validate step in the ZookeeperCluster controller.
//!
//! Synchronously validates inputs that don't require a Kubernetes client. Produces
//! [`ValidatedInputs`], consumed by the rest of `reconcile_zk`.
use product_config::{ProductConfigManager, types::PropertyNameKind};
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{
cli::OperatorEnvironmentOptions,
commons::product_image_selection::{self, ResolvedProductImage},
product_config_utils::{
ValidatedRoleConfigByPropertyKind, transform_all_roles_to_config,
validate_all_roles_and_groups_config,
},
};
use crate::{
crd::{
CONTAINER_IMAGE_BASE_NAME, JVM_SECURITY_PROPERTIES_FILE, ZOOKEEPER_PROPERTIES_FILE,
ZookeeperRole, authentication, security::ZookeeperSecurity, v1alpha1,
},
zk_controller::dereference::DereferencedObjects,
};
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to resolve product image"))]
ResolveProductImage {
source: product_image_selection::Error,
},
#[snafu(display("failed to validate authentication classes"))]
InvalidAuthenticationClassConfiguration { source: authentication::Error },
#[snafu(display("object defines no server role"))]
NoServerRole,
#[snafu(display("failed to generate product config"))]
GenerateProductConfig {
source: stackable_operator::product_config_utils::Error,
},
#[snafu(display("invalid product config"))]
InvalidProductConfig {
source: stackable_operator::product_config_utils::Error,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
/// Synchronous inputs the rest of `reconcile_zk` needs after dereferencing.
pub struct ValidatedInputs {
pub resolved_product_image: ResolvedProductImage,
pub zookeeper_security: ZookeeperSecurity,
pub validated_role_config: ValidatedRoleConfigByPropertyKind,
}
/// Validates the cluster spec and the dereferenced inputs.
pub fn validate(
zk: &v1alpha1::ZookeeperCluster,
dereferenced_objects: &DereferencedObjects,
operator_environment: &OperatorEnvironmentOptions,
product_config: &ProductConfigManager,
) -> Result<ValidatedInputs> {
let resolved_product_image = zk
.spec
.image
.resolve(
CONTAINER_IMAGE_BASE_NAME,
&operator_environment.image_repository,
crate::built_info::PKG_VERSION,
)
.context(ResolveProductImageSnafu)?;
let resolved_authentication_classes = dereferenced_objects
.authentication_classes
.validate()
.context(InvalidAuthenticationClassConfigurationSnafu)?;
let zookeeper_security = ZookeeperSecurity::new(zk, resolved_authentication_classes);
let validated_role_config =
validated_product_config(zk, &resolved_product_image.product_version, product_config)?;
Ok(ValidatedInputs {
resolved_product_image,
zookeeper_security,
validated_role_config,
})
}
fn validated_product_config(
zk: &v1alpha1::ZookeeperCluster,
product_version: &str,
product_config: &ProductConfigManager,
) -> Result<ValidatedRoleConfigByPropertyKind> {
let server_role = zk.spec.servers.clone().context(NoServerRoleSnafu)?;
let role_config = transform_all_roles_to_config(
zk,
&[(
ZookeeperRole::Server.to_string(),
(
vec![
PropertyNameKind::Env,
PropertyNameKind::File(ZOOKEEPER_PROPERTIES_FILE.to_string()),
PropertyNameKind::File(JVM_SECURITY_PROPERTIES_FILE.to_string()),
],
server_role,
),
)]
.into(),
)
.context(GenerateProductConfigSnafu)?;
validate_all_roles_and_groups_config(
product_version,
&role_config,
product_config,
false,
false,
)
.context(InvalidProductConfigSnafu)
}