|
| 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 | + controller::FIELD_MANAGER, |
| 13 | + crd::{OpenSearchCluster, OpenSearchClusterVersion}, |
| 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 | + OpenSearchCluster::merged_crd(OpenSearchClusterVersion::V1Alpha1).context(MergeCrdSnafu)?, |
| 35 | + OpenSearchCluster::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 | +} |
0 commit comments