1
0
Fork 0

feat: add highest random weight in kubernetes CRD

This commit is contained in:
Landry Benguigui 2025-09-19 10:18:04 +02:00 committed by GitHub
parent c09d3fb03c
commit 634f892370
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 1328 additions and 24 deletions

View file

@ -216,13 +216,17 @@ type configBuilder struct {
func (c configBuilder) buildTraefikService(ctx context.Context, tService *traefikv1alpha1.TraefikService, conf map[string]*dynamic.Service) error {
id := provider.Normalize(makeID(tService.Namespace, tService.Name))
if tService.Spec.Weighted != nil {
switch {
case tService.Spec.Weighted != nil:
return c.buildServicesLB(ctx, tService.Namespace, tService.Spec, id, conf)
} else if tService.Spec.Mirroring != nil {
case tService.Spec.Mirroring != nil:
return c.buildMirroring(ctx, tService, id, conf)
}
case tService.Spec.HighestRandomWeight != nil:
return c.buildHRW(ctx, tService, id, conf)
default:
return errors.New("unspecified service type")
return errors.New("unspecified service type")
}
}
// buildServicesLB creates the configuration for the load-balancer of services named id, and defined in tService.
@ -329,7 +333,7 @@ func (c configBuilder) buildServersLB(namespace string, svc traefikv1alpha1.Load
// TODO: remove this when the fake client apply default values.
if svc.Strategy != "" {
switch svc.Strategy {
case dynamic.BalancerStrategyWRR, dynamic.BalancerStrategyP2C:
case dynamic.BalancerStrategyWRR, dynamic.BalancerStrategyP2C, dynamic.BalancerStrategyHRW:
lb.Strategy = svc.Strategy
// Here we are just logging a warning as the default value is already applied.
@ -644,6 +648,38 @@ func (c configBuilder) nameAndService(ctx context.Context, parentNamespace strin
}
}
func (c configBuilder) buildHRW(ctx context.Context, tService *traefikv1alpha1.TraefikService, id string, conf map[string]*dynamic.Service) error {
var hrwServices []dynamic.HRWService
for _, hrwService := range tService.Spec.HighestRandomWeight.Services {
hrwServiceName, k8sService, err := c.nameAndService(ctx, tService.Namespace, hrwService.LoadBalancerSpec)
if err != nil {
return err
}
if k8sService != nil {
conf[hrwServiceName] = k8sService
}
weight := hrwService.Weight
if weight == nil {
weight = func(i int) *int { return &i }(1)
}
hrwServices = append(hrwServices, dynamic.HRWService{
Name: hrwServiceName,
Weight: weight,
})
}
conf[id] = &dynamic.Service{
HighestRandomWeight: &dynamic.HighestRandomWeight{
Services: hrwServices,
},
}
return nil
}
func splitSvcNameProvider(name string) (string, string) {
parts := strings.Split(name, providerNamespaceSeparator)