From 49495a0d1e9a63ccbcf96879ab1431613cdf562c Mon Sep 17 00:00:00 2001 From: Oliver Gould Date: Mon, 25 Nov 2024 18:25:54 +0000 Subject: [PATCH] chore(policy): simplify status controller type matching This change reduces boilerplate when switching between types in the status controller. No functional changes. --- policy-controller/k8s/status/src/index.rs | 14 +++++++------- policy-controller/k8s/status/src/resource_id.rs | 7 +++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/policy-controller/k8s/status/src/index.rs b/policy-controller/k8s/status/src/index.rs index 2213c3e3a036f..6f828fc4ad71e 100644 --- a/policy-controller/k8s/status/src/index.rs +++ b/policy-controller/k8s/status/src/index.rs @@ -269,19 +269,19 @@ impl Controller { // process through the updates queue but not actually patch // any resources. if self.leader { - if id.gkn.group == linkerd_k8s_api::HttpRoute::group(&()) && id.gkn.kind == linkerd_k8s_api::HttpRoute::kind(&()){ + if id.is_a::() { self.patch::(&id.gkn.name, &id.namespace, patch).await; - } else if id.gkn.group == k8s_gateway_api::HttpRoute::group(&()) && id.gkn.kind == k8s_gateway_api::HttpRoute::kind(&()) { + } else if id.is_a::() { self.patch::(&id.gkn.name, &id.namespace, patch).await; - } else if id.gkn.group == k8s_gateway_api::GrpcRoute::group(&()) && id.gkn.kind == k8s_gateway_api::GrpcRoute::kind(&()) { + } else if id.is_a::() { self.patch::(&id.gkn.name, &id.namespace, patch).await; - } else if id.gkn.group == k8s_gateway_api::TcpRoute::group(&()) && id.gkn.kind == k8s_gateway_api::TcpRoute::kind(&()) { + } else if id.is_a::() { self.patch::(&id.gkn.name, &id.namespace, patch).await; - } else if id.gkn.group == k8s_gateway_api::TlsRoute::group(&()) && id.gkn.kind == k8s_gateway_api::TlsRoute::kind(&()) { + } else if id.is_a::() { self.patch::(&id.gkn.name, &id.namespace, patch).await; - } else if id.gkn.group == linkerd_k8s_api::HttpLocalRateLimitPolicy::group(&()) && id.gkn.kind == linkerd_k8s_api::HttpLocalRateLimitPolicy::kind(&()) { + } else if id.is_a::() { self.patch::(&id.gkn.name, &id.namespace, patch).await; - } else if id.gkn.group == linkerd_k8s_api::EgressNetwork::group(&()) && id.gkn.kind == linkerd_k8s_api::EgressNetwork::kind(&()) { + } else if id.is_a::() { self.patch::(&id.gkn.name, &id.namespace, patch).await; } } else { diff --git a/policy-controller/k8s/status/src/resource_id.rs b/policy-controller/k8s/status/src/resource_id.rs index f930fe02b18d7..e5c515a14a47d 100644 --- a/policy-controller/k8s/status/src/resource_id.rs +++ b/policy-controller/k8s/status/src/resource_id.rs @@ -42,4 +42,11 @@ impl NamespaceGroupKindName { } } } + + pub fn is_a(&self) -> bool + where + K: Resource, + { + self.gkn.group == K::group(&()) && self.gkn.kind == K::kind(&()) + } }