Skip to content

Commit

Permalink
Update address.HoldExternal to allow holding IPv6 as well
Browse files Browse the repository at this point in the history
This is done in order to reuse address holding logic in future mixed protocol NetLB IPv6 implementation. Similar thing has been already done in IPv4.

* Updates config with fields that are IPv4/IPv6 specific.
* Adds tests for HoldExternal IPv6 addresses.
* Updates forwarding_rules_ipv6.go to use address.HoldExternal.
* Removes tearDownResourcesWithWrongNetworkTier from l4netlb as it is already implemented inside address.HoldExternal.
  • Loading branch information
TortillaZHawaii committed Jan 14, 2025
1 parent 84e6562 commit 6dce545
Show file tree
Hide file tree
Showing 5 changed files with 369 additions and 72 deletions.
41 changes: 30 additions & 11 deletions pkg/address/hold.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package address

import (
"fmt"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
api_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -9,6 +11,7 @@ import (
"k8s.io/ingress-gce/pkg/annotations"
"k8s.io/ingress-gce/pkg/composite"
"k8s.io/ingress-gce/pkg/utils"
"k8s.io/ingress-gce/pkg/utils/namer"
"k8s.io/klog/v2"
)

Expand All @@ -19,6 +22,8 @@ type HoldConfig struct {
Service *api_v1.Service
ExistingRules []*composite.ForwardingRule
ForwardingRuleDeleter ForwardingRuleDeleter
IPVersion IPVersion
SubnetworkURL string
}

type ForwardingRuleDeleter interface {
Expand All @@ -31,28 +36,34 @@ type HoldResult struct {
Release func() error
}

// HoldExternalIPv4 will determine which IP to use for forwarding rules
// HoldExternal will determine which IP to use for forwarding rules
// and will hold it for future forwarding rules. After binding
// IP to a forwarding rule call Release to prevent leaks.
func HoldExternalIPv4(cfg HoldConfig) (HoldResult, error) {
func HoldExternal(cfg HoldConfig) (HoldResult, error) {
var err error
res := HoldResult{
Release: func() error { return nil },
}
log := cfg.Logger.WithName("HoldIPv4")

// external specific
subnet := ""
name := utils.LegacyForwardingRuleName(cfg.Service)
log := cfg.Logger.WithName("HoldExternal")

// Determine IP which will be used for this LB. If no forwarding rule has been established
// or specified in the Service spec, then requestedIP = "".
rule := pickForwardingRuleToInferIP(cfg.ExistingRules)
res.IP, err = IPv4ToUse(cfg.Cloud, cfg.Recorder, cfg.Service, rule, subnet)

switch cfg.IPVersion {
case IPv4Version:
res.IP, err = IPv4ToUse(cfg.Cloud, cfg.Recorder, cfg.Service, rule, cfg.SubnetworkURL)
case IPv6Version:
res.IP, err = IPv6ToUse(cfg.Cloud, cfg.Service, rule, cfg.SubnetworkURL, cfg.Logger)
default:
return res, fmt.Errorf("unsupported IP version: '%s', only IPv4 and IPv6 are supported", cfg.IPVersion)
}

if err != nil {
log.Error(err, "IPv4ToUse for service returned error")
log.Error(err, "IPvXToUse for service returned error")
return res, err
}
log.V(2).Info("IP for service", "ip", res.IP)

// We can't use manager for legacy networks
if cfg.Cloud.IsLegacyNetwork() {
Expand All @@ -67,8 +78,8 @@ func HoldExternalIPv4(cfg HoldConfig) (HoldResult, error) {

addrMgr := NewManager(
cfg.Cloud, nm, cfg.Cloud.Region(),
subnet, name, res.IP,
cloud.SchemeExternal, netTier, IPv4Version, cfg.Logger,
cfg.SubnetworkURL, name(cfg), res.IP,
cloud.SchemeExternal, netTier, cfg.IPVersion, cfg.Logger,
)

// If network tier annotation in Service Spec is present
Expand Down Expand Up @@ -124,3 +135,11 @@ func tearDownRulesIfNetworkTierMismatch(deleter ForwardingRuleDeleter, existingR
}
return nil
}

func name(cfg HoldConfig) string {
name := utils.LegacyForwardingRuleName(cfg.Service)
if cfg.IPVersion == IPv6Version {
name = namer.GetSuffixedName(name, "-ipv6")
}
return name
}
Loading

0 comments on commit 6dce545

Please sign in to comment.