forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orchestrators.go
340 lines (313 loc) · 10.5 KB
/
orchestrators.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package api
import (
"strconv"
"strings"
"github.com/Azure/acs-engine/pkg/api/common"
"github.com/Azure/acs-engine/pkg/api/v20170930"
"github.com/Azure/acs-engine/pkg/api/vlabs"
"github.com/blang/semver"
"github.com/pkg/errors"
)
type orchestratorsFunc func(*OrchestratorProfile, bool) ([]*OrchestratorVersionProfile, error)
var funcmap map[string]orchestratorsFunc
var versionsMap map[string][]string
func init() {
funcmap = map[string]orchestratorsFunc{
Kubernetes: kubernetesInfo,
DCOS: dcosInfo,
Swarm: swarmInfo,
SwarmMode: dockerceInfo,
OpenShift: openShiftInfo,
}
versionsMap = map[string][]string{
Kubernetes: common.GetAllSupportedKubernetesVersions(true, false),
DCOS: common.GetAllSupportedDCOSVersions(),
Swarm: common.GetAllSupportedSwarmVersions(),
SwarmMode: common.GetAllSupportedDockerCEVersions(),
OpenShift: common.GetAllSupportedOpenShiftVersions(),
}
}
func validate(orchestrator, version string) (string, error) {
switch {
case strings.EqualFold(orchestrator, Kubernetes):
return Kubernetes, nil
case strings.EqualFold(orchestrator, DCOS):
return DCOS, nil
case strings.EqualFold(orchestrator, Swarm):
return Swarm, nil
case strings.EqualFold(orchestrator, SwarmMode):
return SwarmMode, nil
case strings.EqualFold(orchestrator, OpenShift):
return OpenShift, nil
case orchestrator == "":
if version != "" {
return "", errors.Errorf("Must specify orchestrator for version '%s'", version)
}
default:
return "", errors.Errorf("Unsupported orchestrator '%s'", orchestrator)
}
return "", nil
}
func isVersionSupported(csOrch *OrchestratorProfile) bool {
supported := false
for _, version := range versionsMap[csOrch.OrchestratorType] {
if version == csOrch.OrchestratorVersion {
supported = true
break
}
}
return supported
}
// GetOrchestratorVersionProfileListVLabs returns vlabs OrchestratorVersionProfileList object per (optionally) specified orchestrator and version
func GetOrchestratorVersionProfileListVLabs(orchestrator, version string) (*vlabs.OrchestratorVersionProfileList, error) {
apiOrchs, err := getOrchestratorVersionProfileList(orchestrator, version)
if err != nil {
return nil, err
}
orchList := &vlabs.OrchestratorVersionProfileList{}
orchList.Orchestrators = []*vlabs.OrchestratorVersionProfile{}
for _, orch := range apiOrchs {
orchList.Orchestrators = append(orchList.Orchestrators, ConvertOrchestratorVersionProfileToVLabs(orch))
}
return orchList, nil
}
// GetOrchestratorVersionProfileListV20170930 returns v20170930 OrchestratorVersionProfileList object per (optionally) specified orchestrator and version
func GetOrchestratorVersionProfileListV20170930(orchestrator, version string) (*v20170930.OrchestratorVersionProfileList, error) {
apiOrchs, err := getOrchestratorVersionProfileList(orchestrator, version)
if err != nil {
return nil, err
}
orchList := &v20170930.OrchestratorVersionProfileList{}
for _, orch := range apiOrchs {
orchList.Properties.Orchestrators = append(orchList.Properties.Orchestrators, ConvertOrchestratorVersionProfileToV20170930(orch))
}
return orchList, nil
}
func getOrchestratorVersionProfileList(orchestrator, version string) ([]*OrchestratorVersionProfile, error) {
var err error
if orchestrator, err = validate(orchestrator, version); err != nil {
return nil, err
}
orchs := []*OrchestratorVersionProfile{}
if len(orchestrator) == 0 {
// return all orchestrators
for _, f := range funcmap {
arr, err := f(&OrchestratorProfile{}, false)
if err != nil {
return nil, err
}
orchs = append(orchs, arr...)
}
} else {
if orchs, err = funcmap[orchestrator](&OrchestratorProfile{OrchestratorType: orchestrator, OrchestratorVersion: version}, false); err != nil {
return nil, err
}
}
return orchs, nil
}
// GetOrchestratorVersionProfile returns orchestrator info for upgradable container service
func GetOrchestratorVersionProfile(orch *OrchestratorProfile, hasWindows bool) (*OrchestratorVersionProfile, error) {
if orch.OrchestratorVersion == "" {
return nil, errors.New("Missing Orchestrator Version")
}
switch orch.OrchestratorType {
case Kubernetes, DCOS:
arr, err := funcmap[orch.OrchestratorType](orch, hasWindows)
if err != nil {
return nil, err
}
// has to be exactly one element per specified orchestrator/version
if len(arr) != 1 {
return nil, errors.New("Ambiguous Orchestrator Versions")
}
return arr[0], nil
default:
return nil, errors.Errorf("Upgrade operation is not supported for '%s'", orch.OrchestratorType)
}
}
func kubernetesInfo(csOrch *OrchestratorProfile, hasWindows bool) ([]*OrchestratorVersionProfile, error) {
orchs := []*OrchestratorVersionProfile{}
if csOrch.OrchestratorVersion == "" {
// get info for all supported versions
for _, ver := range common.GetAllSupportedKubernetesVersions(false, hasWindows) {
upgrades, err := kubernetesUpgrades(&OrchestratorProfile{OrchestratorVersion: ver}, hasWindows)
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: ver,
},
Default: ver == common.GetDefaultKubernetesVersion(hasWindows),
Upgrades: upgrades,
})
}
} else {
if !isVersionSupported(csOrch) {
return nil, errors.Errorf("Kubernetes version %s is not supported", csOrch.OrchestratorVersion)
}
upgrades, err := kubernetesUpgrades(csOrch, hasWindows)
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
Default: csOrch.OrchestratorVersion == common.GetDefaultKubernetesVersion(hasWindows),
Upgrades: upgrades,
})
}
return orchs, nil
}
func kubernetesUpgrades(csOrch *OrchestratorProfile, hasWindows bool) ([]*OrchestratorProfile, error) {
ret := []*OrchestratorProfile{}
currentVer, err := semver.Make(csOrch.OrchestratorVersion)
if err != nil {
return nil, err
}
nextNextMinorString := strconv.FormatUint(currentVer.Major, 10) + "." + strconv.FormatUint(currentVer.Minor+2, 10) + ".0-alpha.0"
upgradeableVersions := common.GetVersionsBetween(common.GetAllSupportedKubernetesVersions(false, hasWindows), csOrch.OrchestratorVersion, nextNextMinorString, false, true)
for _, ver := range upgradeableVersions {
ret = append(ret, &OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: ver,
})
}
return ret, nil
}
func dcosInfo(csOrch *OrchestratorProfile, hasWindows bool) ([]*OrchestratorVersionProfile, error) {
orchs := []*OrchestratorVersionProfile{}
if csOrch.OrchestratorVersion == "" {
// get info for all supported versions
for _, ver := range common.AllDCOSSupportedVersions {
upgrades, err := dcosUpgrades(&OrchestratorProfile{OrchestratorVersion: ver})
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: DCOS,
OrchestratorVersion: ver,
},
Default: ver == common.DCOSDefaultVersion,
Upgrades: upgrades,
})
}
} else {
if !isVersionSupported(csOrch) {
return nil, errors.Errorf("DCOS version %s is not supported", csOrch.OrchestratorVersion)
}
// get info for the specified version
upgrades, err := dcosUpgrades(csOrch)
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: DCOS,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
Default: csOrch.OrchestratorVersion == common.DCOSDefaultVersion,
Upgrades: upgrades,
})
}
return orchs, nil
}
func dcosUpgrades(csOrch *OrchestratorProfile) ([]*OrchestratorProfile, error) {
ret := []*OrchestratorProfile{}
switch csOrch.OrchestratorVersion {
case common.DCOSVersion1Dot11Dot0:
ret = append(ret, &OrchestratorProfile{
OrchestratorType: DCOS,
OrchestratorVersion: common.DCOSVersion1Dot11Dot2,
})
}
return ret, nil
}
func swarmInfo(csOrch *OrchestratorProfile, hasWindows bool) ([]*OrchestratorVersionProfile, error) {
if csOrch.OrchestratorVersion == "" {
return []*OrchestratorVersionProfile{
{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Swarm,
OrchestratorVersion: SwarmVersion,
},
},
}, nil
}
if !isVersionSupported(csOrch) {
return nil, errors.Errorf("Swarm version %s is not supported", csOrch.OrchestratorVersion)
}
return []*OrchestratorVersionProfile{
{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Swarm,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
},
}, nil
}
func dockerceInfo(csOrch *OrchestratorProfile, hasWindows bool) ([]*OrchestratorVersionProfile, error) {
if csOrch.OrchestratorVersion == "" {
return []*OrchestratorVersionProfile{
{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: SwarmMode,
OrchestratorVersion: DockerCEVersion,
},
},
}, nil
}
if !isVersionSupported(csOrch) {
return nil, errors.Errorf("Docker CE version %s is not supported", csOrch.OrchestratorVersion)
}
return []*OrchestratorVersionProfile{
{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: SwarmMode,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
},
}, nil
}
func openShiftInfo(csOrch *OrchestratorProfile, hasWindows bool) ([]*OrchestratorVersionProfile, error) {
orchs := []*OrchestratorVersionProfile{}
if csOrch.OrchestratorVersion == "" {
// get info for all supported versions
for _, ver := range common.GetAllSupportedOpenShiftVersions() {
if ver == common.OpenShiftVersionUnstable {
continue
}
// TODO: populate OrchestratorVersionProfile.Upgrades
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: OpenShift,
OrchestratorVersion: ver,
},
Default: ver == common.OpenShiftDefaultVersion,
})
}
} else {
if !isVersionSupported(csOrch) {
return nil, errors.Errorf("OpenShift version %s is not supported", csOrch.OrchestratorVersion)
}
// TODO: populate OrchestratorVersionProfile.Upgrades
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: OpenShift,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
Default: csOrch.OrchestratorVersion == common.OpenShiftDefaultVersion,
})
}
return orchs, nil
}