-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
1661 lines (1521 loc) · 61.8 KB
/
mod.rs
File metadata and controls
1661 lines (1521 loc) · 61.8 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::{BTreeMap, HashMap, HashSet};
use indoc::formatdoc;
use product_config::types::PropertyNameKind;
use security::add_cert_to_jvm_trust_store_cmd;
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{
builder::pod::volume::ListenerOperatorVolumeSourceBuilderError,
client::Client,
commons::{
affinity::StackableAffinity,
cluster_operation::ClusterOperation,
product_image_selection::ProductImage,
resources::{NoRuntimeLimits, Resources},
},
config::{
fragment::{self, Fragment, FromFragment, ValidationError},
merge::Merge,
},
crd::{authentication::core, s3},
deep_merger::ObjectOverrides,
k8s_openapi::api::core::v1::{PodTemplateSpec, Volume},
kube::{CustomResource, ResourceExt},
kvp::ObjectLabels,
memory::{BinaryMultiple, MemoryQuantity},
product_config_utils::{Configuration, Error as ConfigError},
product_logging::{
self,
framework::{create_vector_shutdown_file_command, remove_vector_shutdown_file_command},
spec::Logging,
},
role_utils::{CommonConfiguration, GenericRoleConfig, JavaCommonConfig, Role, RoleGroup},
schemars::{self, JsonSchema},
shared::time::Duration,
status::condition::{ClusterCondition, HasStatusCondition},
utils::{COMMON_BASH_TRAP_FUNCTIONS, crds::raw_object_list_schema},
versioned::versioned,
};
use strum::{Display, EnumDiscriminants, EnumIter, EnumString, IntoStaticStr};
use crate::crd::{
affinity::get_affinity,
authorization::DruidAuthorization,
resource::RoleResource,
tls::{DruidTls, default_druid_tls},
v1alpha1::DruidRoleConfig,
};
pub mod affinity;
pub mod authentication;
pub mod authorization;
pub mod memory;
pub mod resource;
pub mod security;
pub mod storage;
pub mod tls;
pub const APP_NAME: &str = "druid";
pub const OPERATOR_NAME: &str = "druid.stackable.tech";
pub const FIELD_MANAGER: &str = "druid-operator";
// config directories
pub const DRUID_CONFIG_DIRECTORY: &str = "/stackable/config";
pub const HDFS_CONFIG_DIRECTORY: &str = "/stackable/hdfs";
pub const LOG_CONFIG_DIRECTORY: &str = "/stackable/log_config";
pub const RW_CONFIG_DIRECTORY: &str = "/stackable/rwconfig";
// config file names
pub const JVM_CONFIG: &str = "jvm.config";
pub const RUNTIME_PROPS: &str = "runtime.properties";
pub const LOG4J2_CONFIG: &str = "log4j2.properties";
pub const JVM_SECURITY_PROPERTIES_FILE: &str = "security.properties";
// store directories
pub const STACKABLE_TRUST_STORE: &str = "/stackable/truststore.p12";
pub const STACKABLE_TRUST_STORE_PASSWORD: &str = "changeit";
pub const STACKABLE_LOG_DIR: &str = "/stackable/log";
// store file names
pub const DRUID_LOG_FILE: &str = "druid.log4j2.xml";
pub const PROP_SEGMENT_CACHE_LOCATIONS: &str = "druid.segmentCache.locations";
pub const PATH_SEGMENT_CACHE: &str = "/stackable/var/druid/segment-cache";
/////////////////////////////
// CONFIG PROPERTIES //
/////////////////////////////
// extensions
pub const EXTENSIONS_LOADLIST: &str = "druid.extensions.loadList";
// zookeeper
pub const ZOOKEEPER_CONNECTION_STRING: &str = "druid.zk.service.host";
// deep storage
pub const DS_TYPE: &str = "druid.storage.type";
pub const DS_DIRECTORY: &str = "druid.storage.storageDirectory";
// S3
pub const DS_BUCKET: &str = "druid.storage.bucket";
pub const DS_BASE_KEY: &str = "druid.storage.baseKey";
pub const S3_ENDPOINT_URL: &str = "druid.s3.endpoint.url";
pub const S3_ACCESS_KEY: &str = "druid.s3.accessKey";
pub const S3_SECRET_KEY: &str = "druid.s3.secretKey";
pub const S3_PATH_STYLE_ACCESS: &str = "druid.s3.enablePathStyleAccess";
// OPA
pub const AUTH_AUTHORIZERS: &str = "druid.auth.authorizers";
pub const AUTH_AUTHORIZERS_VALUE: &str = "[\"OpaAuthorizer\"]";
pub const AUTH_AUTHORIZER_OPA_TYPE: &str = "druid.auth.authorizer.OpaAuthorizer.type";
pub const AUTH_AUTHORIZER_OPA_TYPE_VALUE: &str = "opa";
pub const AUTH_AUTHORIZER_OPA_URI: &str = "druid.auth.authorizer.OpaAuthorizer.opaUri";
// metadata storage config properties
const METADATA_STORAGE_TYPE: &str = "druid.metadata.storage.type";
const METADATA_STORAGE_URI: &str = "druid.metadata.storage.connector.connectURI";
const METADATA_STORAGE_HOST: &str = "druid.metadata.storage.connector.host";
const METADATA_STORAGE_PORT: &str = "druid.metadata.storage.connector.port";
const METADATA_STORAGE_USER: &str = "druid.metadata.storage.connector.user";
const METADATA_STORAGE_PASSWORD: &str = "druid.metadata.storage.connector.password";
// indexer properties
pub const INDEXER_JAVA_OPTS: &str = "druid.indexer.runner.javaOptsArray";
// historical settings
pub const PROCESSING_BUFFER_SIZE_BYTES: &str = "druid.processing.buffer.sizeBytes";
pub const PROCESSING_NUM_MERGE_BUFFERS: &str = "druid.processing.numMergeBuffers";
pub const PROCESSING_NUM_THREADS: &str = "druid.processing.numThreads";
// extra
pub const CREDENTIALS_SECRET_PROPERTY: &str = "credentialsSecret";
// logs
pub const MAX_DRUID_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity {
value: 10.0,
unit: BinaryMultiple::Mebi,
};
// metrics
pub const PROMETHEUS_PORT: &str = "druid.emitter.prometheus.port";
pub const METRICS_PORT_NAME: &str = "metrics";
pub const METRICS_PORT: u16 = 9090;
pub const COOKIE_PASSPHRASE_ENV: &str = "OIDC_COOKIE_PASSPHRASE";
// DB credentials - both of these are read from an env var by Druid with the ${env:...} syntax
pub const DB_USERNAME_ENV: &str = "DB_USERNAME_ENV";
pub const DB_PASSWORD_ENV: &str = "DB_PASSWORD_ENV";
// Graceful shutdown timeouts
const DEFAULT_BROKER_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_minutes_unchecked(5);
const DEFAULT_COORDINATOR_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_minutes_unchecked(5);
const DEFAULT_MIDDLEMANAGER_GRACEFUL_SHUTDOWN_TIMEOUT: Duration =
Duration::from_minutes_unchecked(5);
const DEFAULT_ROUTER_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_minutes_unchecked(5);
const DEFAULT_HISTORICAL_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_minutes_unchecked(5);
// Auto TLS certificate lifetime
const DEFAULT_BROKER_SECRET_LIFETIME: Duration = Duration::from_days_unchecked(1);
const DEFAULT_COORDINATOR_SECRET_LIFETIME: Duration = Duration::from_days_unchecked(1);
const DEFAULT_MIDDLE_SECRET_LIFETIME: Duration = Duration::from_days_unchecked(1);
const DEFAULT_ROUTER_SECRET_LIFETIME: Duration = Duration::from_days_unchecked(1);
const DEFAULT_HISTORICAL_SECRET_LIFETIME: Duration = Duration::from_days_unchecked(1);
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[snafu(display("missing secret lifetime"))]
MissingSecretLifetime,
#[snafu(display("failed to resolve S3 connection"))]
ResolveS3Connection {
source: stackable_operator::crd::s3::v1alpha1::ConnectionError,
},
#[snafu(display("failed to resolve S3 bucket"))]
ResolveS3Bucket {
source: stackable_operator::crd::s3::v1alpha1::BucketError,
},
#[snafu(display("2 differing s3 connections were given, this is unsupported by Druid"))]
IncompatibleS3Connections,
#[snafu(display("the role group {rolegroup_name} is not defined"))]
CannotRetrieveRoleGroup { rolegroup_name: String },
#[snafu(display("missing namespace for resource {name}"))]
MissingNamespace { name: String },
#[snafu(display("fragment validation failure"))]
FragmentValidationFailure { source: ValidationError },
#[snafu(display("failed to build Labels"))]
LabelBuild {
source: stackable_operator::kvp::LabelError,
},
#[snafu(display("failed to build listener volume"))]
BuildListenerVolume {
source: ListenerOperatorVolumeSourceBuilderError,
},
#[snafu(display("failed to apply group listener"))]
ApplyGroupListener {
source: stackable_operator::cluster_resources::Error,
},
}
#[versioned(
version(name = "v1alpha1"),
crates(
kube_core = "stackable_operator::kube::core",
kube_client = "stackable_operator::kube::client",
k8s_openapi = "stackable_operator::k8s_openapi",
schemars = "stackable_operator::schemars",
versioned = "stackable_operator::versioned"
)
)]
pub mod versioned {
/// A Druid cluster stacklet. This resource is managed by the Stackable operator for Apache Druid.
/// Find more information on how to use it and the resources that the operator generates in the
/// [operator documentation](DOCS_BASE_URL_PLACEHOLDER/druid/).
#[versioned(crd(
group = "druid.stackable.tech",
kind = "DruidCluster",
plural = "druidclusters",
shortname = "druid",
status = "DruidClusterStatus",
namespaced
))]
#[derive(Clone, CustomResource, Debug, Deserialize, JsonSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DruidClusterSpec {
/// Common cluster wide configuration that can not differ or be overridden on a role or role group level.
pub cluster_config: v1alpha1::DruidClusterConfig,
// no doc - docs provided by the struct.
pub image: ProductImage,
// no doc - docs provided by the struct.
pub brokers: Role<BrokerConfigFragment, DruidRoleConfig, JavaCommonConfig>,
// no doc - docs provided by the struct.
pub coordinators: Role<CoordinatorConfigFragment, DruidRoleConfig, JavaCommonConfig>,
// no doc - docs provided by the struct.
pub historicals: Role<HistoricalConfigFragment, GenericRoleConfig, JavaCommonConfig>,
// no doc - docs provided by the struct.
pub middle_managers: Role<MiddleManagerConfigFragment, GenericRoleConfig, JavaCommonConfig>,
// no doc - docs provided by the struct.
pub routers: Role<RouterConfigFragment, DruidRoleConfig, JavaCommonConfig>,
// no doc - docs provided by the struct.
#[serde(default)]
pub cluster_operation: ClusterOperation,
// no doc - docs provided by the struct.
#[serde(default)]
pub object_overrides: ObjectOverrides,
}
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DruidClusterConfig {
/// Additional extensions to load in Druid.
/// The operator will automatically load all extensions needed based on the cluster
/// configuration, but for extra functionality which the operator cannot anticipate, it can
/// sometimes be necessary to load additional extensions.
/// Add configuration for additional extensions using [configuration override for Druid](DOCS_BASE_URL_PLACEHOLDER/druid/usage-guide/overrides).
#[serde(default)]
pub additional_extensions: HashSet<String>,
/// List of [AuthenticationClasses](DOCS_BASE_URL_PLACEHOLDER/concepts/authentication)
/// to use for authenticating users. TLS, LDAP and OIDC authentication are supported. More information in
/// the [Druid operator security documentation](DOCS_BASE_URL_PLACEHOLDER/druid/usage-guide/security#_authentication).
///
/// For TLS: Please note that the SecretClass used to authenticate users needs to be the same
/// as the SecretClass used for internal communication.
#[serde(default)]
pub authentication: Vec<core::v1alpha1::ClientAuthenticationDetails>,
/// Authorization settings for Druid like OPA
#[serde(skip_serializing_if = "Option::is_none")]
pub authorization: Option<DruidAuthorization>,
/// [Druid deep storage configuration](DOCS_BASE_URL_PLACEHOLDER/druid/usage-guide/deep-storage).
/// Only one backend can be used at a time. Either HDFS or S3 are supported.
pub deep_storage: DeepStorageSpec,
/// Configuration properties for data ingestion tasks.
#[serde(skip_serializing_if = "Option::is_none")]
pub ingestion: Option<IngestionSpec>,
/// Druid requires an SQL database to store metadata into. Specify connection information here.
pub metadata_storage_database: DatabaseConnectionSpec,
/// TLS encryption settings for Druid, more information in the
/// [security documentation](DOCS_BASE_URL_PLACEHOLDER/druid/usage-guide/security).
/// This setting only affects server and internal communication.
/// It does not affect client tls authentication, use `clusterConfig.authentication` instead.
#[serde(default = "default_druid_tls", skip_serializing_if = "Option::is_none")]
pub tls: Option<DruidTls>,
/// Druid requires a ZooKeeper cluster connection to run.
/// Provide the name of the ZooKeeper [discovery ConfigMap](DOCS_BASE_URL_PLACEHOLDER/concepts/service_discovery)
/// here. When using the [Stackable operator for Apache ZooKeeper](DOCS_BASE_URL_PLACEHOLDER/zookeeper/)
/// to deploy a ZooKeeper cluster, this will simply be the name of your ZookeeperCluster resource.
pub zookeeper_config_map_name: String,
/// Name of the Vector aggregator [discovery ConfigMap](DOCS_BASE_URL_PLACEHOLDER/concepts/service_discovery).
/// It must contain the key `ADDRESS` with the address of the Vector aggregator.
/// Follow the [logging tutorial](DOCS_BASE_URL_PLACEHOLDER/tutorials/logging-vector-aggregator)
/// to learn how to configure log aggregation with Vector.
#[serde(skip_serializing_if = "Option::is_none")]
pub vector_aggregator_config_map_name: Option<String>,
/// Extra volumes similar to `.spec.volumes` on a Pod to mount into every container, this can be useful to for
/// example make client certificates, keytabs or similar things available to processors. These volumes will be
/// mounted into all pods at `/stackable/userdata/{volumename}`.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[schemars(schema_with = "raw_object_list_schema")]
pub extra_volumes: Vec<Volume>,
}
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DruidRoleConfig {
#[serde(flatten)]
pub common: GenericRoleConfig,
#[serde(default = "druid_default_listener_class")]
pub listener_class: String,
}
}
// Required to retrieve the conditions from the cluster status
impl HasStatusCondition for v1alpha1::DruidCluster {
fn conditions(&self) -> Vec<ClusterCondition> {
match &self.status {
Some(status) => status.conditions.clone(),
None => vec![],
}
}
}
impl v1alpha1::DruidCluster {
pub fn common_compute_files(
&self,
file: &str,
) -> Result<BTreeMap<String, Option<String>>, ConfigError> {
let mut result = BTreeMap::new();
match file {
JVM_CONFIG => {}
RUNTIME_PROPS => {
let mds = &self.spec.cluster_config.metadata_storage_database;
result.insert(
METADATA_STORAGE_TYPE.to_string(),
Some(mds.db_type.to_string()),
);
result.insert(
METADATA_STORAGE_URI.to_string(),
Some(mds.conn_string.to_string()),
);
result.insert(
METADATA_STORAGE_HOST.to_string(),
Some(mds.host.to_string()),
);
result.insert(
METADATA_STORAGE_PORT.to_string(),
Some(mds.port.to_string()),
);
if mds.credentials_secret.is_some() {
result.insert(
METADATA_STORAGE_USER.to_string(),
Some(format!("${{env:{DB_USERNAME_ENV}}}")),
);
result.insert(
METADATA_STORAGE_PASSWORD.to_string(),
Some(format!("${{env:{DB_PASSWORD_ENV}}}")),
);
}
// OPA
if let Some(DruidAuthorization { opa: _ }) = &self.spec.cluster_config.authorization
{
result.insert(
AUTH_AUTHORIZERS.to_string(),
Some(AUTH_AUTHORIZERS_VALUE.to_string()),
);
result.insert(
AUTH_AUTHORIZER_OPA_TYPE.to_string(),
Some(AUTH_AUTHORIZER_OPA_TYPE_VALUE.to_string()),
);
// The opaUri still needs to be set, but that requires a discovery config map and is handled in the druid_controller.rs
}
// deep storage
result.insert(
DS_TYPE.to_string(),
Some(self.spec.cluster_config.deep_storage.to_string()),
);
match self.spec.cluster_config.deep_storage.clone() {
DeepStorageSpec::Hdfs(hdfs) => {
result.insert(DS_DIRECTORY.to_string(), Some(hdfs.directory));
}
DeepStorageSpec::S3(s3_spec) => {
if let Some(key) = &s3_spec.base_key {
result.insert(DS_BASE_KEY.to_string(), Some(key.to_string()));
}
// bucket information (name, connection) needs to be resolved first,
// that is done directly in the controller
}
}
// metrics
result.insert(PROMETHEUS_PORT.to_string(), Some(METRICS_PORT.to_string()));
}
_ => {}
}
Ok(result)
}
#[allow(clippy::type_complexity)]
pub fn build_role_properties(
&self,
) -> HashMap<
String,
(
Vec<PropertyNameKind>,
Role<
impl Configuration<Configurable = v1alpha1::DruidCluster>,
GenericRoleConfig,
JavaCommonConfig,
>,
),
> {
let config_files = vec![
PropertyNameKind::Env,
PropertyNameKind::File(JVM_CONFIG.to_string()),
PropertyNameKind::File(RUNTIME_PROPS.to_string()),
PropertyNameKind::File(JVM_SECURITY_PROPERTIES_FILE.to_string()),
];
vec![
(
DruidRole::Broker.to_string(),
(
config_files.clone(),
extract_role_from_role_config::<BrokerConfig>(self.spec.brokers.clone())
.erase(),
),
),
(
DruidRole::Coordinator.to_string(),
(
config_files.clone(),
extract_role_from_role_config::<CoordinatorConfig>(
self.spec.coordinators.clone(),
)
.erase(),
),
),
(
DruidRole::Historical.to_string(),
(config_files.clone(), self.spec.historicals.clone().erase()),
),
(
DruidRole::MiddleManager.to_string(),
(
config_files.clone(),
self.spec.middle_managers.clone().erase(),
),
),
(
DruidRole::Router.to_string(),
(
config_files,
extract_role_from_role_config::<RouterConfig>(self.spec.routers.clone())
.erase(),
),
),
]
.into_iter()
.collect()
}
/// If an s3 connection for ingestion is given, as well as an s3 connection for deep storage, they need to be the same.
/// This function returns the resolved connection, or raises an Error if the connections are not identical.
pub async fn get_s3_connection(
&self,
client: &Client,
) -> Result<Option<s3::v1alpha1::ConnectionSpec>, Error> {
// retrieve connection for ingestion (can be None)
let ingestion_conn = if let Some(ic) = self
.spec
.cluster_config
.ingestion
.as_ref()
.and_then(|is| is.s3connection.as_ref())
{
Some(
ic.clone()
.resolve(
client,
self.namespace()
.context(MissingNamespaceSnafu {
name: &self.name_unchecked(),
})?
.as_ref(),
)
.await
.context(ResolveS3ConnectionSnafu)?,
)
} else {
None
};
// retrieve connection for deep storage (can be None)
let storage_conn = match &self.spec.cluster_config.deep_storage {
DeepStorageSpec::S3(s3_spec) => {
let inlined_bucket = s3_spec
.bucket
.clone()
.resolve(
client,
self.namespace()
.context(MissingNamespaceSnafu {
name: &self.name_unchecked(),
})?
.as_ref(),
)
.await
.context(ResolveS3BucketSnafu)?;
Some(inlined_bucket.connection)
}
_ => None,
};
// if both connections are specified and are identical, return it
// if they differ, raise an error
// if only one connection is specified, return it
if ingestion_conn.is_some() && storage_conn.is_some() {
if ingestion_conn == storage_conn {
Ok(ingestion_conn)
} else {
Err(Error::IncompatibleS3Connections)
}
} else if ingestion_conn.is_some() {
Ok(ingestion_conn)
} else if storage_conn.is_some() {
Ok(storage_conn)
} else {
Ok(None)
}
}
/// Returns true if the cluster uses an s3 connection.
/// This is a quicker convenience function over the [v1alpha1::DruidCluster::get_s3_connection] function.
pub fn uses_s3(&self) -> bool {
let s3_ingestion = self
.spec
.cluster_config
.ingestion
.as_ref()
.and_then(|spec| spec.s3connection.as_ref())
.is_some();
let s3_storage = self.spec.cluster_config.deep_storage.is_s3();
s3_ingestion || s3_storage
}
/// Returns the merged and validated configuration for all roles
pub fn merged_config(&self) -> Result<MergedConfig, Error> {
let deep_storage = &self.spec.cluster_config.deep_storage;
Ok(MergedConfig {
brokers: v1alpha1::DruidCluster::merged_role(
&extract_role_from_role_config::<BrokerConfig>(self.spec.brokers.clone()),
&BrokerConfig::default_config(&self.name_any(), &DruidRole::Broker, deep_storage),
)?,
coordinators: v1alpha1::DruidCluster::merged_role(
&extract_role_from_role_config::<CoordinatorConfig>(self.spec.coordinators.clone()),
&CoordinatorConfig::default_config(
&self.name_any(),
&DruidRole::Coordinator,
deep_storage,
),
)?,
historicals: v1alpha1::DruidCluster::merged_role(
&self.spec.historicals,
&HistoricalConfig::default_config(
&self.name_any(),
&DruidRole::Historical,
deep_storage,
),
)?,
middle_managers: v1alpha1::DruidCluster::merged_role(
&self.spec.middle_managers,
&MiddleManagerConfig::default_config(
&self.name_any(),
&DruidRole::MiddleManager,
deep_storage,
),
)?,
routers: v1alpha1::DruidCluster::merged_role(
&extract_role_from_role_config::<RouterConfig>(self.spec.routers.clone()),
&RouterConfig::default_config(&self.name_any(), &DruidRole::Router, deep_storage),
)?,
})
}
/// Merges and validates the role groups of the given role with the given default configuration
fn merged_role<T>(
role: &Role<T::Fragment, GenericRoleConfig, JavaCommonConfig>,
default_config: &T::Fragment,
) -> Result<HashMap<String, RoleGroup<T, JavaCommonConfig>>, Error>
where
T: FromFragment,
T::Fragment: Clone + Merge,
{
let mut merged_role_config = HashMap::new();
for (rolegroup_name, rolegroup) in &role.role_groups {
let merged_rolegroup_config = v1alpha1::DruidCluster::merged_rolegroup(
rolegroup,
&role.config.config,
default_config,
)?;
merged_role_config.insert(rolegroup_name.to_owned(), merged_rolegroup_config);
}
Ok(merged_role_config)
}
/// Merges and validates the given role group with the given role and default configurations
fn merged_rolegroup<T>(
rolegroup: &RoleGroup<T::Fragment, JavaCommonConfig>,
role_config: &T::Fragment,
default_config: &T::Fragment,
) -> Result<RoleGroup<T, JavaCommonConfig>, Error>
where
T: FromFragment,
T::Fragment: Clone + Merge,
{
let merged_config = v1alpha1::DruidCluster::merged_rolegroup_config(
&rolegroup.config.config,
role_config,
default_config,
)?;
Ok(RoleGroup {
config: CommonConfiguration {
config: merged_config,
config_overrides: rolegroup.config.config_overrides.to_owned(),
env_overrides: rolegroup.config.env_overrides.to_owned(),
cli_overrides: rolegroup.config.cli_overrides.to_owned(),
pod_overrides: rolegroup.config.pod_overrides.to_owned(),
product_specific_common_config: rolegroup
.config
.product_specific_common_config
.to_owned(),
},
replicas: rolegroup.replicas,
})
}
pub fn generic_role_config(&self, role: &DruidRole) -> &GenericRoleConfig {
match role {
DruidRole::Broker => &self.spec.brokers.role_config.common,
DruidRole::Coordinator => &self.spec.coordinators.role_config.common,
DruidRole::Historical => &self.spec.historicals.role_config,
DruidRole::MiddleManager => &self.spec.middle_managers.role_config,
DruidRole::Router => &self.spec.routers.role_config.common,
}
}
/// Merges and validates the given role group, role, and default configurations
pub fn merged_rolegroup_config<T>(
rolegroup_config: &T::Fragment,
role_config: &T::Fragment,
default_config: &T::Fragment,
) -> Result<T, Error>
where
T: FromFragment,
T::Fragment: Clone + Merge,
{
let mut role_config = role_config.to_owned();
let mut rolegroup_config = rolegroup_config.to_owned();
role_config.merge(default_config);
rolegroup_config.merge(&role_config);
fragment::validate(rolegroup_config).context(FragmentValidationFailureSnafu)
}
pub fn get_role(
&self,
druid_role: &DruidRole,
) -> Role<
impl Configuration<Configurable = v1alpha1::DruidCluster>,
GenericRoleConfig,
JavaCommonConfig,
> {
match druid_role {
DruidRole::Broker => {
extract_role_from_role_config::<BrokerConfig>(self.spec.brokers.clone()).erase()
}
DruidRole::Coordinator => {
extract_role_from_role_config::<CoordinatorConfig>(self.spec.coordinators.clone())
.erase()
}
DruidRole::Historical => self.spec.historicals.clone().erase(),
DruidRole::MiddleManager => self.spec.middle_managers.clone().erase(),
DruidRole::Router => {
extract_role_from_role_config::<RouterConfig>(self.spec.routers.clone()).erase()
}
}
}
pub fn pod_overrides_for_role(&self, role: &DruidRole) -> &PodTemplateSpec {
match role {
DruidRole::Broker => &self.spec.brokers.config.pod_overrides,
DruidRole::Coordinator => &self.spec.coordinators.config.pod_overrides,
DruidRole::Historical => &self.spec.historicals.config.pod_overrides,
DruidRole::MiddleManager => &self.spec.middle_managers.config.pod_overrides,
DruidRole::Router => &self.spec.routers.config.pod_overrides,
}
}
pub fn pod_overrides_for_role_group(
&self,
role: &DruidRole,
role_group: &str,
) -> Option<&PodTemplateSpec> {
match role {
DruidRole::Broker => self
.spec
.brokers
.role_groups
.get(role_group)
.map(|rg| &rg.config.pod_overrides),
DruidRole::Coordinator => self
.spec
.coordinators
.role_groups
.get(role_group)
.map(|rg| &rg.config.pod_overrides),
DruidRole::Historical => self
.spec
.historicals
.role_groups
.get(role_group)
.map(|rg| &rg.config.pod_overrides),
DruidRole::MiddleManager => self
.spec
.middle_managers
.role_groups
.get(role_group)
.map(|rg| &rg.config.pod_overrides),
DruidRole::Router => self
.spec
.routers
.role_groups
.get(role_group)
.map(|rg| &rg.config.pod_overrides),
}
}
}
#[derive(
Clone,
Debug,
Deserialize,
Display,
Eq,
EnumIter,
JsonSchema,
Ord,
PartialEq,
PartialOrd,
Serialize,
)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum Container {
Druid,
Prepare,
Vector,
}
/// Common configuration for all role groups
pub struct CommonRoleGroupConfig {
pub resources: RoleResource,
pub logging: Logging<Container>,
pub replicas: Option<u16>,
pub affinity: StackableAffinity,
pub graceful_shutdown_timeout: Option<Duration>,
pub requested_secret_lifetime: Duration,
}
/// Container for the merged and validated role group configurations
///
/// This structure contains for every role a map from the role group names to their configurations.
/// The role group configurations are merged with the role and default configurations. The product
/// configuration is not applied.
pub struct MergedConfig {
/// Merged configuration of the broker role
pub brokers: HashMap<String, RoleGroup<BrokerConfig, JavaCommonConfig>>,
/// Merged configuration of the coordinator role
pub coordinators: HashMap<String, RoleGroup<CoordinatorConfig, JavaCommonConfig>>,
/// Merged configuration of the historical role
pub historicals: HashMap<String, RoleGroup<HistoricalConfig, JavaCommonConfig>>,
/// Merged configuration of the middle manager role
pub middle_managers: HashMap<String, RoleGroup<MiddleManagerConfig, JavaCommonConfig>>,
/// Merged configuration of the router role
pub routers: HashMap<String, RoleGroup<RouterConfig, JavaCommonConfig>>,
}
impl MergedConfig {
/// Returns the common configuration for the given role and rolegroup name
pub fn common_config(
&self,
role: &DruidRole,
rolegroup_name: &str,
) -> Result<CommonRoleGroupConfig, Error> {
match role {
DruidRole::Broker => {
let rolegroup = self
.brokers
.get(rolegroup_name)
.context(CannotRetrieveRoleGroupSnafu { rolegroup_name })?;
Ok(CommonRoleGroupConfig {
resources: RoleResource::Druid(rolegroup.config.config.resources.to_owned()),
logging: rolegroup.config.config.logging.to_owned(),
replicas: rolegroup.replicas,
affinity: rolegroup.config.config.affinity.clone(),
graceful_shutdown_timeout: rolegroup.config.config.graceful_shutdown_timeout,
requested_secret_lifetime: rolegroup
.config
.config
.requested_secret_lifetime
.context(MissingSecretLifetimeSnafu)?,
})
}
DruidRole::Coordinator => {
let rolegroup = self
.coordinators
.get(rolegroup_name)
.context(CannotRetrieveRoleGroupSnafu { rolegroup_name })?;
Ok(CommonRoleGroupConfig {
resources: RoleResource::Druid(rolegroup.config.config.resources.to_owned()),
logging: rolegroup.config.config.logging.to_owned(),
replicas: rolegroup.replicas,
affinity: rolegroup.config.config.affinity.clone(),
graceful_shutdown_timeout: rolegroup.config.config.graceful_shutdown_timeout,
requested_secret_lifetime: rolegroup
.config
.config
.requested_secret_lifetime
.context(MissingSecretLifetimeSnafu)?,
})
}
DruidRole::Historical => {
let rolegroup = self
.historicals
.get(rolegroup_name)
.context(CannotRetrieveRoleGroupSnafu { rolegroup_name })?;
Ok(CommonRoleGroupConfig {
resources: RoleResource::Historical(
rolegroup.config.config.resources.to_owned(),
),
logging: rolegroup.config.config.logging.to_owned(),
replicas: rolegroup.replicas,
affinity: rolegroup.config.config.affinity.clone(),
graceful_shutdown_timeout: rolegroup.config.config.graceful_shutdown_timeout,
requested_secret_lifetime: rolegroup
.config
.config
.requested_secret_lifetime
.context(MissingSecretLifetimeSnafu)?,
})
}
DruidRole::MiddleManager => {
let rolegroup = self
.middle_managers
.get(rolegroup_name)
.context(CannotRetrieveRoleGroupSnafu { rolegroup_name })?;
Ok(CommonRoleGroupConfig {
resources: RoleResource::Druid(rolegroup.config.config.resources.to_owned()),
logging: rolegroup.config.config.logging.to_owned(),
replicas: rolegroup.replicas,
affinity: rolegroup.config.config.affinity.clone(),
graceful_shutdown_timeout: rolegroup.config.config.graceful_shutdown_timeout,
requested_secret_lifetime: rolegroup
.config
.config
.requested_secret_lifetime
.context(MissingSecretLifetimeSnafu)?,
})
}
DruidRole::Router => {
let rolegroup = self
.routers
.get(rolegroup_name)
.context(CannotRetrieveRoleGroupSnafu { rolegroup_name })?;
Ok(CommonRoleGroupConfig {
resources: RoleResource::Druid(rolegroup.config.config.resources.to_owned()),
logging: rolegroup.config.config.logging.to_owned(),
replicas: rolegroup.replicas,
affinity: rolegroup.config.config.affinity.clone(),
graceful_shutdown_timeout: rolegroup.config.config.graceful_shutdown_timeout,
requested_secret_lifetime: rolegroup
.config
.config
.requested_secret_lifetime
.context(MissingSecretLifetimeSnafu)?,
})
}
}
}
}
impl Default for v1alpha1::DruidRoleConfig {
fn default() -> Self {
v1alpha1::DruidRoleConfig {
listener_class: druid_default_listener_class(),
common: Default::default(),
}
}
}
fn druid_default_listener_class() -> String {
"cluster-internal".to_string()
}
#[derive(
Clone,
Debug,
Deserialize,
Display,
EnumIter,
Eq,
Hash,
JsonSchema,
PartialEq,
Serialize,
EnumString,
)]
pub enum DruidRole {
#[strum(serialize = "coordinator")]
Coordinator,
#[strum(serialize = "broker")]
Broker,
#[strum(serialize = "historical")]
Historical,
#[strum(serialize = "middlemanager")]
MiddleManager,
#[strum(serialize = "router")]
Router,
}
impl DruidRole {
/// Returns the name of the internal druid process name associated with the role.
/// These strings are used by druid internally to identify processes.
fn get_process_name(&self) -> &str {
match &self {
DruidRole::Coordinator => "coordinator",
DruidRole::Broker => "broker",
DruidRole::Historical => "historical",
DruidRole::MiddleManager => "middleManager",
DruidRole::Router => "router",
}
}
/// Returns the http port for every role
pub fn get_http_port(&self) -> u16 {
match &self {
DruidRole::Coordinator => 8081,
DruidRole::Broker => 8082,
DruidRole::Historical => 8083,
DruidRole::MiddleManager => 8091,
DruidRole::Router => 8888,
}
}
/// Returns the https port for every role
pub fn get_https_port(&self) -> u16 {
match &self {
DruidRole::Coordinator => 8281,
DruidRole::Broker => 8282,
DruidRole::Historical => 8283,
DruidRole::MiddleManager => 8291,
DruidRole::Router => 9088,
}
}
/// Return the default graceful shutdown timeout
pub fn default_graceful_shutdown_timeout(&self) -> Duration {
match &self {
DruidRole::Coordinator => DEFAULT_COORDINATOR_GRACEFUL_SHUTDOWN_TIMEOUT,
DruidRole::Broker => DEFAULT_BROKER_GRACEFUL_SHUTDOWN_TIMEOUT,
DruidRole::Historical => DEFAULT_HISTORICAL_GRACEFUL_SHUTDOWN_TIMEOUT,
DruidRole::MiddleManager => DEFAULT_MIDDLEMANAGER_GRACEFUL_SHUTDOWN_TIMEOUT,
DruidRole::Router => DEFAULT_ROUTER_GRACEFUL_SHUTDOWN_TIMEOUT,
}
}
pub fn main_container_prepare_commands(
&self,
s3: Option<&s3::v1alpha1::ConnectionSpec>,