-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapply.rs
More file actions
130 lines (109 loc) · 3.99 KB
/
Copy pathapply.rs
File metadata and controls
130 lines (109 loc) · 3.99 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
//! The apply step in the OpenSearchCluster controller
use std::marker::PhantomData;
use snafu::{ResultExt, Snafu};
use stackable_operator::{
client::Client,
cluster_resources::{ClusterResource, ClusterResourceApplyStrategy, ClusterResources},
deep_merger::ObjectOverrides,
v2::{
cluster_resources::cluster_resources_new,
types::{
kubernetes::{NamespaceName, Uid},
operator::ClusterName,
},
},
};
use strum::{EnumDiscriminants, IntoStaticStr};
use super::{Applied, ContextNames, KubernetesResources, Prepared};
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
pub enum Error {
#[snafu(display("failed to apply resource"))]
ApplyResource {
source: stackable_operator::cluster_resources::Error,
},
#[snafu(display("failed to delete orphaned resources"))]
DeleteOrphanedResources {
source: stackable_operator::cluster_resources::Error,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
/// Applier for the Kubernetes resource specifications produced by this controller
///
/// The implementation is not tied to this controller and could theoretically be moved to
/// stackable_operator if [`KubernetesResources`] would contain all possible resource types.
pub struct Applier<'a> {
client: &'a Client,
cluster_resources: ClusterResources<'a>,
}
impl<'a> Applier<'a> {
pub fn new(
client: &'a Client,
names: &ContextNames,
cluster_name: &ClusterName,
cluster_namespace: &NamespaceName,
cluster_uid: &Uid,
apply_strategy: ClusterResourceApplyStrategy,
object_overrides: &'a ObjectOverrides,
) -> Applier<'a> {
let cluster_resources = cluster_resources_new(
&names.product_name,
&names.operator_name,
&names.controller_name,
cluster_name,
cluster_namespace,
cluster_uid,
apply_strategy,
object_overrides,
);
Applier {
client,
cluster_resources,
}
}
/// Applies the given Kubernetes resources and marks them as applied
pub async fn apply(
mut self,
resources: KubernetesResources<Prepared>,
) -> Result<KubernetesResources<Applied>> {
let services = self.add_resources(resources.services).await?;
let listeners = self.add_resources(resources.listeners).await?;
let config_maps = self.add_resources(resources.config_maps).await?;
let service_accounts = self.add_resources(resources.service_accounts).await?;
let role_bindings = self.add_resources(resources.role_bindings).await?;
let pod_disruption_budgets = self.add_resources(resources.pod_disruption_budgets).await?;
// 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.
let stateful_sets = self.add_resources(resources.stateful_sets).await?;
self.cluster_resources
.delete_orphaned_resources(self.client)
.await
.context(DeleteOrphanedResourcesSnafu)?;
Ok(KubernetesResources {
stateful_sets,
services,
listeners,
config_maps,
service_accounts,
role_bindings,
pod_disruption_budgets,
status: PhantomData,
})
}
async fn add_resources<T: ClusterResource + Sync>(
&mut self,
resources: Vec<T>,
) -> Result<Vec<T>> {
let mut applied_resources = vec![];
for resource in resources {
let applied_resource = self
.cluster_resources
.add(self.client, resource)
.await
.context(ApplyResourceSnafu)?;
applied_resources.push(applied_resource);
}
Ok(applied_resources)
}
}