-
Notifications
You must be signed in to change notification settings - Fork 23
/
dep_scale.go
171 lines (150 loc) · 4.95 KB
/
dep_scale.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rest
import (
"fmt"
"net/http"
"path"
"strconv"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/ystia/yorc/deployments"
"github.com/ystia/yorc/log"
"github.com/ystia/yorc/tasks"
)
func (s *Server) scaleHandler(w http.ResponseWriter, r *http.Request) {
var params httprouter.Params
ctx := r.Context()
params = ctx.Value(paramsLookupKey).(httprouter.Params)
id := params.ByName("id")
nodeName := params.ByName("nodeName")
kv := s.consulClient.KV()
dExits, err := deployments.DoesDeploymentExists(s.consulClient.KV(), id)
if err != nil {
log.Panicf("%v", err)
}
if !dExits {
writeError(w, r, errNotFound)
return
}
if len(nodeName) == 0 {
log.Panic("You must provide a nodename")
}
var instancesDelta int
if value, ok := r.URL.Query()["delta"]; ok {
if instancesDelta, err = strconv.Atoi(value[0]); err != nil {
writeError(w, r, newBadRequestError(err))
return
} else if instancesDelta == 0 {
writeError(w, r, newBadRequestError(errors.New("You need to provide a non zero value as 'delta' parameter")))
return
}
} else {
writeError(w, r, newBadRequestError(errors.New("You need to provide a 'delta' parameter")))
return
}
exists, err := deployments.DoesNodeExist(kv, id, nodeName)
if err != nil {
log.Panic(err)
}
if !exists {
writeError(w, r, errNotFound)
return
}
var ok bool
if ok, err = deployments.HasScalableCapability(kv, id, nodeName); err != nil {
log.Panic(err)
} else if !ok {
writeError(w, r, newBadRequestParameter("node", errors.Errorf("Node %q must be scalable", nodeName)))
return
}
log.Debugf("Scaling %d instances of node %q", instancesDelta, nodeName)
var taskID string
if instancesDelta > 0 {
taskID, err = s.scaleOut(id, nodeName, uint32(instancesDelta))
} else {
taskID, err = s.scaleIn(id, nodeName, uint32(-instancesDelta))
}
if err != nil {
if ok, _ := tasks.IsAnotherLivingTaskAlreadyExistsError(err); ok {
writeError(w, r, newBadRequestError(err))
return
}
if restError, ok := err.(*Error); ok {
writeError(w, r, restError)
return
}
log.Panic(err)
}
w.Header().Set("Location", fmt.Sprintf("/deployments/%s/tasks/%s", id, taskID))
w.WriteHeader(http.StatusAccepted)
}
func (s *Server) scaleOut(id, nodeName string, instancesDelta uint32) (string, error) {
kv := s.consulClient.KV()
maxInstances, err := deployments.GetMaxNbInstancesForNode(kv, id, nodeName)
if err != nil {
return "", err
}
currentNbInstance, err := deployments.GetNbInstancesForNode(kv, id, nodeName)
if err != nil {
return "", err
}
if currentNbInstance+instancesDelta > maxInstances {
log.Debug("The delta is too high, the max instances number is chosen")
instancesDelta = maxInstances - currentNbInstance
if instancesDelta == 0 {
return "", newBadRequestMessage("Maximum number of instances reached")
}
}
instancesByNodes, err := deployments.CreateNewNodeStackInstances(kv, id, nodeName, int(instancesDelta))
if err != nil {
return "", err
}
data := make(map[string]string)
for scalableNode, nodeInstances := range instancesByNodes {
data[path.Join("nodes", scalableNode)] = nodeInstances
}
// Add related workflow
data["workflowName"] = "install"
return s.tasksCollector.RegisterTaskWithData(id, tasks.TaskTypeScaleOut, data)
}
func (s *Server) scaleIn(id, nodeName string, instancesDelta uint32) (string, error) {
kv := s.consulClient.KV()
minInstances, err := deployments.GetMinNbInstancesForNode(kv, id, nodeName)
if err != nil {
return "", err
}
currentNbInstance, err := deployments.GetNbInstancesForNode(kv, id, nodeName)
if err != nil {
return "", err
}
if currentNbInstance-instancesDelta < minInstances {
log.Debug("The delta is too low, the min instances number is chosen")
instancesDelta = currentNbInstance - minInstances
if instancesDelta == 0 {
return "", newBadRequestMessage("Minimum number of instances reached")
}
}
instancesByNodes, err := deployments.SelectNodeStackInstances(kv, id, nodeName, int(instancesDelta))
if err != nil {
return "", err
}
data := make(map[string]string)
for scalableNode, nodeInstances := range instancesByNodes {
data[path.Join("nodes", scalableNode)] = nodeInstances
}
// Add related workflow
data["workflowName"] = "uninstall"
return s.tasksCollector.RegisterTaskWithData(id, tasks.TaskTypeScaleIn, data)
}