@@ -29,7 +29,6 @@ import (
2929
3030 "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
3131 "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
32- "github.com/google/go-cmp/cmp"
3332 compute "google.golang.org/api/compute/v1"
3433 v1 "k8s.io/api/core/v1"
3534 "k8s.io/apimachinery/pkg/types"
@@ -188,121 +187,104 @@ func (g *Cloud) ensureInternalLoadBalancer(clusterName, clusterID string, svc *v
188187
189188 // Get all existing forwarding rules for this service.
190189 // A service can have a forwarding rule with the base name, or with a protocol suffix.
191- existingFwdRules := make (map [string ]* compute.ForwardingRule )
192- // The `existingFwdRule` is the one with the base name, passed into this function.
193- if existingFwdRule != nil {
194- existingFwdRules [existingFwdRule .Name ] = existingFwdRule
195- }
190+ // TODO: Remove existingFwdRules and use the one from the function.
196191 // Check for forwarding rules with protocol suffixes.
197- if len (groupedPorts ) > 1 {
198- for protocol := range groupedPorts {
199- frName := fmt .Sprintf ("%s-%s" , loadBalancerName , strings .ToLower (string (protocol )))
200- if _ , ok := existingFwdRules [frName ]; ok {
201- continue
202- }
203- fr , err := g .GetRegionForwardingRule (frName , g .region )
204- if err != nil && ! isNotFound (err ) {
205- return nil , err
206- }
207- if fr != nil {
208- existingFwdRules [fr .Name ] = fr
209- }
210- }
211- }
212-
213- desiredFwdRuleNames := sets .NewString ()
214- var desiredFwdRuleProtocols []v1.Protocol
215- for protocol := range groupedPorts {
216- desiredFwdRuleProtocols = append (desiredFwdRuleProtocols , protocol )
192+ existingForwardingRules , err := g .getExistingForwardingRules (loadBalancerName )
193+ if err != nil {
194+ return nil , err
217195 }
218- // Sort protocols to have a stable order for naming and processing.
219- sort .Slice (desiredFwdRuleProtocols , func (i , j int ) bool {
220- return desiredFwdRuleProtocols [i ] < desiredFwdRuleProtocols [j ]
221- })
222196
223197 var createdFwdRules []* compute.ForwardingRule
224198 var desiredBackendServices = make (map [string ]bool )
225199
226- for _ , protocol := range desiredFwdRuleProtocols {
227- portStruct := groupedPorts [protocol ]
228- ports := portStruct .portRanges
229-
230- // Each protocol gets its own backend service.
231- // The backend service name must be unique per protocol.
232- // Pass a single-protocol map to makeBackendServiceName.
233- singleProtocolGroupedPorts := map [v1.Protocol ]ProtocolPorts {protocol : portStruct }
234- backendServiceName := makeBackendServiceName (loadBalancerName , clusterID , sharedBackend , scheme , singleProtocolGroupedPorts , svc .Spec .SessionAffinity )
235- desiredBackendServices [backendServiceName ] = true
236- backendServiceLink := g .getBackendServiceLink (backendServiceName )
237-
238- bsDescription := makeBackendServiceDescription (nm , sharedBackend )
239- err = g .ensureInternalBackendService (backendServiceName , bsDescription , svc .Spec .SessionAffinity , scheme , protocol , igLinks , hc .SelfLink )
240- if err != nil {
241- return nil , err
242- }
243-
200+ var desiredForwardingRules map [v1.Protocol ]* compute.ForwardingRule
201+ for protocol , protocolPorts := range groupedPorts {
244202 // Each protocol gets its own forwarding rule.
245203 // If there's only one protocol, the forwarding rule name is the load balancer name.
246204 // Otherwise, it's load-balancer-name-<protocol>.
247205 frName := loadBalancerName
248206 if len (groupedPorts ) > 1 {
249207 frName = fmt .Sprintf ("%s-%s" , loadBalancerName , strings .ToLower (string (protocol )))
250208 }
251- desiredFwdRuleNames .Insert (frName )
252209
253- newFwdRule := & compute.ForwardingRule {
210+ forwardingRule := & compute.ForwardingRule {
254211 Name : frName ,
255212 Description : fwdRuleDescriptionString ,
256213 IPAddress : ipToUse ,
257214 BackendService : backendServiceLink ,
258- Ports : ports ,
259215 IPProtocol : string (protocol ),
260216 LoadBalancingScheme : string (scheme ),
261217 Subnetwork : subnetworkURL ,
262218 Network : g .networkURL ,
263219 }
264220 if options .AllowGlobalAccess {
265- newFwdRule .AllowGlobalAccess = options .AllowGlobalAccess
221+ forwardingRule .AllowGlobalAccess = options .AllowGlobalAccess
266222 }
267- if len (ports ) > maxL4ILBPorts {
268- newFwdRule .Ports = nil
269- newFwdRule .AllPorts = true
223+ if len (protocolPorts .ports ) > maxL4ILBPorts {
224+ forwardingRule .Ports = nil
225+ forwardingRule .AllPorts = true
226+ } else {
227+ for _ , port := range protocolPorts .ports {
228+ forwardingRule .Ports = append (forwardingRule .Ports , strconv .Itoa (port ))
229+ }
270230 }
271231
272232 // Check if a forwarding rule for this protocol already exists.
233+ for _ , existingFwdRule := range existingForwardingRules {
234+ if existingFwdRule .IPProtocol == forwardingRule .IPProtocol {
235+ forwardingRule .Name = existingFwdRule .Name
236+ break
237+ }
238+ }
239+
240+ desiredForwardingRules = append (desiredForwardingRules , forwardingRule )
241+ }
242+
243+ for _ , desiredForwardingRule := range desiredForwardingRules {
273244 var existingFwdRuleForProtocol * compute.ForwardingRule
274- if fr , ok := existingFwdRules [frName ]; ok {
275- existingFwdRuleForProtocol = fr
245+ for _ , existingFwdRule := range existingForwardingRules {
246+ if existingFwdRule .IPProtocol == desiredForwardingRule .IPProtocol {
247+ existingFwdRuleForProtocol = existingFwdRule
248+ break
249+ }
276250 }
277251
278- if err := g .ensureInternalForwardingRule (existingFwdRuleForProtocol , newFwdRule ); err != nil {
252+ if err := g .ensureInternalForwardingRule (existingFwdRuleForProtocol , desiredForwardingRule ); err != nil {
279253 return nil , err
280254 }
281- createdFwdRules = append (createdFwdRules , newFwdRule )
255+ createdFwdRules = append (createdFwdRules , desiredForwardingRule )
282256 }
283257
284258 // Delete any forwarding rules that are no longer needed.
285- for frName , fr := range existingFwdRules {
286- if desiredFwdRuleNames .Has (frName ) {
259+ for frName , fr := range existingForwardingRules {
260+ var matching * compute.ForwardingRule
261+ for _ , desiredForwardingRule := range desiredForwardingRules {
262+ if desiredForwardingRule .Name == fr .Name {
263+ matching = desiredForwardingRule
264+ continue
265+ }
266+ }
267+ if matching != nil {
287268 continue
288269 }
270+
289271 klog .V (2 ).Infof ("ensureInternalLoadBalancer(%v): deleting stale forwarding rule %s" , loadBalancerName , frName )
290272 if err := ignoreNotFound (g .DeleteRegionForwardingRule (frName , g .region )); err != nil {
291273 return nil , err
292274 }
293- // Also delete the associated backend service if it's not used by other forwarding rules.
294- if fr .BackendService != "" {
295- bsName := getNameFromLink (fr .BackendService )
296- if ! desiredBackendServices [bsName ] {
297- klog .V (2 ).Infof ("ensureInternalLoadBalancer(%v): deleting stale backend service %s" , loadBalancerName , bsName )
298- if err := g .teardownInternalBackendService (bsName ); err != nil {
299- klog .Warningf ("ensureInternalLoadBalancer: could not delete old backend service %s: %v" , bsName , err )
300- }
301- }
302- }
303- }
304-
305- if len (createdFwdRules ) == 0 {
275+ // // Also delete the associated backend service if it's not used by other forwarding rules.
276+ // if fr.BackendService != "" {
277+ // bsName := getNameFromLink(fr.BackendService)
278+ // if !desiredBackendServices[bsName] {
279+ // klog.V(2).Infof("ensureInternalLoadBalancer(%v): deleting stale backend service %s", loadBalancerName, bsName)
280+ // if err := g.teardownInternalBackendService(bsName); err != nil {
281+ // klog.Warningf("ensureInternalLoadBalancer: could not delete old backend service %s: %v", bsName, err)
282+ // }
283+ // }
284+ // }
285+ }
286+
287+ if len (desiredForwardingRules ) == 0 {
306288 klog .V (2 ).Infof ("ensureInternalLoadBalancer(%v): no forwarding rules needed, all deleted." , loadBalancerName )
307289 return & v1.LoadBalancerStatus {}, nil
308290 }
0 commit comments