From f76730d2f3de7ee2abfb4feecfa3be3aa50a1036 Mon Sep 17 00:00:00 2001 From: Pavithra Ramesh Date: Sat, 14 Dec 2019 10:55:51 -0800 Subject: [PATCH] Use a dedicated name function for PRIMARY_IP NEG Also using a dedicated PortMap function. --- pkg/neg/controller.go | 5 +++-- pkg/neg/types/interfaces.go | 1 + pkg/neg/types/types.go | 19 +++++++++++++++++++ pkg/utils/namer/namer.go | 8 ++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pkg/neg/controller.go b/pkg/neg/controller.go index 229b091a7f..8a4d44ae47 100644 --- a/pkg/neg/controller.go +++ b/pkg/neg/controller.go @@ -150,7 +150,7 @@ func NewController( reflector: reflector, } - if ctx.Cloud.AlphaFeatureGate != nil && ctx.Cloud.AlphaFeatureGate.Enabled("ILBSubsets") { + if ctx.Cloud.AlphaFeatureGate != nil && ctx.Cloud.AlphaFeatureGate.Enabled(gce.AlphaFeatureILBSubsets) { runL4 = true } @@ -503,7 +503,8 @@ func (c *Controller) mergeVmPrimaryIpNEGsPortInfo(service *apiv1.Service, name t // Insert Empty PortTuple for VmPrimaryIp NEGs. negtypes.SvcPortTuple{}, ) - return portInfoMap.Merge(negtypes.NewPortInfoMap(name.Namespace, name.Name, svcPortSet, c.namer, false, !helpers.RequestsOnlyLocalTraffic(service))) + return portInfoMap.Merge(negtypes.NewPortInfoMapForPrimaryIPNEG(name.Namespace, name.Name, c.namer, + !helpers.RequestsOnlyLocalTraffic(service))) } // mergeDefaultBackendServicePortInfoMap merge the PortInfoMap for the default backend service into portInfoMap diff --git a/pkg/neg/types/interfaces.go b/pkg/neg/types/interfaces.go index e147642c04..0aa2627267 100644 --- a/pkg/neg/types/interfaces.go +++ b/pkg/neg/types/interfaces.go @@ -44,6 +44,7 @@ type NetworkEndpointGroupCloud interface { // NetworkEndpointGroupNamer is an interface for generating network endpoint group name. type NetworkEndpointGroupNamer interface { NEG(namespace, name string, port int32) string + PrimaryIPNEG(namespace, name string) string NEGWithSubset(namespace, name, subset string, port int32) string IsNEG(name string) bool } diff --git a/pkg/neg/types/types.go b/pkg/neg/types/types.go index 3b1943ce7d..d37043b246 100644 --- a/pkg/neg/types/types.go +++ b/pkg/neg/types/types.go @@ -135,6 +135,25 @@ func NewPortInfoMap(namespace, name string, svcPortTupleSet SvcPortTupleSet, nam return ret } +// NewPortInfoMapForPrimaryIPNEG creates PortInfoMap with empty port tuple. Since PRIMARY_VM_IP NEGs target +// the node instead of the pod, there is no port info to be stored. +func NewPortInfoMapForPrimaryIPNEG(namespace, name string, namer NetworkEndpointGroupNamer, randomize bool) PortInfoMap { + ret := PortInfoMap{} + svcPortSet := make(SvcPortTupleSet) + svcPortSet.Insert( + // Insert Empty PortTuple for VmPrimaryIp NEGs. + SvcPortTuple{}, + ) + for svcPortTuple := range svcPortSet { + ret[PortInfoMapKey{svcPortTuple.Port, ""}] = PortInfo{ + PortTuple: svcPortTuple, + NegName: namer.PrimaryIPNEG(namespace, name), + RandomizeEndpoints: randomize, + } + } + return ret +} + // NewPortInfoMapWithDestinationRule create PortInfoMap based on a gaven DesinationRule. // Return error message if the DestinationRule contains duplicated subsets. func NewPortInfoMapWithDestinationRule(namespace, name string, svcPortTupleSet SvcPortTupleSet, namer NetworkEndpointGroupNamer, readinessGate bool, diff --git a/pkg/utils/namer/namer.go b/pkg/utils/namer/namer.go index d6cfda5e24..452a6d1b01 100644 --- a/pkg/utils/namer/namer.go +++ b/pkg/utils/namer/namer.go @@ -458,6 +458,14 @@ func (n *Namer) NEGWithSubset(namespace, name, subset string, port int32) string return fmt.Sprintf("%s-%s-%s-%s-%s-%s", n.negPrefix(), truncNamespace, truncName, truncPort, truncSubset, negSuffix(n.shortUID(), namespace, name, portStr, subset)) } +func (n *Namer) PrimaryIPNEG(namespace, name string) string { + truncFields := TrimFieldsEvenly(maxNEGDescriptiveLabel, namespace, name) + truncNamespace := truncFields[0] + truncName := truncFields[1] + // Use the full cluster UID in the suffix to reduce chance of collision. + return fmt.Sprintf("%s-%s-%s-%s", n.negPrefix(), truncNamespace, truncName, negSuffix(n.UID(), namespace, name, "", "")) +} + // IsNEG returns true if the name is a NEG owned by this cluster. // It checks that the UID is present and a substring of the // cluster uid, since the NEG naming schema truncates it to 8 characters.