1
0
Fork 0

Add HighestRandomWeight Loadbalancing Algorithm

This commit is contained in:
mathieuHa 2025-09-08 12:00:42 +02:00 committed by GitHub
parent 9b42b5b930
commit 02443545e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 936 additions and 112 deletions

View file

@ -53,10 +53,11 @@ type Model struct {
// Service holds a service configuration (can only be of one type at the same time).
type Service struct {
LoadBalancer *ServersLoadBalancer `json:"loadBalancer,omitempty" toml:"loadBalancer,omitempty" yaml:"loadBalancer,omitempty" export:"true"`
Weighted *WeightedRoundRobin `json:"weighted,omitempty" toml:"weighted,omitempty" yaml:"weighted,omitempty" label:"-" export:"true"`
Mirroring *Mirroring `json:"mirroring,omitempty" toml:"mirroring,omitempty" yaml:"mirroring,omitempty" label:"-" export:"true"`
Failover *Failover `json:"failover,omitempty" toml:"failover,omitempty" yaml:"failover,omitempty" label:"-" export:"true"`
LoadBalancer *ServersLoadBalancer `json:"loadBalancer,omitempty" toml:"loadBalancer,omitempty" yaml:"loadBalancer,omitempty" export:"true"`
HighestRandomWeight *HighestRandomWeight `json:"highestRandomWeight,omitempty" toml:"highestRandomWeight,omitempty" yaml:"highestRandomWeight,omitempty" label:"-" export:"true"`
Weighted *WeightedRoundRobin `json:"weighted,omitempty" toml:"weighted,omitempty" yaml:"weighted,omitempty" label:"-" export:"true"`
Mirroring *Mirroring `json:"mirroring,omitempty" toml:"mirroring,omitempty" yaml:"mirroring,omitempty" label:"-" export:"true"`
Failover *Failover `json:"failover,omitempty" toml:"failover,omitempty" yaml:"failover,omitempty" label:"-" export:"true"`
}
// +k8s:deepcopy-gen=true
@ -157,6 +158,19 @@ type WeightedRoundRobin struct {
// +k8s:deepcopy-gen=true
// HighestRandomWeight is a weighted sticky load-balancer of services.
type HighestRandomWeight struct {
Services []HRWService `json:"services,omitempty" toml:"services,omitempty" yaml:"services,omitempty" export:"true"`
// HealthCheck enables automatic self-healthcheck for this service, i.e.
// whenever one of its children is reported as down, this service becomes aware of it,
// and takes it into account (i.e. it ignores the down child) when running the
// load-balancing algorithm. In addition, if the parent of this service also has
// HealthCheck enabled, this service reports to its parent any status change.
HealthCheck *HealthCheck `json:"healthCheck,omitempty" toml:"healthCheck,omitempty" yaml:"healthCheck,omitempty" label:"allowEmpty" file:"allowEmpty" kv:"allowEmpty" export:"true"`
}
// +k8s:deepcopy-gen=true
// WRRService is a reference to a service load-balanced with weighted round-robin.
type WRRService struct {
Name string `json:"name,omitempty" toml:"name,omitempty" yaml:"name,omitempty" export:"true"`
@ -170,6 +184,20 @@ type WRRService struct {
GRPCStatus *GRPCStatus `json:"-" toml:"-" yaml:"-" label:"-" file:"-"`
}
// +k8s:deepcopy-gen=true
// HRWService is a reference to a service load-balanced with highest random weight.
type HRWService struct {
Name string `json:"name,omitempty" toml:"name,omitempty" yaml:"name,omitempty" export:"true"`
Weight *int `json:"weight,omitempty" toml:"weight,omitempty" yaml:"weight,omitempty" export:"true"`
}
// SetDefaults Default values for a HRWService.
func (w *HRWService) SetDefaults() {
defaultWeight := 1
w.Weight = &defaultWeight
}
// SetDefaults Default values for a WRRService.
func (w *WRRService) SetDefaults() {
defaultWeight := 1
@ -231,6 +259,8 @@ const (
BalancerStrategyWRR BalancerStrategy = "wrr"
// BalancerStrategyP2C is the power of two choices strategy.
BalancerStrategyP2C BalancerStrategy = "p2c"
// BalancerStrategyHRW is the power of two choices strategy.
BalancerStrategyHRW BalancerStrategy = "hrw"
)
// +k8s:deepcopy-gen=true

View file

@ -449,6 +449,27 @@ func (in *GrpcWeb) DeepCopy() *GrpcWeb {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *HRWService) DeepCopyInto(out *HRWService) {
*out = *in
if in.Weight != nil {
in, out := &in.Weight, &out.Weight
*out = new(int)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HRWService.
func (in *HRWService) DeepCopy() *HRWService {
if in == nil {
return nil
}
out := new(HRWService)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *HTTPConfiguration) DeepCopyInto(out *HTTPConfiguration) {
*out = *in
@ -688,6 +709,34 @@ func (in *HealthCheck) DeepCopy() *HealthCheck {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *HighestRandomWeight) DeepCopyInto(out *HighestRandomWeight) {
*out = *in
if in.Services != nil {
in, out := &in.Services, &out.Services
*out = make([]HRWService, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.HealthCheck != nil {
in, out := &in.HealthCheck, &out.HealthCheck
*out = new(HealthCheck)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HighestRandomWeight.
func (in *HighestRandomWeight) DeepCopy() *HighestRandomWeight {
if in == nil {
return nil
}
out := new(HighestRandomWeight)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *IPAllowList) DeepCopyInto(out *IPAllowList) {
*out = *in
@ -1566,6 +1615,11 @@ func (in *Service) DeepCopyInto(out *Service) {
*out = new(ServersLoadBalancer)
(*in).DeepCopyInto(*out)
}
if in.HighestRandomWeight != nil {
in, out := &in.HighestRandomWeight, &out.HighestRandomWeight
*out = new(HighestRandomWeight)
(*in).DeepCopyInto(*out)
}
if in.Weighted != nil {
in, out := &in.Weighted, &out.Weighted
*out = new(WeightedRoundRobin)