MCPcopy
hub / github.com/openfaas/faas / SetReplicas

Method SetReplicas

gateway/plugin/external.go:141–181  ·  view source on GitHub ↗

SetReplicas update the replica count

(serviceName, serviceNamespace string, count uint64)

Source from the content-addressed store, hash-verified

139
140// SetReplicas update the replica count
141func (s ExternalServiceQuery) SetReplicas(serviceName, serviceNamespace string, count uint64) error {
142 var err error
143
144 scaleReq := types.ScaleServiceRequest{
145 ServiceName: serviceName,
146 Replicas: count,
147 }
148
149 requestBody, err := json.Marshal(scaleReq)
150 if err != nil {
151 return err
152 }
153
154 start := time.Now()
155 urlPath := fmt.Sprintf("%ssystem/scale-function/%s?namespace=%s", s.URL.String(), serviceName, serviceNamespace)
156 req, _ := http.NewRequest(http.MethodPost, urlPath, bytes.NewReader(requestBody))
157
158 if s.AuthInjector != nil {
159 s.AuthInjector.Inject(req)
160 }
161
162 defer req.Body.Close()
163 res, err := s.ProxyClient.Do(req)
164
165 if err != nil {
166 log.Println(urlPath, err)
167 } else {
168 if res.Body != nil {
169 defer res.Body.Close()
170 }
171 }
172
173 if !(res.StatusCode == http.StatusOK || res.StatusCode == http.StatusAccepted) {
174 err = fmt.Errorf("error scaling HTTP code %d, %s", res.StatusCode, urlPath)
175 }
176
177 log.Printf("SetReplicas [%s.%s] took: %.4fs",
178 serviceName, serviceNamespace, time.Since(start).Seconds())
179
180 return err
181}
182
183// extractLabelValue will parse the provided raw label value and if it fails
184// it will return the provided fallback value and log an message

Callers 2

Calls 2

DoMethod · 0.80
InjectMethod · 0.65

Tested by 2