From 668c502140235025251eb5aee55d295b5bb1ec12 Mon Sep 17 00:00:00 2001 From: kahirokunn Date: Thu, 19 Dec 2024 15:09:42 +0900 Subject: [PATCH] chore: Improve KubernetesRouter selection based on apiGroup Allow `.spec.targetRef` to specify custom resources, which typically use mesh routers. Prevent unnecessary resources by strictly handling Deployment and DaemonSet based on apiGroup derived from apiVersion. Signed-off-by: kahirokunn --- pkg/controller/scheduler.go | 2 +- pkg/router/factory.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/controller/scheduler.go b/pkg/controller/scheduler.go index 6a9a2664a..fdc058442 100644 --- a/pkg/controller/scheduler.go +++ b/pkg/controller/scheduler.go @@ -193,7 +193,7 @@ func (c *Controller) advanceCanary(name string, namespace string) { } // init Kubernetes router - kubeRouter := c.routerFactory.KubernetesRouter(cd.Spec.TargetRef.Kind, labelSelector, labelValue, ports) + kubeRouter := c.routerFactory.KubernetesRouter(cd.Spec.TargetRef.APIVersion, cd.Spec.TargetRef.Kind, labelSelector, labelValue, ports) // reconcile the canary/primary services if err := kubeRouter.Initialize(cd); err != nil { diff --git a/pkg/router/factory.go b/pkg/router/factory.go index 0a62a5ef0..c41eb96bb 100644 --- a/pkg/router/factory.go +++ b/pkg/router/factory.go @@ -58,11 +58,11 @@ func NewFactory(kubeConfig *restclient.Config, kubeClient kubernetes.Interface, } // KubernetesRouter returns a KubernetesRouter interface implementation -func (factory *Factory) KubernetesRouter(kind string, labelSelector string, labelValue string, ports map[string]int32) KubernetesRouter { - switch kind { - case "Service": - return &KubernetesNoopRouter{} - default: // Daemonset or Deployment +func (factory *Factory) KubernetesRouter(apiVersion string, kind string, labelSelector string, labelValue string, ports map[string]int32) KubernetesRouter { + group := strings.Split(apiVersion, "/")[0] + + switch { + case group == "apps" && (kind == "Deployment" || kind == "DaemonSet"): return &KubernetesDefaultRouter{ logger: factory.logger, flaggerClient: factory.flaggerClient, @@ -71,6 +71,9 @@ func (factory *Factory) KubernetesRouter(kind string, labelSelector string, labe labelValue: labelValue, ports: ports, } + + default: + return &KubernetesNoopRouter{} } }