From dd789cd96d2ce7671ba492a01d853a2e191de884 Mon Sep 17 00:00:00 2001 From: Oliver Gould Date: Mon, 25 Nov 2024 11:20:04 -0800 Subject: [PATCH] chore(policy): simplify status controller type matching (#13395) 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 573bb6134f26f..a9c674816af2b 100644 --- a/policy-controller/k8s/status/src/index.rs +++ b/policy-controller/k8s/status/src/index.rs @@ -268,19 +268,19 @@ impl Controller { // process through the updates queue but not actually patch // any resources. if is_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(&()) + } }