forked from Azure/acs-engine
/
orchestrators.go
262 lines (244 loc) · 7.73 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
package api
import (
"fmt"
"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/Masterminds/semver"
)
type orchestratorsFunc func(*OrchestratorProfile) ([]*OrchestratorVersionProfile, error)
var funcmap map[string]orchestratorsFunc
func init() {
funcmap = map[string]orchestratorsFunc{
Kubernetes: kubernetesInfo,
DCOS: dcosInfo,
Swarm: swarmInfo,
SwarmMode: dockerceInfo,
}
}
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 orchestrator == "":
if version != "" {
return "", fmt.Errorf("Must specify orchestrator for version '%s'", version)
}
default:
return "", fmt.Errorf("Unsupported orchestrator '%s'", orchestrator)
}
return "", nil
}
// 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{})
if err != nil {
return nil, err
}
orchs = append(orchs, arr...)
}
} else {
if orchs, err = funcmap[orchestrator](&OrchestratorProfile{OrchestratorVersion: version}); err != nil {
return nil, err
}
}
return orchs, nil
}
// GetOrchestratorVersionProfile returns orchestrator info for upgradable container service
func GetOrchestratorVersionProfile(orch *OrchestratorProfile) (*OrchestratorVersionProfile, error) {
if orch.OrchestratorVersion == "" {
return nil, fmt.Errorf("Missing Orchestrator Version")
}
if orch.OrchestratorType != Kubernetes {
return nil, fmt.Errorf("Upgrade operation is not supported for '%s'", orch.OrchestratorType)
}
arr, err := kubernetesInfo(orch)
if err != nil {
return nil, err
}
// has to be exactly one element per specified orchestrator/version
if len(arr) != 1 {
return nil, fmt.Errorf("Umbiguous Orchestrator Versions")
}
return arr[0], nil
}
func kubernetesInfo(csOrch *OrchestratorProfile) ([]*OrchestratorVersionProfile, error) {
orchs := []*OrchestratorVersionProfile{}
if csOrch.OrchestratorVersion == "" {
// get info for all supported versions
for _, ver := range common.GetAllSupportedKubernetesVersions() {
upgrades, err := kubernetesUpgrades(&OrchestratorProfile{OrchestratorVersion: ver})
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: ver,
},
Default: ver == common.KubernetesDefaultVersion,
Upgrades: upgrades,
})
}
} else {
ver, err := semver.NewVersion(csOrch.OrchestratorVersion)
if err != nil {
return nil, err
}
cons, err := semver.NewConstraint("<1.5.0")
if err != nil {
return nil, err
}
if cons.Check(ver) {
return nil, fmt.Errorf("Kubernetes version %s is not supported", csOrch.OrchestratorVersion)
}
upgrades, err := kubernetesUpgrades(csOrch)
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
Default: csOrch.OrchestratorVersion == common.KubernetesDefaultVersion,
Upgrades: upgrades,
})
}
return orchs, nil
}
func kubernetesUpgrades(csOrch *OrchestratorProfile) ([]*OrchestratorProfile, error) {
ret := []*OrchestratorProfile{}
currentVer, err := semver.NewVersion(csOrch.OrchestratorVersion)
if err != nil {
return nil, err
}
currentMajor, currentMinor, currentPatch := currentVer.Major(), currentVer.Minor(), currentVer.Patch()
var nextMajor, nextMinor int64
switch {
case currentMajor == 1 && currentMinor == 5:
nextMajor = 1
nextMinor = 6
case currentMajor == 1 && currentMinor == 6:
nextMajor = 1
nextMinor = 7
case currentMajor == 1 && currentMinor == 7:
nextMajor = 1
nextMinor = 8
case currentMajor == 1 && currentMinor == 8:
nextMajor = 1
nextMinor = 9
}
for ver, supported := range common.AllKubernetesSupportedVersions {
if !supported {
continue
}
nextVersion, err := semver.NewVersion(ver)
if err != nil {
continue
}
// add patch upgrade
if nextVersion.Major() == currentMajor && nextVersion.Minor() == currentMinor && currentPatch < nextVersion.Patch() {
ret = append(ret, &OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: ver,
})
}
// add next version
if nextVersion.Major() == nextMajor && nextVersion.Minor() == nextMinor {
ret = append(ret, &OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: ver,
})
}
}
return ret, nil
}
func dcosInfo(csOrch *OrchestratorProfile) ([]*OrchestratorVersionProfile, error) {
orchs := []*OrchestratorVersionProfile{}
if csOrch.OrchestratorVersion == "" {
// get info for all supported versions
for _, ver := range common.AllDCOSSupportedVersions {
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: DCOS,
OrchestratorVersion: ver,
},
Default: ver == common.DCOSDefaultVersion,
})
}
} else {
// get info for the specified version
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: DCOS,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
Default: csOrch.OrchestratorVersion == common.DCOSDefaultVersion,
})
}
return orchs, nil
}
func swarmInfo(csOrch *OrchestratorProfile) ([]*OrchestratorVersionProfile, error) {
return []*OrchestratorVersionProfile{
{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Swarm,
OrchestratorVersion: SwarmVersion,
},
},
}, nil
}
func dockerceInfo(csOrch *OrchestratorProfile) ([]*OrchestratorVersionProfile, error) {
return []*OrchestratorVersionProfile{
{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: SwarmMode,
OrchestratorVersion: DockerCEVersion,
},
},
}, nil
}