This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreplicas.go
93 lines (76 loc) · 2.28 KB
/
replicas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Alex Ellis 2017, Ken Fukuyama 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"github.com/alexellis/faas/gateway/requests"
"github.com/openfaas-incubator/faas-rancher/rancher"
"github.com/openfaas-incubator/faas-rancher/types"
)
// MakeReplicaUpdater updates desired count of replicas
func MakeReplicaUpdater(client rancher.BridgeClient) VarsHandler {
return func(w http.ResponseWriter, r *http.Request, vars map[string]string) {
log.Println("Update replicas")
functionName := vars["name"]
req := types.ScaleServiceRequest{}
if r.Body != nil {
defer r.Body.Close()
bytesIn, _ := ioutil.ReadAll(r.Body)
marshalErr := json.Unmarshal(bytesIn, &req)
if marshalErr != nil {
w.WriteHeader(http.StatusBadRequest)
msg := "Cannot parse request. Please pass valid JSON."
w.Write([]byte(msg))
log.Println(msg, marshalErr)
return
}
}
service, findErr := client.FindServiceByName(functionName)
if findErr != nil {
w.WriteHeader(500)
w.Write([]byte("Unable to lookup function deployment " + functionName))
log.Println(findErr)
return
}
updates := make(map[string]string)
updates["scale"] = strconv.FormatInt(req.Replicas, 10)
_, upgradeErr := client.UpdateService(service, updates)
if upgradeErr != nil {
w.WriteHeader(500)
w.Write([]byte("Unable to update function deployment " + functionName))
log.Println(upgradeErr)
return
}
}
}
// MakeReplicaReader reads the amount of replicas for a deployment
func MakeReplicaReader(client rancher.BridgeClient) VarsHandler {
return func(w http.ResponseWriter, r *http.Request, vars map[string]string) {
log.Println("Read replicas")
functionName := vars["name"]
functions, err := getServiceList(client)
if err != nil {
w.WriteHeader(500)
return
}
var found *requests.Function
for _, function := range functions {
if function.Name == functionName {
found = &function
break
}
}
if found == nil {
w.WriteHeader(404)
return
}
functionBytes, _ := json.Marshal(found)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(functionBytes)
}
}