-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcompute.rs
More file actions
1068 lines (977 loc) · 38.4 KB
/
Copy pathcompute.rs
File metadata and controls
1068 lines (977 loc) · 38.4 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, HashSet};
use std::convert::Into;
use std::path::PathBuf;
use k8s_openapi::api::apps::v1::{StatefulSet, StatefulSetSpec, StatefulSetStatus};
use k8s_openapi::api::batch::v1::{Job, JobSpec};
use k8s_openapi::api::core::v1::{
ConfigMap, ConfigMapVolumeSource, Container, ContainerPort, EnvVar, EnvVarSource,
HTTPGetAction, ObjectFieldSelector, PersistentVolumeClaim, PersistentVolumeClaimSpec, Pod,
PodSecurityContext, PodSpec, PodTemplateSpec, Probe, SeccompProfile, SecurityContext, Service,
ServiceAccount, ServicePort, ServiceSpec, Toleration, Volume, VolumeMount,
VolumeResourceRequirements,
};
use k8s_openapi::api::policy::v1::{PodDisruptionBudget, PodDisruptionBudgetSpec};
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;
use kube::api::{DeleteParams, ListParams, Preconditions, PropagationPolicy};
use kube::core::PartialObjectMeta;
use kube::runtime::reflector::{ObjectRef, Store};
use kube::{
api::{Patch, PatchParams},
Api, ResourceExt,
};
use sha2::Digest;
use tracing::{debug, error, warn};
use crate::controllers::restatecluster::controller::Context;
use crate::resources::podidentityassociations::{
PodIdentityAssociation, PodIdentityAssociationSpec,
};
use crate::resources::restateclusters::{RestateClusterSpec, RestateClusterStorage};
use crate::resources::securitygrouppolicies::{
SecurityGroupPolicy, SecurityGroupPolicySecurityGroups, SecurityGroupPolicySpec,
};
use crate::Error;
use super::quantity_parser::QuantityParser;
use super::{label_selector, mandatory_labels, object_meta};
fn restate_service_account(
base_metadata: &ObjectMeta,
annotations: Option<&BTreeMap<String, String>>,
) -> ServiceAccount {
let mut metadata = object_meta(base_metadata, "restate");
if let Some(annotations) = annotations {
match &mut metadata.annotations {
Some(existing_annotations) => {
existing_annotations.extend(annotations.iter().map(|(k, v)| (k.clone(), v.clone())))
}
None => metadata.annotations = Some(annotations.clone()),
}
}
ServiceAccount {
metadata,
..Default::default()
}
}
fn restate_configmap(base_metadata: &ObjectMeta, config: Option<&str>) -> ConfigMap {
let config: String = config.unwrap_or_default().into();
let mut hasher = sha2::Sha256::new();
hasher.update(config.as_bytes());
let result = u32::from_le_bytes(hasher.finalize()[..4].try_into().unwrap());
let metadata = object_meta(base_metadata, format!("restate-config-{result:x}"));
ConfigMap {
metadata,
data: Some(BTreeMap::from([("config.toml".into(), config)])),
..Default::default()
}
}
fn restate_pod_identity_association(
ns: &str,
base_metadata: &ObjectMeta,
pod_identity_association_cluster: &str,
pod_identity_association_role_arn: &str,
) -> PodIdentityAssociation {
PodIdentityAssociation {
metadata: object_meta(base_metadata, "restate"),
spec: PodIdentityAssociationSpec {
cluster_name: Some(pod_identity_association_cluster.into()),
namespace: ns.into(),
service_account: "restate".into(),
role_arn: Some(pod_identity_association_role_arn.into()),
client_request_token: None,
cluster_ref: None,
role_ref: None,
tags: None,
},
status: None,
}
}
fn restate_security_group_policy(
base_metadata: &ObjectMeta,
aws_security_groups: &[String],
) -> SecurityGroupPolicy {
SecurityGroupPolicy {
metadata: object_meta(base_metadata, "restate"),
spec: SecurityGroupPolicySpec {
security_groups: Some(SecurityGroupPolicySecurityGroups {
group_ids: Some(aws_security_groups.into()),
}),
pod_selector: Some(label_selector(base_metadata)),
service_account_selector: None,
},
}
}
fn restate_service(
base_metadata: &ObjectMeta,
annotations: Option<&BTreeMap<String, String>>,
) -> Service {
let mut metadata = object_meta(base_metadata, "restate");
if let Some(annotations) = annotations {
match &mut metadata.annotations {
Some(existing_annotations) => {
existing_annotations.extend(annotations.iter().map(|(k, v)| (k.clone(), v.clone())))
}
None => metadata.annotations = Some(annotations.clone()),
}
}
Service {
metadata,
spec: Some(ServiceSpec {
selector: label_selector(base_metadata).match_labels,
ports: Some(vec![
ServicePort {
app_protocol: Some("kubernetes.io/h2c".into()),
port: 8080,
name: Some("ingress".into()),
..Default::default()
},
ServicePort {
app_protocol: Some("kubernetes.io/h2c".into()),
port: 9070,
name: Some("admin".into()),
..Default::default()
},
ServicePort {
app_protocol: Some("kubernetes.io/h2c".into()),
port: 5122,
name: Some("metrics".into()),
..Default::default()
},
]),
..Default::default()
}),
status: None,
}
}
fn restate_cluster_service(base_metadata: &ObjectMeta) -> Service {
Service {
metadata: object_meta(base_metadata, "restate-cluster"),
spec: Some(ServiceSpec {
selector: label_selector(base_metadata).match_labels,
ports: Some(vec![ServicePort {
app_protocol: Some("kubernetes.io/h2c".into()),
port: 5122,
name: Some("node".into()),
..Default::default()
}]),
// We want all pods in the StatefulSet to have their addresses published for
// the sake of the other Restate pods even before they're ready, since they
// have to be able to talk to each other in order to become ready.
publish_not_ready_addresses: Some(true),
cluster_ip: Some("None".into()), // headless service
..Default::default()
}),
status: None,
}
}
fn restate_pod_disruption_budget(base_metadata: &ObjectMeta) -> PodDisruptionBudget {
PodDisruptionBudget {
metadata: object_meta(base_metadata, "restate"),
spec: Some(PodDisruptionBudgetSpec {
// 1 is a sane default for clusters of all sizes:
// cluster size one it will allow downtime, but this is unavoidable when draining nodes
// cluster size of 3 with r=2, it will prevent rollouts leading to unavailability
// cluster size of 5 with r=3, it is conservative but not unreasonable
max_unavailable: Some(IntOrString::Int(1)),
min_available: None,
selector: Some(label_selector(base_metadata)),
unhealthy_pod_eviction_policy: None,
}),
status: None,
}
}
fn env(cluster_name: &str, custom: Option<&[EnvVar]>) -> Vec<EnvVar> {
let defaults = [
("RESTATE_LOG_FORMAT", "json"),
("RESTATE_CLUSTER_NAME", cluster_name),
("RESTATE_BASE_DIR", "/restate-data"),
("RUST_BACKTRACE", "1"),
("RUST_LIB_BACKTRACE", "0"),
("RESTATE_CONFIG", "/config/config.toml"),
(
"RESTATE_ADVERTISED_ADDRESS",
// POD_NAME comes from the downward api, below
"http://$(POD_NAME).restate-cluster:5122",
),
(
"RESTATE_NODE_NAME",
// POD_NAME comes from the downward api, below
"$(POD_NAME)",
),
];
// allow crd to override our defaults
let custom_names: HashSet<&str> = custom
.map(|custom| custom.iter().map(|e| e.name.as_ref()).collect())
.unwrap_or_default();
let defaults = defaults
.into_iter()
.filter(|(k, _)| !custom_names.contains(k))
.map(|(k, v)| EnvVar {
name: k.into(),
value: Some(v.into()),
value_from: None,
});
let defaults = Some(EnvVar {
name: "POD_NAME".into(),
value: None,
value_from: Some(EnvVarSource {
config_map_key_ref: None,
field_ref: Some(ObjectFieldSelector {
api_version: None,
field_path: "metadata.name".into(),
}),
resource_field_ref: None,
secret_key_ref: None,
}),
})
.into_iter()
.chain(defaults);
if let Some(custom) = custom {
defaults.chain(custom.iter().cloned()).collect()
} else {
defaults.collect()
}
}
const RESTATE_STATEFULSET_NAME: &str = "restate";
fn restate_statefulset(
base_metadata: &ObjectMeta,
spec: &RestateClusterSpec,
pod_annotations: Option<BTreeMap<String, String>>,
signing_key: Option<(Volume, PathBuf)>,
cm_name: String,
) -> StatefulSet {
let metadata = object_meta(base_metadata, RESTATE_STATEFULSET_NAME);
let labels = metadata.labels.clone();
let pod_annotations = match (pod_annotations, metadata.annotations.clone()) {
(Some(pod_annotations), Some(mut base_annotations)) => {
base_annotations.extend(pod_annotations);
Some(base_annotations)
}
(Some(annotations), None) | (None, Some(annotations)) => Some(annotations),
(None, None) => None,
};
let mut volume_mounts = vec![
VolumeMount {
name: "storage".into(),
mount_path: "/restate-data".into(),
..Default::default()
},
VolumeMount {
name: "tmp".into(),
mount_path: "/tmp".into(),
..Default::default()
},
VolumeMount {
name: "config".into(),
mount_path: "/config".into(),
read_only: Some(true),
..Default::default()
},
];
let mut volumes = vec![
Volume {
name: "tmp".into(),
empty_dir: Some(Default::default()),
..Default::default()
},
Volume {
name: "config".into(),
config_map: Some(ConfigMapVolumeSource {
name: cm_name,
..Default::default()
}),
..Default::default()
},
];
let mut env = env(
spec.cluster_name
.as_ref()
.or(base_metadata.name.as_ref())
.unwrap(),
spec.compute.env.as_deref(),
);
if let Some((volume, relative_path)) = signing_key {
let mut absolute_path = PathBuf::from("/signing-key");
volume_mounts.push(VolumeMount {
mount_path: absolute_path.to_str().unwrap().into(),
name: volume.name.clone(),
read_only: Some(true),
..Default::default()
});
volumes.push(volume);
absolute_path.push(relative_path);
env.push(EnvVar {
name: "RESTATE_REQUEST_IDENTITY_PRIVATE_KEY_PEM_FILE".into(),
value: Some(absolute_path.to_str().unwrap().into()),
value_from: None,
})
}
StatefulSet {
metadata,
spec: Some(StatefulSetSpec {
replicas: spec.compute.replicas,
selector: label_selector(base_metadata),
service_name: "restate-cluster".into(),
template: PodTemplateSpec {
metadata: Some(ObjectMeta {
labels,
annotations: pod_annotations,
..Default::default()
}),
spec: Some(PodSpec {
affinity: spec.compute.affinity.clone(),
automount_service_account_token: Some(false),
dns_policy: spec.compute.dns_policy.clone(),
dns_config: spec.compute.dns_config.clone(),
image_pull_secrets: spec.compute.image_pull_secrets.clone(),
containers: vec![Container {
name: "restate".into(),
image: Some(spec.compute.image.clone()),
image_pull_policy: spec.compute.image_pull_policy.clone(),
env: Some(env),
ports: Some(vec![
ContainerPort {
name: Some("ingress".into()),
container_port: 8080,
..Default::default()
},
ContainerPort {
name: Some("admin".into()),
container_port: 9070,
..Default::default()
},
ContainerPort {
name: Some("metrics".into()),
container_port: 5122,
..Default::default()
},
]),
readiness_probe: Some(Probe {
http_get: Some(HTTPGetAction {
port: IntOrString::Int(9070),
path: Some("/health".into()),
..Default::default()
}),
..Default::default()
}),
resources: spec.compute.resources.clone(),
security_context: Some(SecurityContext {
read_only_root_filesystem: Some(true),
allow_privilege_escalation: Some(false),
..Default::default()
}),
volume_mounts: Some(volume_mounts),
..Default::default()
}],
security_context: Some(PodSecurityContext {
run_as_user: Some(1000),
run_as_group: Some(3000),
fs_group: Some(2000),
fs_group_change_policy: Some("OnRootMismatch".into()),
seccomp_profile: Some(SeccompProfile {
type_: "RuntimeDefault".into(),
localhost_profile: None,
}),
..Default::default()
}),
service_account_name: Some("restate".into()),
termination_grace_period_seconds: Some(60),
volumes: Some(volumes),
tolerations: spec.compute.tolerations.clone(),
node_selector: spec.compute.node_selector.clone(),
..Default::default()
}),
},
// It's important to start multiple pods at the same time in case multiple pods died.
// Otherwise, we risk unavailability of an already configured metadata cluster
pod_management_policy: Some("Parallel".to_owned()),
volume_claim_templates: Some(vec![PersistentVolumeClaim {
metadata: ObjectMeta {
name: Some("storage".into()),
labels: Some(mandatory_labels(base_metadata)), // caution needed; these cannot be changed
..Default::default()
},
spec: Some(PersistentVolumeClaimSpec {
storage_class_name: spec.storage.storage_class_name.clone(),
volume_attributes_class_name: spec.storage.volume_attributes_class_name.clone(),
access_modes: Some(vec!["ReadWriteOnce".into()]),
resources: Some(restate_pvc_resources(&spec.storage)),
..Default::default()
}),
status: None,
}]),
..Default::default()
}),
status: None,
}
}
fn restate_pvc_resources(storage: &RestateClusterStorage) -> VolumeResourceRequirements {
VolumeResourceRequirements {
requests: Some(BTreeMap::from([(
"storage".to_string(),
Quantity(format!("{}", storage.storage_request_bytes)),
)])),
limits: None,
}
}
pub async fn reconcile_compute(
ctx: &Context,
namespace: &str,
base_metadata: &ObjectMeta,
spec: &RestateClusterSpec,
signing_key: Option<(Volume, PathBuf)>,
) -> Result<(), Error> {
let ss_api: Api<StatefulSet> = Api::namespaced(ctx.client.clone(), namespace);
let cm_api: Api<ConfigMap> = Api::namespaced(ctx.client.clone(), namespace);
let pvc_api: Api<PersistentVolumeClaim> = Api::namespaced(ctx.client.clone(), namespace);
let svc_api: Api<Service> = Api::namespaced(ctx.client.clone(), namespace);
let svcacc_api: Api<ServiceAccount> = Api::namespaced(ctx.client.clone(), namespace);
let pia_api: Api<PodIdentityAssociation> = Api::namespaced(ctx.client.clone(), namespace);
let job_api: Api<Job> = Api::namespaced(ctx.client.clone(), namespace);
let pod_api: Api<Pod> = Api::namespaced(ctx.client.clone(), namespace);
let sgp_api: Api<SecurityGroupPolicy> = Api::namespaced(ctx.client.clone(), namespace);
let pdb_api: Api<PodDisruptionBudget> = Api::namespaced(ctx.client.clone(), namespace);
apply_service_account(
namespace,
&svcacc_api,
restate_service_account(
base_metadata,
spec.security
.as_ref()
.and_then(|s| s.service_account_annotations.as_ref()),
),
)
.await?;
let cm = restate_configmap(base_metadata, spec.config.as_deref());
let cm_name: String = cm.metadata.name.as_ref().unwrap().into();
apply_configmap(namespace, &cm_api, cm).await?;
let mut pod_annotations: Option<BTreeMap<String, String>> = None;
match (
ctx.aws_pod_identity_association_cluster.as_ref(),
spec.security
.as_ref()
.and_then(|s| s.aws_pod_identity_association_role_arn.as_ref()),
) {
(
Some(aws_pod_identity_association_cluster),
Some(aws_pod_identity_association_role_arn),
) => {
let pia = apply_pod_identity_association(
namespace,
&pia_api,
restate_pod_identity_association(
namespace,
base_metadata,
aws_pod_identity_association_cluster,
aws_pod_identity_association_role_arn,
),
)
.await?;
if !is_pod_identity_association_synced(pia) {
return Err(Error::NotReady { reason: "PodIdentityAssociationNotSynced".into(), message: "Waiting for the AWS ACK controller to provision the Pod Identity Association with IAM".into(), requeue_after: None });
}
check_pia(
namespace,
base_metadata,
spec.compute.tolerations.as_ref(),
&job_api,
&pod_api,
)
.await?;
// Pods MUST roll when these change, so we will apply these parameters as annotations to the pod meta
let pod_annotations = pod_annotations.get_or_insert_with(Default::default);
pod_annotations.insert(
"restate.dev/aws-pod-identity-association-cluster".into(),
aws_pod_identity_association_cluster.to_owned(),
);
pod_annotations.insert(
"restate.dev/aws-pod-identity-association-role-arn".into(),
aws_pod_identity_association_role_arn.to_owned(),
);
}
(Some(_), None) => {
delete_pod_identity_association(namespace, &pia_api, "restate").await?;
delete_job(namespace, &job_api, "restate-pia-canary").await?;
}
(None, Some(aws_pod_identity_association_role_arn)) => {
warn!("Ignoring AWS pod identity association role ARN {aws_pod_identity_association_role_arn} as the operator is not configured with --aws-pod-identity-association-cluster");
}
(None, None) => {}
};
match spec
.security
.as_ref()
.and_then(|s| s.aws_pod_security_groups.as_deref())
{
Some(aws_pod_security_groups)
if ctx.security_group_policy_installed && !aws_pod_security_groups.is_empty() =>
{
apply_security_group_policy(
namespace,
&sgp_api,
restate_security_group_policy(base_metadata, aws_pod_security_groups),
)
.await?;
let pod_annotations = pod_annotations.get_or_insert_with(Default::default);
// Pods MUST roll when these change, so we will apply the groups as annotations to the pod meta
pod_annotations.insert(
"restate.dev/aws-security-groups".into(),
aws_pod_security_groups.join(","),
);
}
None | Some(_) if ctx.security_group_policy_installed => {
delete_security_group_policy(namespace, &sgp_api, "restate").await?;
}
Some(aws_pod_security_groups) if !aws_pod_security_groups.is_empty() => {
warn!("Ignoring AWS pod security groups {} as the SecurityGroupPolicy CRD is not installed", aws_pod_security_groups.join(","));
}
None | Some(_) => {}
}
let restate_service = restate_service(
base_metadata,
spec.security
.as_ref()
.and_then(|s| s.service_annotations.as_ref()),
);
apply_service(namespace, &svc_api, restate_service).await?;
let restate_cluster_service = restate_cluster_service(base_metadata);
apply_service(namespace, &svc_api, restate_cluster_service).await?;
apply_pod_disruption_budget(
namespace,
&pdb_api,
restate_pod_disruption_budget(base_metadata),
)
.await?;
change_statefulset_storage(
namespace,
base_metadata,
&ss_api,
&ctx.ss_store,
&pvc_api,
&ctx.pvc_meta_store,
&spec.storage,
)
.await?;
let ss = apply_stateful_set(
namespace,
&ss_api,
restate_statefulset(base_metadata, spec, pod_annotations, signing_key, cm_name),
)
.await?;
validate_stateful_set_status(ss.status, spec.compute.replicas.unwrap_or(1))?;
Ok(())
}
async fn apply_service(namespace: &str, ss_api: &Api<Service>, ss: Service) -> Result<(), Error> {
let name = ss.metadata.name.as_ref().unwrap();
let params: PatchParams = PatchParams::apply("restate-operator").force();
debug!("Applying Service {} in namespace {}", name, namespace);
ss_api.patch(name, ¶ms, &Patch::Apply(&ss)).await?;
Ok(())
}
async fn apply_service_account(
namespace: &str,
svcacc_api: &Api<ServiceAccount>,
svcacc: ServiceAccount,
) -> Result<(), Error> {
let name = svcacc.metadata.name.as_ref().unwrap();
let params: PatchParams = PatchParams::apply("restate-operator").force();
debug!(
"Applying ServiceAccount {} in namespace {}",
name, namespace
);
svcacc_api
.patch(name, ¶ms, &Patch::Apply(&svcacc))
.await?;
Ok(())
}
async fn apply_configmap(
namespace: &str,
cm_api: &Api<ConfigMap>,
cm: ConfigMap,
) -> Result<(), Error> {
let name = cm.metadata.name.as_ref().unwrap();
let params: PatchParams = PatchParams::apply("restate-operator").force();
debug!("Applying ConfigMap {} in namespace {}", name, namespace);
cm_api.patch(name, ¶ms, &Patch::Apply(&cm)).await?;
Ok(())
}
async fn apply_pod_identity_association(
namespace: &str,
pia_api: &Api<PodIdentityAssociation>,
pia: PodIdentityAssociation,
) -> Result<PodIdentityAssociation, Error> {
let name = pia.metadata.name.as_ref().unwrap();
let params: PatchParams = PatchParams::apply("restate-operator").force();
debug!(
"Applying PodIdentityAssociation {} in namespace {}",
name, namespace
);
Ok(pia_api.patch(name, ¶ms, &Patch::Apply(&pia)).await?)
}
async fn check_pia(
namespace: &str,
base_metadata: &ObjectMeta,
tolerations: Option<&Vec<Toleration>>,
job_api: &Api<Job>,
pod_api: &Api<Pod>,
) -> Result<(), Error> {
let name = "restate-pia-canary";
let params: PatchParams = PatchParams::apply("restate-operator").force();
let mut metadata = object_meta(base_metadata, name);
let labels = metadata.labels.get_or_insert(Default::default());
if let Some(existing) = labels.get_mut("app.kubernetes.io/name") {
*existing = name.into()
} else {
labels.insert("app.kubernetes.io/name".into(), name.into());
}
debug!(
"Applying PodIdentityAssociation canary Job in namespace {}",
namespace
);
let created = job_api
.patch(
name,
¶ms,
&Patch::Apply(&Job {
metadata,
spec: Some(JobSpec {
// single-use job that we delete on failuire; don't want to wait 10 seconds for retries
backoff_limit: Some(1),
template: PodTemplateSpec {
metadata: None,
spec: Some(PodSpec {
service_account_name: Some("restate".into()),
containers: vec![Container {
name: "canary".into(),
image: Some("busybox:uclibc".into()),
command: Some(vec![
"grep".into(),
"-q".into(),
"AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE".into(),
"/proc/self/environ".into(),
]),
..Default::default()
}],
tolerations: tolerations.cloned(),
restart_policy: Some("Never".into()),
..Default::default()
}),
},
..Default::default()
}),
status: None,
}),
)
.await?;
if let Some(conditions) = created.status.and_then(|s| s.conditions) {
for condition in conditions {
if condition.status != "True" {
continue;
}
match condition.type_.as_str() {
"Complete" => {
debug!(
"PodIdentityAssociation canary check succeeded in namespace {}",
namespace
);
return Ok(());
}
"Failed" => {
error!(
"PodIdentityAssociation canary check failed in namespace {}, deleting Job",
namespace
);
delete_job(namespace, job_api, name).await?;
return Err(Error::NotReady {
reason: "PodIdentityAssociationCanaryFailed".into(),
message: "Canary pod did not receive Pod Identity credentials; PIA webhook may need to catch up".into(),
// job watch will cover this
requeue_after: None,
});
}
_ => {}
}
}
}
// if we are here then the job hasn't succeeded or failed yet; lets try and figure things out a bit quicker
// because it takes times for pods to schedule etc
let pods = pod_api
.list(&ListParams::default().labels(&format!(
"batch.kubernetes.io/job-name={name},batch.kubernetes.io/controller-uid={}",
created.metadata.uid.unwrap()
)))
.await?;
if let Some(pod) = pods.items.first() {
if pod
.spec
.as_ref()
.and_then(|s| s.volumes.as_ref())
.map(|vs| vs.iter().any(|v| v.name == "eks-pod-identity-token"))
.unwrap_or(false)
{
debug!(
"PodIdentityAssociation canary check succeeded via pod lookup in namespace {}",
namespace
);
return Ok(());
}
debug!(
"PodIdentityAssociation canary check failed via pod lookup in namespace {}, deleting Job",
namespace
);
delete_job(namespace, job_api, name).await?;
return Err(Error::NotReady {
reason: "PodIdentityAssociationCanaryFailed".into(),
message: "Canary pod did not receive Pod Identity credentials; PIA webhook may need to catch up".into(),
// job watch will cover this
requeue_after: None,
});
}
// no pods; we generally expect this immediately after creating the job
debug!(
"PodIdentityAssociation canary Job not yet succeeded in namespace {}",
namespace
);
Err(Error::NotReady {
reason: "PodIdentityAssociationCanaryPending".into(),
message: "Canary Job has not yet succeeded; PIA webhook may need to catch up".into(),
// job watch will cover this
requeue_after: None,
})
}
fn is_pod_identity_association_synced(pia: PodIdentityAssociation) -> bool {
if let Some(status) = pia.status {
if let Some(conditions) = status.conditions {
if let Some(synced) = conditions
.iter()
.find(|cond| cond.r#type == "ACK.ResourceSynced")
{
if synced.status == "True" {
return true;
}
}
}
}
false
}
async fn delete_pod_identity_association(
namespace: &str,
pia_api: &Api<PodIdentityAssociation>,
name: &str,
) -> Result<(), Error> {
debug!(
"Ensuring PodIdentityAssociation {} in namespace {} does not exist",
name, namespace
);
match pia_api.delete(name, &DeleteParams::default()).await {
Err(kube::Error::Api(kube::error::ErrorResponse { code: 404, .. })) => Ok(()),
Err(err) => Err(err.into()),
Ok(_) => Ok(()),
}
}
async fn delete_job(namespace: &str, job_api: &Api<Job>, name: &str) -> Result<(), Error> {
debug!(
"Ensuring Job {} in namespace {} does not exist",
name, namespace
);
match job_api.delete(name, &DeleteParams::default()).await {
Err(kube::Error::Api(kube::error::ErrorResponse { code: 404, .. })) => Ok(()),
Err(err) => Err(err.into()),
Ok(_) => Ok(()),
}
}
async fn apply_security_group_policy(
namespace: &str,
pia_api: &Api<SecurityGroupPolicy>,
pia: SecurityGroupPolicy,
) -> Result<(), Error> {
let name = pia.metadata.name.as_ref().unwrap();
let params: PatchParams = PatchParams::apply("restate-operator").force();
debug!(
"Applying SecurityGroupPolicy {} in namespace {}",
name, namespace
);
pia_api.patch(name, ¶ms, &Patch::Apply(&pia)).await?;
Ok(())
}
async fn delete_security_group_policy(
namespace: &str,
sgp_api: &Api<SecurityGroupPolicy>,
name: &str,
) -> Result<(), Error> {
debug!(
"Ensuring SecurityGroupPolicy {} in namespace {} does not exist",
name, namespace
);
match sgp_api.delete(name, &DeleteParams::default()).await {
Err(kube::Error::Api(kube::error::ErrorResponse { code: 404, .. })) => Ok(()),
Err(err) => Err(err.into()),
Ok(_) => Ok(()),
}
}
async fn change_statefulset_storage(
namespace: &str,
base_metadata: &ObjectMeta,
ss_api: &Api<StatefulSet>,
ss_store: &Store<StatefulSet>,
pvc_api: &Api<PersistentVolumeClaim>,
pvc_meta_store: &Store<PartialObjectMeta<PersistentVolumeClaim>>,
storage: &RestateClusterStorage,
) -> Result<(), Error> {
let params: PatchParams = PatchParams::apply("restate-operator").force();
let resources = Some(restate_pvc_resources(storage));
// ensure all existing pvcs have the right size and VAC set
// first, filter the pvc meta store for our label selector
let labels = mandatory_labels(base_metadata);
let pvcs = pvc_meta_store.state().into_iter().filter(|pvc_meta| {
for (k, v) in &labels {
if pvc_meta.labels().get(k) != Some(v) {
return false;
}
}
true
});
for pvc in pvcs {
let name = pvc.name_any();
debug!(
"Applying PersistentVolumeClaim {} in namespace {}",
name, namespace
);
let mut patched_spec = PersistentVolumeClaimSpec {
resources: resources.clone(),
..Default::default()
};
// if we don't have a particular vac to set, don't update an existing one as it can be defaulted by the CSI driver
if let Some(volume_attributes_class_name) = &storage.volume_attributes_class_name {
patched_spec.volume_attributes_class_name = Some(volume_attributes_class_name.clone());
}
let pvc = pvc_api
.patch(
&name,
¶ms,
&Patch::Apply(PersistentVolumeClaim {
metadata: ObjectMeta {
name: Some(name.clone()),
..Default::default()
},
spec: Some(patched_spec),
status: None,
}),
)
.await?;
if pvc.status.and_then(|s| s.phase).as_deref() != Some("Bound") {
return Err(Error::NotReady {
reason: "PersistentVolumeClaimNotBound".into(),
message: format!(
"PersistentVolumeClaim {} is not yet bound to a volume",
name
),
requeue_after: None,
});
}
}
let existing = match ss_store.get(&ObjectRef::new(RESTATE_STATEFULSET_NAME).within(namespace)) {
Some(existing) => existing,
// no statefulset in cache; possibilities:
// 1. first run and it hasn't ever been created => do nothing
// 3. we deleted it in a previous reconciliation, and the cache reflects this => do nothing
// 2. it has just been created, but cache doesn't have it yet. we'll reconcile again when it enters cache => do nothing
None => return Ok(()),
};
let existing_storage_spec = existing
.spec
.as_ref()
.and_then(|spec| spec.volume_claim_templates.as_ref())
.and_then(|templates| templates.first())
.and_then(|storage| storage.spec.as_ref());
let existing_storage_request = existing_storage_spec
.and_then(|spec| spec.resources.as_ref())
.and_then(|resources| resources.requests.as_ref())
.and_then(|requests| requests.get("storage").map(|storage| storage.to_bytes()));
let existing_vac =
existing_storage_spec.and_then(|spec| spec.volume_attributes_class_name.as_deref());
match existing_storage_request {
// check if we can interpret the statefulset as having the same storage request and the same vac
Some(Ok(Some(bytes)))
if bytes == storage.storage_request_bytes
&& existing_vac == storage.volume_attributes_class_name.as_deref() =>
{
return Ok(())
}
_ => {}
}
debug!(
"Deleting (with orphan propagation policy) StatefulSet {} in namespace {}",
RESTATE_STATEFULSET_NAME, namespace
);
// expansion case or vac change case - we would have failed when updating the pvcs if this was a contraction
// we have already updated the pvcs, we just need to delete and recreate the statefulset
// we *must* delete with an orphan propagation policy; this means the deletion will *not* cascade down
// to the pods that this statefulset owns.
// recreation will happen later in the reconcile loop
ss_api
.delete(
RESTATE_STATEFULSET_NAME,
&DeleteParams {
propagation_policy: Some(PropagationPolicy::Orphan),
grace_period_seconds: Some(0),
preconditions: Some(Preconditions {