Skip to content

Commit 713ca9c

Browse files
Techassiadwk67labrenbesbernauersiegfriedweber
committed
chore: Import framework code from opensearch-operator
This commit imports the framework code from the opensearch-operator by first moving over the relevant files (located at rust/operator-binary/ src/framework) commit-by-commit, then squashing them in a interactive rebase. This commit message includes a list of the individual commit titles. It also includes all authore and co-authors of the original commits as co-authors. List of commits: - feat: Add listener support (opensearch-operator#17) - feat: Make OPENSEARCH_HOME and OPENSEARCH_PATH_CONF overridable (opensearch-operator#18) - Fix technical debts (opensearch-operator#20) - test: Add unit tests (opensearch-operator#32) - feat: Improve name types (opensearch-operator#35) - chore: Upgrade dependencies (opensearch-operator#37) - feat: Support log configuration and log aggregation (opensearch-operator#40) - feat: Support regular expressions in attributed_string_type (opensearch-operator#88) - feat: TLS support (opensearch-operator#55) - feat: Support objectOverrides (opensearch-operator#93) - chore: Use anchors in regular expressions (opensearch-operator#102) - chore: Move controller utility functions to separate module (opensearch-operator#104) - chore: Add Port type (opensearch-operator#105) - feat: Service discovery and exposition (opensearch-operator#94) - chore: Bump OpenSearch version 3.4.0 (opensearch-operator#108) - chore: Bump to stackable-operator 0.106.1 (opensearch-operator#116) - feat: Allow the configuration of the security plugin (opensearch-operator#117) - feat: Support hot-reloading for security configuration files (opensearch-operator#130) - chore: Upgrade stackable-operator to 0.110.1 (opensearch-operator#137) Co-authored-by: Andrew Kenworthy <1712947+adwk67@users.noreply.github.com> Co-authored-by: Benedikt Labrenz <benedikt@labrenz.org> Co-authored-by: Sebastian Bernauer <sebastian.bernauer@stackable.tech> Co-authored-by: Siegfried Weber <mail@siegfriedweber.net> Co-authored-by: Malte Sander <malte.sander.it@gmail.com>
1 parent 7a5f0c3 commit 713ca9c

23 files changed

Lines changed: 4111 additions & 0 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod meta;
2+
pub mod pdb;
3+
pub mod pod;
4+
pub mod statefulset;
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use stackable_operator::{
2+
builder::meta::OwnerReferenceBuilder,
3+
k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference, kube::Resource,
4+
};
5+
6+
use crate::framework::{HasName, HasUid};
7+
8+
/// Infallible variant of
9+
/// [`stackable_operator::builder::meta::ObjectMetaBuilder::ownerreference_from_resource`]
10+
pub fn ownerreference_from_resource(
11+
resource: &(impl Resource<DynamicType = ()> + HasName + HasUid),
12+
block_owner_deletion: Option<bool>,
13+
controller: Option<bool>,
14+
) -> OwnerReference {
15+
OwnerReferenceBuilder::new()
16+
// Set api_version, kind, name and additionally the UID if it exists.
17+
.initialize_from_resource(resource)
18+
// Ensure that the name is set.
19+
.name(resource.to_name())
20+
// Ensure that the UID is set.
21+
.uid(resource.to_uid().to_string())
22+
.block_owner_deletion_opt(block_owner_deletion)
23+
.controller_opt(controller)
24+
.build()
25+
.expect(
26+
"OwnerReference should be created because the resource has an api_version, kind, name \
27+
and uid.",
28+
)
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use std::borrow::Cow;
34+
35+
use stackable_operator::{
36+
k8s_openapi::apimachinery::pkg::apis::meta::v1::{ObjectMeta, OwnerReference},
37+
kube::Resource,
38+
};
39+
40+
use crate::framework::{HasName, HasUid, Uid, builder::meta::ownerreference_from_resource};
41+
42+
struct Cluster {
43+
object_meta: ObjectMeta,
44+
}
45+
46+
impl Cluster {
47+
fn new() -> Self {
48+
Cluster {
49+
object_meta: ObjectMeta {
50+
name: Some("cluster-name".to_owned()),
51+
uid: Some("a6b89911-d48e-4328-88d6-b9251226583d".to_owned()),
52+
..ObjectMeta::default()
53+
},
54+
}
55+
}
56+
}
57+
58+
impl Resource for Cluster {
59+
type DynamicType = ();
60+
type Scope = ();
61+
62+
fn kind(_dt: &Self::DynamicType) -> Cow<'_, str> {
63+
Cow::from("kind")
64+
}
65+
66+
fn group(_dt: &Self::DynamicType) -> Cow<'_, str> {
67+
Cow::from("group")
68+
}
69+
70+
fn version(_dt: &Self::DynamicType) -> Cow<'_, str> {
71+
Cow::from("version")
72+
}
73+
74+
fn plural(_dt: &Self::DynamicType) -> Cow<'_, str> {
75+
Cow::from("plural")
76+
}
77+
78+
fn meta(&self) -> &ObjectMeta {
79+
&self.object_meta
80+
}
81+
82+
fn meta_mut(&mut self) -> &mut ObjectMeta {
83+
&mut self.object_meta
84+
}
85+
}
86+
87+
impl HasName for Cluster {
88+
fn to_name(&self) -> String {
89+
self.object_meta
90+
.name
91+
.clone()
92+
.expect("should be set in Cluster::new")
93+
}
94+
}
95+
96+
impl HasUid for Cluster {
97+
fn to_uid(&self) -> Uid {
98+
Uid::from_str_unsafe(
99+
&self
100+
.object_meta
101+
.uid
102+
.clone()
103+
.expect("should be set in Cluster::new"),
104+
)
105+
}
106+
}
107+
108+
#[test]
109+
fn test_ownerreference_from_resource() {
110+
let actual_owner_reference =
111+
ownerreference_from_resource(&Cluster::new(), Some(true), Some(true));
112+
113+
let expected_owner_reference = OwnerReference {
114+
api_version: "group/version".to_owned(),
115+
block_owner_deletion: Some(true),
116+
controller: Some(true),
117+
kind: "kind".to_owned(),
118+
name: "cluster-name".to_owned(),
119+
uid: "a6b89911-d48e-4328-88d6-b9251226583d".to_owned(),
120+
};
121+
122+
assert_eq!(expected_owner_reference, actual_owner_reference);
123+
}
124+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
use stackable_operator::{
2+
builder::pdb::PodDisruptionBudgetBuilder,
3+
k8s_openapi::apimachinery::pkg::apis::meta::v1::LabelSelector,
4+
kube::{Resource, api::ObjectMeta},
5+
};
6+
7+
use crate::framework::{
8+
HasName, HasUid, NameIsValidLabelValue,
9+
types::operator::{ControllerName, OperatorName, ProductName, RoleName},
10+
};
11+
12+
/// Infallible variant of
13+
/// [`stackable_operator::builder::pdb::PodDisruptionBudgetBuilder::new_with_role`]
14+
pub fn pod_disruption_budget_builder_with_role(
15+
owner: &(impl Resource<DynamicType = ()> + HasName + NameIsValidLabelValue + HasUid),
16+
product_name: &ProductName,
17+
role_name: &RoleName,
18+
operator_name: &OperatorName,
19+
controller_name: &ControllerName,
20+
) -> PodDisruptionBudgetBuilder<ObjectMeta, LabelSelector, ()> {
21+
PodDisruptionBudgetBuilder::new_with_role(
22+
owner,
23+
&product_name.to_label_value(),
24+
&role_name.to_label_value(),
25+
&operator_name.to_label_value(),
26+
&controller_name.to_label_value(),
27+
)
28+
.expect(
29+
"PodDisruptionBudgetBuilder should be created because the owner has an object name and UID \
30+
and all given parameters produce valid label values.",
31+
)
32+
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use std::borrow::Cow;
37+
38+
use stackable_operator::{
39+
k8s_openapi::{
40+
api::policy::v1::{PodDisruptionBudget, PodDisruptionBudgetSpec},
41+
apimachinery::pkg::{
42+
apis::meta::v1::{LabelSelector, ObjectMeta, OwnerReference},
43+
util::intstr::IntOrString,
44+
},
45+
},
46+
kube::Resource,
47+
};
48+
49+
use crate::framework::{
50+
HasName, HasUid, NameIsValidLabelValue,
51+
builder::pdb::pod_disruption_budget_builder_with_role,
52+
types::{
53+
kubernetes::Uid,
54+
operator::{ControllerName, OperatorName, ProductName, RoleName},
55+
},
56+
};
57+
58+
struct Cluster {
59+
object_meta: ObjectMeta,
60+
}
61+
62+
impl Cluster {
63+
fn new() -> Self {
64+
Cluster {
65+
object_meta: ObjectMeta {
66+
name: Some("cluster-name".to_owned()),
67+
uid: Some("a6b89911-d48e-4328-88d6-b9251226583d".to_owned()),
68+
..ObjectMeta::default()
69+
},
70+
}
71+
}
72+
}
73+
74+
impl Resource for Cluster {
75+
type DynamicType = ();
76+
type Scope = ();
77+
78+
fn kind(_dt: &Self::DynamicType) -> Cow<'_, str> {
79+
Cow::from("kind")
80+
}
81+
82+
fn group(_dt: &Self::DynamicType) -> Cow<'_, str> {
83+
Cow::from("group")
84+
}
85+
86+
fn version(_dt: &Self::DynamicType) -> Cow<'_, str> {
87+
Cow::from("version")
88+
}
89+
90+
fn plural(_dt: &Self::DynamicType) -> Cow<'_, str> {
91+
Cow::from("plural")
92+
}
93+
94+
fn meta(&self) -> &ObjectMeta {
95+
&self.object_meta
96+
}
97+
98+
fn meta_mut(&mut self) -> &mut ObjectMeta {
99+
&mut self.object_meta
100+
}
101+
}
102+
103+
impl HasName for Cluster {
104+
fn to_name(&self) -> String {
105+
self.object_meta
106+
.name
107+
.clone()
108+
.expect("should be set in Cluster::new")
109+
}
110+
}
111+
112+
impl HasUid for Cluster {
113+
fn to_uid(&self) -> Uid {
114+
Uid::from_str_unsafe(
115+
&self
116+
.object_meta
117+
.uid
118+
.clone()
119+
.expect("should be set in Cluster::new"),
120+
)
121+
}
122+
}
123+
124+
impl NameIsValidLabelValue for Cluster {
125+
fn to_label_value(&self) -> String {
126+
self.object_meta
127+
.name
128+
.clone()
129+
.expect("should be set in Cluster::new")
130+
}
131+
}
132+
133+
#[test]
134+
fn test_pod_disruption_budget_builder_with_role() {
135+
let actual_pdb = pod_disruption_budget_builder_with_role(
136+
&Cluster::new(),
137+
&ProductName::from_str_unsafe("my-product"),
138+
&RoleName::from_str_unsafe("my-role"),
139+
&OperatorName::from_str_unsafe("my-operator"),
140+
&ControllerName::from_str_unsafe("my-controller"),
141+
)
142+
.with_max_unavailable(2)
143+
.build();
144+
145+
let expected_pdb = PodDisruptionBudget {
146+
metadata: ObjectMeta {
147+
labels: Some(
148+
[
149+
("app.kubernetes.io/component", "my-role"),
150+
("app.kubernetes.io/instance", "cluster-name"),
151+
("app.kubernetes.io/managed-by", "my-operator_my-controller"),
152+
("app.kubernetes.io/name", "my-product"),
153+
]
154+
.map(|(k, v)| (k.to_owned(), v.to_owned()))
155+
.into(),
156+
),
157+
name: Some("cluster-name-my-role".to_owned()),
158+
owner_references: Some(vec![OwnerReference {
159+
api_version: "group/version".to_owned(),
160+
controller: Some(true),
161+
kind: "kind".to_owned(),
162+
name: "cluster-name".to_owned(),
163+
uid: "a6b89911-d48e-4328-88d6-b9251226583d".to_owned(),
164+
..OwnerReference::default()
165+
}]),
166+
..ObjectMeta::default()
167+
},
168+
spec: Some(PodDisruptionBudgetSpec {
169+
max_unavailable: Some(IntOrString::Int(2)),
170+
selector: Some(LabelSelector {
171+
match_labels: Some(
172+
[
173+
("app.kubernetes.io/component", "my-role"),
174+
("app.kubernetes.io/instance", "cluster-name"),
175+
("app.kubernetes.io/name", "my-product"),
176+
]
177+
.map(|(k, v)| (k.to_owned(), v.to_owned()))
178+
.into(),
179+
),
180+
..LabelSelector::default()
181+
}),
182+
..PodDisruptionBudgetSpec::default()
183+
}),
184+
..PodDisruptionBudget::default()
185+
};
186+
187+
assert_eq!(expected_pdb, actual_pdb);
188+
}
189+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod container;
2+
pub mod volume;

0 commit comments

Comments
 (0)