Skip to content

Commit

Permalink
Refactor L4 Namer, add getSuffixedName function
Browse files Browse the repository at this point in the history
  • Loading branch information
panslava committed Aug 25, 2022
1 parent eb6d2ba commit 255c631
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/utils/namer/l4_namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func (namer *L4Namer) L4Backend(namespace, name string) (string, bool) {
truncFields := TrimFieldsEvenly(maximumL4CombinedLength, namespace, name)
truncNamespace := truncFields[0]
truncName := truncFields[1]
return strings.Join([]string{namer.v2Prefix, namer.v2ClusterUID, truncNamespace, truncName, namer.suffix(namespace, name)}, "-"), true

return strings.Join([]string{namer.v2Prefix, namer.v2ClusterUID, truncNamespace, truncName, namer.getClusterSuffix(namespace, name)}, "-"), true
}

// L4ForwardingRule returns the name of the L4 forwarding rule name based on the service namespace, name and protocol.
Expand All @@ -71,7 +70,7 @@ func (namer *L4Namer) L4ForwardingRule(namespace, name, protocol string) string
truncFields := TrimFieldsEvenly(maximumL4CombinedLength-protoLen, namespace, name)
truncNamespace := truncFields[0]
truncName := truncFields[1]
return strings.Join([]string{namer.v2Prefix, protocol, namer.v2ClusterUID, truncNamespace, truncName, namer.suffix(namespace, name)}, "-")
return strings.Join([]string{namer.v2Prefix, protocol, namer.v2ClusterUID, truncNamespace, truncName, namer.getClusterSuffix(namespace, name)}, "-")
}

// L4HealthCheck returns the name of the L4 LB Healthcheck
Expand All @@ -97,19 +96,28 @@ func (namer *L4Namer) IsNEG(name string) bool {
return strings.HasPrefix(name, namer.v2Prefix+"-"+namer.v2ClusterUID)
}

// suffix returns hash string of length 8 of a concatenated string generated from
// getClusterSuffix returns hash string of length 8 of a concatenated string generated from
// kube-system uid, namespace and name. These fields in combination define an l4 load-balancer uniquely.
func (n *L4Namer) suffix(namespace, name string) string {
func (n *L4Namer) getClusterSuffix(namespace, name string) string {
lbString := strings.Join([]string{n.v2ClusterUID, namespace, name}, ";")
return common.ContentHash(lbString, 8)
}

// hcFirewallName generates the firewall name for the given healthcheck.
// It ensures that the name is atmost 63 chars long.
func (n *L4Namer) hcFirewallName(hcName string) string {
maxHcNameLen := maxResourceNameLength - len(firewallHcSuffix)
if len(hcName) > maxHcNameLen {
hcName = hcName[:maxHcNameLen]
return getSuffixedName(hcName, firewallHcSuffix)
}

func getSuffixedName(name string, suffix string) string {
trimmedName := ensureSpaceForSuffix(name, suffix)
return trimmedName + suffix
}

func ensureSpaceForSuffix(name string, suffix string) string {
maxRealNameLen := maxResourceNameLength - len(suffix)
if len(name) > maxRealNameLen {
name = name[:maxRealNameLen]
}
return hcName + firewallHcSuffix
return name
}

0 comments on commit 255c631

Please sign in to comment.