Commit 3ea26e74 authored by Nick Sardo's avatar Nick Sardo

Annotation for opting into backend sharing; Use hash suffix for sharing; Fix resource GC

parent d97d80fb
...@@ -21,12 +21,17 @@ import "k8s.io/kubernetes/pkg/api/v1" ...@@ -21,12 +21,17 @@ import "k8s.io/kubernetes/pkg/api/v1"
type LoadBalancerType string type LoadBalancerType string
const ( const (
// ServiceAnnotationLoadBalancerType is the annotation used on a service with type LoadBalancer // ServiceAnnotationLoadBalancerType is annotated on a service with type LoadBalancer
// dictates what specific kind of GCP LB should be assembled. // dictates what specific kind of GCP LB should be assembled.
// Currently, only "internal" is supported. // Currently, only "internal" is supported.
ServiceAnnotationLoadBalancerType = "cloud.google.com/load-balancer-type" ServiceAnnotationLoadBalancerType = "cloud.google.com/load-balancer-type"
LBTypeInternal LoadBalancerType = "internal" LBTypeInternal LoadBalancerType = "internal"
// ServiceAnnotationInternalBackendShare is annotated on a service with "true" when users
// want to share GCP Backend Services for a set of internal load balancers.
// ALPHA feature - this may be removed in a future release.
ServiceAnnotationILBBackendShare = "cloud.google.com/load-balancer-backend-share"
) )
// GetLoadBalancerAnnotationType returns the type of GCP load balancer which should be assembled. // GetLoadBalancerAnnotationType returns the type of GCP load balancer which should be assembled.
...@@ -49,3 +54,12 @@ func GetLoadBalancerAnnotationType(service *v1.Service) (LoadBalancerType, bool) ...@@ -49,3 +54,12 @@ func GetLoadBalancerAnnotationType(service *v1.Service) (LoadBalancerType, bool)
return v, false return v, false
} }
} }
func GetLoadBalancerAnnotationBackendShare(service *v1.Service) bool {
l, exists := service.Annotations[ServiceAnnotationILBBackendShare]
if exists && l == "true" {
return true
}
return false
}
...@@ -17,6 +17,8 @@ limitations under the License. ...@@ -17,6 +17,8 @@ limitations under the License.
package gce package gce
import ( import (
"crypto/sha1"
"encoding/hex"
"fmt" "fmt"
"strings" "strings"
...@@ -33,15 +35,16 @@ func makeInstanceGroupName(clusterID string) string { ...@@ -33,15 +35,16 @@ func makeInstanceGroupName(clusterID string) string {
func makeBackendServiceName(loadBalancerName, clusterID string, shared bool, scheme lbScheme, protocol v1.Protocol, svcAffinity v1.ServiceAffinity) string { func makeBackendServiceName(loadBalancerName, clusterID string, shared bool, scheme lbScheme, protocol v1.Protocol, svcAffinity v1.ServiceAffinity) string {
if shared { if shared {
affinity := "" hash := sha1.New()
switch svcAffinity {
case v1.ServiceAffinityClientIP: // For every non-nil option, hash its value. Currently, only service affinity is relevant.
affinity = "clientip" hash.Write([]byte(string(svcAffinity)))
default:
affinity = "noaffinity" hashed := hex.EncodeToString(hash.Sum(nil))
} hashed = hashed[:16]
return fmt.Sprintf("k8s-%s-%s-%s-%s", clusterID, strings.ToLower(string(scheme)), strings.ToLower(string(protocol)), affinity) // 3 + 1 + 16 + 1 + 8 + 1 + 3 + 16
return fmt.Sprintf("k8s-%s-%s-%s-nmv1-%s", clusterID, strings.ToLower(string(scheme)), strings.ToLower(string(protocol)), hashed)
} }
return loadBalancerName return loadBalancerName
} }
...@@ -54,11 +57,11 @@ func makeHealthCheckName(loadBalancerName, clusterID string, shared bool) string ...@@ -54,11 +57,11 @@ func makeHealthCheckName(loadBalancerName, clusterID string, shared bool) string
return loadBalancerName return loadBalancerName
} }
func makeHealthCheckFirewallkNameFromHC(healthCheckName string) string { func makeHealthCheckFirewallNameFromHC(healthCheckName string) string {
return healthCheckName + "-hc" return healthCheckName + "-hc"
} }
func makeHealthCheckFirewallkName(loadBalancerName, clusterID string, shared bool) string { func makeHealthCheckFirewallName(loadBalancerName, clusterID string, shared bool) string {
if shared { if shared {
return fmt.Sprintf("k8s-%s-node-hc", clusterID) return fmt.Sprintf("k8s-%s-node-hc", clusterID)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment