Skip to content

Commit 61742f8

Browse files
committed
feat: Add conversion webhook
1 parent 23a674a commit 61742f8

8 files changed

Lines changed: 2277 additions & 132 deletions

File tree

Cargo.lock

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

Cargo.nix

Lines changed: 1719 additions & 85 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/nifi-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/main.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#![allow(clippy::result_large_err)]
44
use std::sync::Arc;
55

6+
use anyhow::anyhow;
67
use clap::Parser;
78
use crd::v1alpha1::NifiClusteringBackend;
8-
use futures::{FutureExt, StreamExt};
9+
use futures::{FutureExt, StreamExt, TryFutureExt};
910
use stackable_operator::{
1011
YamlSchema,
1112
cli::{Command, RunArguments},
@@ -34,6 +35,7 @@ use stackable_operator::{
3435
use crate::{
3536
controller::NIFI_FULL_CONTROLLER_NAME,
3637
crd::{NifiCluster, NifiClusterVersion, authorization::NifiOpaConfig, v1alpha1},
38+
webhooks::conversion::create_webhook_server,
3739
};
3840

3941
mod config;
@@ -45,12 +47,14 @@ mod product_logging;
4547
mod reporting_task;
4648
mod security;
4749
mod service;
50+
mod webhooks;
4851

4952
mod built_info {
5053
include!(concat!(env!("OUT_DIR"), "/built.rs"));
5154
}
5255

5356
const OPERATOR_NAME: &str = "nifi.stackable.tech";
57+
const FIELD_MANAGER: &str = "nifi-operator";
5458

5559
#[derive(Parser)]
5660
#[clap(about, author)]
@@ -66,7 +70,7 @@ async fn main() -> anyhow::Result<()> {
6670
Command::Crd => NifiCluster::merged_crd(NifiClusterVersion::V1Alpha1)?
6771
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?,
6872
Command::Run(RunArguments {
69-
operator_environment: _,
73+
operator_environment,
7074
watch_namespace,
7175
product_config,
7276
maintenance,
@@ -98,17 +102,28 @@ async fn main() -> anyhow::Result<()> {
98102
.run(sigterm_watcher.handle())
99103
.map(anyhow::Ok);
100104

101-
let product_config = product_config.load(&[
102-
"deploy/config-spec/properties.yaml",
103-
"/etc/stackable/nifi-operator/config-spec/properties.yaml",
104-
])?;
105-
106105
let client = stackable_operator::client::initialize_operator(
107106
Some(OPERATOR_NAME.to_string()),
108107
&common.cluster_info,
109108
)
110109
.await?;
111110

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/nifi-operator/config-spec/properties.yaml",
125+
])?;
126+
112127
let event_recorder = Arc::new(Recorder::new(
113128
client.as_kube_client(),
114129
Reporter {
@@ -188,7 +203,7 @@ async fn main() -> anyhow::Result<()> {
188203
)
189204
.map(anyhow::Ok);
190205

191-
futures::try_join!(nifi_controller, eos_checker)?;
206+
futures::try_join!(nifi_controller, eos_checker, webhook_server)?;
192207
}
193208
}
194209

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