Skip to content

Commit 632ec96

Browse files
committed
feat: Add conversion webhook
1 parent 627d44c commit 632ec96

5 files changed

Lines changed: 97 additions & 48 deletions

File tree

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+
))

rust/operator-binary/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use stackable_operator::{
1818
telemetry::Tracing,
1919
utils::signal::SignalWatcher,
2020
};
21-
use webhook::create_webhook_server;
21+
use webhooks::create_webhook_server;
2222

2323
mod restart_controller;
2424
mod utils;
25-
mod webhook;
25+
mod webhooks;
2626

2727
mod built_info {
2828
include!(concat!(env!("OUT_DIR"), "/built.rs"));
@@ -124,6 +124,7 @@ async fn main() -> anyhow::Result<()> {
124124
ctx,
125125
&operator_environment,
126126
disable_restarter_mutating_webhook,
127+
maintenance.disable_crd_maintenance,
127128
client.as_kube_client(),
128129
)
129130
.await?;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use stackable_operator::{
2+
crd::{
3+
authentication::core::{AuthenticationClass, AuthenticationClassVersion},
4+
s3::{S3Bucket, S3BucketVersion, S3Connection, S3ConnectionVersion},
5+
},
6+
kube::Client,
7+
webhook::webhooks::{ConversionWebhook, ConversionWebhookOptions, Webhook},
8+
};
9+
10+
use crate::FIELD_MANAGER;
11+
12+
pub fn create_webhook(disable_crd_maintenance: bool, client: Client) -> Box<impl Webhook> {
13+
let crds_and_handlers = vec![
14+
(
15+
AuthenticationClass::merged_crd(AuthenticationClassVersion::V1Alpha1).unwrap(),
16+
AuthenticationClass::try_convert as fn(_) -> _,
17+
),
18+
(
19+
S3Connection::merged_crd(S3ConnectionVersion::V1Alpha1).unwrap(),
20+
S3Connection::try_convert as fn(_) -> _,
21+
),
22+
(
23+
S3Bucket::merged_crd(S3BucketVersion::V1Alpha1).unwrap(),
24+
S3Bucket::try_convert as fn(_) -> _,
25+
),
26+
];
27+
28+
let conversion_webhook_options = ConversionWebhookOptions {
29+
disable_crd_maintenance,
30+
field_manager: FIELD_MANAGER.to_owned(),
31+
};
32+
33+
let (conversion_webhook, _initial_reconcile_rx) =
34+
ConversionWebhook::new(crds_and_handlers, client, conversion_webhook_options);
35+
36+
Box::new(conversion_webhook)
37+
}
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
use std::sync::Arc;
22

3-
use restarter_mutate_sts::{
4-
add_sts_restarter_annotations_handler, get_sts_restarter_mutating_webhook_configuration,
5-
};
63
use snafu::{ResultExt, Snafu};
74
use stackable_operator::{
85
cli::OperatorEnvironmentOptions,
96
kube::Client,
10-
webhook::{
11-
WebhookServer, WebhookServerError, WebhookServerOptions,
12-
webhooks::{MutatingWebhook, MutatingWebhookOptions, Webhook},
13-
},
7+
webhook::{WebhookServer, WebhookServerError, WebhookServerOptions, webhooks::Webhook},
148
};
159

16-
use crate::{FIELD_MANAGER, restart_controller::statefulset::Ctx};
10+
use crate::restart_controller::statefulset::Ctx;
1711

12+
mod conversion;
1813
mod restarter_mutate_sts;
1914

2015
#[derive(Debug, Snafu)]
@@ -27,24 +22,23 @@ pub async fn create_webhook_server(
2722
ctx: Arc<Ctx>,
2823
operator_environment: &OperatorEnvironmentOptions,
2924
disable_restarter_mutating_webhook: bool,
25+
disable_crd_maintenance: bool,
3026
client: Client,
3127
) -> Result<WebhookServer, Error> {
3228
let mut webhooks: Vec<Box<dyn Webhook>> = vec![];
33-
if !disable_restarter_mutating_webhook {
34-
let mutating_webhook_options = MutatingWebhookOptions {
35-
disable_mwc_maintenance: disable_restarter_mutating_webhook,
36-
field_manager: FIELD_MANAGER.to_owned(),
37-
};
38-
39-
webhooks.push(Box::new(MutatingWebhook::new(
40-
get_sts_restarter_mutating_webhook_configuration(),
41-
add_sts_restarter_annotations_handler,
42-
ctx,
43-
client,
44-
mutating_webhook_options,
45-
)));
29+
30+
if let Some(webhook) = restarter_mutate_sts::create_webhook(
31+
ctx,
32+
disable_restarter_mutating_webhook,
33+
client.clone(),
34+
) {
35+
webhooks.push(webhook);
4636
}
4737

38+
// TODO (@Techassi): The conversion webhook should also allow to be disabled, rework the
39+
// granularity of these options.
40+
webhooks.push(conversion::create_webhook(disable_crd_maintenance, client));
41+
4842
let webhook_options = WebhookServerOptions {
4943
socket_addr: WebhookServer::DEFAULT_SOCKET_ADDRESS,
5044
webhook_namespace: operator_environment.operator_namespace.to_owned(),

rust/operator-binary/src/webhook/restarter_mutate_sts.rs renamed to rust/operator-binary/src/webhooks/restarter_mutate_sts.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::BTreeMap, sync::Arc};
1+
use std::{collections::BTreeMap, ops::Not, sync::Arc};
22

33
use json_patch::{AddOperation, Patch, PatchOperation, jsonptr::PointerBuf};
44
use stackable_operator::{
@@ -13,16 +13,44 @@ use stackable_operator::{
1313
},
1414
apimachinery::pkg::apis::meta::v1::LabelSelector,
1515
},
16-
kube::core::admission::{AdmissionRequest, AdmissionResponse},
16+
kube::{
17+
Client,
18+
core::admission::{AdmissionRequest, AdmissionResponse},
19+
},
1720
kvp::Label,
21+
webhook::webhooks::{MutatingWebhookOptions, Webhook},
1822
};
1923

2024
use crate::{
21-
OPERATOR_NAME,
25+
FIELD_MANAGER, OPERATOR_NAME,
2226
restart_controller::statefulset::{Ctx, get_updated_restarter_annotations},
2327
};
2428

25-
pub fn get_sts_restarter_mutating_webhook_configuration() -> MutatingWebhookConfiguration {
29+
pub fn create_webhook(
30+
ctx: Arc<Ctx>,
31+
disable_restarter_mutating_webhook: bool,
32+
client: Client,
33+
) -> Option<Box<impl Webhook>> {
34+
disable_restarter_mutating_webhook.not().then(|| {
35+
let mutating_webhook_options = MutatingWebhookOptions {
36+
// TODO (@Techassi): Make disabling webhooks and maintenance options more granular.
37+
// NOTE: Technically this can never not be "false", so we could hard-code it. I'll leave
38+
// it as is for now.
39+
disable_mwc_maintenance: disable_restarter_mutating_webhook,
40+
field_manager: FIELD_MANAGER.to_owned(),
41+
};
42+
43+
Box::new(stackable_operator::webhook::webhooks::MutatingWebhook::new(
44+
get_sts_restarter_mutating_webhook_configuration(),
45+
add_sts_restarter_annotations_handler,
46+
ctx,
47+
client,
48+
mutating_webhook_options,
49+
))
50+
})
51+
}
52+
53+
fn get_sts_restarter_mutating_webhook_configuration() -> MutatingWebhookConfiguration {
2654
let webhook_name = "restarter-sts-enricher.stackable.tech";
2755
let metadata = ObjectMetaBuilder::new()
2856
.name(webhook_name)
@@ -75,7 +103,7 @@ pub fn get_sts_restarter_mutating_webhook_configuration() -> MutatingWebhookConf
75103
}
76104
}
77105

78-
pub async fn add_sts_restarter_annotations_handler(
106+
async fn add_sts_restarter_annotations_handler(
79107
ctx: Arc<Ctx>,
80108
request: AdmissionRequest<StatefulSet>,
81109
) -> AdmissionResponse {

0 commit comments

Comments
 (0)