-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.rs
More file actions
182 lines (160 loc) · 6.33 KB
/
Copy pathbuild.rs
File metadata and controls
182 lines (160 loc) · 6.33 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
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Build steps that turn the [`ValidatedCluster`](super::ValidatedCluster) into
//! Kubernetes resource specifications.
use std::str::FromStr;
use snafu::{ResultExt, Snafu};
use stackable_operator::{utils::cluster_info::KubernetesClusterInfo, v2::types::common::Port};
use crate::controller::{
KubernetesResources, RoleGroupName, ValidatedCluster,
build::resource::{
config_map::build_rolegroup_config_map,
daemonset::build_server_rolegroup_daemonset,
discovery::build_discovery_config_map,
service::{
build_rolegroup_headless_service, build_rolegroup_metrics_service,
build_server_role_service,
},
},
};
pub mod properties;
pub mod resource;
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to build ConfigMap for role group {role_group}"))]
ConfigMap {
source: resource::config_map::Error,
role_group: RoleGroupName,
},
#[snafu(display("failed to build DaemonSet for role group {role_group}"))]
DaemonSet {
source: resource::daemonset::Error,
role_group: RoleGroupName,
},
#[snafu(display("failed to build the discovery ConfigMap"))]
Discovery { source: resource::discovery::Error },
}
/// Builds every Kubernetes resource for the given validated cluster.
///
/// Does not need a Kubernetes client: every reference to another Kubernetes resource is already
/// dereferenced and validated by this point, so the errors returned here are resource-assembly
/// failures only.
///
/// `service_account_name` is the name of the RBAC `ServiceAccount` the Pods run under (the RBAC
/// resources are built and applied separately, in the reconcile step). `cluster_info` supplies the
/// Kubernetes cluster domain used in the discovery URL and the sidecar environment.
pub fn build(
cluster: &ValidatedCluster,
service_account_name: &str,
opa_bundle_builder_image: &str,
user_info_fetcher_image: &str,
cluster_info: &KubernetesClusterInfo,
) -> Result<KubernetesResources, Error> {
let mut daemon_sets = vec![];
let mut services = vec![];
let mut config_maps = vec![];
// The role-level load-balanced Service, which is not bound to a single role group.
services.push(build_server_role_service(cluster));
for role_group_configs in cluster.role_group_configs.values() {
for (role_group_name, role_group) in role_group_configs {
config_maps.push(
build_rolegroup_config_map(cluster, role_group_name, role_group).context(
ConfigMapSnafu {
role_group: role_group_name.clone(),
},
)?,
);
services.push(build_rolegroup_headless_service(cluster, role_group_name));
services.push(build_rolegroup_metrics_service(cluster, role_group_name));
daemon_sets.push(
build_server_rolegroup_daemonset(
cluster,
role_group_name,
role_group,
opa_bundle_builder_image,
user_info_fetcher_image,
service_account_name,
cluster_info,
)
.context(DaemonSetSnafu {
role_group: role_group_name.clone(),
})?,
);
}
}
// The cluster-level discovery ConfigMap.
config_maps.push(build_discovery_config_map(cluster, cluster_info).context(DiscoverySnafu)?);
Ok(KubernetesResources {
daemon_sets,
services,
config_maps,
})
}
/// The port the bundle-builder sidecar listens on, and which OPA connects to for bundle polling.
pub(crate) const BUNDLE_BUILDER_PORT: Port = Port(3030);
// Placeholder role-group name for the recommended labels of the role-level `Service`, which is not
// bound to a single role group. `global` matches the historical `app.kubernetes.io/role-group`
// value.
stackable_operator::constant!(pub(crate) PLACEHOLDER_ROLE_LEVEL_ROLE_GROUP: RoleGroupName = "global");
// Placeholder role-group name for the recommended labels of the discovery `ConfigMap`, which is a
// cluster-level object not bound to a single role group.
stackable_operator::constant!(pub(crate) PLACEHOLDER_DISCOVERY_ROLE_GROUP: RoleGroupName = "discovery");
#[cfg(test)]
mod tests {
use serde_json::json;
use stackable_operator::{
commons::networking::DomainName, kube::Resource, utils::cluster_info::KubernetesClusterInfo,
};
use super::build;
use crate::controller::{
ValidatedCluster, build::properties::test_support::validated_cluster_from_spec,
};
fn cluster_info() -> KubernetesClusterInfo {
KubernetesClusterInfo {
cluster_domain: DomainName::try_from("cluster.local").unwrap(),
}
}
fn sorted_names(resources: &[impl Resource]) -> Vec<&str> {
let mut names: Vec<&str> = resources
.iter()
.filter_map(|resource| resource.meta().name.as_deref())
.collect();
names.sort();
names
}
/// A validated single-role-group cluster with no TLS or extra sidecars.
fn cluster() -> ValidatedCluster {
validated_cluster_from_spec(json!({
"image": { "productVersion": "1.2.3" },
"servers": { "roleGroups": { "default": {} } },
}))
}
#[test]
fn build_produces_expected_resource_names() {
let resources = build(
&cluster(),
"test-opa-serviceaccount",
"bundle-builder-image",
"user-info-fetcher-image",
&cluster_info(),
)
.expect("build succeeds");
// One DaemonSet per role group.
assert_eq!(
sorted_names(&resources.daemon_sets),
["test-opa-server-default"]
);
// The role-level Service plus a headless and a metrics Service per role group.
assert_eq!(
sorted_names(&resources.services),
[
"test-opa-server",
"test-opa-server-default-headless",
"test-opa-server-default-metrics",
]
);
// The per-role-group ConfigMap plus the cluster-level discovery ConfigMap (`test-opa`).
assert_eq!(
sorted_names(&resources.config_maps),
["test-opa", "test-opa-server-default"]
);
}
}