Skip to content

Commit

Permalink
refactor: reconstruct the judgment of OwningGatewayLabels (#2555)
Browse files Browse the repository at this point in the history
reflector: reconstruct the judgment of OwningGatewayLabels

Signed-off-by: ShyunnY <[email protected]>
Co-authored-by: zirain <[email protected]>
  • Loading branch information
ShyunnY and zirain authored Feb 6, 2024
1 parent f21f9ff commit 3f37499
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/infrastructure/kubernetes/proxy/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
}
52 changes: 52 additions & 0 deletions internal/infrastructure/kubernetes/proxy/resource_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

}

0 comments on commit 3f37499

Please sign in to comment.