This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
/
normal.go
278 lines (229 loc) · 8.27 KB
/
normal.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package rancher
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/docker/libcompose/utils"
"github.com/rancher/go-rancher/v2"
"github.com/rancher/rancher-compose/digest"
)
type NormalFactory struct {
}
func (f *NormalFactory) Hash(service *RancherService) (digest.ServiceHash, error) {
hash, _, err := f.configAndHash(service)
return hash, err
}
func (f *NormalFactory) configAndHash(r *RancherService) (digest.ServiceHash, *CompositeService, error) {
rancherService, launchConfig, secondaryLaunchConfigs, err := f.config(r)
if err != nil {
return digest.ServiceHash{}, nil, err
}
hash, err := digest.CreateServiceHash(rancherService, launchConfig, secondaryLaunchConfigs)
if err != nil {
return digest.ServiceHash{}, nil, err
}
rancherService.LaunchConfig = launchConfig
rancherService.LaunchConfig.Labels[digest.ServiceHashKey] = hash.LaunchConfig
rancherService.Metadata[digest.ServiceHashKey] = hash.Service
for _, secondaryLaunchConfig := range secondaryLaunchConfigs {
secondaryLaunchConfig.Labels[digest.ServiceHashKey] = hash.SecondaryLaunchConfigs[secondaryLaunchConfig.Name]
rancherService.SecondaryLaunchConfigs = append(rancherService.SecondaryLaunchConfigs, secondaryLaunchConfig)
}
return hash, rancherService, nil
}
func (f *NormalFactory) config(r *RancherService) (*CompositeService, *client.LaunchConfig, []client.SecondaryLaunchConfig, error) {
launchConfig, secondaryLaunchConfigs, err := createLaunchConfigs(r)
if err != nil {
return nil, nil, nil, err
}
rancherConfig, _ := r.context.RancherConfig[r.name]
service := &CompositeService{
Service: client.Service{
Name: r.name,
Metadata: r.Metadata(),
Scale: int64(r.getConfiguredScale()),
ScalePolicy: rancherConfig.ScalePolicy,
RetainIp: rancherConfig.RetainIp,
StackId: r.Context().Stack.Id,
SelectorContainer: r.SelectorContainer(),
SelectorLink: r.SelectorLink(),
},
ExternalIpAddresses: rancherConfig.ExternalIps,
Hostname: rancherConfig.Hostname,
HealthCheck: r.HealthCheck(""),
StorageDriver: rancherConfig.StorageDriver,
NetworkDriver: rancherConfig.NetworkDriver,
}
if service.NetworkDriver != nil {
service.NetworkDriver.CniConfig = convertNestedMapKeysToStrings(service.NetworkDriver.CniConfig)
}
if err := populateLbFields(r, &launchConfig, service); err != nil {
return nil, nil, nil, err
}
return service, &launchConfig, secondaryLaunchConfigs, nil
}
func (f *NormalFactory) Create(r *RancherService) error {
hash, service, err := f.configAndHash(r)
if err != nil {
return err
}
logrus.Debugf("Creating service %s with hash: %#v", r.name, hash)
switch FindServiceType(r) {
case ExternalServiceType:
return r.context.Client.Create(client.EXTERNAL_SERVICE_TYPE, &service, nil)
case DnsServiceType:
return r.context.Client.Create(client.DNS_SERVICE_TYPE, &service, nil)
case LegacyLbServiceType:
fallthrough
case LbServiceType:
return r.context.Client.Create(client.LOAD_BALANCER_SERVICE_TYPE, &service, nil)
case StorageDriverType:
return r.context.Client.Create(client.STORAGE_DRIVER_SERVICE_TYPE, &service, nil)
case NetworkDriverType:
return r.context.Client.Create(client.NETWORK_DRIVER_SERVICE_TYPE, &service, nil)
default:
_, err = r.context.Client.Service.Create(&service.Service)
}
return err
}
func (f *NormalFactory) Rollback(r *RancherService) error {
existingService, err := r.FindExisting(r.Name())
if err != nil || existingService == nil {
return err
}
existingService, err = r.Client().Service.ActionRollback(existingService)
if err != nil {
return err
}
return r.Wait(existingService)
}
func isForce(name string, force bool, selected []string) bool {
if !force {
return false
}
if len(selected) == 0 {
return true
}
return utils.Contains(selected, name)
}
func (f *NormalFactory) Upgrade(r *RancherService, force bool, selected []string) error {
existingService, err := r.FindExisting(r.Name())
if err != nil || existingService == nil {
return err
}
if existingService.State != "active" && existingService.State != "inactive" {
return fmt.Errorf("Service %s must be state=active or inactive to upgrade, currently: state=%s", r.Name(), existingService.State)
}
existingHash, _ := digest.LookupHash(existingService)
secondaryNames := []string{}
removedSecondaryNames := []string{}
hash, err := f.Hash(r)
if err != nil {
return err
}
service := hash.Service != existingHash.Service || isForce(r.Name(), force, selected)
launchConfig := hash.LaunchConfig != existingHash.LaunchConfig || isForce(r.Name(), force, selected)
for oldSecondary := range existingHash.SecondaryLaunchConfigs {
if _, ok := hash.SecondaryLaunchConfigs[oldSecondary]; !ok {
removedSecondaryNames = append(removedSecondaryNames, oldSecondary)
}
}
for newSecondary, newHash := range hash.SecondaryLaunchConfigs {
if oldHash, ok := existingHash.SecondaryLaunchConfigs[newSecondary]; ok {
if oldHash != newHash || isForce(newSecondary, force, selected) {
secondaryNames = append(secondaryNames, newSecondary)
}
} else {
secondaryNames = append(secondaryNames, newSecondary)
}
}
return f.upgrade(r, existingService, service, launchConfig, secondaryNames, removedSecondaryNames)
}
func (f *NormalFactory) upgrade(r *RancherService, existingService *client.Service, service, launchConfig bool, secondaryNames, removedSecondaryNames []string) error {
_, config, err := f.configAndHash(r)
if err != nil {
return err
}
serviceUpgrade := &client.ServiceUpgrade{
InServiceStrategy: &client.InServiceUpgradeStrategy{
BatchSize: r.context.BatchSize,
IntervalMillis: r.context.Interval,
StartFirst: r.RancherConfig().UpgradeStrategy.StartFirst,
},
}
if launchConfig {
serviceUpgrade.InServiceStrategy.LaunchConfig = config.LaunchConfig
}
for _, name := range secondaryNames {
for _, secondaryLaunchConfig := range config.SecondaryLaunchConfigs {
if secondaryLaunchConfig.Name == name {
serviceUpgrade.InServiceStrategy.SecondaryLaunchConfigs = append(serviceUpgrade.InServiceStrategy.SecondaryLaunchConfigs, secondaryLaunchConfig)
}
}
}
for _, removedSecondaryName := range removedSecondaryNames {
serviceUpgrade.InServiceStrategy.SecondaryLaunchConfigs = append(serviceUpgrade.InServiceStrategy.SecondaryLaunchConfigs, client.SecondaryLaunchConfig{
Name: removedSecondaryName,
ImageUuid: "rancher/none",
})
}
if service {
// Scale must be changed through "scale" not "up", so always copy scale existing scale
config.Scale = existingService.Scale
logrus.Infof("Updating %s", r.Name())
schemaType := client.SERVICE_TYPE
switch FindServiceType(r) {
case ExternalServiceType:
schemaType = client.EXTERNAL_SERVICE_TYPE
case DnsServiceType:
schemaType = client.DNS_SERVICE_TYPE
case LbServiceType:
schemaType = client.LOAD_BALANCER_SERVICE_TYPE
case NetworkDriverType:
schemaType = client.NETWORK_DRIVER_SERVICE_TYPE
case StorageDriverType:
schemaType = client.STORAGE_DRIVER_SERVICE_TYPE
}
if err := r.context.Client.Update(schemaType, &existingService.Resource, config, existingService); err != nil {
return err
}
if err := r.Wait(existingService); err != nil {
return err
}
}
if launchConfig || len(secondaryNames) > 0 {
logrus.Infof("Upgrading %s", r.Name())
existingService, err = r.Client().Service.ActionUpgrade(existingService, serviceUpgrade)
if err != nil {
return err
}
}
return r.Wait(existingService)
}
func convertNestedMapKeysToStrings(service map[string]interface{}) map[string]interface{} {
newService := map[string]interface{}{}
for k, v := range service {
newService[k] = convertKeysToStrings(v)
}
return newService
}
func convertKeysToStrings(item interface{}) interface{} {
switch typedDatas := item.(type) {
case map[interface{}]interface{}:
newMap := make(map[string]interface{})
for key, value := range typedDatas {
stringKey := key.(string)
newMap[stringKey] = convertKeysToStrings(value)
}
return newMap
case []interface{}:
// newArray := make([]interface{}, 0) will cause golint to complain
var newArray []interface{}
newArray = make([]interface{}, 0)
for _, value := range typedDatas {
newArray = append(newArray, convertKeysToStrings(value))
}
return newArray
default:
return item
}
}