This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 526
/
orchestrators.go
205 lines (185 loc) · 6.93 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package api
import (
"strconv"
"strings"
"github.com/Azure/aks-engine/pkg/api/common"
"github.com/Azure/aks-engine/pkg/api/vlabs"
"github.com/blang/semver"
"github.com/pkg/errors"
)
type orchestratorsFunc func(*OrchestratorProfile, bool, bool) ([]*OrchestratorVersionProfile, error)
var funcmap map[string]orchestratorsFunc
var versionsMap map[string][]string
var versionsMapAzureStack map[string][]string
func init() {
funcmap = map[string]orchestratorsFunc{
Kubernetes: kubernetesInfo,
}
versionsMap = map[string][]string{
Kubernetes: common.GetAllSupportedKubernetesVersions(true, false, false),
}
versionsMapAzureStack = map[string][]string{
Kubernetes: common.GetAllSupportedKubernetesVersions(true, false, true),
}
}
func validate(orchestrator, version string) (string, error) {
switch {
case strings.EqualFold(orchestrator, Kubernetes):
return Kubernetes, 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, isAzureStackCloud bool) bool {
supported := false
versions := versionsMap[csOrch.OrchestratorType]
if isAzureStackCloud {
versions = versionsMapAzureStack[csOrch.OrchestratorType]
}
for _, version := range versions {
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, windows bool, azureEnv string) (*vlabs.OrchestratorVersionProfileList, error) {
apiOrchs, err := GetOrchestratorVersionProfileList(orchestrator, version, windows, azureEnv)
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
}
// GetOrchestratorVersionProfileList returns a list of unversioned OrchestratorVersionProfile objects per (optionally) specified orchestrator and version
func GetOrchestratorVersionProfileList(orchestrator, version string, windows bool, azureEnv string) ([]*OrchestratorVersionProfile, error) {
var err error
isAzureStackCloud := (strings.EqualFold(azureEnv, AzureStackCloud))
if orchestrator, err = validate(orchestrator, version); err != nil {
return nil, err
}
orchs := []*OrchestratorVersionProfile{}
if len(orchestrator) == 0 {
// return all orchestrators
for _, f := range funcmap {
var arr []*OrchestratorVersionProfile
arr, err = f(&OrchestratorProfile{}, false, isAzureStackCloud)
if err != nil {
return nil, err
}
orchs = append(orchs, arr...)
}
} else {
if orchs, err = funcmap[orchestrator](&OrchestratorProfile{OrchestratorType: orchestrator, OrchestratorVersion: version}, windows, isAzureStackCloud); err != nil {
return nil, err
}
}
return orchs, nil
}
// GetOrchestratorVersionProfile returns orchestrator info for upgradable container service
func GetOrchestratorVersionProfile(orch *OrchestratorProfile, hasWindows bool, isAzureStackCloud bool) (*OrchestratorVersionProfile, error) {
if orch.OrchestratorVersion == "" {
return nil, errors.New("Missing Orchestrator Version")
}
switch orch.OrchestratorType {
case Kubernetes:
arr, err := funcmap[orch.OrchestratorType](orch, hasWindows, isAzureStackCloud)
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, isAzureStackCloud bool) ([]*OrchestratorVersionProfile, error) {
orchs := []*OrchestratorVersionProfile{}
if csOrch.OrchestratorVersion == "" {
// get info for all supported versions
for _, ver := range common.GetAllSupportedKubernetesVersions(false, hasWindows, isAzureStackCloud) {
upgrades, err := kubernetesUpgrades(&OrchestratorProfile{OrchestratorVersion: ver}, hasWindows, isAzureStackCloud)
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: ver,
},
Default: ver == common.GetDefaultKubernetesVersion(hasWindows, isAzureStackCloud),
Upgrades: upgrades,
})
}
} else {
if !isVersionSupported(csOrch, isAzureStackCloud) {
return nil, errors.Errorf("Kubernetes version %s is not supported", csOrch.OrchestratorVersion)
}
upgrades, err := kubernetesUpgrades(csOrch, hasWindows, isAzureStackCloud)
if err != nil {
return nil, err
}
orchs = append(orchs,
&OrchestratorVersionProfile{
OrchestratorProfile: OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: csOrch.OrchestratorVersion,
},
Default: csOrch.OrchestratorVersion == common.GetDefaultKubernetesVersion(hasWindows, isAzureStackCloud),
Upgrades: upgrades,
})
}
return orchs, nil
}
func kubernetesUpgrades(csOrch *OrchestratorProfile, hasWindows bool, isAzureStackCloud bool) ([]*OrchestratorProfile, error) {
ret := []*OrchestratorProfile{}
upgradeVersions, err := getKubernetesAvailableUpgradeVersions(csOrch.OrchestratorVersion, common.GetAllSupportedKubernetesVersions(false, hasWindows, isAzureStackCloud))
if err != nil {
return nil, err
}
for _, ver := range upgradeVersions {
ret = append(ret, &OrchestratorProfile{
OrchestratorType: Kubernetes,
OrchestratorVersion: ver,
})
}
return ret, nil
}
func getKubernetesAvailableUpgradeVersions(orchestratorVersion string, supportedVersions []string) ([]string, error) {
var skipUpgradeMinor string
currentVer, err := semver.Make(orchestratorVersion)
if err != nil {
return nil, err
}
versionsGT := common.GetVersionsGt(supportedVersions, orchestratorVersion, false, true)
if len(versionsGT) != 0 {
min, err := semver.Make(common.GetMinVersion(versionsGT, true))
if err != nil {
return nil, err
}
if currentVer.Major >= min.Major && currentVer.Minor+1 < min.Minor {
skipUpgradeMinor = strconv.FormatUint(min.Major, 10) + "." + strconv.FormatUint(min.Minor+1, 10) + ".0-alpha.0"
} else {
skipUpgradeMinor = strconv.FormatUint(currentVer.Major, 10) + "." + strconv.FormatUint(currentVer.Minor+2, 10) + ".0-alpha.0"
}
return common.GetVersionsBetween(supportedVersions, orchestratorVersion, skipUpgradeMinor, false, true), nil
}
return []string{}, nil
}