-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for weighted k8s service #2293
Changes from 5 commits
fdef617
7d47c00
552c0e7
14fa790
9f396c3
3320b62
dc3e24e
3dad9a0
95b3f7c
fa54600
21984e9
31c1506
9a25cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
sync-catalog: add ability to support weighted loadbalancing by service annotation `consul.hashicorp.com/service-weight: <number>` | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -511,6 +511,18 @@ func (t *ServiceResource) generateRegistrations(key string) { | |
r.Service = &rs | ||
r.Service.ID = serviceID(r.Service.Service, ip) | ||
r.Service.Address = ip | ||
// Adding information about service weight. | ||
// Overrides the existing weight if present | ||
if weight, ok := svc.Annotations[annotationServiceWeight]; ok && weight != "" { | ||
err := setServiceWeight(weight, len(ips), &r) | ||
if err != nil { | ||
t.Log.Debug("assertion: The service with ", | ||
"key", key, | ||
"service", baseService.Service, | ||
"namespace", baseService.Namespace, | ||
"expects a positive integer value, however observed value was not integer: ", weight) | ||
} | ||
} | ||
t.consulMap[key] = append(t.consulMap[key], &r) | ||
} | ||
|
||
|
@@ -547,6 +559,20 @@ func (t *ServiceResource) generateRegistrations(key string) { | |
r.Service.ID = serviceID(r.Service.Service, addr) | ||
r.Service.Address = addr | ||
|
||
// Adding information about service weight. | ||
// Overrides the existing weight if present | ||
if weight, ok := svc.Annotations[annotationServiceWeight]; ok && weight != "" { | ||
ingress_count := len(svc.Status.LoadBalancer.Ingress) | ||
err := setServiceWeight(weight, ingress_count, &r) | ||
if err != nil { | ||
t.Log.Debug("assertion: The service with ", | ||
"key", key, | ||
"service", baseService.Service, | ||
"namespace", baseService.Namespace, | ||
"expects a positive integer value, however observed value was not integer: ", weight) | ||
} | ||
} | ||
|
||
t.consulMap[key] = append(t.consulMap[key], &r) | ||
} | ||
} | ||
|
@@ -999,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. id change this method to return the service weight or an error, providing the value of the annotation as an input. we should avoid sending in the consul registration into it. it will make this is a cleaner interface that is significantly easier to test. |
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can set weight to whatever value we are getting. We don't need to divide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup! we should set the weight that is set on the annotation and we dont need the app count for the reasons @absolutelightning described! |
||
if perAppWeight < 1 { | ||
perAppWeight = 1 | ||
} | ||
r.Service.Weights = consulapi.AgentWeights{ | ||
Passing: perAppWeight, | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these lines 567 - 577 and lines 518 - 528 are exactly the same. can we take it out in a function as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code at both place is constructing the CatalogRegistration object, there is no logic which is duplicate. Read the comment #2293 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is ok to keep as is. it appears that the only behavior here is an assignment which IMO is not really duplication.