forked from Azure/acs-engine
/
apiloader.go
380 lines (346 loc) · 13.6 KB
/
apiloader.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package api
import (
"encoding/json"
"io/ioutil"
"reflect"
"github.com/Azure/acs-engine/pkg/api/agentPoolOnlyApi/v20170831"
apvlabs "github.com/Azure/acs-engine/pkg/api/agentPoolOnlyApi/vlabs"
"github.com/Azure/acs-engine/pkg/api/common"
"github.com/Azure/acs-engine/pkg/api/upgrade/v20170930"
"github.com/Azure/acs-engine/pkg/api/v20160330"
"github.com/Azure/acs-engine/pkg/api/v20160930"
"github.com/Azure/acs-engine/pkg/api/v20170131"
"github.com/Azure/acs-engine/pkg/api/v20170701"
"github.com/Azure/acs-engine/pkg/api/vlabs"
"github.com/Azure/acs-engine/pkg/i18n"
log "github.com/sirupsen/logrus"
)
// Apiloader represents the object that loads api model
type Apiloader struct {
Translator *i18n.Translator
}
// LoadContainerServiceFromFile loads an ACS Cluster API Model from a JSON file
func (a *Apiloader) LoadContainerServiceFromFile(jsonFile string, validate bool, existingContainerService *ContainerService) (*ContainerService, string, error) {
contents, e := ioutil.ReadFile(jsonFile)
if e != nil {
return nil, "", a.Translator.Errorf("error reading file %s: %s", jsonFile, e.Error())
}
return a.DeserializeContainerService(contents, validate, existingContainerService)
}
// DeserializeContainerService loads an ACS Cluster API Model, validates it, and returns the unversioned representation
func (a *Apiloader) DeserializeContainerService(contents []byte, validate bool, existingContainerService *ContainerService) (*ContainerService, string, error) {
m := &TypeMeta{}
if err := json.Unmarshal(contents, &m); err != nil {
return nil, "", err
}
version := m.APIVersion
service, err := a.LoadContainerService(contents, version, validate, existingContainerService)
if service == nil || err != nil {
if isAgentPoolOnlyClusterJSON(contents) {
log.Info("No masterProfile: interpreting API model as agent pool only")
service, err := a.LoadContainerServiceForAgentPoolOnlyCluster(contents, version, validate)
if service == nil || err != nil {
log.Infof("Error returned by LoadContainerServiceForAgentPoolOnlyCluster: %+v", err)
}
return service, version, err
}
log.Infof("Error returned by LoadContainerService: %+v", err)
}
return service, version, err
}
// LoadContainerService loads an ACS Cluster API Model, validates it, and returns the unversioned representation
func (a *Apiloader) LoadContainerService(
contents []byte,
version string,
validate bool,
existingContainerService *ContainerService) (*ContainerService, error) {
switch version {
case v20160930.APIVersion:
containerService := &v20160930.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if existingContainerService != nil {
vecs := ConvertContainerServiceToV20160930(existingContainerService)
if e := containerService.Merge(vecs); e != nil {
return nil, e
}
}
setContainerServiceDefaultsv20160930(containerService)
if e := containerService.Properties.Validate(); validate && e != nil {
return nil, e
}
return ConvertV20160930ContainerService(containerService), nil
case v20160330.APIVersion:
containerService := &v20160330.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if existingContainerService != nil {
vecs := ConvertContainerServiceToV20160330(existingContainerService)
if e := containerService.Merge(vecs); e != nil {
return nil, e
}
}
setContainerServiceDefaultsv20160330(containerService)
if e := containerService.Properties.Validate(); validate && e != nil {
return nil, e
}
return ConvertV20160330ContainerService(containerService), nil
case v20170131.APIVersion:
containerService := &v20170131.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if existingContainerService != nil {
vecs := ConvertContainerServiceToV20170131(existingContainerService)
if e := containerService.Merge(vecs); e != nil {
return nil, e
}
}
setContainerServiceDefaultsv20170131(containerService)
if e := containerService.Properties.Validate(); validate && e != nil {
return nil, e
}
return ConvertV20170131ContainerService(containerService), nil
case v20170701.APIVersion:
containerService := &v20170701.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if existingContainerService != nil {
vecs := ConvertContainerServiceToV20170701(existingContainerService)
if e := containerService.Merge(vecs); e != nil {
return nil, e
}
}
setContainerServiceDefaultsv20170701(containerService)
if e := containerService.Properties.Validate(); validate && e != nil {
return nil, e
}
return ConvertV20170701ContainerService(containerService), nil
case vlabs.APIVersion:
containerService := &vlabs.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if e := checkJSONKeys(contents, reflect.TypeOf(*containerService), reflect.TypeOf(TypeMeta{})); e != nil {
return nil, e
}
if existingContainerService != nil {
vecs := ConvertContainerServiceToVLabs(existingContainerService)
if e := containerService.Merge(vecs); e != nil {
return nil, e
}
}
setContainerServiceDefaultsvlabs(containerService)
if e := containerService.Properties.Validate(); validate && e != nil {
return nil, e
}
return ConvertVLabsContainerService(containerService), nil
default:
return nil, a.Translator.Errorf("unrecognized APIVersion '%s'", version)
}
}
// LoadContainerServiceForAgentPoolOnlyCluster loads an ACS Cluster API Model, validates it, and returns the unversioned representation
func (a *Apiloader) LoadContainerServiceForAgentPoolOnlyCluster(contents []byte, version string, validate bool) (*ContainerService, error) {
switch version {
case v20170831.APIVersion:
managedCluster := &v20170831.ManagedCluster{}
if e := json.Unmarshal(contents, &managedCluster); e != nil {
return nil, e
}
setManagedClusterDefaultsv20170831(managedCluster)
if e := managedCluster.Properties.Validate(); validate && e != nil {
return nil, e
}
return ConvertV20170831AgentPoolOnly(managedCluster), nil
case apvlabs.APIVersion:
managedCluster := &apvlabs.ManagedCluster{}
if e := json.Unmarshal(contents, &managedCluster); e != nil {
return nil, e
}
setManagedClusterDefaultsvlabs(managedCluster)
if e := managedCluster.Properties.Validate(); validate && e != nil {
return nil, e
}
return ConvertVLabsAgentPoolOnly(managedCluster), nil
default:
return nil, a.Translator.Errorf("unrecognized APIVersion in LoadContainerServiceForAgentPoolOnlyCluster '%s'", version)
}
}
// UpdateContainerServiceForUpgrade pre-validates upgrade operation and updates container service
func (a *Apiloader) UpdateContainerServiceForUpgrade(
contents []byte,
version string,
cs *ContainerService,
allowCurrentVersionUpgrade bool) error {
unverOrch := &OrchestratorProfile{}
switch version {
case v20170930.APIVersion:
up := &v20170930.OrchestratorProfile{}
if e := json.Unmarshal(contents, up); e != nil {
return a.Translator.Errorf(e.Error())
}
if e := up.ValidateForUpgrade(); e != nil {
return a.Translator.Errorf(e.Error())
}
convertV20170930OrchestratorProfile(up, unverOrch)
case vlabs.APIVersion:
up := &vlabs.OrchestratorProfile{}
if e := json.Unmarshal(contents, up); e != nil {
return a.Translator.Errorf(e.Error())
}
if e := up.ValidateForUpgrade(); e != nil {
return a.Translator.Errorf(e.Error())
}
convertVLabsOrchestratorProfile(up, unverOrch)
default:
return a.Translator.Errorf("unrecognized APIVersion in UpdateContainerServiceForUpgrade '%s'", version)
}
// get available upgrades for container service
orchestratorInfo, e := GetOrchestratorVersionProfile(cs.Properties.OrchestratorProfile)
if e != nil {
return e
}
// add current version if upgrade has failed
if allowCurrentVersionUpgrade {
release := cs.Properties.OrchestratorProfile.OrchestratorRelease
orchestratorInfo.Upgrades = append(orchestratorInfo.Upgrades, &OrchestratorProfile{
OrchestratorRelease: release,
OrchestratorVersion: common.KubeReleaseToVersion[release]})
}
// validate desired upgrade version and set goal state
for _, up := range orchestratorInfo.Upgrades {
if up.OrchestratorRelease == unverOrch.OrchestratorRelease {
cs.Properties.OrchestratorProfile.OrchestratorRelease = up.OrchestratorRelease
cs.Properties.OrchestratorProfile.OrchestratorVersion = up.OrchestratorVersion
return nil
}
}
return a.Translator.Errorf("Kubernetes %s cannot be upgraded to %s",
cs.Properties.OrchestratorProfile.OrchestratorRelease, unverOrch.OrchestratorRelease)
}
// SerializeContainerService takes an unversioned container service and returns the bytes
func (a *Apiloader) SerializeContainerService(containerService *ContainerService, version string) ([]byte, error) {
if containerService.Properties != nil && containerService.Properties.HostedMasterProfile != nil {
b, err := a.serializeHostedContainerService(containerService, version)
if err == nil && b != nil {
return b, nil
}
}
switch version {
case v20160930.APIVersion:
v20160930ContainerService := ConvertContainerServiceToV20160930(containerService)
armContainerService := &V20160930ARMContainerService{}
armContainerService.ContainerService = v20160930ContainerService
armContainerService.APIVersion = version
b, err := json.MarshalIndent(armContainerService, "", " ")
if err != nil {
return nil, err
}
return b, nil
case v20160330.APIVersion:
v20160330ContainerService := ConvertContainerServiceToV20160330(containerService)
armContainerService := &V20160330ARMContainerService{}
armContainerService.ContainerService = v20160330ContainerService
armContainerService.APIVersion = version
b, err := json.MarshalIndent(armContainerService, "", " ")
if err != nil {
return nil, err
}
return b, nil
case v20170131.APIVersion:
v20170131ContainerService := ConvertContainerServiceToV20170131(containerService)
armContainerService := &V20170131ARMContainerService{}
armContainerService.ContainerService = v20170131ContainerService
armContainerService.APIVersion = version
b, err := json.MarshalIndent(armContainerService, "", " ")
if err != nil {
return nil, err
}
return b, nil
case v20170701.APIVersion:
v20170701ContainerService := ConvertContainerServiceToV20170701(containerService)
armContainerService := &V20170701ARMContainerService{}
armContainerService.ContainerService = v20170701ContainerService
armContainerService.APIVersion = version
b, err := json.MarshalIndent(armContainerService, "", " ")
if err != nil {
return nil, err
}
return b, nil
case vlabs.APIVersion:
vlabsContainerService := ConvertContainerServiceToVLabs(containerService)
armContainerService := &VlabsARMContainerService{}
armContainerService.ContainerService = vlabsContainerService
armContainerService.APIVersion = version
b, err := json.MarshalIndent(armContainerService, "", " ")
if err != nil {
return nil, err
}
return b, nil
default:
return nil, a.Translator.Errorf("invalid version %s for conversion back from unversioned object", version)
}
}
func (a *Apiloader) serializeHostedContainerService(containerService *ContainerService, version string) ([]byte, error) {
switch version {
case v20170831.APIVersion:
v20170831ContainerService := ConvertContainerServiceToV20170831AgentPoolOnly(containerService)
armContainerService := &V20170831ARMManagedContainerService{}
armContainerService.ManagedCluster = v20170831ContainerService
armContainerService.APIVersion = version
b, err := json.MarshalIndent(armContainerService, "", " ")
if err != nil {
return nil, err
}
return b, nil
default:
return nil, a.Translator.Errorf("invalid version %s for conversion back from unversioned object", version)
}
}
// Sets default container service property values for any appropriate zero values
func setContainerServiceDefaultsv20160930(c *v20160930.ContainerService) {
if c.Properties.OrchestratorProfile == nil {
c.Properties.OrchestratorProfile = &v20160930.OrchestratorProfile{
OrchestratorType: v20160930.DCOS,
}
}
}
// Sets default container service property values for any appropriate zero values
func setContainerServiceDefaultsv20160330(c *v20160330.ContainerService) {
if c.Properties.OrchestratorProfile == nil {
c.Properties.OrchestratorProfile = &v20160330.OrchestratorProfile{
OrchestratorType: v20160330.DCOS,
}
}
}
// Sets default container service property values for any appropriate zero values
func setContainerServiceDefaultsv20170131(c *v20170131.ContainerService) {
if c.Properties.OrchestratorProfile == nil {
c.Properties.OrchestratorProfile = &v20170131.OrchestratorProfile{
OrchestratorType: v20170131.DCOS,
}
}
}
// Sets default container service property values for any appropriate zero values
func setContainerServiceDefaultsv20170701(c *v20170701.ContainerService) {
if c.Properties.OrchestratorProfile != nil {
c.Properties.OrchestratorProfile.OrchestratorVersion = ""
}
}
// Sets default container service property values for any appropriate zero values
func setContainerServiceDefaultsvlabs(c *vlabs.ContainerService) {
if c.Properties.OrchestratorProfile != nil {
c.Properties.OrchestratorProfile.OrchestratorVersion = ""
}
}
// Sets default HostedMaster property values for any appropriate zero values
func setManagedClusterDefaultsv20170831(hm *v20170831.ManagedCluster) {
hm.Properties.KubernetesVersion = ""
}
// Sets default HostedMaster property values for any appropriate zero values
func setManagedClusterDefaultsvlabs(hm *apvlabs.ManagedCluster) {
hm.Properties.KubernetesVersion = ""
}