-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpdb.rs
More file actions
162 lines (147 loc) · 4.32 KB
/
pdb.rs
File metadata and controls
162 lines (147 loc) · 4.32 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
use std::cmp::{max, min};
use snafu::{ResultExt, Snafu};
use stackable_operator::{
builder::pdb::PodDisruptionBudgetBuilder,
client::Client,
cluster_resources::ClusterResources,
commons::pdb::PdbConfig,
kube::ResourceExt,
kvp::{LabelError, LabelExt, Labels},
};
use crate::{
OPERATOR_NAME,
crd::{HdfsNodeRole, constants::APP_NAME, v1alpha1},
hdfs_controller::RESOURCE_MANAGER_HDFS_CONTROLLER,
labels::add_stackable_labels,
};
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("cannot create PodDisruptionBudget for role {role:?}"))]
CreatePdb {
source: stackable_operator::builder::pdb::Error,
role: String,
},
#[snafu(display("cannot apply role group PodDisruptionBudget {name:?}"))]
ApplyPdb {
source: stackable_operator::cluster_resources::Error,
name: String,
},
#[snafu(display("failed to build stackable label"))]
BuildStackableLabel { source: LabelError },
}
pub async fn add_pdbs(
pdb: &PdbConfig,
hdfs: &v1alpha1::HdfsCluster,
role: &HdfsNodeRole,
client: &Client,
cluster_resources: &mut ClusterResources<'_>,
) -> Result<(), Error> {
if !pdb.enabled {
return Ok(());
}
let max_unavailable = pdb.max_unavailable.unwrap_or(match role {
HdfsNodeRole::Name => max_unavailable_name_nodes(),
HdfsNodeRole::Data => max_unavailable_data_nodes(
hdfs.num_datanodes(),
hdfs.spec.cluster_config.dfs_replication as u16,
),
HdfsNodeRole::Journal => max_unavailable_journal_nodes(),
});
let mut pdb = PodDisruptionBudgetBuilder::new_with_role(
hdfs,
APP_NAME,
&role.to_string(),
OPERATOR_NAME,
RESOURCE_MANAGER_HDFS_CONTROLLER,
)
.with_context(|_| CreatePdbSnafu {
role: role.to_string(),
})?
.with_max_unavailable(max_unavailable)
.build();
pdb.add_labels(
add_stackable_labels(Labels::new(), hdfs.metadata.labels.clone())
.context(BuildStackableLabelSnafu)?,
);
let pdb_name = pdb.name_any();
cluster_resources
.add(client, pdb)
.await
.with_context(|_| ApplyPdbSnafu { name: pdb_name })?;
Ok(())
}
fn max_unavailable_name_nodes() -> u16 {
1
}
fn max_unavailable_journal_nodes() -> u16 {
1
}
fn max_unavailable_data_nodes(num_datanodes: u16, dfs_replication: u16) -> u16 {
// There must always be a datanode to serve the block.
// If we would simply subtract one from the `dfs_replication`, we would end up
// with a single point of failure, so we subtract two instead.
let max_unavailable = dfs_replication.saturating_sub(2);
// We need to make sure at least one datanode remains by having at most
// n - 1 datanodes unavailable. We subtract two to avoid a single point of failure.
let max_unavailable = min(max_unavailable, num_datanodes.saturating_sub(2));
// Clamp to at least a single node allowed to be offline, so we don't block Kubernetes nodes from draining.
max(max_unavailable, 1)
}
#[cfg(test)]
mod test {
use rstest::rstest;
use super::*;
#[rstest]
#[case(0, 0, 1)]
#[case(0, 1, 1)]
#[case(0, 2, 1)]
#[case(0, 3, 1)]
#[case(0, 4, 1)]
#[case(0, 5, 1)]
#[case(1, 0, 1)]
#[case(1, 1, 1)]
#[case(1, 2, 1)]
#[case(1, 3, 1)]
#[case(1, 4, 1)]
#[case(1, 5, 1)]
#[case(2, 0, 1)]
#[case(2, 1, 1)]
#[case(2, 2, 1)]
#[case(2, 3, 1)]
#[case(2, 4, 1)]
#[case(2, 5, 1)]
#[case(3, 0, 1)]
#[case(3, 1, 1)]
#[case(3, 2, 1)]
#[case(3, 3, 1)]
#[case(3, 4, 1)]
#[case(3, 5, 1)]
#[case(4, 0, 1)]
#[case(4, 1, 1)]
#[case(4, 2, 1)]
#[case(4, 3, 1)]
#[case(4, 4, 2)]
#[case(4, 5, 2)]
#[case(5, 0, 1)]
#[case(5, 1, 1)]
#[case(5, 2, 1)]
#[case(5, 3, 1)]
#[case(5, 4, 2)]
#[case(5, 5, 3)]
#[case(100, 0, 1)]
#[case(100, 1, 1)]
#[case(100, 2, 1)]
#[case(100, 3, 1)]
#[case(100, 4, 2)]
#[case(100, 5, 3)]
#[case(100, 10, 8)]
#[case(100, 100, 98)]
fn test_max_unavailable_data_nodes(
#[case] num_datanodes: u16,
#[case] dfs_replication: u16,
#[case] expected: u16,
) {
let max_unavailable = max_unavailable_data_nodes(num_datanodes, dfs_replication);
assert_eq!(max_unavailable, expected)
}
}