diff --git a/pkg/loadbalancers/forwarding_rules.go b/pkg/loadbalancers/forwarding_rules.go index 9295f8fb58..af16abc25b 100644 --- a/pkg/loadbalancers/forwarding_rules.go +++ b/pkg/loadbalancers/forwarding_rules.go @@ -195,39 +195,39 @@ func (l *L7) getEffectiveIP() (string, bool, error) { // ensureForwardingRule creates a forwarding rule with the given name, if it does not exist. It updates the existing // forwarding rule if needed. -func (l *L4) ensureForwardingRule(loadBalancerName, bsLink string, options gce.ILBOptions, existingFwdRule *composite.ForwardingRule) (*composite.ForwardingRule, error) { +func (l4 *L4) ensureForwardingRule(loadBalancerName, bsLink string, options gce.ILBOptions, existingFwdRule *composite.ForwardingRule) (*composite.ForwardingRule, error) { // version used for creating the existing forwarding rule. version := meta.VersionGA - if l.cloud.IsLegacyNetwork() { - l.recorder.Event(l.Service, v1.EventTypeWarning, "ILBOptionsIgnored", "Internal LoadBalancer options are not supported with Legacy Networks.") + if l4.cloud.IsLegacyNetwork() { + l4.recorder.Event(l4.Service, v1.EventTypeWarning, "ILBOptionsIgnored", "Internal LoadBalancer options are not supported with Legacy Networks.") options = gce.ILBOptions{} } - subnetworkURL := l.cloud.SubnetworkURL() + subnetworkURL := l4.cloud.SubnetworkURL() // Custom subnet feature is always enabled when running L4 controller. // Changes to subnet annotation will be picked up and reflected in the forwarding rule. // Removing the annotation will set the forwarding rule to use the default subnet. if options.SubnetName != "" { - key, err := l.CreateKey(loadBalancerName) + key, err := l4.CreateKey(loadBalancerName) if err != nil { return nil, err } subnetKey := *key subnetKey.Name = options.SubnetName - subnetworkURL = cloud.SelfLink(meta.VersionGA, l.cloud.NetworkProjectID(), "subnetworks", &subnetKey) + subnetworkURL = cloud.SelfLink(meta.VersionGA, l4.cloud.NetworkProjectID(), "subnetworks", &subnetKey) } // Determine IP which will be used for this LB. If no forwarding rule has been established // or specified in the Service spec, then requestedIP = "". - ipToUse := l4lbIPToUse(l.Service, existingFwdRule, subnetworkURL) + ipToUse := l4lbIPToUse(l4.Service, existingFwdRule, subnetworkURL) klog.V(2).Infof("ensureForwardingRule(%v): Using subnet %q for LoadBalancer IP %s", loadBalancerName, subnetworkURL, ipToUse) var addrMgr *addressManager // If the network is not a legacy network, use the address manager - if !l.cloud.IsLegacyNetwork() { - nm := types.NamespacedName{Namespace: l.Service.Namespace, Name: l.Service.Name}.String() + if !l4.cloud.IsLegacyNetwork() { + nm := types.NamespacedName{Namespace: l4.Service.Namespace, Name: l4.Service.Name}.String() // ILB can be created only in Premium Tier - addrMgr = newAddressManager(l.cloud, nm, l.cloud.Region(), subnetworkURL, loadBalancerName, ipToUse, cloud.SchemeInternal, cloud.NetworkTierPremium) + addrMgr = newAddressManager(l4.cloud, nm, l4.cloud.Region(), subnetworkURL, loadBalancerName, ipToUse, cloud.SchemeInternal, cloud.NetworkTierPremium) var err error ipToUse, _, err = addrMgr.HoldAddress() if err != nil { @@ -243,11 +243,11 @@ func (l *L4) ensureForwardingRule(loadBalancerName, bsLink string, options gce.I }() } - servicePorts := l.Service.Spec.Ports + servicePorts := l4.Service.Spec.Ports ports := utils.GetPorts(servicePorts) protocol := utils.GetProtocol(servicePorts) // Create the forwarding rule - frDesc, err := utils.MakeL4LBServiceDescription(utils.ServiceKeyFunc(l.Service.Namespace, l.Service.Name), ipToUse, + frDesc, err := utils.MakeL4LBServiceDescription(utils.ServiceKeyFunc(l4.Service.Namespace, l4.Service.Name), ipToUse, version, false, utils.ILB) if err != nil { return nil, fmt.Errorf("Failed to compute description for forwarding rule %s, err: %w", loadBalancerName, @@ -261,7 +261,7 @@ func (l *L4) ensureForwardingRule(loadBalancerName, bsLink string, options gce.I IPProtocol: string(protocol), LoadBalancingScheme: string(cloud.SchemeInternal), Subnetwork: subnetworkURL, - Network: l.cloud.NetworkURL(), + Network: l4.cloud.NetworkURL(), NetworkTier: cloud.NetworkTierDefault.ToGCEValue(), Version: version, BackendService: bsLink, @@ -287,17 +287,17 @@ func (l *L4) ensureForwardingRule(loadBalancerName, bsLink string, options gce.I // If the forwarding rule pointed to a backend service which does not match the controller naming scheme, // that resouce could be leaked. It is not being deleted here because that is a user-managed resource. klog.V(2).Infof("ensureForwardingRule: forwarding rule changed - Existing - %+v\n, New - %+v\n, Diff(-existing, +new) - %s\n. Deleting existing forwarding rule.", existingFwdRule, fr, frDiff) - if err = l.forwardingRules.Delete(existingFwdRule.Name); err != nil { + if err = l4.forwardingRules.Delete(existingFwdRule.Name); err != nil { return nil, err } - l.recorder.Eventf(l.Service, corev1.EventTypeNormal, events.SyncIngress, "ForwardingRule %q deleted", existingFwdRule.Name) + l4.recorder.Eventf(l4.Service, corev1.EventTypeNormal, events.SyncIngress, "ForwardingRule %q deleted", existingFwdRule.Name) } klog.V(2).Infof("ensureForwardingRule: Creating/Recreating forwarding rule - %s", fr.Name) - if err = l.forwardingRules.Create(fr); err != nil { + if err = l4.forwardingRules.Create(fr); err != nil { return nil, err } - fr, err = l.forwardingRules.Get(fr.Name) + fr, err = l4.forwardingRules.Get(fr.Name) if err != nil { return nil, err } diff --git a/pkg/loadbalancers/l4.go b/pkg/loadbalancers/l4.go index 0938bfc1b2..92f2b91ac9 100644 --- a/pkg/loadbalancers/l4.go +++ b/pkg/loadbalancers/l4.go @@ -87,8 +87,8 @@ func NewL4Handler(service *corev1.Service, cloud *gce.Cloud, scope meta.KeyType, } // CreateKey generates a meta.Key for a given GCE resource name. -func (l *L4) CreateKey(name string) (*meta.Key, error) { - return composite.CreateKey(l.cloud, name, l.scope) +func (l4 *L4) CreateKey(name string) (*meta.Key, error) { + return composite.CreateKey(l4.cloud, name, l4.scope) } // getILBOptions fetches the optional features requested on the given ILB service. @@ -98,45 +98,45 @@ func getILBOptions(svc *corev1.Service) gce.ILBOptions { } // EnsureInternalLoadBalancerDeleted performs a cleanup of all GCE resources for the given loadbalancer service. -func (l *L4) EnsureInternalLoadBalancerDeleted(svc *corev1.Service) *L4ILBSyncResult { - klog.V(2).Infof("EnsureInternalLoadBalancerDeleted(%s): attempting delete of load balancer resources", l.NamespacedName.String()) +func (l4 *L4) EnsureInternalLoadBalancerDeleted(svc *corev1.Service) *L4ILBSyncResult { + klog.V(2).Infof("EnsureInternalLoadBalancerDeleted(%s): attempting delete of load balancer resources", l4.NamespacedName.String()) result := &L4ILBSyncResult{SyncType: SyncTypeDelete, StartTime: time.Now()} // All resources use the L4Backend Name, except forwarding rule. - name, ok := l.namer.L4Backend(svc.Namespace, svc.Name) + name, ok := l4.namer.L4Backend(svc.Namespace, svc.Name) if !ok { result.Error = fmt.Errorf("Namer does not support L4 Backends") return result } - frName := l.GetFRName() - key, err := l.CreateKey(frName) + frName := l4.GetFRName() + key, err := l4.CreateKey(frName) if err != nil { - klog.Errorf("Failed to create key for LoadBalancer resources with name %s for service %s, err %v", frName, l.NamespacedName.String(), err) + klog.Errorf("Failed to create key for LoadBalancer resources with name %s for service %s, err %v", frName, l4.NamespacedName.String(), err) result.Error = err return result } // If any resource deletion fails, log the error and continue cleanup. - if err = utils.IgnoreHTTPNotFound(composite.DeleteForwardingRule(l.cloud, key, meta.VersionGA)); err != nil { - klog.Errorf("Failed to delete forwarding rule for internal loadbalancer service %s, err %v", l.NamespacedName.String(), err) + if err = utils.IgnoreHTTPNotFound(composite.DeleteForwardingRule(l4.cloud, key, meta.VersionGA)); err != nil { + klog.Errorf("Failed to delete forwarding rule for internal loadbalancer service %s, err %v", l4.NamespacedName.String(), err) result.Error = err result.GCEResourceInError = annotations.ForwardingRuleResource } - if err = ensureAddressDeleted(l.cloud, name, l.cloud.Region()); err != nil { - klog.Errorf("Failed to delete address for internal loadbalancer service %s, err %v", l.NamespacedName.String(), err) + if err = ensureAddressDeleted(l4.cloud, name, l4.cloud.Region()); err != nil { + klog.Errorf("Failed to delete address for internal loadbalancer service %s, err %v", l4.NamespacedName.String(), err) result.Error = err result.GCEResourceInError = annotations.AddressResource } // delete firewall rule allowing load balancer source ranges - err = l.deleteFirewall(name) + err = l4.deleteFirewall(name) if err != nil { - klog.Errorf("Failed to delete firewall rule %s for internal loadbalancer service %s, err %v", name, l.NamespacedName.String(), err) + klog.Errorf("Failed to delete firewall rule %s for internal loadbalancer service %s, err %v", name, l4.NamespacedName.String(), err) result.GCEResourceInError = annotations.FirewallRuleResource result.Error = err } // Delete backend service - err = utils.IgnoreHTTPNotFound(l.backendPool.Delete(name, meta.VersionGA, meta.Regional)) + err = utils.IgnoreHTTPNotFound(l4.backendPool.Delete(name, meta.VersionGA, meta.Regional)) if err != nil { - klog.Errorf("Failed to delete backends for internal loadbalancer service %s, err %v", l.NamespacedName.String(), err) + klog.Errorf("Failed to delete backends for internal loadbalancer service %s, err %v", l4.NamespacedName.String(), err) result.GCEResourceInError = annotations.BackendServiceResource result.Error = err } @@ -148,7 +148,7 @@ func (l *L4) EnsureInternalLoadBalancerDeleted(svc *corev1.Service) *L4ILBSyncRe // When service is deleted we need to check both health checks shared and non-shared // and delete them if needed. for _, isShared := range []bool{true, false} { - resourceInError, err := l.l4HealthChecks.DeleteHealthCheck(svc, l.namer, isShared, meta.Global, utils.ILB) + resourceInError, err := l4.l4HealthChecks.DeleteHealthCheck(svc, l4.namer, isShared, meta.Global, utils.ILB) if err != nil { result.GCEResourceInError = resourceInError result.Error = err @@ -157,11 +157,11 @@ func (l *L4) EnsureInternalLoadBalancerDeleted(svc *corev1.Service) *L4ILBSyncRe return result } -func (l *L4) deleteFirewall(name string) error { - err := firewalls.EnsureL4FirewallRuleDeleted(l.cloud, name) +func (l4 *L4) deleteFirewall(name string) error { + err := firewalls.EnsureL4FirewallRuleDeleted(l4.cloud, name) if err != nil { if fwErr, ok := err.(*firewalls.FirewallXPNError); ok { - l.recorder.Eventf(l.Service, corev1.EventTypeNormal, "XPN", fwErr.Message) + l4.recorder.Eventf(l4.Service, corev1.EventTypeNormal, "XPN", fwErr.Message) return nil } return err @@ -172,18 +172,18 @@ func (l *L4) deleteFirewall(name string) error { // GetFRName returns the name of the forwarding rule for the given ILB service. // This appends the protocol to the forwarding rule name, which will help supporting multiple protocols in the same ILB // service. -func (l *L4) GetFRName() string { - protocol := utils.GetProtocol(l.Service.Spec.Ports) - return l.getFRNameWithProtocol(string(protocol)) +func (l4 *L4) GetFRName() string { + protocol := utils.GetProtocol(l4.Service.Spec.Ports) + return l4.getFRNameWithProtocol(string(protocol)) } -func (l *L4) getFRNameWithProtocol(protocol string) string { - return l.namer.L4ForwardingRule(l.Service.Namespace, l.Service.Name, strings.ToLower(protocol)) +func (l4 *L4) getFRNameWithProtocol(protocol string) string { + return l4.namer.L4ForwardingRule(l4.Service.Namespace, l4.Service.Name, strings.ToLower(protocol)) } // EnsureInternalLoadBalancer ensures that all GCE resources for the given loadbalancer service have // been created. It returns a LoadBalancerStatus with the updated ForwardingRule IP address. -func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) *L4ILBSyncResult { +func (l4 *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) *L4ILBSyncResult { result := &L4ILBSyncResult{ Annotations: make(map[string]string), StartTime: time.Now(), @@ -196,18 +196,18 @@ func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) result.SyncType = SyncTypeUpdate } - l.Service = svc + l4.Service = svc // All resources use the L4Backend name, except forwarding rule. - name, ok := l.namer.L4Backend(l.Service.Namespace, l.Service.Name) + name, ok := l4.namer.L4Backend(l4.Service.Namespace, l4.Service.Name) if !ok { result.Error = fmt.Errorf("Namer does not support L4 VMIPNEGs") return result } - options := getILBOptions(l.Service) + options := getILBOptions(l4.Service) // create healthcheck - sharedHC := !helpers.RequestsOnlyLocalTraffic(l.Service) - hcResult := l.l4HealthChecks.EnsureL4HealthCheck(l.Service, l.namer, sharedHC, meta.Global, utils.ILB, nodeNames) + sharedHC := !helpers.RequestsOnlyLocalTraffic(l4.Service) + hcResult := l4.l4HealthChecks.EnsureL4HealthCheck(l4.Service, l4.namer, sharedHC, meta.Global, utils.ILB, nodeNames) if hcResult.Err != nil { result.GCEResourceInError = hcResult.GceResourceInError @@ -216,35 +216,35 @@ func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) } result.Annotations[annotations.HealthcheckKey] = hcResult.HCName - servicePorts := l.Service.Spec.Ports + servicePorts := l4.Service.Spec.Ports portRanges := utils.GetServicePortRanges(servicePorts) protocol := utils.GetProtocol(servicePorts) // Check if protocol has changed for this service. In this case, forwarding rule should be deleted before // the backend service can be updated. - existingBS, err := l.backendPool.Get(name, meta.VersionGA, l.scope) + existingBS, err := l4.backendPool.Get(name, meta.VersionGA, l4.scope) err = utils.IgnoreHTTPNotFound(err) if err != nil { klog.Errorf("Failed to lookup existing backend service, ignoring err: %v", err) } - existingFR, err := l.forwardingRules.Get(l.GetFRName()) + existingFR, err := l4.forwardingRules.Get(l4.GetFRName()) if existingBS != nil && existingBS.Protocol != string(protocol) { - klog.Infof("Protocol changed from %q to %q for service %s", existingBS.Protocol, string(protocol), l.NamespacedName) + klog.Infof("Protocol changed from %q to %q for service %s", existingBS.Protocol, string(protocol), l4.NamespacedName) // Delete forwarding rule if it exists - frName := l.getFRNameWithProtocol(existingBS.Protocol) - existingFR, err = l.forwardingRules.Get(frName) + frName := l4.getFRNameWithProtocol(existingBS.Protocol) + existingFR, err = l4.forwardingRules.Get(frName) if err != nil { klog.Errorf("Failed to get forwarding rule %s, err %v", frName, err) } - err = l.forwardingRules.Delete(frName) + err = l4.forwardingRules.Delete(frName) if err != nil { klog.Errorf("Failed to delete forwarding rule %s, err %v", frName, err) } } // ensure backend service - bs, err := l.backendPool.EnsureL4BackendService(name, hcResult.HCLink, string(protocol), string(l.Service.Spec.SessionAffinity), - string(cloud.SchemeInternal), l.NamespacedName, meta.VersionGA) + bs, err := l4.backendPool.EnsureL4BackendService(name, hcResult.HCLink, string(protocol), string(l4.Service.Spec.SessionAffinity), + string(cloud.SchemeInternal), l4.NamespacedName, meta.VersionGA) if err != nil { result.GCEResourceInError = annotations.BackendServiceResource result.Error = err @@ -252,8 +252,8 @@ func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) } result.Annotations[annotations.BackendServiceKey] = name // create fr rule - frName := l.GetFRName() - fr, err := l.ensureForwardingRule(frName, bs.SelfLink, options, existingFR) + frName := l4.GetFRName() + fr, err := l4.ensureForwardingRule(frName, bs.SelfLink, options, existingFR) if err != nil { klog.Errorf("EnsureInternalLoadBalancer: Failed to create forwarding rule - %v", err) result.GCEResourceInError = annotations.ForwardingRuleResource @@ -267,7 +267,7 @@ func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) } // ensure firewalls - sourceRanges, err := helpers.GetLoadBalancerSourceRanges(l.Service) + sourceRanges, err := helpers.GetLoadBalancerSourceRanges(l4.Service) if err != nil { result.Error = err return result @@ -283,7 +283,7 @@ func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) L4Type: utils.ILB, } - if err := firewalls.EnsureL4LBFirewallForNodes(l.Service, &nodesFWRParams, l.cloud, l.recorder); err != nil { + if err := firewalls.EnsureL4LBFirewallForNodes(l4.Service, &nodesFWRParams, l4.cloud, l4.recorder); err != nil { result.GCEResourceInError = annotations.FirewallRuleResource result.Error = err return result