Skip to content

Commit caf1eb0

Browse files
committed
Implement default upper bound for scaling
OpenFaaS CE is licensed for 5/5 replicas, for higher load get in touch with us about OpenFaaS Standard. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 6da860a commit caf1eb0

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

pkg/handlers/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"net/http"
1313

1414
"github.com/openfaas/faas-provider/types"
@@ -42,7 +42,7 @@ func MakeDeleteHandler(defaultNamespace string, clientset *kubernetes.Clientset)
4242
return
4343
}
4444

45-
body, _ := ioutil.ReadAll(r.Body)
45+
body, _ := io.ReadAll(r.Body)
4646

4747
request := types.DeleteFunctionRequest{}
4848
err := json.Unmarshal(body, &request)

pkg/handlers/deploy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"log"
1313
"net/http"
1414
"sort"
@@ -40,7 +40,7 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht
4040
defer r.Body.Close()
4141
}
4242

43-
body, _ := ioutil.ReadAll(r.Body)
43+
body, _ := io.ReadAll(r.Body)
4444

4545
request := types.FunctionDeployment{}
4646
err := json.Unmarshal(body, &request)

pkg/handlers/namespaces.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"encoding/json"
1111
"fmt"
12+
"io"
1213
"io/ioutil"
1314
"log"
1415
"net/http"
@@ -65,7 +66,7 @@ func NewNamespaceResolver(defaultNamespace string, kube kubernetes.Interface) Na
6566
}
6667

6768
case http.MethodPost, http.MethodPut, http.MethodDelete:
68-
body, _ := ioutil.ReadAll(r.Body)
69+
body, _ := io.ReadAll(r.Body)
6970
err := json.Unmarshal(body, &req)
7071
if err != nil {
7172
log.Printf("error while getting namespace: %s\n", err)

pkg/handlers/replica_reader.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
glog "k8s.io/klog"
2020
)
2121

22+
// MaxReplicas licensed for OpenFaaS CE is 5/5
23+
const MaxReplicas = 5
24+
2225
// MakeReplicaReader reads the amount of replicas for a deployment
2326
func MakeReplicaReader(defaultNamespace string, lister v1.DeploymentLister) http.HandlerFunc {
2427
return func(w http.ResponseWriter, r *http.Request) {

pkg/handlers/replica_updater.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"log"
1313
"net/http"
1414

@@ -44,7 +44,7 @@ func MakeReplicaUpdater(defaultNamespace string, clientset *kubernetes.Clientset
4444

4545
if r.Body != nil {
4646
defer r.Body.Close()
47-
bytesIn, _ := ioutil.ReadAll(r.Body)
47+
bytesIn, _ := io.ReadAll(r.Body)
4848
marshalErr := json.Unmarshal(bytesIn, &req)
4949
if marshalErr != nil {
5050
w.WriteHeader(http.StatusBadRequest)
@@ -56,7 +56,9 @@ func MakeReplicaUpdater(defaultNamespace string, clientset *kubernetes.Clientset
5656
}
5757

5858
if req.Replicas == 0 {
59-
http.Error(w, fmt.Errorf("for OpenFaaS CE, replicas cannot be set to 0").Error(), http.StatusBadRequest)
59+
http.Error(w, "replicas cannot be set to 0 in OpenFaaS CE",
60+
http.StatusBadRequest)
61+
return
6062
}
6163

6264
options := metav1.GetOptions{
@@ -77,17 +79,19 @@ func MakeReplicaUpdater(defaultNamespace string, clientset *kubernetes.Clientset
7779

7880
oldReplicas := *deployment.Spec.Replicas
7981
replicas := int32(req.Replicas)
82+
if replicas >= MaxReplicas {
83+
replicas = MaxReplicas
84+
}
8085

8186
log.Printf("Set replicas - %s %s, %d/%d\n", functionName, lookupNamespace, replicas, oldReplicas)
8287

8388
deployment.Spec.Replicas = &replicas
8489

85-
_, err = clientset.AppsV1().Deployments(lookupNamespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
90+
if _, err = clientset.AppsV1().Deployments(lookupNamespace).
91+
Update(context.TODO(), deployment, metav1.UpdateOptions{}); err != nil {
8692

87-
if err != nil {
88-
w.WriteHeader(http.StatusInternalServerError)
89-
w.Write([]byte("Unable to update function deployment " + functionName))
90-
log.Println(err)
93+
log.Printf("unable to update function deployment: %s, %s", functionName, err)
94+
http.Error(w, fmt.Sprintf("unable to update function deployment: %s", functionName), http.StatusInternalServerError)
9195
return
9296
}
9397

pkg/handlers/secrets_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"net/http"
1313
"net/http/httptest"
1414
"strings"
@@ -284,7 +284,7 @@ func Test_SecretsHandler_ListEmpty(t *testing.T) {
284284
if resp.StatusCode != http.StatusOK {
285285
t.Errorf("want status code '%d', got '%d'", http.StatusOK, resp.StatusCode)
286286
}
287-
body, _ := ioutil.ReadAll(resp.Body)
287+
body, _ := io.ReadAll(resp.Body)
288288

289289
if string(body) == "null" {
290290
t.Errorf(`want empty list to be valid json i.e. "[]", but was %q`, string(body))

pkg/handlers/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"context"
88
"encoding/json"
99
"fmt"
10-
"io/ioutil"
10+
"io"
1111
"log"
1212
"net/http"
1313
"time"
@@ -26,7 +26,7 @@ func MakeUpdateHandler(defaultNamespace string, factory k8s.FunctionFactory) htt
2626
defer r.Body.Close()
2727
}
2828

29-
body, _ := ioutil.ReadAll(r.Body)
29+
body, _ := io.ReadAll(r.Body)
3030

3131
request := types.FunctionDeployment{}
3232
err := json.Unmarshal(body, &request)

0 commit comments

Comments
 (0)