Skip to content

Commit 3f29772

Browse files
committed
Add CLI flag to disable mutating webhook
1 parent 54480ad commit 3f29772

File tree

3 files changed

+81
-52
lines changed

3 files changed

+81
-52
lines changed

rust/operator-binary/src/main.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use stackable_operator::{
1717
shared::yaml::SerializeOptions,
1818
telemetry::Tracing,
1919
};
20-
use webhooks::create_webhook;
20+
use webhook::create_webhook;
2121

2222
mod restart_controller;
2323
mod utils;
24-
mod webhooks;
24+
mod webhook;
2525

2626
mod built_info {
2727
include!(concat!(env!("OUT_DIR"), "/built.rs"));
@@ -34,7 +34,20 @@ pub const FIELD_MANAGER: &str = "commons-operator";
3434
#[clap(about, author)]
3535
struct Opts {
3636
#[clap(subcommand)]
37-
cmd: Command,
37+
cmd: Command<CommonsOperatorRunArguments>,
38+
}
39+
40+
#[derive(Debug, PartialEq, Eq, Parser)]
41+
pub struct CommonsOperatorRunArguments {
42+
#[command(flatten)]
43+
pub common: RunArguments,
44+
45+
/// Don't start the controller mutating webhook and maintain the MutatingWebhookConfiguration.
46+
///
47+
/// The mutating webhook is used to prevent an unneeded restart of the first Pod of freshly
48+
/// created StatefulSets. It can be turned off in case you can accept an unneeded Pod restart.
49+
#[arg(long, env)]
50+
pub disable_restarter_mutating_webhook: bool,
3851
}
3952

4053
#[tokio::main]
@@ -49,12 +62,16 @@ async fn main() -> anyhow::Result<()> {
4962
S3Bucket::merged_crd(S3BucketVersion::V1Alpha1)?
5063
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?;
5164
}
52-
Command::Run(RunArguments {
53-
product_config: _,
54-
watch_namespace,
55-
operator_environment,
56-
maintenance,
57-
common,
65+
Command::Run(CommonsOperatorRunArguments {
66+
common:
67+
RunArguments {
68+
product_config: _,
69+
watch_namespace,
70+
operator_environment,
71+
maintenance,
72+
common,
73+
},
74+
disable_restarter_mutating_webhook,
5875
}) => {
5976
// NOTE (@NickLarsenNZ): Before stackable-telemetry was used:
6077
// - The console log level was set by `COMMONS_OPERATOR_LOG`, and is now `CONSOLE_LOG` (when using Tracing::pre_configured).
@@ -98,8 +115,7 @@ async fn main() -> anyhow::Result<()> {
98115
let webhook = create_webhook(
99116
ctx,
100117
&operator_environment,
101-
// TODO: Make user configurable
102-
false,
118+
disable_restarter_mutating_webhook,
103119
client.as_kube_client(),
104120
)
105121
.await?;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::sync::Arc;
2+
3+
use restarter_mutate_sts::{add_sts_restarter_annotation, get_mutating_webhook_configuration};
4+
use snafu::{ResultExt, Snafu};
5+
use stackable_operator::{
6+
cli::OperatorEnvironmentOptions,
7+
kube::Client,
8+
webhook::{
9+
WebhookError, WebhookOptions, WebhookServer,
10+
servers::{MutatingWebhookServer, WebhookServerImplementation},
11+
},
12+
};
13+
14+
use crate::{FIELD_MANAGER, restart_controller::statefulset::Ctx};
15+
16+
mod restarter_mutate_sts;
17+
18+
#[derive(Debug, Snafu)]
19+
pub enum Error {
20+
#[snafu(display("failed to create webhook server"))]
21+
CreateWebhookServer { source: WebhookError },
22+
}
23+
24+
pub async fn create_webhook<'a>(
25+
ctx: Arc<Ctx>,
26+
operator_environment: &'a OperatorEnvironmentOptions,
27+
disable_restarter_mutating_webhook: bool,
28+
client: Client,
29+
) -> Result<WebhookServer, Error> {
30+
let mut webhooks: Vec<Box<dyn WebhookServerImplementation>> = vec![];
31+
if !disable_restarter_mutating_webhook {
32+
webhooks.push(Box::new(MutatingWebhookServer::new(
33+
get_mutating_webhook_configuration(),
34+
add_sts_restarter_annotation,
35+
ctx,
36+
disable_restarter_mutating_webhook,
37+
client,
38+
FIELD_MANAGER.to_owned(),
39+
)));
40+
}
41+
42+
let webhook_options = WebhookOptions {
43+
socket_addr: WebhookServer::DEFAULT_SOCKET_ADDRESS,
44+
operator_namespace: operator_environment.operator_namespace.to_owned(),
45+
operator_service_name: operator_environment.operator_service_name.to_owned(),
46+
};
47+
WebhookServer::new(webhook_options, webhooks)
48+
.await
49+
.context(CreateWebhookServerSnafu)
50+
}

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

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use std::{collections::BTreeMap, sync::Arc};
22

33
use json_patch::{AddOperation, Patch, PatchOperation, jsonptr::PointerBuf};
4-
use snafu::{ResultExt, Snafu};
54
use stackable_operator::{
65
builder::meta::ObjectMetaBuilder,
7-
cli::OperatorEnvironmentOptions,
86
k8s_openapi::{
97
api::{
108
admissionregistration::v1::{
@@ -15,51 +13,16 @@ use stackable_operator::{
1513
},
1614
apimachinery::pkg::apis::meta::v1::LabelSelector,
1715
},
18-
kube::{
19-
Client,
20-
core::admission::{AdmissionRequest, AdmissionResponse},
21-
},
16+
kube::core::admission::{AdmissionRequest, AdmissionResponse},
2217
kvp::Label,
23-
webhook::{WebhookError, WebhookOptions, WebhookServer, servers::MutatingWebhookServer},
2418
};
2519

2620
use crate::{
27-
FIELD_MANAGER, OPERATOR_NAME,
21+
OPERATOR_NAME,
2822
restart_controller::statefulset::{Ctx, get_updated_restarter_annotations},
2923
};
3024

31-
#[derive(Debug, Snafu)]
32-
pub enum Error {
33-
#[snafu(display("failed to create webhook server"))]
34-
CreateWebhookServer { source: WebhookError },
35-
}
36-
37-
pub async fn create_webhook<'a>(
38-
ctx: Arc<Ctx>,
39-
operator_environment: &'a OperatorEnvironmentOptions,
40-
disable_mutating_webhook_configuration_maintenance: bool,
41-
client: Client,
42-
) -> Result<WebhookServer, Error> {
43-
let mutating_webhook_server = MutatingWebhookServer::new(
44-
get_mutating_webhook_configuration(),
45-
add_sts_restarter_annotation,
46-
ctx,
47-
disable_mutating_webhook_configuration_maintenance,
48-
client,
49-
FIELD_MANAGER.to_owned(),
50-
);
51-
52-
let webhook_options = WebhookOptions {
53-
socket_addr: WebhookServer::DEFAULT_SOCKET_ADDRESS,
54-
operator_namespace: operator_environment.operator_namespace.to_owned(),
55-
operator_service_name: operator_environment.operator_service_name.to_owned(),
56-
};
57-
WebhookServer::new(webhook_options, vec![Box::new(mutating_webhook_server)])
58-
.await
59-
.context(CreateWebhookServerSnafu)
60-
}
61-
62-
fn get_mutating_webhook_configuration() -> MutatingWebhookConfiguration {
25+
pub fn get_mutating_webhook_configuration() -> MutatingWebhookConfiguration {
6326
let webhook_name = "restarter-sts-enricher.stackable.tech";
6427
let metadata = ObjectMetaBuilder::new()
6528
.name(webhook_name)
@@ -107,7 +70,7 @@ fn get_mutating_webhook_configuration() -> MutatingWebhookConfiguration {
10770
}
10871
}
10972

110-
async fn add_sts_restarter_annotation(
73+
pub async fn add_sts_restarter_annotation(
11174
ctx: Arc<Ctx>,
11275
request: AdmissionRequest<StatefulSet>,
11376
) -> AdmissionResponse {

0 commit comments

Comments
 (0)