|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package kgateway |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/intermediate" |
| 21 | + "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/kgateway" |
| 22 | + "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/shared" |
| 23 | + |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 27 | +) |
| 28 | + |
| 29 | +// applyLoadBalancingPolicy projects the LoadBalancing IR policy into one or more |
| 30 | +// Kgateway BackendConfigPolicies. |
| 31 | +// |
| 32 | +// Semantics: |
| 33 | +// - We create at most one BackendConfigPolicy per Service. |
| 34 | +// - If SessionAffinity is configured for a Service, it takes precedence and |
| 35 | +// this function will not override the ring-hash configuration. |
| 36 | +// - If there is no SessionAffinity for a Service and LoadBalancing.Strategy is |
| 37 | +// "round_robin", we configure LoadBalancer.RoundRobin on the BackendConfigPolicy. |
| 38 | +// - TargetRefs are populated with all core Service backends that this Policy covers |
| 39 | +// (based on RuleBackendSources). |
| 40 | +func applyLoadBalancingPolicy( |
| 41 | + pol intermediate.Policy, |
| 42 | + httpRouteKey types.NamespacedName, |
| 43 | + httpRouteCtx intermediate.HTTPRouteContext, |
| 44 | + backendCfg map[types.NamespacedName]*kgateway.BackendConfigPolicy, |
| 45 | +) bool { |
| 46 | + // Only care about explicit round_robin strategies. |
| 47 | + if pol.LoadBalancing == nil || pol.LoadBalancing.Strategy != intermediate.LoadBalancingStrategyRoundRobin { |
| 48 | + return false |
| 49 | + } |
| 50 | + |
| 51 | + touched := false |
| 52 | + |
| 53 | + for _, idx := range pol.RuleBackendSources { |
| 54 | + if idx.Rule >= len(httpRouteCtx.Spec.Rules) { |
| 55 | + continue |
| 56 | + } |
| 57 | + rule := httpRouteCtx.Spec.Rules[idx.Rule] |
| 58 | + if idx.Backend >= len(rule.BackendRefs) { |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + br := rule.BackendRefs[idx.Backend] |
| 63 | + |
| 64 | + // Only core Service backends. |
| 65 | + if br.BackendRef.Group != nil && *br.BackendRef.Group != "" { |
| 66 | + continue |
| 67 | + } |
| 68 | + if br.BackendRef.Kind != nil && *br.BackendRef.Kind != "Service" { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + svcName := string(br.BackendRef.Name) |
| 73 | + if svcName == "" { |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + svcKey := types.NamespacedName{ |
| 78 | + Namespace: httpRouteKey.Namespace, |
| 79 | + Name: svcName, |
| 80 | + } |
| 81 | + |
| 82 | + // Create or reuse BackendConfigPolicy per Service. |
| 83 | + bcp, exists := backendCfg[svcKey] |
| 84 | + if !exists { |
| 85 | + policyName := svcName + "-backend-config" |
| 86 | + bcp = &kgateway.BackendConfigPolicy{ |
| 87 | + ObjectMeta: metav1.ObjectMeta{ |
| 88 | + Name: policyName, |
| 89 | + Namespace: httpRouteKey.Namespace, |
| 90 | + }, |
| 91 | + Spec: kgateway.BackendConfigPolicySpec{ |
| 92 | + TargetRefs: []shared.LocalPolicyTargetReference{ |
| 93 | + { |
| 94 | + Group: "", |
| 95 | + Kind: "Service", |
| 96 | + Name: gwv1.ObjectName(svcName), |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + bcp.SetGroupVersionKind(BackendConfigPolicyGVK) |
| 102 | + backendCfg[svcKey] = bcp |
| 103 | + } |
| 104 | + |
| 105 | + // Respect session affinity precedence: |
| 106 | + // If RingHash is already set (via applySessionAffinityPolicy), do not override. |
| 107 | + if bcp.Spec.LoadBalancer != nil && bcp.Spec.LoadBalancer.RingHash != nil { |
| 108 | + // TODO [danehans] add a notification that we are skipping due to session affinity. |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + if bcp.Spec.LoadBalancer == nil { |
| 113 | + bcp.Spec.LoadBalancer = &kgateway.LoadBalancer{} |
| 114 | + } |
| 115 | + |
| 116 | + // Set explicit round_robin. |
| 117 | + bcp.Spec.LoadBalancer.RoundRobin = &kgateway.LoadBalancerRoundRobinConfig{} |
| 118 | + |
| 119 | + touched = true |
| 120 | + } |
| 121 | + |
| 122 | + return touched |
| 123 | +} |
0 commit comments