mirror of
https://github.com/openfaas/faas.git
synced 2025-06-21 14:23:25 +00:00
Disable KeepAlive on proxy (#21)
* Service lookup * Disable KeepAlive to force RR for replias. * set KeepAlive to 0 * Remove temporary file * Support white and rename function * Remove debug from node sample
This commit is contained in:
@ -12,63 +12,14 @@ import (
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
// CalculateReplicas decides what replica count to set depending on a Prometheus alert
|
||||
func CalculateReplicas(status string, currentReplicas uint64) uint64 {
|
||||
newReplicas := currentReplicas
|
||||
|
||||
if status == "firing" {
|
||||
if currentReplicas == 1 {
|
||||
newReplicas = 5
|
||||
} else {
|
||||
if currentReplicas+5 > 20 {
|
||||
newReplicas = 20
|
||||
} else {
|
||||
newReplicas = currentReplicas + 5
|
||||
}
|
||||
}
|
||||
} else { // Resolved event.
|
||||
newReplicas = 1
|
||||
}
|
||||
return newReplicas
|
||||
}
|
||||
|
||||
func scaleService(req requests.PrometheusAlert, c *client.Client) error {
|
||||
var err error
|
||||
//Todo: convert to loop / handler.
|
||||
serviceName := req.Alerts[0].Labels.FunctionName
|
||||
service, _, inspectErr := c.ServiceInspectWithRaw(context.Background(), serviceName)
|
||||
if inspectErr == nil {
|
||||
|
||||
currentReplicas := *service.Spec.Mode.Replicated.Replicas
|
||||
status := req.Status
|
||||
newReplicas := CalculateReplicas(status, currentReplicas)
|
||||
|
||||
if newReplicas == currentReplicas {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("Scaling %s to %d replicas.\n", serviceName, newReplicas)
|
||||
service.Spec.Mode.Replicated.Replicas = &newReplicas
|
||||
updateOpts := types.ServiceUpdateOptions{}
|
||||
updateOpts.RegistryAuthFrom = types.RegistryAuthFromSpec
|
||||
|
||||
response, updateErr := c.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, updateOpts)
|
||||
if updateErr != nil {
|
||||
err = updateErr
|
||||
}
|
||||
log.Println(response)
|
||||
|
||||
} else {
|
||||
err = inspectErr
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// MakeAlertHandler handles alerts from Prometheus Alertmanager
|
||||
func MakeAlertHandler(c *client.Client) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("Alert received.")
|
||||
body, readErr := ioutil.ReadAll(r.Body)
|
||||
|
||||
log.Println(string(body))
|
||||
|
||||
if readErr != nil {
|
||||
log.Println(readErr)
|
||||
return
|
||||
@ -92,3 +43,58 @@ func MakeAlertHandler(c *client.Client) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CalculateReplicas decides what replica count to set depending on a Prometheus alert
|
||||
func CalculateReplicas(status string, currentReplicas uint64) uint64 {
|
||||
newReplicas := currentReplicas
|
||||
|
||||
if status == "firing" {
|
||||
if currentReplicas == 1 {
|
||||
newReplicas = 5
|
||||
} else {
|
||||
if currentReplicas+5 > 20 {
|
||||
newReplicas = 20
|
||||
} else {
|
||||
newReplicas = currentReplicas + 5
|
||||
}
|
||||
}
|
||||
} else { // Resolved event.
|
||||
newReplicas = 1
|
||||
}
|
||||
return newReplicas
|
||||
}
|
||||
|
||||
func scaleService(req requests.PrometheusAlert, c *client.Client) error {
|
||||
var err error
|
||||
serviceName := req.Alerts[0].Labels.FunctionName
|
||||
|
||||
if len(serviceName) > 0 {
|
||||
|
||||
service, _, inspectErr := c.ServiceInspectWithRaw(context.Background(), serviceName)
|
||||
if inspectErr == nil {
|
||||
|
||||
currentReplicas := *service.Spec.Mode.Replicated.Replicas
|
||||
status := req.Status
|
||||
newReplicas := CalculateReplicas(status, currentReplicas)
|
||||
|
||||
if newReplicas == currentReplicas {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("Scaling %s to %d replicas.\n", serviceName, newReplicas)
|
||||
service.Spec.Mode.Replicated.Replicas = &newReplicas
|
||||
updateOpts := types.ServiceUpdateOptions{}
|
||||
updateOpts.RegistryAuthFrom = types.RegistryAuthFromSpec
|
||||
|
||||
response, updateErr := c.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, updateOpts)
|
||||
if updateErr != nil {
|
||||
err = updateErr
|
||||
}
|
||||
log.Println(response)
|
||||
|
||||
} else {
|
||||
err = inspectErr
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user