Skip to content

Commit 683334e

Browse files
committed
feat: Add conversion webhook
1 parent 48bc554 commit 683334e

9 files changed

Lines changed: 2271 additions & 126 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/druid-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
@@ -58,6 +58,7 @@ pub mod tls;
5858

5959
pub const APP_NAME: &str = "druid";
6060
pub const OPERATOR_NAME: &str = "druid.stackable.tech";
61+
pub const FIELD_MANAGER: &str = "druid-operator";
6162

6263
// config directories
6364
pub const DRUID_CONFIG_DIRECTORY: &str = "/stackable/config";

rust/operator-binary/src/main.rs

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

55
use std::sync::Arc;
66

7+
use anyhow::anyhow;
78
use clap::Parser;
89
use druid_controller::{DRUID_CONTROLLER_NAME, FULL_CONTROLLER_NAME};
9-
use futures::{FutureExt, StreamExt};
10+
use futures::{FutureExt, StreamExt, TryFutureExt};
1011
use stackable_operator::{
1112
YamlSchema,
1213
cli::{Command, RunArguments},
@@ -31,7 +32,10 @@ use stackable_operator::{
3132
utils::signal::SignalWatcher,
3233
};
3334

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

3640
mod authentication;
3741
mod config;
@@ -44,6 +48,7 @@ mod listener;
4448
mod operations;
4549
mod product_logging;
4650
mod service;
51+
mod webhooks;
4752

4853
mod built_info {
4954
include!(concat!(env!("OUT_DIR"), "/built.rs"));
@@ -63,7 +68,7 @@ async fn main() -> anyhow::Result<()> {
6368
Command::Crd => DruidCluster::merged_crd(DruidClusterVersion::V1Alpha1)?
6469
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?,
6570
Command::Run(RunArguments {
66-
operator_environment: _,
71+
operator_environment,
6772
watch_namespace,
6873
product_config,
6974
maintenance,
@@ -95,16 +100,28 @@ async fn main() -> anyhow::Result<()> {
95100
.run(sigterm_watcher.handle())
96101
.map(anyhow::Ok);
97102

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

109+
let webhook_server = create_webhook_server(
110+
&operator_environment,
111+
maintenance.disable_crd_maintenance,
112+
client.as_kube_client(),
113+
)
114+
.await?;
115+
116+
let webhook_server = webhook_server
117+
.run(sigterm_watcher.handle())
118+
.map_err(|err| anyhow!(err).context("failed to run webhook server"));
119+
120+
let product_config = product_config.load(&[
121+
"deploy/config-spec/properties.yaml",
122+
"/etc/stackable/druid-operator/config-spec/properties.yaml",
123+
])?;
124+
108125
let event_recorder = Arc::new(Recorder::new(
109126
client.as_kube_client(),
110127
Reporter {
@@ -170,7 +187,7 @@ async fn main() -> anyhow::Result<()> {
170187
)
171188
.map(anyhow::Ok);
172189

173-
futures::try_join!(druid_controller, eos_checker)?;
190+
futures::try_join!(druid_controller, eos_checker, webhook_server)?;
174191
}
175192
}
176193

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::{DruidCluster, DruidClusterVersion, 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+
DruidCluster::merged_crd(DruidClusterVersion::V1Alpha1).context(MergeCrdSnafu)?,
32+
DruidCluster::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)