-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzk_controller.rs
1224 lines (1107 loc) · 42.3 KB
/
zk_controller.rs
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 [`v1alpha1::ZookeeperCluster`]
use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
hash::Hasher,
str::FromStr,
sync::Arc,
};
use const_format::concatcp;
use fnv::FnvHasher;
use indoc::formatdoc;
use product_config::{
types::PropertyNameKind,
writer::{to_java_properties_string, PropertiesWriterError},
ProductConfigManager,
};
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{
builder::{
self,
configmap::ConfigMapBuilder,
meta::ObjectMetaBuilder,
pod::{container::ContainerBuilder, resources::ResourceRequirementsBuilder, PodBuilder},
},
cluster_resources::{ClusterResourceApplyStrategy, ClusterResources},
commons::{product_image_selection::ResolvedProductImage, rbac::build_rbac_resources},
k8s_openapi::{
api::{
apps::v1::{StatefulSet, StatefulSetSpec},
core::v1::{
ConfigMap, ConfigMapVolumeSource, EmptyDirVolumeSource, EnvVar, EnvVarSource,
ExecAction, ObjectFieldSelector, PodSecurityContext, Probe, Service,
ServiceAccount, ServicePort, ServiceSpec, Volume,
},
},
apimachinery::pkg::apis::meta::v1::LabelSelector,
DeepMerge,
},
kube::{
api::DynamicObject,
core::{error_boundary, DeserializeGuard},
runtime::controller,
Resource, ResourceExt,
},
kvp::{Label, LabelError, Labels},
logging::controller::ReconcilerError,
product_config_utils::{transform_all_roles_to_config, validate_all_roles_and_groups_config},
product_logging::{
self,
framework::{
create_vector_shutdown_file_command, remove_vector_shutdown_file_command, LoggingError,
},
spec::{
ConfigMapLogConfig, ContainerLogConfig, ContainerLogConfigChoice,
CustomContainerLogConfig,
},
},
role_utils::{GenericRoleConfig, RoleGroupRef},
status::condition::{
compute_conditions, operations::ClusterOperationsConditionBuilder,
statefulset::StatefulSetConditionBuilder,
},
time::Duration,
utils::{cluster_info::KubernetesClusterInfo, COMMON_BASH_TRAP_FUNCTIONS},
};
use strum::{EnumDiscriminants, IntoStaticStr};
use crate::{
command::create_init_container_command_args,
config::jvm::{construct_non_heap_jvm_args, construct_zk_server_heap_env},
crd::{
security::{self, ZookeeperSecurity},
v1alpha1, ZookeeperRole, DOCKER_IMAGE_BASE_NAME, JVM_SECURITY_PROPERTIES_FILE,
MAX_PREPARE_LOG_FILE_SIZE, MAX_ZK_LOG_FILES_SIZE, STACKABLE_CONFIG_DIR, STACKABLE_DATA_DIR,
STACKABLE_LOG_CONFIG_DIR, STACKABLE_LOG_DIR, STACKABLE_RW_CONFIG_DIR,
ZOOKEEPER_PROPERTIES_FILE,
},
discovery::{self, build_discovery_configmaps},
operations::{graceful_shutdown::add_graceful_shutdown_config, pdb::add_pdbs},
product_logging::{extend_role_group_config_map, resolve_vector_aggregator_address},
utils::build_recommended_labels,
ObjectRef, APP_NAME, OPERATOR_NAME,
};
pub const ZK_CONTROLLER_NAME: &str = "zookeepercluster";
pub const ZK_FULL_CONTROLLER_NAME: &str = concatcp!(ZK_CONTROLLER_NAME, '.', OPERATOR_NAME);
pub const ZK_UID: i64 = 1000;
pub struct Ctx {
pub client: stackable_operator::client::Client,
pub product_config: ProductConfigManager,
}
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[snafu(display("missing secret lifetime"))]
MissingSecretLifetime,
#[snafu(display("ZookeeperCluster object is invalid"))]
InvalidZookeeperCluster {
source: error_boundary::InvalidObject,
},
#[snafu(display("crd validation failure"))]
CrdValidationFailure { source: crate::crd::Error },
#[snafu(display("object defines no server role"))]
NoServerRole,
#[snafu(display("could not parse role [{role}]"))]
RoleParseFailure {
source: strum::ParseError,
role: String,
},
#[snafu(display("internal operator failure"))]
InternalOperatorFailure { source: crate::crd::Error },
#[snafu(display("failed to calculate global service name"))]
GlobalServiceNameNotFound,
#[snafu(display("failed to calculate service name for role {}", rolegroup))]
RoleGroupServiceNameNotFound {
rolegroup: RoleGroupRef<v1alpha1::ZookeeperCluster>,
},
#[snafu(display("failed to apply global Service"))]
ApplyRoleService {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to apply Service for {}", rolegroup))]
ApplyRoleGroupService {
source: stackable_operator::cluster_resources::Error,
rolegroup: RoleGroupRef<v1alpha1::ZookeeperCluster>,
},
#[snafu(display("failed to build ConfigMap for {}", rolegroup))]
BuildRoleGroupConfig {
source: stackable_operator::builder::configmap::Error,
rolegroup: RoleGroupRef<v1alpha1::ZookeeperCluster>,
},
#[snafu(display("failed to apply ConfigMap for {}", rolegroup))]
ApplyRoleGroupConfig {
source: stackable_operator::cluster_resources::Error,
rolegroup: RoleGroupRef<v1alpha1::ZookeeperCluster>,
},
#[snafu(display("failed to apply StatefulSet for {}", rolegroup))]
ApplyRoleGroupStatefulSet {
source: stackable_operator::cluster_resources::Error,
rolegroup: RoleGroupRef<v1alpha1::ZookeeperCluster>,
},
#[snafu(display("failed to generate product config"))]
GenerateProductConfig {
source: stackable_operator::product_config_utils::Error,
},
#[snafu(display("invalid product config"))]
InvalidProductConfig {
source: stackable_operator::product_config_utils::Error,
},
#[snafu(display("failed to serialize [{ZOOKEEPER_PROPERTIES_FILE}] for {}", rolegroup))]
SerializeZooCfg {
source: PropertiesWriterError,
rolegroup: RoleGroupRef<v1alpha1::ZookeeperCluster>,
},
#[snafu(display("object is missing metadata to build owner reference"))]
ObjectMissingMetadataForOwnerRef {
source: stackable_operator::builder::meta::Error,
},
#[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 update status"))]
ApplyStatus {
source: stackable_operator::client::Error,
},
#[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 delete orphaned resources"))]
DeleteOrphans {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to resolve the Vector aggregator address"))]
ResolveVectorAggregatorAddress {
source: crate::product_logging::Error,
},
#[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 initialize security context"))]
FailedToInitializeSecurityContext { source: crate::crd::security::Error },
#[snafu(display("failed to resolve and merge config for role and role group"))]
FailedToResolveConfig { source: crate::crd::Error },
#[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 build label"))]
BuildLabel { source: LabelError },
#[snafu(display("failed to build object meta data"))]
ObjectMeta {
source: stackable_operator::builder::meta::Error,
},
#[snafu(display("failed to add TLS volume mounts"))]
AddTlsVolumeMounts { source: security::Error },
#[snafu(display("failed to configure logging"))]
ConfigureLogging { 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("failed to create cluster resources"))]
CreateClusterResources {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to construct JVM arguments"))]
ConstructJvmArguments { source: crate::config::jvm::Error },
}
impl ReconcilerError for Error {
fn category(&self) -> &'static str {
ErrorDiscriminants::from(self).into()
}
fn secondary_object(&self) -> Option<ObjectRef<DynamicObject>> {
match self {
Error::MissingSecretLifetime => None,
Error::InvalidZookeeperCluster { source: _ } => None,
Error::CrdValidationFailure { .. } => None,
Error::NoServerRole => None,
Error::RoleParseFailure { .. } => None,
Error::InternalOperatorFailure { .. } => None,
Error::GlobalServiceNameNotFound => None,
Error::RoleGroupServiceNameNotFound { .. } => None,
Error::ApplyRoleService { .. } => None,
Error::ApplyRoleGroupService { .. } => None,
Error::BuildRoleGroupConfig { .. } => None,
Error::ApplyRoleGroupConfig { .. } => None,
Error::ApplyRoleGroupStatefulSet { .. } => None,
Error::GenerateProductConfig { .. } => None,
Error::InvalidProductConfig { .. } => None,
Error::SerializeZooCfg { .. } => None,
Error::ObjectMissingMetadataForOwnerRef { .. } => None,
Error::BuildDiscoveryConfig { .. } => None,
Error::ApplyDiscoveryConfig { .. } => None,
Error::ApplyStatus { .. } => None,
Error::ApplyServiceAccount { .. } => None,
Error::ApplyRoleBinding { .. } => None,
Error::BuildRbacResources { .. } => None,
Error::DeleteOrphans { .. } => None,
Error::ResolveVectorAggregatorAddress { .. } => None,
Error::InvalidLoggingConfig { .. } => None,
Error::FailedToInitializeSecurityContext { .. } => None,
Error::FailedToResolveConfig { .. } => None,
Error::FailedToCreatePdb { .. } => None,
Error::GracefulShutdown { .. } => None,
Error::BuildLabel { .. } => None,
Error::ObjectMeta { .. } => None,
Error::AddTlsVolumeMounts { .. } => None,
Error::ConfigureLogging { .. } => None,
Error::AddVolume { .. } => None,
Error::AddVolumeMount { .. } => None,
Error::CreateClusterResources { .. } => None,
Error::ConstructJvmArguments { .. } => None,
}
}
}
pub async fn reconcile_zk(
zk: Arc<DeserializeGuard<v1alpha1::ZookeeperCluster>>,
ctx: Arc<Ctx>,
) -> Result<controller::Action> {
tracing::info!("Starting reconcile");
let zk =
zk.0.as_ref()
.map_err(error_boundary::InvalidObject::clone)
.context(InvalidZookeeperClusterSnafu)?;
let client = &ctx.client;
let resolved_product_image = zk
.spec
.image
.resolve(DOCKER_IMAGE_BASE_NAME, crate::built_info::CARGO_PKG_VERSION);
let mut cluster_resources = ClusterResources::new(
APP_NAME,
OPERATOR_NAME,
ZK_CONTROLLER_NAME,
&zk.object_ref(&()),
ClusterResourceApplyStrategy::from(&zk.spec.cluster_operation),
)
.context(CreateClusterResourcesSnafu)?;
let validated_config = validate_all_roles_and_groups_config(
&resolved_product_image.app_version_label,
&transform_all_roles_to_config(
zk,
[(
ZookeeperRole::Server.to_string(),
(
vec![
PropertyNameKind::Env,
PropertyNameKind::File(ZOOKEEPER_PROPERTIES_FILE.to_string()),
PropertyNameKind::File(JVM_SECURITY_PROPERTIES_FILE.to_string()),
],
zk.spec.servers.clone().context(NoServerRoleSnafu)?,
),
)]
.into(),
)
.context(GenerateProductConfigSnafu)?,
&ctx.product_config,
false,
false,
)
.context(InvalidProductConfigSnafu)?;
let role_server_config = validated_config
.get(&ZookeeperRole::Server.to_string())
.map(Cow::Borrowed)
.unwrap_or_default();
let vector_aggregator_address = resolve_vector_aggregator_address(zk, client)
.await
.context(ResolveVectorAggregatorAddressSnafu)?;
let zookeeper_security = ZookeeperSecurity::new_from_zookeeper_cluster(client, zk)
.await
.context(FailedToInitializeSecurityContextSnafu)?;
let (rbac_sa, rbac_rolebinding) = build_rbac_resources(
zk,
APP_NAME,
cluster_resources
.get_required_labels()
.context(BuildLabelSnafu)?,
)
.context(BuildRbacResourcesSnafu)?;
cluster_resources
.add(client, rbac_sa.clone())
.await
.context(ApplyServiceAccountSnafu)?;
cluster_resources
.add(client, rbac_rolebinding)
.await
.context(ApplyRoleBindingSnafu)?;
let server_role_service = cluster_resources
.add(
client,
build_server_role_service(zk, &resolved_product_image, &zookeeper_security)?,
)
.await
.context(ApplyRoleServiceSnafu)?;
let mut ss_cond_builder = StatefulSetConditionBuilder::default();
let zk_role = ZookeeperRole::Server;
for (rolegroup_name, rolegroup_config) in role_server_config.iter() {
let rolegroup = zk.server_rolegroup_ref(rolegroup_name);
let merged_config = zk
.merged_config(&ZookeeperRole::Server, &rolegroup)
.context(FailedToResolveConfigSnafu)?;
let rg_service = build_server_rolegroup_service(
zk,
&rolegroup,
&resolved_product_image,
&zookeeper_security,
)?;
let rg_configmap = build_server_rolegroup_config_map(
zk,
&rolegroup,
rolegroup_config,
&resolved_product_image,
vector_aggregator_address.as_deref(),
&zookeeper_security,
&client.kubernetes_cluster_info,
)?;
let rg_statefulset = build_server_rolegroup_statefulset(
zk,
&zk_role,
&rolegroup,
rolegroup_config,
&zookeeper_security,
&resolved_product_image,
&merged_config,
&rbac_sa,
)?;
cluster_resources
.add(client, rg_service)
.await
.with_context(|_| ApplyRoleGroupServiceSnafu {
rolegroup: rolegroup.clone(),
})?;
cluster_resources
.add(client, rg_configmap)
.await
.with_context(|_| ApplyRoleGroupConfigSnafu {
rolegroup: rolegroup.clone(),
})?;
ss_cond_builder.add(
cluster_resources
.add(client, rg_statefulset)
.await
.with_context(|_| ApplyRoleGroupStatefulSetSnafu {
rolegroup: rolegroup.clone(),
})?,
);
}
let role_config = zk.role_config(&zk_role);
if let Some(GenericRoleConfig {
pod_disruption_budget: pdb,
}) = role_config
{
add_pdbs(pdb, zk, &zk_role, client, &mut cluster_resources)
.await
.context(FailedToCreatePdbSnafu)?;
}
// std's SipHasher is deprecated, and DefaultHasher is unstable across Rust releases.
// We don't /need/ stability, but it's still nice to avoid spurious changes where possible.
let mut discovery_hash = FnvHasher::with_key(0);
for discovery_cm in build_discovery_configmaps(
zk,
zk,
client,
ZK_CONTROLLER_NAME,
&server_role_service,
None,
&resolved_product_image,
&zookeeper_security,
)
.await
.context(BuildDiscoveryConfigSnafu)?
{
let discovery_cm = cluster_resources
.add(client, discovery_cm)
.await
.context(ApplyDiscoveryConfigSnafu)?;
if let Some(generation) = discovery_cm.metadata.resource_version {
discovery_hash.write(generation.as_bytes())
}
}
let cluster_operation_cond_builder =
ClusterOperationsConditionBuilder::new(&zk.spec.cluster_operation);
let status = v1alpha1::ZookeeperClusterStatus {
// Serialize as a string to discourage users from trying to parse the value,
// and to keep things flexible if we end up changing the hasher at some point.
discovery_hash: Some(discovery_hash.finish().to_string()),
conditions: compute_conditions(zk, &[&ss_cond_builder, &cluster_operation_cond_builder]),
};
cluster_resources
.delete_orphaned_resources(client)
.await
.context(DeleteOrphansSnafu)?;
client
.apply_patch_status(OPERATOR_NAME, zk, &status)
.await
.context(ApplyStatusSnafu)?;
Ok(controller::Action::await_change())
}
/// The server-role service is the primary endpoint that should be used by clients that do not perform internal load balancing,
/// including targets outside of the cluster.
///
/// Note that you should generally *not* hard-code clients to use these services; instead, create a [`v1alpha1::ZookeeperZnode`](`v1alpha1::ZookeeperZnode`)
/// and use the connection string that it gives you.
pub fn build_server_role_service(
zk: &v1alpha1::ZookeeperCluster,
resolved_product_image: &ResolvedProductImage,
zookeeper_security: &ZookeeperSecurity,
) -> Result<Service> {
let role_name = ZookeeperRole::Server.to_string();
let role_svc_name = zk
.server_role_service_name()
.context(GlobalServiceNameNotFoundSnafu)?;
let metadata = ObjectMetaBuilder::new()
.name_and_namespace(zk)
.name(&role_svc_name)
.ownerreference_from_resource(zk, None, Some(true))
.context(ObjectMissingMetadataForOwnerRefSnafu)?
.with_recommended_labels(build_recommended_labels(
zk,
ZK_CONTROLLER_NAME,
&resolved_product_image.app_version_label,
&role_name,
"global",
))
.context(ObjectMetaSnafu)?
.build();
let service_selector_labels =
Labels::role_selector(zk, APP_NAME, &role_name).context(BuildLabelSnafu)?;
let service_spec = ServiceSpec {
ports: Some(vec![ServicePort {
name: Some("zk".to_string()),
port: zookeeper_security.client_port().into(),
protocol: Some("TCP".to_string()),
..ServicePort::default()
}]),
selector: Some(service_selector_labels.into()),
type_: Some(zk.spec.cluster_config.listener_class.k8s_service_type()),
..ServiceSpec::default()
};
Ok(Service {
metadata,
spec: Some(service_spec),
status: None,
})
}
/// The rolegroup [`ConfigMap`] configures the rolegroup based on the configuration given by the administrator
fn build_server_rolegroup_config_map(
zk: &v1alpha1::ZookeeperCluster,
rolegroup: &RoleGroupRef<v1alpha1::ZookeeperCluster>,
server_config: &HashMap<PropertyNameKind, BTreeMap<String, String>>,
resolved_product_image: &ResolvedProductImage,
vector_aggregator_address: Option<&str>,
zookeeper_security: &ZookeeperSecurity,
cluster_info: &KubernetesClusterInfo,
) -> Result<ConfigMap> {
let mut zoo_cfg: BTreeMap<_, _> = zk
.pods()
.into_iter()
.flatten()
.map(|pod| {
(
format!("server.{}", pod.zookeeper_myid),
format!(
"{}:2888:3888;{}",
pod.fqdn(cluster_info),
zookeeper_security.client_port()
),
)
})
.collect();
zoo_cfg.extend(zookeeper_security.config_settings());
let jvm_sec_props: BTreeMap<String, Option<String>> = server_config
.get(&PropertyNameKind::File(
JVM_SECURITY_PROPERTIES_FILE.to_string(),
))
.cloned()
.unwrap_or_default()
.into_iter()
.map(|(k, v)| (k, Some(v)))
.collect();
let role =
ZookeeperRole::from_str(&rolegroup.role).with_context(|_| RoleParseFailureSnafu {
role: rolegroup.role.to_string(),
})?;
// configOverrides need to go last
zoo_cfg.extend(
server_config
.get(&PropertyNameKind::File(
ZOOKEEPER_PROPERTIES_FILE.to_string(),
))
.cloned()
.unwrap_or_default(),
);
let zk_data: BTreeMap<String, Option<String>> =
zoo_cfg.into_iter().map(|(k, v)| (k, Some(v))).collect();
let mut cm_builder = ConfigMapBuilder::new();
cm_builder
.metadata(
ObjectMetaBuilder::new()
.name_and_namespace(zk)
.name(rolegroup.object_name())
.ownerreference_from_resource(zk, None, Some(true))
.context(ObjectMissingMetadataForOwnerRefSnafu)?
.with_recommended_labels(build_recommended_labels(
zk,
ZK_CONTROLLER_NAME,
&resolved_product_image.app_version_label,
&rolegroup.role,
&rolegroup.role_group,
))
.context(ObjectMetaSnafu)?
.build(),
)
.add_data(
JVM_SECURITY_PROPERTIES_FILE,
to_java_properties_string(jvm_sec_props.iter()).with_context(|_| {
SerializeZooCfgSnafu {
rolegroup: rolegroup.clone(),
}
})?,
)
.add_data(
ZOOKEEPER_PROPERTIES_FILE,
to_java_properties_string(zk_data.iter()).with_context(|_| SerializeZooCfgSnafu {
rolegroup: rolegroup.clone(),
})?,
);
extend_role_group_config_map(
zk,
role,
rolegroup,
vector_aggregator_address,
&mut cm_builder,
)
.context(InvalidLoggingConfigSnafu {
cm_name: rolegroup.object_name(),
})?;
cm_builder
.build()
.with_context(|_| BuildRoleGroupConfigSnafu {
rolegroup: rolegroup.clone(),
})
}
/// The rolegroup [`Service`] is a headless service that allows direct access to the instances of a certain rolegroup
///
/// This is mostly useful for internal communication between peers, or for clients that perform client-side load balancing.
fn build_server_rolegroup_service(
zk: &v1alpha1::ZookeeperCluster,
rolegroup: &RoleGroupRef<v1alpha1::ZookeeperCluster>,
resolved_product_image: &ResolvedProductImage,
zookeeper_security: &ZookeeperSecurity,
) -> Result<Service> {
let prometheus_label =
Label::try_from(("prometheus.io/scrape", "true")).context(BuildLabelSnafu)?;
let metadata = ObjectMetaBuilder::new()
.name_and_namespace(zk)
.name(rolegroup.object_name())
.ownerreference_from_resource(zk, None, Some(true))
.context(ObjectMissingMetadataForOwnerRefSnafu)?
.with_recommended_labels(build_recommended_labels(
zk,
ZK_CONTROLLER_NAME,
&resolved_product_image.app_version_label,
&rolegroup.role,
&rolegroup.role_group,
))
.context(ObjectMetaSnafu)?
.with_label(prometheus_label)
.build();
let service_selector_labels =
Labels::role_group_selector(zk, APP_NAME, &rolegroup.role, &rolegroup.role_group)
.context(BuildLabelSnafu)?;
let service_spec = ServiceSpec {
// Internal communication does not need to be exposed
type_: Some("ClusterIP".to_string()),
cluster_ip: Some("None".to_string()),
ports: Some(vec![
ServicePort {
name: Some("zk".to_string()),
port: zookeeper_security.client_port().into(),
protocol: Some("TCP".to_string()),
..ServicePort::default()
},
ServicePort {
name: Some("metrics".to_string()),
port: 9505,
protocol: Some("TCP".to_string()),
..ServicePort::default()
},
]),
selector: Some(service_selector_labels.into()),
publish_not_ready_addresses: Some(true),
..ServiceSpec::default()
};
Ok(Service {
metadata,
spec: Some(service_spec),
status: None,
})
}
/// 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 [`Service`] (from [`build_server_rolegroup_service`]).
#[allow(clippy::too_many_arguments)]
fn build_server_rolegroup_statefulset(
zk: &v1alpha1::ZookeeperCluster,
zk_role: &ZookeeperRole,
rolegroup_ref: &RoleGroupRef<v1alpha1::ZookeeperCluster>,
server_config: &HashMap<PropertyNameKind, BTreeMap<String, String>>,
zookeeper_security: &ZookeeperSecurity,
resolved_product_image: &ResolvedProductImage,
merged_config: &v1alpha1::ZookeeperConfig,
service_account: &ServiceAccount,
) -> Result<StatefulSet> {
let role = zk.role(zk_role).context(InternalOperatorFailureSnafu)?;
let rolegroup = zk
.rolegroup(rolegroup_ref)
.context(InternalOperatorFailureSnafu)?;
let logging = zk
.logging(zk_role, rolegroup_ref)
.context(CrdValidationFailureSnafu)?;
let env_vars = server_config
.get(&PropertyNameKind::Env)
.into_iter()
.flatten()
.map(|(k, v)| EnvVar {
name: k.clone(),
value: Some(v.clone()),
..EnvVar::default()
})
.collect::<Vec<_>>();
let (pvc, resources) = zk
.resources(zk_role, rolegroup_ref)
.context(CrdValidationFailureSnafu)?;
let mut cb_prepare =
ContainerBuilder::new("prepare").expect("invalid hard-coded container name");
let mut cb_zookeeper =
ContainerBuilder::new(APP_NAME).expect("invalid hard-coded container name");
let mut pod_builder = PodBuilder::new();
let requested_secret_lifetime = merged_config
.requested_secret_lifetime
.context(MissingSecretLifetimeSnafu)?;
// add volumes and mounts depending on tls / auth settings
zookeeper_security
.add_volume_mounts(
&mut pod_builder,
&mut cb_zookeeper,
&requested_secret_lifetime,
)
.context(AddTlsVolumeMountsSnafu)?;
let mut args = Vec::new();
if let Some(ContainerLogConfig {
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
}) = logging.containers.get(&v1alpha1::Container::Prepare)
{
args.push(product_logging::framework::capture_shell_output(
STACKABLE_LOG_DIR,
"prepare",
log_config,
));
}
args.extend(create_init_container_command_args());
let container_prepare = 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![args.join("\n")])
.add_env_vars(env_vars.clone())
.add_env_vars(vec![EnvVar {
name: "POD_NAME".to_string(),
value_from: Some(EnvVarSource {
field_ref: Some(ObjectFieldSelector {
api_version: Some("v1".to_string()),
field_path: "metadata.name".to_string(),
}),
..EnvVarSource::default()
}),
..EnvVar::default()
}])
.add_volume_mount("data", STACKABLE_DATA_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount("config", STACKABLE_CONFIG_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount("rwconfig", STACKABLE_RW_CONFIG_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount("log", STACKABLE_LOG_DIR)
.context(AddVolumeMountSnafu)?
.resources(
ResourceRequirementsBuilder::new()
.with_cpu_request("200m")
.with_cpu_limit("800m")
.with_memory_request("512Mi")
.with_memory_limit("512Mi")
.build(),
)
.build();
let container_zk = cb_zookeeper
.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![formatdoc! {"
{COMMON_BASH_TRAP_FUNCTIONS}
{remove_vector_shutdown_file_command}
prepare_signal_handlers
containerdebug --output={STACKABLE_LOG_DIR}/containerdebug-state.json --loop &
bin/zkServer.sh start-foreground {STACKABLE_RW_CONFIG_DIR}/zoo.cfg &
wait_for_termination $!
{create_vector_shutdown_file_command}
",
remove_vector_shutdown_file_command =
remove_vector_shutdown_file_command(STACKABLE_LOG_DIR),
create_vector_shutdown_file_command =
create_vector_shutdown_file_command(STACKABLE_LOG_DIR),
}])
.add_env_vars(env_vars)
.add_env_var(
"ZK_SERVER_HEAP",
construct_zk_server_heap_env(merged_config).context(ConstructJvmArgumentsSnafu)?,
)
.add_env_var(
"SERVER_JVMFLAGS",
construct_non_heap_jvm_args(zk, role, &rolegroup_ref.role_group)
.context(ConstructJvmArgumentsSnafu)?,
)
.add_env_var(
"CONTAINERDEBUG_LOG_DIRECTORY",
format!("{STACKABLE_LOG_DIR}/containerdebug"),
)
// Only allow the global load balancing service to send traffic to pods that are members of the quorum
// This also acts as a hint to the StatefulSet controller to wait for each pod to enter quorum before taking down the next
.readiness_probe(Probe {
exec: Some(ExecAction {
command: Some(vec![
"bash".to_string(),
"-c".to_string(),
// We don't have telnet or netcat in the container images, but
// we can use Bash's virtual /dev/tcp filesystem to accomplish the same thing
format!(
"exec 3<>/dev/tcp/127.0.0.1/{} && echo srvr >&3 && grep '^Mode: ' <&3",
zookeeper_security.client_port()
),
]),
}),
period_seconds: Some(1),
..Probe::default()
})
.add_container_port("zk", zookeeper_security.client_port().into())
.add_container_port("zk-leader", 2888)
.add_container_port("zk-election", 3888)
.add_container_port("metrics", 9505)
.add_volume_mount("data", STACKABLE_DATA_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount("config", STACKABLE_CONFIG_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount("log-config", STACKABLE_LOG_CONFIG_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount("rwconfig", STACKABLE_RW_CONFIG_DIR)
.context(AddVolumeMountSnafu)?
.add_volume_mount("log", STACKABLE_LOG_DIR)
.context(AddVolumeMountSnafu)?
.resources(resources)
.build();
let pb_metadata = ObjectMetaBuilder::new()
.with_recommended_labels(build_recommended_labels(
zk,
ZK_CONTROLLER_NAME,
&resolved_product_image.app_version_label,
&rolegroup_ref.role,
&rolegroup_ref.role_group,
))
.context(ObjectMetaSnafu)?
.build();
pod_builder
.metadata(pb_metadata)
.image_pull_secrets_from_product_image(resolved_product_image)
.add_init_container(container_prepare)
.add_container(container_zk)
.affinity(&merged_config.affinity)
.add_volume(Volume {
name: "config".to_string(),
config_map: Some(ConfigMapVolumeSource {
name: rolegroup_ref.object_name(),
..ConfigMapVolumeSource::default()
}),
..Volume::default()
})
.context(AddVolumeSnafu)?
.add_volume(Volume {
empty_dir: Some(EmptyDirVolumeSource {
medium: None,
size_limit: None,
}),
name: "rwconfig".to_string(),
..Volume::default()
})
.context(AddVolumeSnafu)?
.add_empty_dir_volume(
"log",
Some(product_logging::framework::calculate_log_volume_size_limit(
&[MAX_ZK_LOG_FILES_SIZE, MAX_PREPARE_LOG_FILE_SIZE],
)),
)
.context(AddVolumeSnafu)?
.security_context(PodSecurityContext {
run_as_user: Some(ZK_UID),
run_as_group: Some(0),
fs_group: Some(1000),
..PodSecurityContext::default()
})
.service_account_name(service_account.name_any());
if let Some(ContainerLogConfig {
choice:
Some(ContainerLogConfigChoice::Custom(CustomContainerLogConfig {
custom: ConfigMapLogConfig { config_map },
})),
}) = logging.containers.get(&v1alpha1::Container::Zookeeper)
{
pod_builder
.add_volume(Volume {
name: "log-config".to_string(),
config_map: Some(ConfigMapVolumeSource {
name: config_map.into(),
..ConfigMapVolumeSource::default()
}),
..Volume::default()
})
.context(AddVolumeSnafu)?;
} else {
pod_builder
.add_volume(Volume {
name: "log-config".to_string(),
config_map: Some(ConfigMapVolumeSource {
name: rolegroup_ref.object_name(),
..ConfigMapVolumeSource::default()