Skip to content

Commit

Permalink
Addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
srahul3 committed Jun 7, 2023
1 parent 552c0e7 commit 14fa790
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions control-plane/catalog/to-consul/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func (t *ServiceResource) generateRegistrations(key string) {
// Adding information about service weight.
// Overrides the existing weight if present
if weight, ok := svc.Annotations[annotationServiceWeight]; ok || weight != "" {
err := t.setServiceWeight(weight, len(ips), r)
err := setServiceWeight(weight, len(ips), &r)
if err != nil {
t.Log.Debug("assertion: The service with ",
"key", key,
Expand Down Expand Up @@ -563,7 +563,7 @@ func (t *ServiceResource) generateRegistrations(key string) {
// Overrides the existing weight if present
if weight, ok := svc.Annotations[annotationServiceWeight]; ok || weight != "" {
ingress_count := len(svc.Status.LoadBalancer.Ingress)
err := t.setServiceWeight(weight, ingress_count, r)
err := setServiceWeight(weight, ingress_count, &r)
if err != nil {
t.Log.Debug("assertion: The service with ",
"key", key,
Expand Down Expand Up @@ -667,24 +667,6 @@ func (t *ServiceResource) generateRegistrations(key string) {
}
}

// Sets the passing service weight
func (t *ServiceResource) setServiceWeight(weight string, appsCount int, r consulapi.CatalogRegistration) error {
// error validation if the input param is a number
weightI, err := strconv.Atoi(weight)
if err != nil {
return err
} else if weightI > 1 {
var perAppWeight = weightI / appsCount
if perAppWeight < 1 {
perAppWeight = 1
}
r.Service.Weights = consulapi.AgentWeights{
Passing: perAppWeight,
}
}
return nil
}

func (t *ServiceResource) registerServiceInstance(
baseNode consulapi.CatalogRegistration,
baseService consulapi.AgentService,
Expand Down Expand Up @@ -1043,3 +1025,26 @@ func (t *ServiceResource) isIngressService(key string) bool {
func consulHealthCheckID(k8sNS string, serviceID string) string {
return fmt.Sprintf("%s/%s", k8sNS, serviceID)
}

// Sets the passing service weight
func setServiceWeight(weight string, appsCount int, r *consulapi.CatalogRegistration) error {
// error validation if the input param is a number
weightI, err := strconv.Atoi(weight)
if err != nil {
return err
}

if weightI <= 1 {
return nil
}

var perAppWeight = weightI / appsCount
if perAppWeight < 1 {
perAppWeight = 1
}
r.Service.Weights = consulapi.AgentWeights{
Passing: perAppWeight,
}

return nil
}

0 comments on commit 14fa790

Please sign in to comment.