GetReplicas replica count for function
(serviceName, serviceNamespace string)
| 60 | |
| 61 | // GetReplicas replica count for function |
| 62 | func (s ExternalServiceQuery) GetReplicas(serviceName, serviceNamespace string) (scaling.ServiceQueryResponse, error) { |
| 63 | start := time.Now() |
| 64 | |
| 65 | var err error |
| 66 | var emptyServiceQueryResponse scaling.ServiceQueryResponse |
| 67 | |
| 68 | function := types.FunctionStatus{} |
| 69 | |
| 70 | urlPath := fmt.Sprintf("%ssystem/function/%s?namespace=%s&usage=%v", |
| 71 | s.URL.String(), |
| 72 | serviceName, |
| 73 | serviceNamespace, |
| 74 | s.IncludeUsage) |
| 75 | |
| 76 | req, err := http.NewRequest(http.MethodGet, urlPath, nil) |
| 77 | if err != nil { |
| 78 | return emptyServiceQueryResponse, err |
| 79 | } |
| 80 | |
| 81 | if s.AuthInjector != nil { |
| 82 | s.AuthInjector.Inject(req) |
| 83 | } |
| 84 | |
| 85 | res, err := s.ProxyClient.Do(req) |
| 86 | if err != nil { |
| 87 | log.Println(urlPath, err) |
| 88 | return emptyServiceQueryResponse, err |
| 89 | |
| 90 | } |
| 91 | |
| 92 | var bytesOut []byte |
| 93 | if res.Body != nil { |
| 94 | bytesOut, _ = io.ReadAll(res.Body) |
| 95 | defer res.Body.Close() |
| 96 | } |
| 97 | |
| 98 | if res.StatusCode == http.StatusOK { |
| 99 | if err := json.Unmarshal(bytesOut, &function); err != nil { |
| 100 | log.Printf("Unable to unmarshal: %q, %s", string(bytesOut), err) |
| 101 | return emptyServiceQueryResponse, err |
| 102 | } |
| 103 | |
| 104 | // log.Printf("GetReplicas [%s.%s] took: %fs", serviceName, serviceNamespace, time.Since(start).Seconds()) |
| 105 | |
| 106 | } else { |
| 107 | log.Printf("GetReplicas [%s.%s] took: %.4fs, code: %d\n", serviceName, serviceNamespace, time.Since(start).Seconds(), res.StatusCode) |
| 108 | return emptyServiceQueryResponse, fmt.Errorf("server returned non-200 status code (%d) for function, %s, body: %s", res.StatusCode, serviceName, string(bytesOut)) |
| 109 | } |
| 110 | |
| 111 | minReplicas := uint64(scaling.DefaultMinReplicas) |
| 112 | maxReplicas := uint64(scaling.DefaultMaxReplicas) |
| 113 | scalingFactor := uint64(scaling.DefaultScalingFactor) |
| 114 | availableReplicas := function.AvailableReplicas |
| 115 | |
| 116 | if function.Labels != nil { |
| 117 | labels := *function.Labels |
| 118 | |
| 119 | minReplicas = extractLabelValue(labels[scaling.MinScaleLabel], minReplicas) |