forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiloader.go
398 lines (372 loc) · 15.4 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package api
import (
"crypto/rand"
"encoding/json"
"io/ioutil"
"reflect"
"github.com/Azure/acs-engine/pkg/api/agentPoolOnlyApi/v20170831"
"github.com/Azure/acs-engine/pkg/api/agentPoolOnlyApi/v20180331"
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/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/helpers"
"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, isUpdate 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, isUpdate, existingContainerService)
}
// DeserializeContainerService loads an ACS Cluster API Model, validates it, and returns the unversioned representation
func (a *Apiloader) DeserializeContainerService(contents []byte, validate, isUpdate 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, isUpdate, 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, isUpdate, "", existingContainerService)
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, isUpdate bool,
existingContainerService *ContainerService) (*ContainerService, error) {
var curOrchVersion string
hasExistingCS := existingContainerService != nil
if hasExistingCS {
curOrchVersion = existingContainerService.Properties.OrchestratorProfile.OrchestratorVersion
}
switch version {
case v20160930.APIVersion:
containerService := &v20160930.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if hasExistingCS {
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
}
unversioned := ConvertV20160930ContainerService(containerService)
if curOrchVersion != "" {
unversioned.Properties.OrchestratorProfile.OrchestratorVersion = curOrchVersion
}
return unversioned, nil
case v20160330.APIVersion:
containerService := &v20160330.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if hasExistingCS {
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
}
unversioned := ConvertV20160330ContainerService(containerService)
if curOrchVersion != "" {
unversioned.Properties.OrchestratorProfile.OrchestratorVersion = curOrchVersion
}
return unversioned, nil
case v20170131.APIVersion:
containerService := &v20170131.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if hasExistingCS {
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
}
unversioned := ConvertV20170131ContainerService(containerService)
if curOrchVersion != "" {
unversioned.Properties.OrchestratorProfile.OrchestratorVersion = curOrchVersion
}
return unversioned, nil
case v20170701.APIVersion:
containerService := &v20170701.ContainerService{}
if e := json.Unmarshal(contents, &containerService); e != nil {
return nil, e
}
if hasExistingCS {
vecs := ConvertContainerServiceToV20170701(existingContainerService)
if e := containerService.Merge(vecs); e != nil {
return nil, e
}
}
if e := containerService.Properties.Validate(isUpdate); validate && e != nil {
return nil, e
}
unversioned := ConvertV20170701ContainerService(containerService)
if curOrchVersion != "" &&
(containerService.Properties.OrchestratorProfile == nil ||
containerService.Properties.OrchestratorProfile.OrchestratorVersion == "") {
unversioned.Properties.OrchestratorProfile.OrchestratorVersion = curOrchVersion
}
return unversioned, 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 hasExistingCS {
vecs := ConvertContainerServiceToVLabs(existingContainerService)
if e := containerService.Merge(vecs); e != nil {
return nil, e
}
}
if e := containerService.Properties.Validate(isUpdate); validate && e != nil {
return nil, e
}
unversioned := ConvertVLabsContainerService(containerService)
if curOrchVersion != "" &&
(containerService.Properties.OrchestratorProfile == nil ||
(containerService.Properties.OrchestratorProfile.OrchestratorVersion == "" &&
containerService.Properties.OrchestratorProfile.OrchestratorRelease == "")) {
unversioned.Properties.OrchestratorProfile.OrchestratorVersion = curOrchVersion
}
return unversioned, 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, isUpdate bool,
defaultKubernetesVersion string,
existingContainerService *ContainerService) (*ContainerService, bool, error) {
hasExistingCS := existingContainerService != nil
IsSSHAutoGenerated := false
switch version {
case v20170831.APIVersion:
managedCluster := &v20170831.ManagedCluster{}
if e := json.Unmarshal(contents, &managedCluster); e != nil {
return nil, IsSSHAutoGenerated, e
}
// verify orchestrator version
if len(managedCluster.Properties.KubernetesVersion) > 0 && !common.AllKubernetesSupportedVersions[managedCluster.Properties.KubernetesVersion] {
return nil, IsSSHAutoGenerated, a.Translator.Errorf("The selected orchestrator version '%s' is not supported", managedCluster.Properties.KubernetesVersion)
}
// use defaultKubernetesVersion arg if no version was supplied in the request contents
if managedCluster.Properties.KubernetesVersion == "" && defaultKubernetesVersion != "" {
if !common.AllKubernetesSupportedVersions[defaultKubernetesVersion] {
return nil, IsSSHAutoGenerated, a.Translator.Errorf("The selected orchestrator version '%s' is not supported", defaultKubernetesVersion)
}
managedCluster.Properties.KubernetesVersion = defaultKubernetesVersion
}
if e := managedCluster.Properties.Validate(); validate && e != nil {
return nil, IsSSHAutoGenerated, e
}
return ConvertV20170831AgentPoolOnly(managedCluster), false, nil
case v20180331.APIVersion:
managedCluster := &v20180331.ManagedCluster{}
if e := json.Unmarshal(contents, &managedCluster); e != nil {
return nil, IsSSHAutoGenerated, e
}
// verify orchestrator version
if len(managedCluster.Properties.KubernetesVersion) > 0 && !common.AllKubernetesSupportedVersions[managedCluster.Properties.KubernetesVersion] {
return nil, IsSSHAutoGenerated, a.Translator.Errorf("The selected orchestrator version '%s' is not supported", managedCluster.Properties.KubernetesVersion)
}
// use defaultKubernetesVersion arg if no version was supplied in the request contents
if managedCluster.Properties.KubernetesVersion == "" && defaultKubernetesVersion != "" {
if !common.AllKubernetesSupportedVersions[defaultKubernetesVersion] {
return nil, IsSSHAutoGenerated, a.Translator.Errorf("The selected orchestrator version '%s' is not supported", defaultKubernetesVersion)
}
if hasExistingCS {
managedCluster.Properties.KubernetesVersion = existingContainerService.Properties.OrchestratorProfile.OrchestratorVersion
} else {
managedCluster.Properties.KubernetesVersion = defaultKubernetesVersion
}
}
if hasExistingCS {
vemc := ConvertContainerServiceToV20180331AgentPoolOnly(existingContainerService)
if e := managedCluster.Merge(vemc); e != nil {
return nil, IsSSHAutoGenerated, e
}
}
if e := managedCluster.Properties.Validate(); validate && e != nil {
return nil, IsSSHAutoGenerated, e
}
// only generate ssh key on new cluster
if !hasExistingCS && managedCluster.Properties.LinuxProfile == nil {
linuxProfile := &v20180331.LinuxProfile{}
linuxProfile.AdminUsername = "azureuser"
_, publicKey, err := helpers.CreateSSH(rand.Reader, a.Translator)
if err != nil {
return nil, IsSSHAutoGenerated, err
}
linuxProfile.SSH.PublicKeys = []v20180331.PublicKey{{KeyData: publicKey}}
managedCluster.Properties.LinuxProfile = linuxProfile
IsSSHAutoGenerated = true
}
return ConvertV20180331AgentPoolOnly(managedCluster), IsSSHAutoGenerated, nil
case apvlabs.APIVersion:
managedCluster := &apvlabs.ManagedCluster{}
if e := json.Unmarshal(contents, &managedCluster); e != nil {
return nil, IsSSHAutoGenerated, e
}
if e := managedCluster.Properties.Validate(); validate && e != nil {
return nil, IsSSHAutoGenerated, e
}
return ConvertVLabsAgentPoolOnly(managedCluster), IsSSHAutoGenerated, nil
default:
return nil, IsSSHAutoGenerated, a.Translator.Errorf("unrecognized APIVersion in LoadContainerServiceForAgentPoolOnlyCluster '%s'", version)
}
}
// 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 := helpers.JSONMarshalIndent(armContainerService, "", " ", false)
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 := helpers.JSONMarshalIndent(armContainerService, "", " ", false)
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 := helpers.JSONMarshalIndent(armContainerService, "", " ", false)
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 := helpers.JSONMarshalIndent(armContainerService, "", " ", false)
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 := helpers.JSONMarshalIndent(armContainerService, "", " ", false)
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 := helpers.JSONMarshalIndent(armContainerService, "", " ", false)
if err != nil {
return nil, err
}
return b, nil
case v20180331.APIVersion:
v20180331ContainerService := ConvertContainerServiceToV20180331AgentPoolOnly(containerService)
armContainerService := &V20180331ARMManagedContainerService{}
armContainerService.ManagedCluster = v20180331ContainerService
armContainerService.APIVersion = version
b, err := helpers.JSONMarshalIndent(armContainerService, "", " ", false)
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,
}
}
}