-
Notifications
You must be signed in to change notification settings - Fork 155
/
manager.go
329 lines (278 loc) · 9.61 KB
/
manager.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 version
import (
"errors"
"fmt"
semverlib "github.com/Masterminds/semver/v3"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
kubermaticlog "k8c.io/kubermatic/v2/pkg/log"
"k8c.io/kubermatic/v2/pkg/validation/nodeupdate"
)
var (
errVersionNotFound = errors.New("version not found")
errNoDefaultVersion = errors.New("no default version configured")
)
// Manager is a object to handle versions & updates from a predefined config.
type Manager struct {
versions []*Version
updates []*Update
providerIncompatibilities []*ProviderIncompatibility
}
type ProviderIncompatibility struct {
Provider kubermaticv1.ProviderType `json:"provider"`
Version string `json:"version"`
Condition kubermaticv1.ConditionType `json:"condition"`
Operation kubermaticv1.OperationType `json:"operation"`
}
// Version is the object representing a Kubernetes version.
type Version struct {
Version *semverlib.Version `json:"version"`
Default bool `json:"default,omitempty"`
}
// Update represents an update option for a cluster.
type Update struct {
From string `json:"from"`
To string `json:"to"`
Automatic bool `json:"automatic,omitempty"`
AutomaticNodeUpdate bool `json:"automaticNodeUpdate,omitempty"`
}
// New returns a instance of Manager.
func New(versions []*Version, updates []*Update, providerIncompatibilities []*ProviderIncompatibility) *Manager {
return &Manager{
updates: updates,
versions: versions,
providerIncompatibilities: providerIncompatibilities,
}
}
// NewFromConfiguration converts the configured versions/updates into the datatypes used by
// this package and returns a version.Manager on success.
func NewFromConfiguration(config *kubermaticv1.KubermaticConfiguration) *Manager {
updates := []*Update{}
versions := []*Version{}
incompatibilities := []*ProviderIncompatibility{}
k8s := config.Spec.Versions
for _, k8sVersion := range k8s.Versions {
versions = append(versions, &Version{
Version: k8sVersion.Semver(),
Default: k8s.Default != nil && k8sVersion.Equal(k8s.Default),
})
}
for _, u := range k8s.Updates {
updates = append(updates, &Update{
From: u.From,
To: u.To,
Automatic: u.Automatic != nil && *u.Automatic,
AutomaticNodeUpdate: u.AutomaticNodeUpdate != nil && *u.AutomaticNodeUpdate,
})
}
for _, incomp := range k8s.ProviderIncompatibilities {
incompatibilities = append(incompatibilities, &ProviderIncompatibility{
Provider: kubermaticv1.ProviderType(incomp.Provider),
Version: incomp.Version,
Condition: incomp.Condition,
Operation: incomp.Operation,
})
}
return New(versions, updates, incompatibilities)
}
// GetDefault returns the default version.
func (m *Manager) GetDefault() (*Version, error) {
for _, v := range m.versions {
if v.Default {
return v, nil
}
}
return nil, errNoDefaultVersion
}
// GetVersion returns the Versions for s.
func (m *Manager) GetVersion(s string) (*Version, error) {
sv, err := semverlib.NewVersion(s)
if err != nil {
return nil, fmt.Errorf("failed to parse version %s: %w", s, err)
}
for _, v := range m.versions {
if v.Version.Equal(sv) {
return v, nil
}
}
return nil, errVersionNotFound
}
// GetVersions returns all Versions which don't result in automatic updates, regardless of the cloud provider.
func (m *Manager) GetVersions() ([]*Version, error) {
var masterVersions []*Version
for _, v := range m.versions {
autoUpdate, err := m.AutomaticControlplaneUpdate(v.Version.String())
if err != nil {
kubermaticlog.Logger.Errorf("Failed to get AutomaticUpdate for version %s: %v", v.Version.String(), err)
continue
}
if autoUpdate != nil {
continue
}
masterVersions = append(masterVersions, v)
}
return masterVersions, nil
}
// GetVersionsForProvider returns all Versions which don't result in automatic updates.
func (m *Manager) GetVersionsForProvider(provider kubermaticv1.ProviderType, conditions ...kubermaticv1.ConditionType) ([]*Version, error) {
versions, err := m.GetVersions()
if err != nil {
return nil, err
}
filtered := []*Version{}
for _, v := range versions {
compatible, err := checkProviderCompatibility(v.Version, provider, kubermaticv1.CreateOperation, m.providerIncompatibilities, conditions...)
if err != nil {
return nil, err
}
if compatible {
filtered = append(filtered, &Version{
Version: v.Version,
Default: v.Default,
})
}
}
return filtered, nil
}
// AutomaticNodeUpdate returns an automatic node update or nil.
func (m *Manager) AutomaticNodeUpdate(fromVersionRaw, controlPlaneVersion string) (*Version, error) {
version, err := m.automaticUpdate(fromVersionRaw, true)
if err != nil || version == nil {
return version, err
}
controlPlaneSemver, err := semverlib.NewVersion(controlPlaneVersion)
if err != nil {
return nil, fmt.Errorf("failed to parse controlplane version: %w", err)
}
if err := nodeupdate.EnsureVersionCompatible(controlPlaneSemver, version.Version); err != nil {
return nil, err
}
return version, nil
}
// AutomaticControlplaneUpdate returns a version if an automatic update can be found for the version
// passed in.
func (m *Manager) AutomaticControlplaneUpdate(fromVersionRaw string) (*Version, error) {
return m.automaticUpdate(fromVersionRaw, false)
}
func (m *Manager) automaticUpdate(fromVersionRaw string, isForNode bool) (*Version, error) {
from, err := semverlib.NewVersion(fromVersionRaw)
if err != nil {
return nil, fmt.Errorf("failed to parse version %s: %w", fromVersionRaw, err)
}
isAutomatic := func(u *Update) bool {
if isForNode {
return u.AutomaticNodeUpdate
}
return u.Automatic
}
var toVersions []string
for _, u := range m.updates {
if !isAutomatic(u) {
continue
}
uFrom, err := semverlib.NewConstraint(u.From)
if err != nil {
return nil, fmt.Errorf("failed to parse from constraint %s: %w", u.From, err)
}
if !uFrom.Check(from) {
continue
}
// Automatic updates must not be a constraint. They must be version.
if _, err = semverlib.NewVersion(u.To); err != nil {
return nil, fmt.Errorf("failed to parse to version %s: %w", u.To, err)
}
toVersions = append(toVersions, u.To)
}
if len(toVersions) == 0 {
return nil, nil
}
if len(toVersions) > 1 {
return nil, fmt.Errorf("more than one automatic update found for version. Not allowed. Automatic updates to: %v", toVersions)
}
version, err := m.GetVersion(toVersions[0])
if err != nil {
return nil, fmt.Errorf("failed to get Version for %s: %w", toVersions[0], err)
}
return version, nil
}
// GetPossibleUpdates returns possible updates for the version passed in.
func (m *Manager) GetPossibleUpdates(fromVersionRaw string, provider kubermaticv1.ProviderType, conditions ...kubermaticv1.ConditionType) ([]*Version, error) {
from, err := semverlib.NewVersion(fromVersionRaw)
if err != nil {
return nil, fmt.Errorf("failed to parse version %s: %w", fromVersionRaw, err)
}
var possibleVersions []*Version
var toConstraints []*semverlib.Constraints
for _, u := range m.updates {
uFrom, err := semverlib.NewConstraint(u.From)
if err != nil {
return nil, fmt.Errorf("failed to parse from constraint %s: %w", u.From, err)
}
if !uFrom.Check(from) {
continue
}
uTo, err := semverlib.NewConstraint(u.To)
if err != nil {
return nil, fmt.Errorf("failed to parse to constraint %s: %w", u.To, err)
}
toConstraints = append(toConstraints, uTo)
}
for _, c := range toConstraints {
for _, v := range m.versions {
if c.Check(v.Version) && !from.Equal(v.Version) {
compatible, err := checkProviderCompatibility(v.Version, provider, kubermaticv1.UpdateOperation, m.providerIncompatibilities, conditions...)
if err != nil {
return nil, err
}
if compatible {
possibleVersions = append(possibleVersions, v)
}
}
}
}
return possibleVersions, nil
}
func (m *Manager) GetIncompatibilities() []*ProviderIncompatibility {
return m.providerIncompatibilities
}
func (m *Manager) GetKubeOnePossibleUpdates(fromVersionRaw string, provider kubermaticv1.ProviderType, conditions ...kubermaticv1.ConditionType) ([]*Version, error) {
from, err := semverlib.NewVersion(fromVersionRaw)
if err != nil {
return nil, fmt.Errorf("failed to parse version %s: %w", fromVersionRaw, err)
}
var possibleVersions []*Version
var toConstraints []*semverlib.Constraints
for _, u := range m.updates {
uFrom, err := semverlib.NewConstraint(u.From)
if err != nil {
return nil, fmt.Errorf("failed to parse from constraint %s: %w", u.From, err)
}
if !uFrom.Check(from) {
continue
}
uTo, err := semverlib.NewConstraint(u.To)
if err != nil {
return nil, fmt.Errorf("failed to parse to constraint %s: %w", u.To, err)
}
toConstraints = append(toConstraints, uTo)
}
for _, c := range toConstraints {
for _, v := range m.versions {
if c.Check(v.Version) && !from.Equal(v.Version) && from.LessThan(v.Version) {
possibleVersions = append(possibleVersions, v)
}
}
}
return possibleVersions, nil
}