-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig_map.rs
More file actions
169 lines (144 loc) · 5.39 KB
/
Copy pathconfig_map.rs
File metadata and controls
169 lines (144 loc) · 5.39 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Assembles the rolegroup [`ConfigMap`] from the [`ValidatedCluster`], dispatching to the
//! per-file builders in [`crate::controller::build::properties`].
use snafu::{ResultExt, Snafu};
use stackable_operator::{
builder::configmap::ConfigMapBuilder, k8s_openapi::api::core::v1::ConfigMap,
product_logging::framework::VECTOR_CONFIG_FILE,
};
use crate::controller::{
OpaRoleGroupConfig, RoleGroupName, ValidatedCluster,
build::properties::{ConfigFileName, config_json, product_logging, user_info_fetcher},
};
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to build config.json"))]
BuildConfigJson { source: config_json::Error },
#[snafu(display("failed to build user-info-fetcher.json"))]
BuildUserInfoFetcher { source: user_info_fetcher::Error },
#[snafu(display("failed to assemble ConfigMap for role group {role_group}"))]
Assemble {
source: stackable_operator::builder::configmap::Error,
role_group: RoleGroupName,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
/// The rolegroup [`ConfigMap`] configures the rolegroup based on the configuration given by the
/// administrator.
///
/// The Vector agent config (`vector.yaml`) is added only when the Vector agent is enabled for this
/// role group.
pub fn build_rolegroup_config_map(
cluster: &ValidatedCluster,
role_group_name: &RoleGroupName,
rolegroup_config: &OpaRoleGroupConfig,
) -> Result<ConfigMap> {
let mut cm_builder = ConfigMapBuilder::new();
let metadata = cluster
.object_meta(
cluster
.role_group_resource_names(role_group_name)
.role_group_config_map()
.to_string(),
role_group_name,
)
.build();
cm_builder.metadata(metadata).add_data(
ConfigFileName::ConfigJson.to_string(),
config_json::build(&rolegroup_config.config, &rolegroup_config.config_overrides)
.context(BuildConfigJsonSnafu)?,
);
if let Some(user_info) = &cluster.cluster_config.user_info {
cm_builder.add_data(
ConfigFileName::UserInfoFetcher.to_string(),
user_info_fetcher::build(user_info).context(BuildUserInfoFetcherSnafu)?,
);
}
if rolegroup_config.config.logging.vector_container.is_some() {
cm_builder.add_data(
VECTOR_CONFIG_FILE,
product_logging::vector_config_file_content(),
);
}
cm_builder.build().with_context(|_| AssembleSnafu {
role_group: role_group_name.clone(),
})
}
#[cfg(test)]
mod tests {
use serde_json::{Value, json};
use super::*;
use crate::{
controller::build::properties::test_support::validated_cluster_from_spec, crd::OpaRole,
};
/// Renders the ConfigMap of the `default` server role group of an `OpaCluster` built from `spec`.
fn build_config_map(spec: Value) -> ConfigMap {
let validated = validated_cluster_from_spec(spec);
let role = OpaRole::Server;
let (role_group_name, rg) = validated.role_group_configs[&role]
.iter()
.next()
.expect("the default role group should exist");
build_rolegroup_config_map(&validated, role_group_name, rg)
.expect("the config map should build")
}
#[test]
fn renders_config_json_without_user_info() {
let cm = build_config_map(json!({
"image": { "productVersion": "1.2.3" },
"servers": { "roleGroups": { "default": {} } },
}));
let data = cm.data.as_ref().expect("config map data");
assert!(data.contains_key("config.json"));
assert!(!data.contains_key("user-info-fetcher.json"));
}
#[test]
fn renders_user_info_fetcher_json_when_configured() {
let cm = build_config_map(json!({
"image": { "productVersion": "1.2.3" },
"clusterConfig": {
"userInfo": {
"backend": {
"experimentalXfscAas": {
"hostname": "aas.default.svc.cluster.local",
"port": 5000,
}
}
}
},
"servers": { "roleGroups": { "default": {} } },
}));
let data = cm.data.as_ref().expect("config map data");
assert!(data.contains_key("config.json"));
assert!(data.contains_key("user-info-fetcher.json"));
}
#[test]
fn renders_vector_yaml_when_agent_enabled() {
let cm = build_config_map(json!({
"image": { "productVersion": "1.2.3" },
"clusterConfig": { "vectorAggregatorConfigMapName": "vector-aggregator-discovery" },
"servers": {
"config": { "logging": { "enableVectorAgent": true } },
"roleGroups": { "default": {} },
},
}));
assert!(
cm.data
.as_ref()
.expect("config map data")
.contains_key(VECTOR_CONFIG_FILE)
);
}
#[test]
fn omits_vector_yaml_when_agent_disabled() {
let cm = build_config_map(json!({
"image": { "productVersion": "1.2.3" },
"servers": { "roleGroups": { "default": {} } },
}));
assert!(
!cm.data
.as_ref()
.expect("config map data")
.contains_key(VECTOR_CONFIG_FILE)
);
}
}