-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
201 lines (181 loc) · 6.56 KB
/
main.rs
File metadata and controls
201 lines (181 loc) · 6.56 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
use std::{str::FromStr, sync::Arc};
use clap::Parser as _;
use crd::{OpenSearchCluster, OpenSearchClusterVersion, v1alpha1};
use framework::types::operator::OperatorName;
use futures::{FutureExt, StreamExt};
use snafu::{ResultExt as _, Snafu};
use stackable_operator::{
YamlSchema as _,
cli::{Command, RunArguments},
crd::listener,
eos::EndOfSupportChecker,
k8s_openapi::api::{
apps::v1::StatefulSet,
core::v1::{ConfigMap, Service, ServiceAccount},
policy::v1::PodDisruptionBudget,
rbac::v1::RoleBinding,
},
kube::{
core::DeserializeGuard,
runtime::{
Controller,
events::{Recorder, Reporter},
watcher,
},
},
logging::controller::report_controller_reconciled,
shared::yaml::SerializeOptions,
telemetry::Tracing,
};
use strum::{EnumDiscriminants, IntoStaticStr};
mod controller;
mod crd;
mod framework;
mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[snafu(display("failed to initialize tracing subscribers"))]
InitTracing {
source: stackable_operator::telemetry::tracing::Error,
},
#[snafu(display("failed to initialize end-of-support checker"))]
InitEndOfSupportChecker {
source: stackable_operator::eos::Error,
},
#[snafu(display("failed to merge CRD versions"))]
MergeCrd {
source: stackable_operator::kube::core::crd::MergeError,
},
#[snafu(display("failed to serialize CRD"))]
SerializeCrd {
source: stackable_operator::shared::yaml::Error,
},
#[snafu(display("failed to create Kubernetes client"))]
CreateClient {
source: stackable_operator::client::Error,
},
}
#[derive(clap::Parser)]
#[clap(about, author)]
struct Opts {
#[clap(subcommand)]
cmd: Command,
}
#[tokio::main]
#[snafu::report]
async fn main() -> Result<()> {
let opts = Opts::parse();
match opts.cmd {
Command::Crd => {
OpenSearchCluster::merged_crd(OpenSearchClusterVersion::V1Alpha1)
.context(MergeCrdSnafu)?
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())
.context(SerializeCrdSnafu)?;
}
Command::Run(RunArguments {
operator_environment: _,
product_config: _,
watch_namespace,
maintenance,
common,
}) => {
let _tracing_guard = Tracing::pre_configured(built_info::PKG_NAME, common.telemetry)
.init()
.context(InitTracingSnafu)?;
tracing::info!(
built_info.pkg_version = built_info::PKG_VERSION,
built_info.git_version = built_info::GIT_VERSION,
built_info.target = built_info::TARGET,
built_info.built_time_utc = built_info::BUILT_TIME_UTC,
built_info.rustc_version = built_info::RUSTC_VERSION,
"Starting {description}",
description = built_info::PKG_DESCRIPTION
);
let eos_checker =
EndOfSupportChecker::new(built_info::BUILT_TIME_UTC, maintenance.end_of_support)
.context(InitEndOfSupportCheckerSnafu)?
.run()
.map(Ok);
let operator_name = OperatorName::from_str("opensearch.stackable.tech")
.expect("should be a valid operator name");
let client = stackable_operator::client::initialize_operator(
Some(format!("{operator_name}")),
&common.cluster_info,
)
.await
.context(CreateClientSnafu)?;
let controller_context = controller::Context::new(client.clone(), operator_name);
let full_controller_name = controller_context.full_controller_name();
let event_recorder = Arc::new(Recorder::new(
client.as_kube_client(),
Reporter {
controller: full_controller_name.clone(),
instance: None,
},
));
let controller = Controller::new(
watch_namespace.get_api::<DeserializeGuard<v1alpha1::OpenSearchCluster>>(&client),
watcher::Config::default(),
)
.owns(
watch_namespace.get_api::<DeserializeGuard<ConfigMap>>(&client),
watcher::Config::default(),
)
.owns(
watch_namespace.get_api::<DeserializeGuard<listener::v1alpha1::Listener>>(&client),
watcher::Config::default(),
)
.owns(
watch_namespace.get_api::<DeserializeGuard<RoleBinding>>(&client),
watcher::Config::default(),
)
.owns(
watch_namespace.get_api::<DeserializeGuard<PodDisruptionBudget>>(&client),
watcher::Config::default(),
)
.owns(
watch_namespace.get_api::<DeserializeGuard<Service>>(&client),
watcher::Config::default(),
)
.owns(
watch_namespace.get_api::<DeserializeGuard<ServiceAccount>>(&client),
watcher::Config::default(),
)
.owns(
watch_namespace.get_api::<DeserializeGuard<StatefulSet>>(&client),
watcher::Config::default(),
)
.shutdown_on_signal()
.run(
controller::reconcile,
controller::error_policy,
Arc::new(controller_context),
)
.for_each_concurrent(
16, // concurrency limit
|result| {
// The event_recorder needs to be shared across all invocations, so that
// events are correctly aggregated
let event_recorder = event_recorder.clone();
let full_controller_name = full_controller_name.clone();
async move {
report_controller_reconciled(
&event_recorder,
&full_controller_name,
&result,
)
.await;
}
},
)
.map(Ok);
futures::try_join!(controller, eos_checker)?;
}
}
Ok(())
}