forked from openfaas/faas-netes
/
replicas.go
125 lines (96 loc) · 3.16 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Alex Ellis 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"
"github.com/gorilla/mux"
"github.com/ngduchai/faas-netes/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// MakeReplicaUpdater updates desired count of replicas
func MakeReplicaUpdater(defaultNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Update replicas")
vars := mux.Vars(r)
functionName := vars["name"]
q := r.URL.Query()
namespace := q.Get("namespace")
lookupNamespace := defaultNamespace
if len(namespace) > 0 {
lookupNamespace = namespace
}
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
}
}
if len(req.ServiceNamespace) > 0 {
lookupNamespace = req.ServiceNamespace
}
options := metav1.GetOptions{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
}
deployment, err := clientset.Apps().Deployments(lookupNamespace).Get(functionName, options)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Unable to lookup function deployment " + functionName))
log.Println(err)
return
}
oldReplicas := *deployment.Spec.Replicas
replicas := int32(req.Replicas)
log.Printf("Set replicas - %s %s, %d/%d\n", functionName, lookupNamespace, replicas, oldReplicas)
deployment.Spec.Replicas = &replicas
_, err = clientset.Apps().Deployments(lookupNamespace).Update(deployment)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Unable to update function deployment " + functionName))
log.Println(err)
return
}
w.WriteHeader(http.StatusAccepted)
}
}
// MakeReplicaReader reads the amount of replicas for a deployment
func MakeReplicaReader(defaultNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
functionName := vars["name"]
q := r.URL.Query()
namespace := q.Get("namespace")
lookupNamespace := defaultNamespace
if len(namespace) > 0 {
lookupNamespace = namespace
}
function, err := getService(lookupNamespace, functionName, clientset)
if err != nil {
log.Printf("Unable to fetch service: %s %s\n", functionName, namespace)
w.WriteHeader(http.StatusInternalServerError)
return
}
if function == nil {
w.WriteHeader(http.StatusNotFound)
return
}
log.Printf("Read replicas - %s %s, %d/%d\n", functionName, lookupNamespace, function.AvailableReplicas, function.Replicas)
functionBytes, _ := json.Marshal(function)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(functionBytes)
}
}