Skip to content

Commit a7e9704

Browse files
authored
Merge pull request kubernetes#81549 from prameshj/ilb-globalaccess
Support GlobalAccess for gce Internal Loadbalancers
2 parents 4170a19 + cae11a1 commit a7e9704

File tree

6 files changed

+628
-61
lines changed

6 files changed

+628
-61
lines changed

staging/src/k8s.io/legacy-cloud-providers/gce/gce_annotations.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ type LoadBalancerType string
3333
const (
3434
// ServiceAnnotationLoadBalancerType is annotated on a service with type LoadBalancer
3535
// dictates what specific kind of GCP LB should be assembled.
36-
// Currently, only "internal" is supported.
37-
ServiceAnnotationLoadBalancerType = "cloud.google.com/load-balancer-type"
36+
// Currently, only "Internal" is supported.
37+
ServiceAnnotationLoadBalancerType = "networking.gke.io/load-balancer-type"
38+
39+
// Deprecating the old-style naming of LoadBalancerType annotation
40+
deprecatedServiceAnnotationLoadBalancerType = "cloud.google.com/load-balancer-type"
3841

3942
// LBTypeInternal is the constant for the official internal type.
4043
LBTypeInternal LoadBalancerType = "Internal"
@@ -50,6 +53,11 @@ const (
5053
// This annotation did not correctly specify "alpha", so both annotations will be checked.
5154
deprecatedServiceAnnotationILBBackendShare = "cloud.google.com/load-balancer-backend-share"
5255

56+
// ServiceAnnotationILBAllowGlobalAccess is annotated on a service with "true" when users
57+
// want to access the Internal LoadBalancer globally, and not restricted to the region it is
58+
// created in.
59+
ServiceAnnotationILBAllowGlobalAccess = "networking.gke.io/internal-load-balancer-allow-global-access"
60+
5361
// NetworkTierAnnotationKey is annotated on a Service object to indicate which
5462
// network tier a GCP LB should use. The valid values are "Standard" and
5563
// "Premium" (default).
@@ -63,23 +71,23 @@ const (
6371
)
6472

6573
// GetLoadBalancerAnnotationType returns the type of GCP load balancer which should be assembled.
66-
func GetLoadBalancerAnnotationType(service *v1.Service) (LoadBalancerType, bool) {
67-
v := LoadBalancerType("")
68-
if service.Spec.Type != v1.ServiceTypeLoadBalancer {
69-
return v, false
70-
}
71-
72-
l, ok := service.Annotations[ServiceAnnotationLoadBalancerType]
73-
v = LoadBalancerType(l)
74-
if !ok {
75-
return v, false
74+
func GetLoadBalancerAnnotationType(service *v1.Service) LoadBalancerType {
75+
var lbType LoadBalancerType
76+
for _, ann := range []string{
77+
ServiceAnnotationLoadBalancerType,
78+
deprecatedServiceAnnotationLoadBalancerType,
79+
} {
80+
if v, ok := service.Annotations[ann]; ok {
81+
lbType = LoadBalancerType(v)
82+
break
83+
}
7684
}
7785

78-
switch v {
86+
switch lbType {
7987
case LBTypeInternal, deprecatedTypeInternalLowerCase:
80-
return LBTypeInternal, true
88+
return LBTypeInternal
8189
default:
82-
return v, false
90+
return lbType
8391
}
8492
}
8593

@@ -118,3 +126,16 @@ func GetServiceNetworkTier(service *v1.Service) (cloud.NetworkTier, error) {
118126
return cloud.NetworkTierDefault, fmt.Errorf("unsupported network tier: %q", v)
119127
}
120128
}
129+
130+
// ILBOptions represents the extra options specified when creating a
131+
// load balancer.
132+
type ILBOptions struct {
133+
// AllowGlobalAccess Indicates whether global access is allowed for the LoadBalancer
134+
AllowGlobalAccess bool
135+
}
136+
137+
// GetLoadBalancerAnnotationAllowGlobalAccess returns if global access is enabled
138+
// for the given loadbalancer service.
139+
func GetLoadBalancerAnnotationAllowGlobalAccess(service *v1.Service) bool {
140+
return service.Annotations[ServiceAnnotationILBAllowGlobalAccess] == "true"
141+
}

staging/src/k8s.io/legacy-cloud-providers/gce/gce_forwardingrule.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/filter"
2424
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
2525
computealpha "google.golang.org/api/compute/v0.alpha"
26+
computebeta "google.golang.org/api/compute/v0.beta"
2627
compute "google.golang.org/api/compute/v1"
2728
)
2829

@@ -102,6 +103,16 @@ func (g *Cloud) GetAlphaRegionForwardingRule(name, region string) (*computealpha
102103
return v, mc.Observe(err)
103104
}
104105

106+
// GetBetaRegionForwardingRule returns the Beta forwarding rule by name & region.
107+
func (g *Cloud) GetBetaRegionForwardingRule(name, region string) (*computebeta.ForwardingRule, error) {
108+
ctx, cancel := cloud.ContextWithCallTimeout()
109+
defer cancel()
110+
111+
mc := newForwardingRuleMetricContextWithVersion("get", region, computeBetaVersion)
112+
v, err := g.c.BetaForwardingRules().Get(ctx, meta.RegionalKey(name, region))
113+
return v, mc.Observe(err)
114+
}
115+
105116
// ListRegionForwardingRules lists all RegionalForwardingRules in the project & region.
106117
func (g *Cloud) ListRegionForwardingRules(region string) ([]*compute.ForwardingRule, error) {
107118
ctx, cancel := cloud.ContextWithCallTimeout()
@@ -122,6 +133,16 @@ func (g *Cloud) ListAlphaRegionForwardingRules(region string) ([]*computealpha.F
122133
return v, mc.Observe(err)
123134
}
124135

136+
// ListBetaRegionForwardingRules lists all RegionalForwardingRules in the project & region.
137+
func (g *Cloud) ListBetaRegionForwardingRules(region string) ([]*computebeta.ForwardingRule, error) {
138+
ctx, cancel := cloud.ContextWithCallTimeout()
139+
defer cancel()
140+
141+
mc := newForwardingRuleMetricContextWithVersion("list", region, computeBetaVersion)
142+
v, err := g.c.BetaForwardingRules().List(ctx, region, filter.None)
143+
return v, mc.Observe(err)
144+
}
145+
125146
// CreateRegionForwardingRule creates and returns a
126147
// RegionalForwardingRule that points to the given BackendService
127148
func (g *Cloud) CreateRegionForwardingRule(rule *compute.ForwardingRule, region string) error {
@@ -133,7 +154,7 @@ func (g *Cloud) CreateRegionForwardingRule(rule *compute.ForwardingRule, region
133154
}
134155

135156
// CreateAlphaRegionForwardingRule creates and returns an Alpha
136-
// forwarding fule in the given region.
157+
// forwarding rule in the given region.
137158
func (g *Cloud) CreateAlphaRegionForwardingRule(rule *computealpha.ForwardingRule, region string) error {
138159
ctx, cancel := cloud.ContextWithCallTimeout()
139160
defer cancel()
@@ -142,6 +163,16 @@ func (g *Cloud) CreateAlphaRegionForwardingRule(rule *computealpha.ForwardingRul
142163
return mc.Observe(g.c.AlphaForwardingRules().Insert(ctx, meta.RegionalKey(rule.Name, region), rule))
143164
}
144165

166+
// CreateBetaRegionForwardingRule creates and returns a Beta
167+
// forwarding rule in the given region.
168+
func (g *Cloud) CreateBetaRegionForwardingRule(rule *computebeta.ForwardingRule, region string) error {
169+
ctx, cancel := cloud.ContextWithCallTimeout()
170+
defer cancel()
171+
172+
mc := newForwardingRuleMetricContextWithVersion("create", region, computeBetaVersion)
173+
return mc.Observe(g.c.BetaForwardingRules().Insert(ctx, meta.RegionalKey(rule.Name, region), rule))
174+
}
175+
145176
// DeleteRegionForwardingRule deletes the RegionalForwardingRule by name & region.
146177
func (g *Cloud) DeleteRegionForwardingRule(name, region string) error {
147178
ctx, cancel := cloud.ContextWithCallTimeout()

staging/src/k8s.io/legacy-cloud-providers/gce/gce_loadbalancer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ func (g *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, svc
152152
default:
153153
status, err = g.ensureExternalLoadBalancer(clusterName, clusterID, svc, existingFwdRule, nodes)
154154
}
155-
klog.V(4).Infof("EnsureLoadBalancer(%v, %v, %v, %v, %v): done ensuring loadbalancer. err: %v", clusterName, svc.Namespace, svc.Name, loadBalancerName, g.region, err)
155+
if err != nil {
156+
klog.Errorf("Failed to EnsureLoadBalancer(%s, %s, %s, %s, %s), err: %v", clusterName, svc.Namespace, svc.Name, loadBalancerName, g.region, err)
157+
return status, err
158+
}
159+
klog.V(4).Infof("EnsureLoadBalancer(%s, %s, %s, %s, %s): done ensuring loadbalancer.", clusterName, svc.Namespace, svc.Name, loadBalancerName, g.region)
156160
return status, err
157161
}
158162

@@ -199,7 +203,7 @@ func (g *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName strin
199203
}
200204

201205
func getSvcScheme(svc *v1.Service) cloud.LbScheme {
202-
if typ, ok := GetLoadBalancerAnnotationType(svc); ok && typ == LBTypeInternal {
206+
if t := GetLoadBalancerAnnotationType(svc); t == LBTypeInternal {
203207
return cloud.SchemeInternal
204208
}
205209
return cloud.SchemeExternal

0 commit comments

Comments
 (0)