Skip to content

Commit b465907

Browse files
committed
feat: Add conversion webhook
1 parent 4969f9b commit b465907

9 files changed

Lines changed: 2242 additions & 97 deletions

File tree

Cargo.lock

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

Cargo.nix

Lines changed: 1690 additions & 56 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/hdfs-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/constants.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub const DEFAULT_DFS_REPLICATION_FACTOR: u8 = 3;
55
pub const FIELD_MANAGER_SCOPE: &str = "hdfscluster";
66

77
pub const APP_NAME: &str = "hdfs";
8+
pub const FIELD_MANAGER: &str = "hdfs-operator";
89

910
pub const HDFS_SITE_XML: &str = "hdfs-site.xml";
1011
pub const CORE_SITE_XML: &str = "core-site.xml";

rust/operator-binary/src/main.rs

Lines changed: 24 additions & 7 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 hdfs_controller::HDFS_FULL_CONTROLLER_NAME;
1011
use stackable_operator::{
1112
YamlSchema,
@@ -35,7 +36,10 @@ use stackable_operator::{
3536
use tracing::info_span;
3637
use tracing_futures::Instrument;
3738

38-
use crate::crd::{HdfsCluster, HdfsClusterVersion, constants::APP_NAME, v1alpha1};
39+
use crate::{
40+
crd::{HdfsCluster, HdfsClusterVersion, constants::APP_NAME, v1alpha1},
41+
webhooks::conversion::create_webhook_server,
42+
};
3943

4044
mod config;
4145
mod container;
@@ -48,6 +52,7 @@ mod operations;
4852
mod product_logging;
4953
mod security;
5054
mod service;
55+
mod webhooks;
5156

5257
mod built_info {
5358
include!(concat!(env!("OUT_DIR"), "/built.rs"));
@@ -69,7 +74,7 @@ async fn main() -> anyhow::Result<()> {
6974
Command::Crd => HdfsCluster::merged_crd(HdfsClusterVersion::V1Alpha1)?
7075
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?,
7176
Command::Run(RunArguments {
72-
operator_environment: _,
77+
operator_environment,
7378
watch_namespace,
7479
product_config,
7580
maintenance,
@@ -101,13 +106,25 @@ async fn main() -> anyhow::Result<()> {
101106
.run(sigterm_watcher.handle())
102107
.map(anyhow::Ok);
103108

109+
let client =
110+
client::initialize_operator(Some(OPERATOR_NAME.to_string()), &common.cluster_info)
111+
.await?;
112+
113+
let webhook_server = create_webhook_server(
114+
&operator_environment,
115+
maintenance.disable_crd_maintenance,
116+
client.as_kube_client(),
117+
)
118+
.await?;
119+
120+
let webhook_server = webhook_server
121+
.run(sigterm_watcher.handle())
122+
.map_err(|err| anyhow!(err).context("failed to run webhook server"));
123+
104124
let product_config = product_config.load(&[
105125
"deploy/config-spec/properties.yaml",
106126
"/etc/stackable/hdfs-operator/config-spec/properties.yaml",
107127
])?;
108-
let client =
109-
client::initialize_operator(Some(OPERATOR_NAME.to_string()), &common.cluster_info)
110-
.await?;
111128

112129
let (store, store_w) = reflector::store();
113130

@@ -199,7 +216,7 @@ async fn main() -> anyhow::Result<()> {
199216
.map(anyhow::Ok);
200217

201218
// kube-runtime's Controller will tokio::spawn each reconciliation, so this only concerns the internal watch machinery
202-
futures::try_join!(hdfs_controller, reflector, eos_checker)?;
219+
futures::try_join!(hdfs_controller, reflector, eos_checker, webhook_server)?;
203220
}
204221
};
205222

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::{HdfsCluster, HdfsClusterVersion, constants::FIELD_MANAGER};
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+
HdfsCluster::merged_crd(HdfsClusterVersion::V1Alpha1).context(MergeCrdSnafu)?,
32+
HdfsCluster::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)