Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update address.HoldExternal to allow holding IPv6 as well #2794

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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