-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrole_builder.rs
More file actions
196 lines (174 loc) · 6.14 KB
/
Copy pathrole_builder.rs
File metadata and controls
196 lines (174 loc) · 6.14 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use stackable_operator::{
builder::meta::ObjectMetaBuilder,
k8s_openapi::{
Resource,
api::{
core::v1::{Service, ServiceAccount, ServicePort, ServiceSpec},
policy::v1::PodDisruptionBudget,
rbac::v1::{ClusterRole, RoleBinding, RoleRef, Subject},
},
},
kube::api::ObjectMeta,
kvp::{
Label, Labels,
consts::{STACKABLE_VENDOR_KEY, STACKABLE_VENDOR_VALUE},
},
};
use super::role_group_builder::{
HTTP_PORT, HTTP_PORT_NAME, RoleGroupBuilder, TRANSPORT_PORT, TRANSPORT_PORT_NAME,
};
use crate::{
controller::{ContextNames, ValidatedCluster},
framework::{
IsLabelValue,
builder::{
meta::ownerreference_from_resource, pdb::pod_disruption_budget_builder_with_role,
},
role_utils::ResourceNames,
},
};
const PDB_DEFAULT_MAX_UNAVAILABLE: u16 = 1;
pub struct RoleBuilder<'a> {
cluster: ValidatedCluster,
context_names: &'a ContextNames,
resource_names: ResourceNames,
}
impl<'a> RoleBuilder<'a> {
pub fn new(cluster: ValidatedCluster, context_names: &'a ContextNames) -> RoleBuilder<'a> {
RoleBuilder {
cluster: cluster.clone(),
context_names,
resource_names: ResourceNames {
cluster_name: cluster.name.clone(),
product_name: context_names.product_name.clone(),
},
}
}
pub fn role_group_builders(&self) -> Vec<RoleGroupBuilder<'_>> {
self.cluster
.role_group_configs
.iter()
.map(|(role_group_name, role_group_config)| {
RoleGroupBuilder::new(
self.resource_names.service_account_name(),
self.cluster.clone(),
role_group_name.clone(),
role_group_config.clone(),
self.context_names,
self.resource_names.discovery_service_name(),
)
})
.collect()
}
pub fn build_service_account(&self) -> ServiceAccount {
let metadata = self.common_metadata(self.resource_names.service_account_name());
ServiceAccount {
metadata,
..ServiceAccount::default()
}
}
pub fn build_role_binding(&self) -> RoleBinding {
let metadata = self.common_metadata(self.resource_names.role_binding_name());
RoleBinding {
metadata,
role_ref: RoleRef {
api_group: ClusterRole::GROUP.to_owned(),
kind: ClusterRole::KIND.to_owned(),
name: self.resource_names.cluster_role_name(),
},
subjects: Some(vec![Subject {
api_group: Some(ServiceAccount::GROUP.to_owned()),
kind: ServiceAccount::KIND.to_owned(),
name: self.resource_names.service_account_name(),
namespace: Some(self.cluster.namespace.clone()),
}]),
}
}
pub fn build_cluster_manager_service(&self) -> Service {
let ports = vec![
ServicePort {
name: Some(HTTP_PORT_NAME.to_owned()),
port: HTTP_PORT.into(),
..ServicePort::default()
},
ServicePort {
name: Some(TRANSPORT_PORT_NAME.to_owned()),
port: TRANSPORT_PORT.into(),
..ServicePort::default()
},
];
let metadata = self.common_metadata(self.resource_names.discovery_service_name());
let service_selector =
RoleGroupBuilder::cluster_manager_labels(&self.cluster, self.context_names);
let service_spec = ServiceSpec {
// Internal communication does not need to be exposed
type_: Some("ClusterIP".to_string()),
cluster_ip: Some("None".to_string()),
ports: Some(ports),
selector: Some(service_selector.into()),
publish_not_ready_addresses: Some(true),
..ServiceSpec::default()
};
Service {
metadata,
spec: Some(service_spec),
status: None,
}
}
pub fn build_pdb(&self) -> Option<PodDisruptionBudget> {
let pdb_config = &self.cluster.role_config.pod_disruption_budget;
if pdb_config.enabled {
let max_unavailable = pdb_config
.max_unavailable
.unwrap_or(PDB_DEFAULT_MAX_UNAVAILABLE);
Some(
pod_disruption_budget_builder_with_role(
&self.cluster,
&self.context_names.product_name,
&ValidatedCluster::role_name(),
&self.context_names.operator_name,
&self.context_names.controller_name,
)
.with_max_unavailable(max_unavailable)
.build(),
)
} else {
None
}
}
fn common_metadata(&self, resource_name: impl Into<String>) -> ObjectMeta {
ObjectMetaBuilder::new()
.name(resource_name)
.namespace(&self.cluster.namespace)
.ownerreference(ownerreference_from_resource(
&self.cluster,
None,
Some(true),
))
.with_labels(self.labels())
.build()
}
/// Labels on role resources
fn labels(&self) -> Labels {
// Well-known Kubernetes labels
let mut labels = Labels::role_selector(
&self.cluster,
&self.context_names.product_name.to_label_value(),
&ValidatedCluster::role_name().to_label_value(),
)
.unwrap();
let managed_by = Label::managed_by(
&self.context_names.operator_name.to_string(),
&self.context_names.controller_name.to_string(),
)
.unwrap();
let version = Label::version(&self.cluster.product_version.to_string()).unwrap();
labels.insert(managed_by);
labels.insert(version);
// Stackable-specific labels
labels
.parse_insert((STACKABLE_VENDOR_KEY, STACKABLE_VENDOR_VALUE))
.unwrap();
labels
}
}