-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.rs
More file actions
1419 lines (1257 loc) · 51.1 KB
/
Copy pathcontroller.rs
File metadata and controls
1419 lines (1257 loc) · 51.1 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
//! Ensures that `Pod`s are configured and running for each [`DruidCluster`][v1alpha1]
//!
//! [v1alpha1]: v1alpha1::DruidCluster
use std::{
collections::{BTreeMap, HashMap},
str::FromStr,
sync::Arc,
};
use const_format::concatcp;
use product_config::{
ProductConfigManager,
types::PropertyNameKind,
writer::{PropertiesWriterError, to_java_properties_string},
};
use snafu::{ResultExt, Snafu};
use stackable_operator::{
builder::{
self,
configmap::ConfigMapBuilder,
meta::ObjectMetaBuilder,
pod::{
PodBuilder, container::ContainerBuilder, resources::ResourceRequirementsBuilder,
security::PodSecurityContextBuilder, volume::VolumeBuilder,
},
},
cli::OperatorEnvironmentOptions,
cluster_resources::{ClusterResourceApplyStrategy, ClusterResources},
commons::{product_image_selection::ResolvedProductImage, rbac::build_rbac_resources},
constants::RESTART_CONTROLLER_ENABLED_LABEL,
crd::s3,
database_connections::drivers::jdbc::JdbcDatabaseConnection as _,
k8s_openapi::{
DeepMerge,
api::{
apps::v1::{StatefulSet, StatefulSetSpec},
core::v1::{ConfigMap, EnvVar, PersistentVolumeClaim, ServiceAccount},
},
apimachinery::pkg::apis::meta::v1::LabelSelector,
},
kube::{
Resource, ResourceExt,
core::{DeserializeGuard, error_boundary},
runtime::{controller::Action, reflector::ObjectRef},
},
kvp::{KeyValuePairError, LabelError, LabelValueError, Labels},
logging::controller::ReconcilerError,
product_logging::{
self,
framework::LoggingError,
spec::{
ConfigMapLogConfig, ContainerLogConfig, ContainerLogConfigChoice,
CustomContainerLogConfig,
},
},
role_utils::RoleGroupRef,
shared::time::Duration,
status::condition::{
compute_conditions, operations::ClusterOperationsConditionBuilder,
statefulset::StatefulSetConditionBuilder,
},
};
use strum::{EnumDiscriminants, IntoStaticStr};
use crate::{
authentication::DruidAuthenticationConfig,
config::jvm::construct_jvm_args,
crd::{
APP_NAME, AUTH_AUTHORIZER_OPA_URI, CommonRoleGroupConfig, Container,
DRUID_CONFIG_DIRECTORY, DS_BUCKET, DeepStorageSpec, DruidClusterStatus, DruidRole,
EXTENSIONS_LOADLIST, HDFS_CONFIG_DIRECTORY, JVM_CONFIG, JVM_SECURITY_PROPERTIES_FILE,
LOG_CONFIG_DIRECTORY, MAX_DRUID_LOG_FILES_SIZE, METRICS_PORT, METRICS_PORT_NAME,
OPERATOR_NAME, RUNTIME_PROPS, RW_CONFIG_DIRECTORY, S3_ACCESS_KEY, S3_ENDPOINT_URL,
S3_PATH_STYLE_ACCESS, S3_SECRET_KEY, STACKABLE_LOG_DIR, ZOOKEEPER_CONNECTION_STRING,
build_recommended_labels, build_string_list, security::DruidTlsSecurity, v1alpha1,
},
discovery::{self, build_discovery_configmaps},
extensions::get_extension_list,
internal_secret::create_shared_internal_secret,
listener::{
LISTENER_VOLUME_DIR, LISTENER_VOLUME_NAME, build_group_listener, build_group_listener_pvc,
group_listener_name, secret_volume_listener_scope,
},
operations::{graceful_shutdown::add_graceful_shutdown_config, pdb::add_pdbs},
product_logging::extend_role_group_config_map,
service::{build_rolegroup_headless_service, build_rolegroup_metrics_service},
};
mod dereference;
mod validate;
pub const DRUID_CONTROLLER_NAME: &str = "druidcluster";
pub const FULL_CONTROLLER_NAME: &str = concatcp!(DRUID_CONTROLLER_NAME, '.', OPERATOR_NAME);
pub(super) const CONTAINER_IMAGE_BASE_NAME: &str = "druid";
// volume names
const DRUID_CONFIG_VOLUME_NAME: &str = "config";
const HDFS_CONFIG_VOLUME_NAME: &str = "hdfs";
const LOG_CONFIG_VOLUME_NAME: &str = "log-config";
const LOG_VOLUME_NAME: &str = "log";
const RW_CONFIG_VOLUME_NAME: &str = "rwconfig";
const USERDATA_MOUNTPOINT: &str = "/stackable/userdata";
pub struct Ctx {
pub client: stackable_operator::client::Client,
pub product_config: ProductConfigManager,
pub operator_environment: OperatorEnvironmentOptions,
}
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[snafu(display("failed to apply Service for {}", rolegroup))]
ApplyRoleGroupService {
source: stackable_operator::cluster_resources::Error,
rolegroup: RoleGroupRef<v1alpha1::DruidCluster>,
},
#[snafu(display("failed to build ConfigMap for {}", rolegroup))]
BuildRoleGroupConfig {
source: stackable_operator::builder::configmap::Error,
rolegroup: RoleGroupRef<v1alpha1::DruidCluster>,
},
#[snafu(display("failed to apply ConfigMap for {}", rolegroup))]
ApplyRoleGroupConfig {
source: stackable_operator::cluster_resources::Error,
rolegroup: RoleGroupRef<v1alpha1::DruidCluster>,
},
#[snafu(display("failed to apply StatefulSet for {}", rolegroup))]
ApplyRoleGroupStatefulSet {
source: stackable_operator::cluster_resources::Error,
rolegroup: RoleGroupRef<v1alpha1::DruidCluster>,
},
#[snafu(display("object is missing metadata to build owner reference"))]
ObjectMissingMetadataForOwnerRef {
source: stackable_operator::builder::meta::Error,
},
#[snafu(display("failed to dereference cluster objects"))]
Dereference { source: dereference::Error },
#[snafu(display("failed to configure S3 connection"))]
ConfigureS3 {
source: stackable_operator::crd::s3::v1alpha1::ConnectionError,
},
#[snafu(display("failed to format runtime properties"))]
PropertiesWriteError { source: PropertiesWriterError },
#[snafu(display("failed to build discovery ConfigMap"))]
BuildDiscoveryConfig { source: discovery::Error },
#[snafu(display("failed to apply discovery ConfigMap"))]
ApplyDiscoveryConfig {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to apply cluster status"))]
ApplyStatus {
source: stackable_operator::client::Error,
},
#[snafu(display(
"Druid does not support skipping the verification of the tls enabled S3 server"
))]
S3TlsNoVerificationNotSupported,
#[snafu(display("could not parse Druid role [{role}]"))]
UnidentifiedDruidRole {
source: strum::ParseError,
role: String,
},
#[snafu(display("failed to resolve and merge config for role and role group"))]
FailedToResolveConfig { source: crate::crd::Error },
#[snafu(display("invalid configuration"))]
InvalidConfiguration { source: crate::crd::Error },
#[snafu(display("failed to create cluster resources"))]
CreateClusterResources {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to delete orphaned resources"))]
DeleteOrphanedResources {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to create container builder with name [{name}]"))]
FailedContainerBuilderCreation {
source: stackable_operator::builder::pod::container::Error,
name: String,
},
#[snafu(display("failed to initialize security context"))]
FailedToInitializeSecurityContext { source: crate::crd::security::Error },
#[snafu(display("failed to get JVM config"))]
GetJvmConfig { source: crate::config::jvm::Error },
#[snafu(display("failed to derive Druid memory settings from resources"))]
DeriveMemorySettings { source: crate::crd::resource::Error },
#[snafu(display("failed to update Druid config from resources"))]
UpdateDruidConfigFromResources { source: crate::crd::resource::Error },
#[snafu(display("failed to retrieve secret for internal communications"))]
FailedInternalSecretCreation {
source: crate::internal_secret::Error,
},
#[snafu(display("vector agent is enabled but vector aggregator ConfigMap is missing"))]
VectorAggregatorConfigMapMissing,
#[snafu(display("failed to add the logging configuration to the ConfigMap [{cm_name}]"))]
InvalidLoggingConfig {
source: crate::product_logging::Error,
cm_name: String,
},
#[snafu(display("failed to create RBAC service account"))]
ApplyServiceAccount {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to create RBAC role binding"))]
ApplyRoleBinding {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to build RBAC resources"))]
BuildRbacResources {
source: stackable_operator::commons::rbac::Error,
},
#[snafu(display(
"failed to serialize [{JVM_SECURITY_PROPERTIES_FILE}] for {}",
rolegroup
))]
JvmSecurityProperties {
source: PropertiesWriterError,
rolegroup: String,
},
#[snafu(display("failed to create PodDisruptionBudget"))]
FailedToCreatePdb {
source: crate::operations::pdb::Error,
},
#[snafu(display("failed to configure graceful shutdown"))]
GracefulShutdown {
source: crate::operations::graceful_shutdown::Error,
},
#[snafu(display("failed to add OIDC Volumes and VolumeMounts to the Pod and containers"))]
AuthVolumesBuild {
source: crate::authentication::Error,
},
#[snafu(display("failed to build labels"))]
LabelBuild { source: LabelError },
#[snafu(display("failed to build metadata"))]
MetadataBuild {
source: stackable_operator::builder::meta::Error,
},
#[snafu(display("failed to get required labels"))]
GetRequiredLabels {
source: KeyValuePairError<LabelValueError>,
},
#[snafu(display("there was an error generating the authentication runtime settings"))]
GenerateAuthenticationRuntimeSettings {
source: crate::authentication::Error,
},
#[snafu(display("failed to build vector container"))]
BuildVectorContainer { source: LoggingError },
#[snafu(display("failed to add needed volume"))]
AddVolume { source: builder::pod::Error },
#[snafu(display("failed to add needed volumeMount"))]
AddVolumeMount {
source: builder::pod::container::Error,
},
#[snafu(display("DruidCluster object is invalid"))]
InvalidDruidCluster {
source: error_boundary::InvalidObject,
},
#[snafu(display("failed to apply group listener"))]
ApplyGroupListener {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to configure listener"))]
ListenerConfiguration { source: crate::listener::Error },
#[snafu(display("failed to configure service"))]
ServiceConfiguration { source: crate::service::Error },
#[snafu(display("failed to validate cluster"))]
ValidateCluster { source: validate::Error },
#[snafu(display("invalid metadata database connection"))]
InvalidMetadataDatabaseConnection {
source: stackable_operator::database_connections::Error,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
impl ReconcilerError for Error {
fn category(&self) -> &'static str {
ErrorDiscriminants::from(self).into()
}
}
pub async fn reconcile_druid(
druid: Arc<DeserializeGuard<v1alpha1::DruidCluster>>,
ctx: Arc<Ctx>,
) -> Result<Action> {
tracing::info!("Starting reconcile");
let druid = druid
.0
.as_ref()
.map_err(error_boundary::InvalidObject::clone)
.context(InvalidDruidClusterSnafu)?;
let client = &ctx.client;
let dereferenced_objects = dereference::dereference(client, druid)
.await
.context(DereferenceSnafu)?;
let validated = validate::validate(
druid,
&dereferenced_objects,
&ctx.operator_environment,
&ctx.product_config,
)
.context(ValidateClusterSnafu)?;
let mut cluster_resources = ClusterResources::new(
APP_NAME,
OPERATOR_NAME,
DRUID_CONTROLLER_NAME,
&druid.object_ref(&()),
ClusterResourceApplyStrategy::from(&druid.spec.cluster_operation),
&druid.spec.object_overrides,
)
.context(CreateClusterResourcesSnafu)?;
let merged_config = druid.merged_config().context(FailedToResolveConfigSnafu)?;
let (rbac_sa, rbac_rolebinding) = build_rbac_resources(
druid,
APP_NAME,
cluster_resources
.get_required_labels()
.context(GetRequiredLabelsSnafu)?,
)
.context(BuildRbacResourcesSnafu)?;
cluster_resources
// We clone rbac_sa because we need to reuse it below
.add(client, rbac_sa.clone())
.await
.context(ApplyServiceAccountSnafu)?;
cluster_resources
.add(client, rbac_rolebinding)
.await
.context(ApplyRoleBindingSnafu)?;
let mut ss_cond_builder = StatefulSetConditionBuilder::default();
for (role_name, role_config) in validated.validated_role_config.iter() {
let druid_role = DruidRole::from_str(role_name).context(UnidentifiedDruidRoleSnafu {
role: role_name.to_string(),
})?;
create_shared_internal_secret(druid, client, DRUID_CONTROLLER_NAME)
.await
.context(FailedInternalSecretCreationSnafu)?;
for (rolegroup_name, rolegroup_config) in role_config.iter() {
let rolegroup = RoleGroupRef {
cluster: ObjectRef::from_obj(druid),
role: role_name.into(),
role_group: rolegroup_name.into(),
};
let merged_rolegroup_config = merged_config
.common_config(&druid_role, rolegroup_name)
.context(FailedToResolveConfigSnafu)?;
let role_group_service_recommended_labels = build_recommended_labels(
druid,
DRUID_CONTROLLER_NAME,
&validated.resolved_product_image.app_version_label_value,
&rolegroup.role,
&rolegroup.role_group,
);
let role_group_service_selector = Labels::role_group_selector(
druid,
APP_NAME,
&rolegroup.role,
&rolegroup.role_group,
)
.context(LabelBuildSnafu)?;
let rg_headless_service = build_rolegroup_headless_service(
druid,
&validated.druid_tls_security,
&druid_role,
&rolegroup,
role_group_service_recommended_labels.clone(),
role_group_service_selector.clone().into(),
)
.context(ServiceConfigurationSnafu)?;
let rg_metrics_service = build_rolegroup_metrics_service(
druid,
&rolegroup,
role_group_service_recommended_labels,
role_group_service_selector.into(),
)
.context(ServiceConfigurationSnafu)?;
let rg_configmap = build_rolegroup_config_map(
druid,
&validated.resolved_product_image,
&rolegroup,
rolegroup_config,
&merged_rolegroup_config,
&dereferenced_objects.zookeeper_connection_string,
dereferenced_objects.opa_connection_string.as_deref(),
dereferenced_objects.s3_connection.as_ref(),
dereferenced_objects.deep_storage_bucket_name.as_deref(),
&validated.druid_tls_security,
&validated.druid_auth_config,
)?;
let rg_statefulset = build_rolegroup_statefulset(
druid,
&validated.resolved_product_image,
&druid_role,
&rolegroup,
rolegroup_config,
&merged_rolegroup_config,
dereferenced_objects.s3_connection.as_ref(),
&validated.druid_tls_security,
&validated.druid_auth_config,
&rbac_sa,
)?;
cluster_resources
.add(client, rg_headless_service)
.await
.with_context(|_| ApplyRoleGroupServiceSnafu {
rolegroup: rolegroup.clone(),
})?;
cluster_resources
.add(client, rg_metrics_service)
.await
.with_context(|_| ApplyRoleGroupServiceSnafu {
rolegroup: rolegroup.clone(),
})?;
cluster_resources
.add(client, rg_configmap)
.await
.with_context(|_| ApplyRoleGroupConfigSnafu {
rolegroup: rolegroup.clone(),
})?;
// Note: The StatefulSet needs to be applied after all ConfigMaps and Secrets it mounts
// to prevent unnecessary Pod restarts.
// See https://github.com/stackabletech/commons-operator/issues/111 for details.
ss_cond_builder.add(
cluster_resources
.add(client, rg_statefulset)
.await
.with_context(|_| ApplyRoleGroupStatefulSetSnafu {
rolegroup: rolegroup.clone(),
})?,
);
}
if let Some(listener_class) = druid_role.listener_class_name(druid) {
if let Some(listener_group_name) = group_listener_name(druid, &druid_role) {
let role_group_listener = build_group_listener(
druid,
build_recommended_labels(
druid,
DRUID_CONTROLLER_NAME,
&validated.resolved_product_image.app_version_label_value,
role_name,
"none",
),
listener_class.to_string(),
listener_group_name,
&druid_role,
&validated.druid_tls_security,
)
.context(ListenerConfigurationSnafu)?;
let listener = cluster_resources
.add(client, role_group_listener)
.await
.context(ApplyGroupListenerSnafu)?;
if druid_role == DruidRole::Router {
// discovery
for discovery_cm in build_discovery_configmaps(
druid,
druid,
&validated.resolved_product_image,
&validated.druid_tls_security,
listener,
)
.await
.context(BuildDiscoveryConfigSnafu)?
{
cluster_resources
.add(client, discovery_cm)
.await
.context(ApplyDiscoveryConfigSnafu)?;
}
}
}
}
let role_config = druid.generic_role_config(&druid_role);
add_pdbs(
&role_config.pod_disruption_budget,
druid,
&druid_role,
client,
&mut cluster_resources,
)
.await
.context(FailedToCreatePdbSnafu)?;
}
let cluster_operation_cond_builder =
ClusterOperationsConditionBuilder::new(&druid.spec.cluster_operation);
let status = DruidClusterStatus {
conditions: compute_conditions(druid, &[&ss_cond_builder, &cluster_operation_cond_builder]),
};
cluster_resources
.delete_orphaned_resources(client)
.await
.context(DeleteOrphanedResourcesSnafu)?;
client
.apply_patch_status(OPERATOR_NAME, druid, &status)
.await
.context(ApplyStatusSnafu)?;
Ok(Action::await_change())
}
#[allow(clippy::too_many_arguments)]
/// The rolegroup [`ConfigMap`] configures the rolegroup based on the configuration given by the administrator
fn build_rolegroup_config_map(
druid: &v1alpha1::DruidCluster,
resolved_product_image: &ResolvedProductImage,
rolegroup: &RoleGroupRef<v1alpha1::DruidCluster>,
rolegroup_config: &HashMap<PropertyNameKind, BTreeMap<String, String>>,
merged_rolegroup_config: &CommonRoleGroupConfig,
zk_connstr: &str,
opa_connstr: Option<&str>,
s3_conn: Option<&s3::v1alpha1::ConnectionSpec>,
deep_storage_bucket_name: Option<&str>,
druid_tls_security: &DruidTlsSecurity,
druid_auth_config: &Option<DruidAuthenticationConfig>,
) -> Result<ConfigMap> {
let druid_role =
DruidRole::from_str(&rolegroup.role).with_context(|_| UnidentifiedDruidRoleSnafu {
role: &rolegroup.role,
})?;
let role = druid.get_role(&druid_role);
let mut cm_conf_data = BTreeMap::new(); // filename -> filecontent
let metadata_database_connection_details = druid
.spec
.cluster_config
.metadata_database
.jdbc_connection_details("metadata")
.context(InvalidMetadataDatabaseConnectionSnafu)?;
for (property_name_kind, config) in rolegroup_config {
let mut conf: BTreeMap<String, Option<String>> = Default::default();
match property_name_kind {
PropertyNameKind::File(file_name) if file_name == RUNTIME_PROPS => {
// Add any properties derived from storage manifests, such as segment cache locations.
// This has to be done here since there is no other suitable place for it.
// Previously such properties were added in the compute_files() function,
// but that code path is now incompatible with the design of fragment merging.
merged_rolegroup_config
.resources
.update_druid_config_file(&mut conf)
.context(UpdateDruidConfigFromResourcesSnafu)?;
// NOTE: druid.host can be set manually - if it isn't, the canonical host name of
// the local host is used. This should work with the agent and k8s host networking
// but might need to be revisited in the future
conf.insert(
ZOOKEEPER_CONNECTION_STRING.to_string(),
Some(zk_connstr.to_string()),
);
conf.insert(
EXTENSIONS_LOADLIST.to_string(),
Some(build_string_list(&get_extension_list(
druid,
druid_tls_security,
druid_auth_config,
))),
);
if let Some(opa_str) = opa_connstr {
conf.insert(
AUTH_AUTHORIZER_OPA_URI.to_string(),
Some(opa_str.to_string()),
);
};
conf.insert(
crate::crd::database::METADATA_STORAGE_TYPE.to_string(),
Some(
druid
.spec
.cluster_config
.metadata_database
.as_metadata_storage_type()
.to_string(),
),
);
conf.insert(
crate::crd::database::METADATA_STORAGE_CONNECTOR_CONNECT_URI.to_string(),
Some(
metadata_database_connection_details
.connection_url
.to_string(),
),
);
if let Some(EnvVar {
name: username_env_name,
..
}) = &metadata_database_connection_details.username_env
{
conf.insert(
crate::crd::database::METADATA_STORAGE_USER.to_string(),
Some(format!("${{env:{username_env_name}}}",)),
);
}
if let Some(EnvVar {
name: password_env_name,
..
}) = &metadata_database_connection_details.password_env
{
conf.insert(
crate::crd::database::METADATA_STORAGE_PASSWORD.to_string(),
Some(format!("${{env:{password_env_name}}}",)),
);
}
if let Some(s3) = s3_conn {
if !s3.region.is_default_config() {
// Raising this as warning instead of returning an error, better safe than sorry.
// It might still work out for the user.
tracing::warn!(
region = ?s3.region,
"You configured a non-default region on the S3Connection.
The S3Connection region field is ignored because Druid uses the AWS SDK v1, which ignores the region if the endpoint is set. \
The host is a required field, therefore the endpoint will always be set."
)
}
conf.insert(
S3_ENDPOINT_URL.to_string(),
Some(s3.endpoint().context(ConfigureS3Snafu)?.to_string()),
);
if let Some((access_key_file, secret_key_file)) = s3.credentials_mount_paths() {
conf.insert(
S3_ACCESS_KEY.to_string(),
Some(format!("${{file:UTF-8:{access_key_file}}}")),
);
conf.insert(
S3_SECRET_KEY.to_string(),
Some(format!("${{file:UTF-8:{secret_key_file}}}")),
);
}
conf.insert(
S3_PATH_STYLE_ACCESS.to_string(),
Some((s3.access_style == s3::v1alpha1::S3AccessStyle::Path).to_string()),
);
}
conf.insert(
DS_BUCKET.to_string(),
deep_storage_bucket_name.map(str::to_string),
);
// add tls encryption / auth properties
druid_tls_security.add_tls_config_properties(&mut conf, &druid_role);
if let Some(auth_config) = druid_auth_config {
conf.extend(
auth_config
.generate_runtime_properties_config(&druid_role)
.context(GenerateAuthenticationRuntimeSettingsSnafu)?,
);
};
let transformed_config: BTreeMap<String, Option<String>> = config
.iter()
.map(|(k, v)| (k.clone(), Some(v.clone())))
.collect();
// extend the config to respect overrides
conf.extend(transformed_config);
let runtime_properties =
to_java_properties_string(conf.iter()).context(PropertiesWriteSnafu)?;
cm_conf_data.insert(RUNTIME_PROPS.to_string(), runtime_properties);
}
PropertyNameKind::File(file_name) if file_name == JVM_CONFIG => {
let (heap, direct) = merged_rolegroup_config
.resources
.get_memory_sizes(&druid_role)
.context(DeriveMemorySettingsSnafu)?;
let jvm_config =
construct_jvm_args(&druid_role, &role, &rolegroup.role_group, heap, direct)
.context(GetJvmConfigSnafu)?;
cm_conf_data.insert(JVM_CONFIG.to_string(), jvm_config);
}
PropertyNameKind::File(file_name) if file_name == JVM_SECURITY_PROPERTIES_FILE => {
let jvm_sec_props: BTreeMap<String, Option<String>> = rolegroup_config
.get(&PropertyNameKind::File(
JVM_SECURITY_PROPERTIES_FILE.to_string(),
))
.cloned()
.unwrap_or_default()
.into_iter()
.map(|(k, v)| (k, Some(v)))
.collect();
cm_conf_data.insert(
JVM_SECURITY_PROPERTIES_FILE.to_string(),
to_java_properties_string(jvm_sec_props.iter()).with_context(|_| {
JvmSecurityPropertiesSnafu {
rolegroup: rolegroup.role_group.clone(),
}
})?,
);
}
_ => {}
}
}
let mut config_map_builder = ConfigMapBuilder::new();
config_map_builder.metadata(
ObjectMetaBuilder::new()
.name_and_namespace(druid)
.name(rolegroup.object_name())
.ownerreference_from_resource(druid, None, Some(true))
.context(ObjectMissingMetadataForOwnerRefSnafu)?
.with_recommended_labels(&build_recommended_labels(
druid,
DRUID_CONTROLLER_NAME,
&resolved_product_image.app_version_label_value,
&rolegroup.role,
&rolegroup.role_group,
))
.context(MetadataBuildSnafu)?
.build(),
);
for (filename, file_content) in cm_conf_data.iter() {
config_map_builder.add_data(filename, file_content);
}
extend_role_group_config_map(
rolegroup,
&merged_rolegroup_config.logging,
&mut config_map_builder,
)
.context(InvalidLoggingConfigSnafu {
cm_name: rolegroup.object_name(),
})?;
config_map_builder
.build()
.with_context(|_| BuildRoleGroupConfigSnafu {
rolegroup: rolegroup.clone(),
})
}
#[allow(clippy::too_many_arguments)]
/// The rolegroup [`StatefulSet`] runs the rolegroup, as configured by the administrator.
///
/// The [`Pod`](`stackable_operator::k8s_openapi::api::core::v1::Pod`)s are accessible through the
/// corresponding [`stackable_operator::k8s_openapi::api::core::v1::Service`] (from [`build_rolegroup_headless_service`]).
fn build_rolegroup_statefulset(
druid: &v1alpha1::DruidCluster,
resolved_product_image: &ResolvedProductImage,
role: &DruidRole,
rolegroup_ref: &RoleGroupRef<v1alpha1::DruidCluster>,
rolegroup_config: &HashMap<PropertyNameKind, BTreeMap<String, String>>,
merged_rolegroup_config: &CommonRoleGroupConfig,
s3_conn: Option<&s3::v1alpha1::ConnectionSpec>,
druid_tls_security: &DruidTlsSecurity,
druid_auth_config: &Option<DruidAuthenticationConfig>,
service_account: &ServiceAccount,
) -> Result<StatefulSet> {
// prepare container builder
let prepare_container_name = Container::Prepare.to_string();
let mut cb_prepare = ContainerBuilder::new(&prepare_container_name).context(
FailedContainerBuilderCreationSnafu {
name: &prepare_container_name,
},
)?;
// druid container builder
let druid_container_name = Container::Druid.to_string();
let mut cb_druid = ContainerBuilder::new(&druid_container_name).context(
FailedContainerBuilderCreationSnafu {
name: &druid_container_name,
},
)?;
// init pod builder
let mut pb = PodBuilder::new();
pb.affinity(&merged_rolegroup_config.affinity);
add_graceful_shutdown_config(
role,
druid_tls_security,
merged_rolegroup_config.graceful_shutdown_timeout,
&mut pb,
&mut cb_druid,
)
.context(GracefulShutdownSnafu)?;
let metadata_database_connection_details = druid
.spec
.cluster_config
.metadata_database
.jdbc_connection_details("metadata")
.context(InvalidMetadataDatabaseConnectionSnafu)?;
let mut main_container_commands = role.main_container_prepare_commands(s3_conn);
let mut prepare_container_commands = vec![];
if let Some(ContainerLogConfig {
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
}) = merged_rolegroup_config
.logging
.containers
.get(&Container::Prepare)
{
// This command needs to be added at the beginning of the shell commands,
// otherwise the output of the following commands will not be captured!
prepare_container_commands.push(product_logging::framework::capture_shell_output(
STACKABLE_LOG_DIR,
&prepare_container_name,
log_config,
));
}
prepare_container_commands.extend(druid_tls_security.build_tls_key_stores_cmd());
if let Some(auth_config) = druid_auth_config {
auth_config
.add_volumes_and_mounts(&mut pb, &mut cb_druid, &mut cb_prepare)
.context(AuthVolumesBuildSnafu)?;
prepare_container_commands.extend(auth_config.prepare_container_commands());
main_container_commands.extend(auth_config.main_container_commands())
}
// volume and volume mounts
druid_tls_security
.add_tls_volume_and_volume_mounts(
&mut cb_prepare,
&mut cb_druid,
&mut pb,
&merged_rolegroup_config.requested_secret_lifetime,
// add listener
secret_volume_listener_scope(role),
)
.context(FailedToInitializeSecurityContextSnafu)?;
if let Some(s3) = s3_conn {
if s3.tls.uses_tls() && !s3.tls.uses_tls_verification() {
S3TlsNoVerificationNotSupportedSnafu.fail()?;
}
s3.add_volumes_and_mounts(&mut pb, vec![&mut cb_druid])
.context(ConfigureS3Snafu)?;
}
add_config_volume_and_volume_mounts(rolegroup_ref, &mut cb_druid, &mut pb)?;
add_log_config_volume_and_volume_mounts(
rolegroup_ref,
merged_rolegroup_config,
&mut cb_druid,
&mut pb,
)?;
add_log_volume_and_volume_mounts(&mut cb_druid, &mut cb_prepare, &mut pb)?;
add_hdfs_cm_volume_and_volume_mounts(
&druid.spec.cluster_config.deep_storage,
&mut cb_druid,
&mut pb,
)?;
merged_rolegroup_config
.resources
.update_volumes_and_volume_mounts(&mut cb_druid, &mut pb)
.context(UpdateDruidConfigFromResourcesSnafu)?;
cb_prepare
.image_from_product_image(resolved_product_image)
.command(vec![
"/bin/bash".to_string(),
"-x".to_string(),
"-euo".to_string(),
"pipefail".to_string(),
"-c".to_string(),
])
.args(vec![prepare_container_commands.join("\n")])
.resources(
ResourceRequirementsBuilder::new()
.with_cpu_request("100m")
.with_cpu_limit("400m")
.with_memory_request("512Mi")
.with_memory_limit("512Mi")
.build(),
);
metadata_database_connection_details.add_to_container(&mut cb_druid);
// rest of env
let mut rest_env = rolegroup_config
.get(&PropertyNameKind::Env)
.iter()
.flat_map(|env_vars| env_vars.iter())
.map(|(k, v)| EnvVar {
name: k.clone(),
value: Some(v.clone()),
..EnvVar::default()
})
.collect::<Vec<_>>();
if let Some(auth_config) = druid_auth_config {
rest_env.extend(auth_config.get_env_var_mounts(druid, role))
}
// Needed for the `containerdebug` process to log it's tracing information to.
rest_env.push(EnvVar {
name: "CONTAINERDEBUG_LOG_DIRECTORY".to_string(),
value: Some(format!("{STACKABLE_LOG_DIR}/containerdebug")),
value_from: None,
});
main_container_commands.push(role.main_container_start_command());
cb_druid
.image_from_product_image(resolved_product_image)
.command(vec![
"/bin/bash".to_string(),
"-x".to_string(),
"-euo".to_string(),
"pipefail".to_string(),
"-c".to_string(),
])
.args(vec![main_container_commands.join("\n")])
.add_env_vars(rest_env)
.add_container_ports(druid_tls_security.container_ports(role))
.add_container_port(METRICS_PORT_NAME, METRICS_PORT.into())
// 10s * 30 = 300s to come up
.startup_probe(druid_tls_security.get_tcp_socket_probe(30, 10, 30, 3))
// 10s * 1 = 10s to get removed from service
.readiness_probe(druid_tls_security.get_tcp_socket_probe(10, 10, 1, 3))
// 10s * 3 = 30s to be restarted
.liveness_probe(druid_tls_security.get_tcp_socket_probe(10, 10, 3, 3))
.resources(merged_rolegroup_config.resources.as_resource_requirements());
// Add extra mounts if any are specified and the current role is MiddleManager
// Extra mounts may be needed for ingestion to add required certificates, truststores or similar
// files.
// Mounts are added to all roles, as we are currently unsure where they may be needed
// Known roles are MiddleManagers for ingestion and Historicals for deep storage (GCS plugin)
// We may at some time in the future revisit this and limit it again to avoid needlessly
// propagating potentially confidential files throughout the cluster