Skip to content

Commit 1966ba8

Browse files
committed
feat: Add conversion webhook
1 parent afc3106 commit 1966ba8

9 files changed

Lines changed: 2272 additions & 127 deletions

File tree

Cargo.lock

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

Cargo.nix

Lines changed: 1712 additions & 78 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/hbase-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"] }

Tiltfile

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ custom_build(
1717
outputs_image_ref_to='result/ref',
1818
)
1919

20-
# Load the latest CRDs from Nix
21-
watch_file('result')
22-
if os.path.exists('result'):
23-
k8s_yaml('result/crds.yaml')
24-
2520
# We need to set the correct image annotation on the operator Deployment to use e.g.
2621
# oci.stackable.tech/sandbox/opa-operator:7y19m3d8clwxlv34v5q2x4p7v536s00g instead of
2722
# oci.stackable.tech/sandbox/opa-operator:0.0.0-dev (which does not exist)
@@ -35,18 +30,12 @@ helm_values = settings.get('helm_values', None)
3530

3631
helm_override_image_repository = 'image.repository=' + registry + '/' + operator_name
3732

38-
# Exclude stale CRDs from Helm chart, and apply the rest
39-
helm_crds, helm_non_crds = filter_yaml(
40-
helm(
41-
'deploy/helm/' + operator_name,
42-
name=operator_name,
43-
namespace="stackable-operators",
44-
set=[
45-
helm_override_image_repository,
46-
],
47-
values=helm_values,
48-
),
49-
api_version = "^apiextensions\\.k8s\\.io/.*$",
50-
kind = "^CustomResourceDefinition$",
51-
)
52-
k8s_yaml(helm_non_crds)
33+
k8s_yaml(helm(
34+
'deploy/helm/' + operator_name,
35+
name=operator_name,
36+
namespace="stackable-operators",
37+
set=[
38+
helm_override_image_repository,
39+
],
40+
values=helm_values,
41+
))

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
@@ -47,6 +47,7 @@ pub mod affinity;
4747
pub mod security;
4848

4949
pub const APP_NAME: &str = "hbase";
50+
pub const FIELD_MANAGER: &str = "hbase-operator";
5051

5152
// This constant is hard coded in hbase-entrypoint.sh
5253
// You need to change it there too.

rust/operator-binary/src/main.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use std::sync::Arc;
66

7+
use anyhow::anyhow;
78
use clap::Parser;
8-
use futures::{FutureExt, StreamExt};
9+
use futures::{FutureExt, StreamExt, TryFutureExt};
910
use hbase_controller::FULL_HBASE_CONTROLLER_NAME;
1011
use stackable_operator::{
1112
YamlSchema,
@@ -31,7 +32,10 @@ use stackable_operator::{
3132
utils::signal::SignalWatcher,
3233
};
3334

34-
use crate::crd::{HbaseCluster, HbaseClusterVersion, v1alpha1};
35+
use crate::{
36+
crd::{HbaseCluster, HbaseClusterVersion, v1alpha1},
37+
webhooks::conversion::create_webhook_server,
38+
};
3539

3640
mod config;
3741
mod crd;
@@ -41,6 +45,7 @@ mod kerberos;
4145
mod operations;
4246
mod product_logging;
4347
mod security;
48+
mod webhooks;
4449
mod zookeeper;
4550

4651
mod built_info {
@@ -65,9 +70,9 @@ async fn main() -> anyhow::Result<()> {
6570
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?;
6671
}
6772
Command::Run(RunArguments {
68-
product_config,
73+
operator_environment,
6974
watch_namespace,
70-
operator_environment: _,
75+
product_config,
7176
maintenance,
7277
common,
7378
}) => {
@@ -97,16 +102,28 @@ async fn main() -> anyhow::Result<()> {
97102
.run(sigterm_watcher.handle())
98103
.map(anyhow::Ok);
99104

100-
let product_config = product_config.load(&[
101-
"deploy/config-spec/properties.yaml",
102-
"/etc/stackable/hbase-operator/config-spec/properties.yaml",
103-
])?;
104105
let client = stackable_operator::client::initialize_operator(
105106
Some(OPERATOR_NAME.to_string()),
106107
&common.cluster_info,
107108
)
108109
.await?;
109110

111+
let webhook_server = create_webhook_server(
112+
&operator_environment,
113+
maintenance.disable_crd_maintenance,
114+
client.as_kube_client(),
115+
)
116+
.await?;
117+
118+
let webhook_server = webhook_server
119+
.run(sigterm_watcher.handle())
120+
.map_err(|err| anyhow!(err).context("failed to run webhook server"));
121+
122+
let product_config = product_config.load(&[
123+
"deploy/config-spec/properties.yaml",
124+
"/etc/stackable/hbase-operator/config-spec/properties.yaml",
125+
])?;
126+
110127
let event_recorder = Arc::new(Recorder::new(
111128
client.as_kube_client(),
112129
Reporter {
@@ -167,7 +184,7 @@ async fn main() -> anyhow::Result<()> {
167184
)
168185
.map(anyhow::Ok);
169186

170-
futures::try_join!(hbase_controller, eos_checker)?;
187+
futures::try_join!(hbase_controller, eos_checker, webhook_server)?;
171188
}
172189
}
173190

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, HbaseCluster, HbaseClusterVersion};
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+
HbaseCluster::merged_crd(HbaseClusterVersion::V1Alpha1).context(MergeCrdSnafu)?,
32+
HbaseCluster::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)