Skip to content

Commit 7c42009

Browse files
committed
router: clean up service unit configuration
Minor refactor and rename of the routeKeys (now getServiceUnits) utility function.
1 parent cecf7b4 commit 7c42009

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

pkg/router/template/plugin_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,10 @@ func (r *TestRouter) AddRoute(route *routeapi.Route) {
178178
config := ServiceAliasConfig{
179179
Host: route.Spec.Host,
180180
Path: route.Spec.Path,
181-
ServiceUnitNames: make(map[string]int32),
181+
ServiceUnitNames: getServiceUnits(route),
182182
}
183183

184-
serviceKeys, weights := routeKeys(route)
185-
for i, key := range serviceKeys {
186-
config.ServiceUnitNames[key] = weights[i]
184+
for key := range config.ServiceUnitNames {
187185
r.CreateServiceUnit(key)
188186
}
189187

pkg/router/template/router.go

+21-24
Original file line numberDiff line numberDiff line change
@@ -584,13 +584,16 @@ func (r *templateRouter) createServiceAliasConfig(route *routeapi.Route, routeKe
584584
Path: route.Spec.Path,
585585
IsWildcard: wildcard,
586586
Annotations: route.Annotations,
587-
ServiceUnitNames: make(map[string]int32),
587+
ServiceUnitNames: getServiceUnits(route),
588588
}
589589

590590
if route.Spec.Port != nil {
591591
config.PreferPort = route.Spec.Port.TargetPort.String()
592592
}
593593

594+
key := fmt.Sprintf("%s %s", config.TLSTermination, routeKey)
595+
config.RoutingKeyName = fmt.Sprintf("%x", md5.Sum([]byte(key)))
596+
594597
tls := route.Spec.TLS
595598
if tls != nil && len(tls.Termination) > 0 {
596599
config.TLSTermination = tls.Termination
@@ -633,16 +636,6 @@ func (r *templateRouter) createServiceAliasConfig(route *routeapi.Route, routeKe
633636
}
634637
}
635638

636-
key := fmt.Sprintf("%s %s", config.TLSTermination, routeKey)
637-
config.RoutingKeyName = fmt.Sprintf("%x", md5.Sum([]byte(key)))
638-
639-
// Weight suggests the % of traffic that a given service will
640-
// receive compared to other services pointed to by the route.
641-
serviceKeys, weights := routeKeys(route)
642-
for i, key := range serviceKeys {
643-
config.ServiceUnitNames[key] = weights[i]
644-
}
645-
646639
return &config
647640
}
648641

@@ -824,22 +817,26 @@ func generateDestCertKey(config *ServiceAliasConfig) string {
824817
return config.Host + destCertPostfix
825818
}
826819

827-
// routeKeys returns the internal router keys to use for the given Route.
828-
// A route can have several services that it can point to, now
829-
func routeKeys(route *routeapi.Route) ([]string, []int32) {
830-
keys := make([]string, 1+len(route.Spec.AlternateBackends))
831-
weights := make([]int32, 1+len(route.Spec.AlternateBackends))
832-
keys[0] = fmt.Sprintf("%s/%s", route.Namespace, route.Spec.To.Name)
833-
if route.Spec.To.Weight != nil {
834-
weights[0] = *route.Spec.To.Weight
820+
// getServiceUnits returns a map of service keys to their weights.
821+
// Weight suggests the % of traffic that a given service will receive
822+
// compared to other services pointed to by the route.
823+
func getServiceUnits(route *routeapi.Route) map[string]int32 {
824+
serviceUnits := make(map[string]int32)
825+
key := fmt.Sprintf("%s/%s", route.Namespace, route.Spec.To.Name)
826+
if route.Spec.To.Weight == nil {
827+
serviceUnits[key] = 0
828+
} else {
829+
serviceUnits[key] = *route.Spec.To.Weight
835830
}
836-
for i, svc := range route.Spec.AlternateBackends {
837-
keys[i+1] = fmt.Sprintf("%s/%s", route.Namespace, svc.Name)
838-
if svc.Weight != nil {
839-
weights[i+1] = *svc.Weight
831+
for _, svc := range route.Spec.AlternateBackends {
832+
key = fmt.Sprintf("%s/%s", route.Namespace, svc.Name)
833+
if svc.Weight == nil {
834+
serviceUnits[key] = 0
835+
} else {
836+
serviceUnits[key] = *svc.Weight
840837
}
841838
}
842-
return keys, weights
839+
return serviceUnits
843840
}
844841

845842
// configsAreEqual determines whether the given service alias configs can be considered equal.

0 commit comments

Comments
 (0)