From 3f37499cef2c76589ccbcf83a77fef92257cf5da Mon Sep 17 00:00:00 2001 From: Shyunn <114235843+ShyunnY@users.noreply.github.com> Date: Wed, 7 Feb 2024 05:49:29 +0800 Subject: [PATCH] refactor: reconstruct the judgment of OwningGatewayLabels (#2555) reflector: reconstruct the judgment of OwningGatewayLabels Signed-off-by: ShyunnY <1147212064@qq.com> Co-authored-by: zirain --- .../kubernetes/proxy/resource_provider.go | 15 ++++-- .../proxy/resource_provider_test.go | 52 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/internal/infrastructure/kubernetes/proxy/resource_provider.go b/internal/infrastructure/kubernetes/proxy/resource_provider.go index 358350400dc..33099e4806c 100644 --- a/internal/infrastructure/kubernetes/proxy/resource_provider.go +++ b/internal/infrastructure/kubernetes/proxy/resource_provider.go @@ -46,7 +46,7 @@ func (r *ResourceRender) Name() string { func (r *ResourceRender) ServiceAccount() (*corev1.ServiceAccount, error) { // Set the labels based on the owning gateway name. labels := envoyLabels(r.infra.GetProxyMetadata().Labels) - if (len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 || len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0) && len(labels[gatewayapi.OwningGatewayClassLabel]) == 0 { + if OwningGatewayLabelsAbsent(labels) { return nil, fmt.Errorf("missing owning gateway labels") } @@ -99,7 +99,7 @@ func (r *ResourceRender) Service() (*corev1.Service, error) { // Set the labels based on the owning gatewayclass name. labels := envoyLabels(r.infra.GetProxyMetadata().Labels) - if (len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 || len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0) && len(labels[gatewayapi.OwningGatewayClassLabel]) == 0 { + if OwningGatewayLabelsAbsent(labels) { return nil, fmt.Errorf("missing owning gateway labels") } @@ -143,7 +143,7 @@ func (r *ResourceRender) Service() (*corev1.Service, error) { func (r *ResourceRender) ConfigMap() (*corev1.ConfigMap, error) { // Set the labels based on the owning gateway name. labels := envoyLabels(r.infra.GetProxyMetadata().Labels) - if (len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 || len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0) && len(labels[gatewayapi.OwningGatewayClassLabel]) == 0 { + if OwningGatewayLabelsAbsent(labels) { return nil, fmt.Errorf("missing owning gateway labels") } @@ -184,7 +184,7 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { dpAnnotations := r.infra.GetProxyMetadata().Annotations labels := r.infra.GetProxyMetadata().Labels dpLabels := envoyLabels(labels) - if (len(dpLabels[gatewayapi.OwningGatewayNameLabel]) == 0 || len(dpLabels[gatewayapi.OwningGatewayNamespaceLabel]) == 0) && len(dpLabels[gatewayapi.OwningGatewayClassLabel]) == 0 { + if OwningGatewayLabelsAbsent(dpLabels) { return nil, fmt.Errorf("missing owning gateway labels") } @@ -294,3 +294,10 @@ func (r *ResourceRender) HorizontalPodAutoscaler() (*autoscalingv2.HorizontalPod return hpa, nil } + +// OwningGatewayLabelsAbsent Check if labels are missing some OwningGatewayLabels +func OwningGatewayLabelsAbsent(labels map[string]string) bool { + return (len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 || + len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0) && + len(labels[gatewayapi.OwningGatewayClassLabel]) == 0 +} diff --git a/internal/infrastructure/kubernetes/proxy/resource_provider_test.go b/internal/infrastructure/kubernetes/proxy/resource_provider_test.go index 6cdb3b58713..6cb35b3af21 100644 --- a/internal/infrastructure/kubernetes/proxy/resource_provider_test.go +++ b/internal/infrastructure/kubernetes/proxy/resource_provider_test.go @@ -746,3 +746,55 @@ func loadHPA(caseName string) (*autoscalingv2.HorizontalPodAutoscaler, error) { _ = yaml.Unmarshal(hpaYAML, hpa) return hpa, nil } + +func TestOwningGatewayLabelsAbsent(t *testing.T) { + + cases := []struct { + caseName string + labels map[string]string + expect bool + }{ + { + caseName: "OwningGatewayClassLabel exist, but lack OwningGatewayNameLabel or OwningGatewayNamespaceLabel", + labels: map[string]string{ + "gateway.envoyproxy.io/owning-gatewayclass": "eg-class", + }, + expect: false, + }, + { + caseName: "OwningGatewayNameLabel and OwningGatewayNamespaceLabel exist, but lack OwningGatewayClassLabel", + labels: map[string]string{ + "gateway.envoyproxy.io/owning-gateway-name": "eg", + "gateway.envoyproxy.io/owning-gateway-namespace": "default", + }, + expect: false, + }, + { + caseName: "OwningGatewayNameLabel exist, but lack OwningGatewayClassLabel and OwningGatewayNamespaceLabel", + labels: map[string]string{ + "gateway.envoyproxy.io/owning-gateway-name": "eg", + }, + expect: true, + }, + { + caseName: "OwningGatewayNamespaceLabel exist, but lack OwningGatewayClassLabel and OwningGatewayNameLabel", + labels: map[string]string{ + "gateway.envoyproxy.io/owning-gateway-namespace": "default", + }, + expect: true, + }, + { + caseName: "lack all labels", + labels: map[string]string{}, + expect: true, + }, + } + + for _, tc := range cases { + t.Run(tc.caseName, func(t *testing.T) { + actual := OwningGatewayLabelsAbsent(tc.labels) + require.Equal(t, tc.expect, actual) + }) + } + +}