(alert requests.PrometheusInnerAlert, service scaling.ServiceQuery, defaultNamespace string)
| 75 | } |
| 76 | |
| 77 | func scaleService(alert requests.PrometheusInnerAlert, service scaling.ServiceQuery, defaultNamespace string) error { |
| 78 | var err error |
| 79 | |
| 80 | serviceName, namespace := middleware.GetNamespace(defaultNamespace, alert.Labels.FunctionName) |
| 81 | |
| 82 | if len(serviceName) > 0 { |
| 83 | queryResponse, getErr := service.GetReplicas(serviceName, namespace) |
| 84 | if getErr == nil { |
| 85 | status := alert.Status |
| 86 | |
| 87 | newReplicas := CalculateReplicas(status, queryResponse.Replicas, uint64(queryResponse.MaxReplicas), queryResponse.MinReplicas, queryResponse.ScalingFactor) |
| 88 | |
| 89 | log.Printf("[Scale] function=%s %d => %d.\n", serviceName, queryResponse.Replicas, newReplicas) |
| 90 | if newReplicas == queryResponse.Replicas { |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | updateErr := service.SetReplicas(serviceName, namespace, newReplicas) |
| 95 | if updateErr != nil { |
| 96 | err = updateErr |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | // CalculateReplicas decides what replica count to set depending on current/desired amount |
| 104 | func CalculateReplicas(status string, currentReplicas uint64, maxReplicas uint64, minReplicas uint64, scalingFactor uint64) uint64 { |
no test coverage detected
searching dependent graphs…