-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.rs
More file actions
146 lines (134 loc) · 5.53 KB
/
Copy pathmain.rs
File metadata and controls
146 lines (134 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// TODO: Look into how to properly resolve `clippy::result_large_err`.
// This will need changes in our and upstream error types.
#![allow(clippy::result_large_err)]
use std::{os::unix::prelude::FileTypeExt, path::PathBuf, pin::pin};
use anyhow::Context;
use clap::Parser;
use csi_server::{
controller::SecretProvisionerController, identity::SecretProvisionerIdentity,
node::SecretProvisionerNode,
};
use futures::{FutureExt, TryStreamExt};
use grpc::csi::v1::{
controller_server::ControllerServer, identity_server::IdentityServer, node_server::NodeServer,
};
use stackable_operator::{CustomResourceExt, cli::ProductOperatorRun, telemetry::Tracing};
use tokio::signal::unix::{SignalKind, signal};
use tokio_stream::wrappers::UnixListenerStream;
use tonic::transport::Server;
use utils::{TonicUnixStream, uds_bind_private};
mod backend;
mod crd;
mod csi_server;
mod external_crd;
mod format;
mod grpc;
mod truststore_controller;
mod utils;
pub const OPERATOR_NAME: &str = "secrets.stackable.tech";
#[derive(clap::Parser)]
#[clap(author, version)]
struct Opts {
#[clap(subcommand)]
cmd: stackable_operator::cli::Command<SecretOperatorRun>,
}
#[derive(clap::Parser)]
struct SecretOperatorRun {
#[clap(long, env)]
csi_endpoint: PathBuf,
/// Unprivileged mode disables any features that require running secret-operator in a privileged container.
///
/// Currently, this means that:
/// - Secret volumes will be stored on disk, rather than in a ramdisk
///
/// Unprivileged mode is EXPERIMENTAL and heavily discouraged, since it increases the risk of leaking secrets.
#[clap(long, env)]
privileged: bool,
#[clap(flatten)]
common: ProductOperatorRun,
}
mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let opts = Opts::parse();
match opts.cmd {
stackable_operator::cli::Command::Crd => {
crd::SecretClass::print_yaml_schema(built_info::PKG_VERSION)?;
crd::TrustStore::print_yaml_schema(built_info::PKG_VERSION)?;
}
stackable_operator::cli::Command::Run(SecretOperatorRun {
csi_endpoint,
privileged,
common:
ProductOperatorRun {
product_config: _,
watch_namespace,
operator_environment: _,
telemetry,
cluster_info,
},
}) => {
// NOTE (@NickLarsenNZ): Before stackable-telemetry was used:
// - The console log level was set by `SECRET_PROVISIONER_LOG`, and is now `CONSOLE_LOG` (when using Tracing::pre_configured).
// - The file log level was set by `SECRET_PROVISIONER_LOG`, and is now set via `FILE_LOG` (when using Tracing::pre_configured).
// - The file log directory was set by `SECRET_PROVISIONER_LOG_DIRECTORY`, and is now set by `ROLLING_LOGS_DIR` (or via `--rolling-logs <DIRECTORY>`).
let _tracing_guard = Tracing::pre_configured(built_info::PKG_NAME, telemetry).init()?;
tracing::info!(
built_info.pkg_version = built_info::PKG_VERSION,
built_info.git_version = built_info::GIT_VERSION,
built_info.target = built_info::TARGET,
built_info.built_time_utc = built_info::BUILT_TIME_UTC,
built_info.rustc_version = built_info::RUSTC_VERSION,
"Starting {description}",
description = built_info::PKG_DESCRIPTION
);
let client = stackable_operator::client::initialize_operator(
Some(OPERATOR_NAME.to_string()),
&cluster_info,
)
.await?;
if csi_endpoint
.symlink_metadata()
.is_ok_and(|meta| meta.file_type().is_socket())
{
let _ = std::fs::remove_file(&csi_endpoint);
}
let mut sigterm = signal(SignalKind::terminate())?;
let csi_server = pin!(
Server::builder()
.add_service(
tonic_reflection::server::Builder::configure()
.include_reflection_service(true)
.register_encoded_file_descriptor_set(grpc::FILE_DESCRIPTOR_SET_BYTES)
.build_v1()?,
)
.add_service(IdentityServer::new(SecretProvisionerIdentity))
.add_service(ControllerServer::new(SecretProvisionerController {
client: client.clone(),
}))
.add_service(NodeServer::new(SecretProvisionerNode {
client: client.clone(),
node_name: cluster_info.kubernetes_node_name.to_owned(),
privileged,
}))
.serve_with_incoming_shutdown(
UnixListenerStream::new(
uds_bind_private(csi_endpoint)
.context("failed to bind CSI listener")?,
)
.map_ok(TonicUnixStream),
sigterm.recv().map(|_| ()),
)
);
let truststore_controller =
pin!(truststore_controller::start(&client, &watch_namespace).map(Ok));
futures::future::select(csi_server, truststore_controller)
.await
.factor_first()
.0?;
}
}
Ok(())
}