-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
259 lines (236 loc) · 9.95 KB
/
Copy pathmod.rs
File metadata and controls
259 lines (236 loc) · 9.95 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::{slice, str::FromStr};
use serde::{Deserialize, Serialize};
use stackable_operator::{
commons::{
affinity::{StackableAffinity, StackableAffinityFragment, affinity_between_role_pods},
cluster_operation::ClusterOperation,
product_image_selection::ProductImage,
resources::{
CpuLimitsFragment, MemoryLimitsFragment, NoRuntimeLimitsFragment, PvcConfig,
PvcConfigFragment, Resources, ResourcesFragment,
},
},
config::{
fragment::Fragment,
merge::{Atomic, Merge},
},
k8s_openapi::{api::core::v1::PodAntiAffinity, apimachinery::pkg::api::resource::Quantity},
kube::CustomResource,
role_utils::{GenericRoleConfig, Role},
schemars::{self, JsonSchema},
status::condition::{ClusterCondition, HasStatusCondition},
time::Duration,
versioned::versioned,
};
use strum::Display;
use crate::framework::{
ClusterName, IsLabelValue, ProductName, RoleName,
role_utils::GenericProductSpecificCommonConfig,
};
const DEFAULT_LISTENER_CLASS: &str = "cluster-internal";
#[versioned(version(name = "v1alpha1"))]
pub mod versioned {
/// A OpenSearch cluster stacklet. This resource is managed by the Stackable operator for OpenSearch.
/// Find more information on how to use it and the resources that the operator generates in the
/// [operator documentation](DOCS_BASE_URL_PLACEHOLDER/opensearch/).
#[derive(Clone, CustomResource, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[versioned(k8s(
group = "opensearch.stackable.tech",
kind = "OpenSearchCluster",
plural = "opensearchclusters",
shortname = "opensearch",
status = "v1alpha1::OpenSearchClusterStatus",
namespaced,
crates(
kube_core = "stackable_operator::kube::core",
k8s_openapi = "stackable_operator::k8s_openapi",
schemars = "stackable_operator::schemars"
)
))]
#[serde(rename_all = "camelCase")]
pub struct OpenSearchClusterSpec {
// no doc string - see ProductImage struct
pub image: ProductImage,
// no doc string - see ClusterOperation struct
#[serde(default)]
pub cluster_operation: ClusterOperation,
/// OpenSearch nodes
pub nodes:
Role<OpenSearchConfigFragment, GenericRoleConfig, GenericProductSpecificCommonConfig>,
}
// The possible node roles are by default the built-in roles and the search role, see
// https://github.com/opensearch-project/OpenSearch/blob/3.0.0/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java#L609-L614.
//
// Plugins can set additional roles, see
// https://github.com/opensearch-project/OpenSearch/blob/3.0.0/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java#L629-L646.
//
// For instance, the ml-commons plugin adds the node role "ml", see
// https://github.com/opensearch-project/ml-commons/blob/3.0.0.0/plugin/src/main/java/org/opensearch/ml/plugin/MachineLearningPlugin.java#L394.
// If such a plugin is added, then this enumeration must be extended accordingly.
#[derive(
Clone, Debug, Deserialize, Display, Eq, JsonSchema, Ord, PartialEq, PartialOrd, Serialize,
)]
// The OpenSearch configuration uses snake_case. To make it easier to match the log output of
// OpenSearch with this cluster configuration, snake_case is also used here.
#[serde(rename_all = "snake_case")]
pub enum NodeRole {
// Built-in node roles
// see https://github.com/opensearch-project/OpenSearch/blob/3.0.0/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java#L341-L346
// TODO https://github.com/Peternator7/strum/issues/113
#[strum(serialize = "cluster_manager")]
ClusterManager,
#[strum(serialize = "coordinating_only")]
CoordinatingOnly,
#[strum(serialize = "data")]
Data,
#[strum(serialize = "ingest")]
Ingest,
#[strum(serialize = "remote_cluster_client")]
RemoteClusterClient,
#[strum(serialize = "warm")]
Warm,
// Search node role
// see https://github.com/opensearch-project/OpenSearch/blob/3.0.0/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java#L313-L339
#[strum(serialize = "search")]
Search,
}
#[derive(Clone, Debug, Fragment, JsonSchema, PartialEq)]
#[fragment_attrs(
derive(
Clone,
Debug,
Default,
Deserialize,
Merge,
JsonSchema,
PartialEq,
Serialize
),
serde(rename_all = "camelCase")
)]
pub struct OpenSearchConfig {
#[fragment_attrs(serde(default))]
pub affinity: StackableAffinity,
/// Time period Pods have to gracefully shut down, e.g. `30m`, `1h` or `2d`. Consult the
/// operator documentation for details.
#[fragment_attrs(serde(default))]
pub graceful_shutdown_timeout: Duration,
pub node_roles: NodeRoles,
#[fragment_attrs(serde(default))]
pub resources: Resources<StorageConfig>,
/// This field controls which [ListenerClass](https://docs.stackable.tech/home/nightly/listener-operator/listenerclass.html) is used to expose the HTTP communication.
#[fragment_attrs(serde(default))]
pub listener_class: String,
}
#[derive(Clone, Debug, Default, JsonSchema, PartialEq, Fragment)]
#[fragment_attrs(
derive(
Clone,
Debug,
Default,
Deserialize,
Merge,
JsonSchema,
PartialEq,
Serialize
),
serde(rename_all = "camelCase")
)]
pub struct StorageConfig {
#[fragment_attrs(serde(default))]
pub data: PvcConfig,
}
#[derive(Clone, Default, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OpenSearchClusterStatus {
/// An opaque value that changes every time a discovery detail does
#[serde(default, skip_serializing_if = "Option::is_none")]
pub discovery_hash: Option<String>,
#[serde(default)]
pub conditions: Vec<ClusterCondition>,
}
}
impl HasStatusCondition for v1alpha1::OpenSearchCluster {
fn conditions(&self) -> Vec<ClusterCondition> {
match &self.status {
Some(status) => status.conditions.clone(),
None => vec![],
}
}
}
impl v1alpha1::OpenSearchConfig {
pub fn default_config(
product_name: &ProductName,
cluster_name: &ClusterName,
role_name: &RoleName,
) -> v1alpha1::OpenSearchConfigFragment {
v1alpha1::OpenSearchConfigFragment {
affinity: StackableAffinityFragment {
pod_affinity: None,
pod_anti_affinity: Some(PodAntiAffinity {
preferred_during_scheduling_ignored_during_execution: Some(vec![
affinity_between_role_pods(
&product_name.to_label_value(),
&cluster_name.to_label_value(),
&role_name.to_label_value(),
1,
),
]),
required_during_scheduling_ignored_during_execution: None,
}),
node_affinity: None,
node_selector: None,
},
// Default taken from the Helm chart, see
// https://github.com/opensearch-project/helm-charts/blob/opensearch-3.0.0/charts/opensearch/values.yaml#L364
graceful_shutdown_timeout: Some(
Duration::from_str("2m").expect("should be a valid duration"),
),
// Defaults taken from the Helm chart, see
// https://github.com/opensearch-project/helm-charts/blob/opensearch-3.0.0/charts/opensearch/values.yaml#L16-L20
node_roles: Some(NodeRoles(vec![
v1alpha1::NodeRole::ClusterManager,
v1alpha1::NodeRole::Ingest,
v1alpha1::NodeRole::Data,
v1alpha1::NodeRole::RemoteClusterClient,
])),
resources: ResourcesFragment {
memory: MemoryLimitsFragment {
// An idle node already requires 2 Gi.
limit: Some(Quantity("2Gi".to_owned())),
runtime_limits: NoRuntimeLimitsFragment {},
},
cpu: CpuLimitsFragment {
// Default taken from the Helm chart, see
// https://github.com/opensearch-project/helm-charts/blob/opensearch-3.0.0/charts/opensearch/values.yaml#L150
min: Some(Quantity("1".to_owned())),
// an arbitrary value
max: Some(Quantity("4".to_owned())),
},
storage: v1alpha1::StorageConfigFragment {
data: PvcConfigFragment {
// Default taken from the Helm chart, see
// https://github.com/opensearch-project/helm-charts/blob/opensearch-3.0.0/charts/opensearch/values.yaml#L220
// This value should be overriden by the user. Data nodes need probably
// more, the other nodes less.
capacity: Some(Quantity("8Gi".to_owned())),
storage_class: None,
selectors: None,
},
},
},
listener_class: Some(DEFAULT_LISTENER_CLASS.to_string()),
}
}
}
#[derive(Clone, Debug, Default, Deserialize, JsonSchema, PartialEq, Serialize)]
pub struct NodeRoles(Vec<v1alpha1::NodeRole>);
impl NodeRoles {
pub fn contains(&self, node_role: &v1alpha1::NodeRole) -> bool {
self.0.contains(node_role)
}
pub fn iter(&self) -> slice::Iter<v1alpha1::NodeRole> {
self.0.iter()
}
}
impl Atomic for NodeRoles {}