Skip to content

Commit 282130b

Browse files
committed
feat: Add conversion webhook
1 parent ad5fb52 commit 282130b

8 files changed

Lines changed: 2278 additions & 124 deletions

File tree

Cargo.lock

Lines changed: 466 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.nix

Lines changed: 1721 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repository = "https://github.com/stackabletech/hive-operator"
1111

1212
[workspace.dependencies]
1313
product-config = { git = "https://github.com/stackabletech/product-config.git", tag = "0.8.0" }
14-
stackable-operator = { git = "https://github.com/stackabletech/operator-rs.git", tag = "stackable-operator-0.105.0", features = ["telemetry", "versioned"] }
14+
stackable-operator = { git = "https://github.com/stackabletech/operator-rs.git", tag = "stackable-operator-0.105.0", features = ["telemetry", "versioned", "webhook"] }
1515

1616
anyhow = "1.0"
1717
built = { version = "0.8", features = ["chrono", "git2"] }

crate-hashes.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/operator-binary/src/crd/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::{crd::affinity::get_affinity, listener::metastore_default_listener_cl
3939
pub mod affinity;
4040
pub mod security;
4141

42+
pub const FIELD_MANAGER: &str = "hive-operator";
4243
pub const APP_NAME: &str = "hive";
4344

4445
// Directories

rust/operator-binary/src/main.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@
22
// This will need changes in our and upstream error types.
33
#![allow(clippy::result_large_err)]
44

5-
mod command;
6-
mod config;
7-
mod controller;
8-
mod crd;
9-
mod discovery;
10-
mod kerberos;
11-
mod listener;
12-
mod operations;
13-
mod product_logging;
14-
mod service;
15-
165
use std::sync::Arc;
176

7+
use anyhow::anyhow;
188
use clap::Parser;
19-
use futures::{FutureExt, StreamExt};
9+
use futures::{FutureExt, StreamExt, TryFutureExt};
2010
use stackable_operator::{
2111
YamlSchema,
2212
cli::{Command, RunArguments},
@@ -44,8 +34,21 @@ use stackable_operator::{
4434
use crate::{
4535
controller::HIVE_FULL_CONTROLLER_NAME,
4636
crd::{HiveCluster, HiveClusterVersion, v1alpha1},
37+
webhooks::conversion::create_webhook_server,
4738
};
4839

40+
mod command;
41+
mod config;
42+
mod controller;
43+
mod crd;
44+
mod discovery;
45+
mod kerberos;
46+
mod listener;
47+
mod operations;
48+
mod product_logging;
49+
mod service;
50+
mod webhooks;
51+
4952
mod built_info {
5053
include!(concat!(env!("OUT_DIR"), "/built.rs"));
5154
}
@@ -66,7 +69,7 @@ async fn main() -> anyhow::Result<()> {
6669
Command::Crd => HiveCluster::merged_crd(HiveClusterVersion::V1Alpha1)?
6770
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?,
6871
Command::Run(RunArguments {
69-
operator_environment: _,
72+
operator_environment,
7073
watch_namespace,
7174
product_config,
7275
maintenance,
@@ -98,16 +101,28 @@ async fn main() -> anyhow::Result<()> {
98101
.run(sigterm_watcher.handle())
99102
.map(anyhow::Ok);
100103

101-
let product_config = product_config.load(&[
102-
"deploy/config-spec/properties.yaml",
103-
"/etc/stackable/hive-operator/config-spec/properties.yaml",
104-
])?;
105-
106104
let client = stackable_operator::client::initialize_operator(
107105
Some(OPERATOR_NAME.to_string()),
108106
&common.cluster_info,
109107
)
110108
.await?;
109+
110+
let webhook_server = create_webhook_server(
111+
&operator_environment,
112+
maintenance.disable_crd_maintenance,
113+
client.as_kube_client(),
114+
)
115+
.await?;
116+
117+
let webhook_server = webhook_server
118+
.run(sigterm_watcher.handle())
119+
.map_err(|err| anyhow!(err).context("failed to run webhook server"));
120+
121+
let product_config = product_config.load(&[
122+
"deploy/config-spec/properties.yaml",
123+
"/etc/stackable/hive-operator/config-spec/properties.yaml",
124+
])?;
125+
111126
let event_recorder = Arc::new(Recorder::new(
112127
client.as_kube_client(),
113128
Reporter {
@@ -173,7 +188,7 @@ async fn main() -> anyhow::Result<()> {
173188
)
174189
.map(anyhow::Ok);
175190

176-
futures::try_join!(hive_controller, eos_checker)?;
191+
futures::try_join!(hive_controller, eos_checker, webhook_server)?;
177192
}
178193
}
179194

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use snafu::{ResultExt, Snafu};
2+
use stackable_operator::{
3+
cli::OperatorEnvironmentOptions,
4+
kube::{Client, core::crd::MergeError},
5+
webhook::{
6+
WebhookServer, WebhookServerError, WebhookServerOptions,
7+
webhooks::{ConversionWebhook, ConversionWebhookOptions},
8+
},
9+
};
10+
11+
use crate::crd::{FIELD_MANAGER, HiveCluster, HiveClusterVersion};
12+
13+
/// Contains errors which can be encountered when creating the conversion webhook server and the
14+
/// CRD maintainer.
15+
#[derive(Debug, Snafu)]
16+
pub enum Error {
17+
#[snafu(display("failed to merge CRD"))]
18+
MergeCrd { source: MergeError },
19+
20+
#[snafu(display("failed to create conversion webhook server"))]
21+
CreateWebhook { source: WebhookServerError },
22+
}
23+
24+
/// Creates and returns a [`WebhookServer`].
25+
pub async fn create_webhook_server(
26+
operator_environment: &OperatorEnvironmentOptions,
27+
disable_crd_maintenance: bool,
28+
client: Client,
29+
) -> Result<WebhookServer, Error> {
30+
let crds_and_handlers = vec![(
31+
HiveCluster::merged_crd(HiveClusterVersion::V1Alpha1).context(MergeCrdSnafu)?,
32+
HiveCluster::try_convert,
33+
)];
34+
35+
let conversion_webhook_options = ConversionWebhookOptions {
36+
disable_crd_maintenance,
37+
field_manager: FIELD_MANAGER.to_owned(),
38+
};
39+
40+
let (conversion_webhook, _initial_reconcile_rx) =
41+
ConversionWebhook::new(crds_and_handlers, client, conversion_webhook_options);
42+
43+
let webhook_server_options = WebhookServerOptions {
44+
socket_addr: WebhookServer::DEFAULT_SOCKET_ADDRESS,
45+
webhook_namespace: operator_environment.operator_namespace.to_owned(),
46+
webhook_service_name: operator_environment.operator_service_name.to_owned(),
47+
};
48+
49+
WebhookServer::new(vec![Box::new(conversion_webhook)], webhook_server_options)
50+
.await
51+
.context(CreateWebhookSnafu)
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod conversion;

0 commit comments

Comments
 (0)