Skip to content

Commit 025722e

Browse files
committed
Rust 2024 let-chains to simplify wait Conditions
started out as a PR to try out let-chains, but found that it solves the ergonomic problem with the wait API that I tried to solve earlier in #1498 Signed-off-by: clux <sszynrae@gmail.com>
1 parent aaf6bac commit 025722e

File tree

3 files changed

+63
-78
lines changed

3 files changed

+63
-78
lines changed

kube-client/src/client/auth/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ impl TryFrom<&AuthInfo> for Auth {
356356
.transpose()
357357
.map_err(Error::MalformedTokenExpirationDate)?;
358358

359-
360359
if let (Some(client_certificate_data), Some(client_key_data)) =
361360
(status.client_certificate_data, status.client_key_data)
362361
{
@@ -431,14 +430,14 @@ fn token_from_gcp_provider(provider: &AuthProviderConfig) -> Result<ProviderToke
431430
}
432431

433432
// Return cached access token if it's still valid
434-
if let Some(access_token) = provider.config.get("access-token") {
435-
if let Some(expiry) = provider.config.get("expiry") {
436-
let expiry_date = expiry
437-
.parse::<DateTime<Utc>>()
438-
.map_err(Error::MalformedTokenExpirationDate)?;
439-
if Utc::now() + SIXTY_SEC < expiry_date {
440-
return Ok(ProviderToken::GcpCommand(access_token.clone(), Some(expiry_date)));
441-
}
433+
if let Some(access_token) = provider.config.get("access-token")
434+
&& let Some(expiry) = provider.config.get("expiry")
435+
{
436+
let expiry_date = expiry
437+
.parse::<DateTime<Utc>>()
438+
.map_err(Error::MalformedTokenExpirationDate)?;
439+
if Utc::now() + SIXTY_SEC < expiry_date {
440+
return Ok(ProviderToken::GcpCommand(access_token.clone(), Some(expiry_date)));
442441
}
443442
}
444443

kube-runtime/src/wait.rs

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ where
7979
/// use k8s_openapi::api::core::v1::Pod;
8080
/// fn my_custom_condition(my_cond: &str) -> impl Condition<Pod> + '_ {
8181
/// move |obj: Option<&Pod>| {
82-
/// if let Some(pod) = &obj {
83-
/// if let Some(status) = &pod.status {
84-
/// if let Some(conds) = &status.conditions {
85-
/// if let Some(pcond) = conds.iter().find(|c| c.type_ == my_cond) {
86-
/// return pcond.status == "True";
87-
/// }
88-
/// }
89-
/// }
82+
/// if let Some(pod) = &obj
83+
/// && let Some(status) = &pod.status
84+
/// && let Some(conds) = &status.conditions
85+
/// && let Some(pcond) = conds.iter().find(|c| c.type_ == my_cond)
86+
/// {
87+
/// return pcond.status == "True";
9088
/// }
9189
/// false
9290
/// }
@@ -196,14 +194,12 @@ pub mod conditions {
196194
#[must_use]
197195
pub fn is_crd_established() -> impl Condition<CustomResourceDefinition> {
198196
|obj: Option<&CustomResourceDefinition>| {
199-
if let Some(o) = obj {
200-
if let Some(s) = &o.status {
201-
if let Some(conds) = &s.conditions {
202-
if let Some(pcond) = conds.iter().find(|c| c.type_ == "Established") {
203-
return pcond.status == "True";
204-
}
205-
}
206-
}
197+
if let Some(o) = obj
198+
&& let Some(s) = &o.status
199+
&& let Some(conds) = &s.conditions
200+
&& let Some(pcond) = conds.iter().find(|c| c.type_ == "Established")
201+
{
202+
return pcond.status == "True";
207203
}
208204
false
209205
}
@@ -213,12 +209,11 @@ pub mod conditions {
213209
#[must_use]
214210
pub fn is_pod_running() -> impl Condition<Pod> {
215211
|obj: Option<&Pod>| {
216-
if let Some(pod) = &obj {
217-
if let Some(status) = &pod.status {
218-
if let Some(phase) = &status.phase {
219-
return phase == "Running";
220-
}
221-
}
212+
if let Some(pod) = &obj
213+
&& let Some(status) = &pod.status
214+
&& let Some(phase) = &status.phase
215+
{
216+
return phase == "Running";
222217
}
223218
false
224219
}
@@ -228,14 +223,12 @@ pub mod conditions {
228223
#[must_use]
229224
pub fn is_job_completed() -> impl Condition<Job> {
230225
|obj: Option<&Job>| {
231-
if let Some(job) = &obj {
232-
if let Some(s) = &job.status {
233-
if let Some(conds) = &s.conditions {
234-
if let Some(pcond) = conds.iter().find(|c| c.type_ == "Complete") {
235-
return pcond.status == "True";
236-
}
237-
}
238-
}
226+
if let Some(job) = &obj
227+
&& let Some(s) = &job.status
228+
&& let Some(conds) = &s.conditions
229+
&& let Some(pcond) = conds.iter().find(|c| c.type_ == "Complete")
230+
{
231+
return pcond.status == "True";
239232
}
240233
false
241234
}
@@ -248,16 +241,14 @@ pub mod conditions {
248241
#[must_use]
249242
pub fn is_deployment_completed() -> impl Condition<Deployment> {
250243
|obj: Option<&Deployment>| {
251-
if let Some(depl) = &obj {
252-
if let Some(s) = &depl.status {
253-
if let Some(conds) = &s.conditions {
254-
if let Some(dcond) = conds.iter().find(|c| {
255-
c.type_ == "Progressing" && c.reason == Some("NewReplicaSetAvailable".to_string())
256-
}) {
257-
return dcond.status == "True";
258-
}
259-
}
260-
}
244+
if let Some(depl) = &obj
245+
&& let Some(s) = &depl.status
246+
&& let Some(conds) = &s.conditions
247+
&& let Some(dcond) = conds.iter().find(|c| {
248+
c.type_ == "Progressing" && c.reason == Some("NewReplicaSetAvailable".to_string())
249+
})
250+
{
251+
return dcond.status == "True";
261252
}
262253
false
263254
}
@@ -267,20 +258,19 @@ pub mod conditions {
267258
#[must_use]
268259
pub fn is_service_loadbalancer_provisioned() -> impl Condition<Service> {
269260
|obj: Option<&Service>| {
270-
if let Some(svc) = &obj {
261+
if let Some(svc) = &obj
262+
&& let Some(spec) = &svc.spec
263+
{
271264
// ignore services that are not type LoadBalancer (return true immediately)
272-
if let Some(spec) = &svc.spec {
273-
if spec.type_ != Some("LoadBalancer".to_string()) {
274-
return true;
275-
}
276-
// carry on if this is a LoadBalancer service
277-
if let Some(s) = &svc.status {
278-
if let Some(lbs) = &s.load_balancer {
279-
if let Some(ings) = &lbs.ingress {
280-
return ings.iter().all(|ip| ip.ip.is_some() || ip.hostname.is_some());
281-
}
282-
}
283-
}
265+
if spec.type_ != Some("LoadBalancer".to_string()) {
266+
return true;
267+
}
268+
// carry on if this is a LoadBalancer service
269+
if let Some(s) = &svc.status
270+
&& let Some(lbs) = &s.load_balancer
271+
&& let Some(ings) = &lbs.ingress
272+
{
273+
return ings.iter().all(|ip| ip.ip.is_some() || ip.hostname.is_some());
284274
}
285275
}
286276
false
@@ -291,14 +281,12 @@ pub mod conditions {
291281
#[must_use]
292282
pub fn is_ingress_provisioned() -> impl Condition<Ingress> {
293283
|obj: Option<&Ingress>| {
294-
if let Some(ing) = &obj {
295-
if let Some(s) = &ing.status {
296-
if let Some(lbs) = &s.load_balancer {
297-
if let Some(ings) = &lbs.ingress {
298-
return ings.iter().all(|ip| ip.ip.is_some() || ip.hostname.is_some());
299-
}
300-
}
301-
}
284+
if let Some(ing) = &obj
285+
&& let Some(s) = &ing.status
286+
&& let Some(lbs) = &s.load_balancer
287+
&& let Some(ings) = &lbs.ingress
288+
{
289+
return ings.iter().all(|ip| ip.ip.is_some() || ip.hostname.is_some());
302290
}
303291
false
304292
}

kube/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,12 @@ mod test {
519519
// TODO: remove these once we can write these functions generically
520520
fn is_each_container_ready() -> impl Condition<Pod> {
521521
|obj: Option<&Pod>| {
522-
if let Some(o) = obj {
523-
if let Some(s) = &o.status {
524-
if let Some(conds) = &s.conditions {
525-
if let Some(pcond) = conds.iter().find(|c| c.type_ == "ContainersReady") {
526-
return pcond.status == "True";
527-
}
528-
}
529-
}
522+
if let Some(o) = obj
523+
&& let Some(s) = &o.status
524+
&& let Some(conds) = &s.conditions
525+
&& let Some(pcond) = conds.iter().find(|c| c.type_ == "ContainersReady")
526+
{
527+
return pcond.status == "True";
530528
}
531529
false
532530
}

0 commit comments

Comments
 (0)