forked from redhat-appstudio/application-service
/
update.go
445 lines (388 loc) · 15.1 KB
/
update.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
Copyright 2021 Red Hat, Inc.
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 controllers
import (
"fmt"
devfileAPIV1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
data "github.com/devfile/library/pkg/devfile/parser/data"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
appstudiov1alpha1 "github.com/redhat-appstudio/application-service/api/v1alpha1"
devfile "github.com/redhat-appstudio/application-service/pkg/devfile"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
func (r *ComponentReconciler) updateComponentDevfileModel(hasCompDevfileData data.DevfileData, component appstudiov1alpha1.Component) error {
log := r.Log.WithValues("Component", "updateComponentDevfileModel")
devfileComponents, err := hasCompDevfileData.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{
ComponentType: devfileAPIV1.ContainerComponentType,
},
})
if err != nil {
return err
}
for i, devfileComponent := range devfileComponents {
compUpdateRequired := false
// Update for Route
if component.Spec.Route != "" {
if len(devfileComponent.Attributes) == 0 {
devfileComponent.Attributes = attributes.Attributes{}
}
if component.Spec.Route != "" {
log.Info(fmt.Sprintf("setting devfile component %s attribute component.Spec.Route %s", devfileComponent.Name, component.Spec.Route))
devfileComponent.Attributes = devfileComponent.Attributes.PutString(routeKey, component.Spec.Route)
compUpdateRequired = true
}
}
// Update for Replica
currentReplica := 0
if len(devfileComponent.Attributes) == 0 {
devfileComponent.Attributes = attributes.Attributes{}
} else {
var err error
currentReplica = int(devfileComponent.Attributes.GetNumber(replicaKey, &err))
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
}
}
}
if currentReplica != component.Spec.Replicas {
log.Info(fmt.Sprintf("setting devfile component %s attribute component.Spec.Replicas to %v", devfileComponent.Name, component.Spec.Replicas))
devfileComponent.Attributes = devfileComponent.Attributes.PutInteger(replicaKey, component.Spec.Replicas)
compUpdateRequired = true
}
// Update for Port
if i == 0 && component.Spec.TargetPort > 0 {
for i, endpoint := range devfileComponent.Container.Endpoints {
log.Info(fmt.Sprintf("setting devfile component %s endpoint %s port to %v", devfileComponent.Name, endpoint.Name, component.Spec.TargetPort))
endpoint.TargetPort = component.Spec.TargetPort
compUpdateRequired = true
devfileComponent.Container.Endpoints[i] = endpoint
}
}
// Update for Env
for _, env := range component.Spec.Env {
if env.ValueFrom != nil {
return fmt.Errorf("env.ValueFrom is not supported at the moment, use env.value")
}
name := env.Name
value := env.Value
isPresent := false
for i, devfileEnv := range devfileComponent.Container.Env {
if devfileEnv.Name == name {
isPresent = true
log.Info(fmt.Sprintf("setting devfileComponent %s env %s value to %v", devfileComponent.Name, devfileEnv.Name, value))
devfileEnv.Value = value
devfileComponent.Container.Env[i] = devfileEnv
compUpdateRequired = true
}
}
if !isPresent {
log.Info(fmt.Sprintf("appending to devfile component %s env %s : %v", devfileComponent.Name, name, value))
devfileComponent.Container.Env = append(devfileComponent.Container.Env, devfileAPIV1.EnvVar{Name: name, Value: value})
compUpdateRequired = true
}
}
// Update for limits
limits := component.Spec.Resources.Limits
if len(limits) > 0 {
// CPU Limit
resourceCPULimit := limits[corev1.ResourceCPU]
if resourceCPULimit.String() != "" {
log.Info(fmt.Sprintf("setting devfile component %s cpu limit to %s", devfileComponent.Name, resourceCPULimit.String()))
devfileComponent.Container.CpuLimit = resourceCPULimit.String()
compUpdateRequired = true
}
// Memory Limit
resourceMemoryLimit := limits[corev1.ResourceMemory]
if resourceMemoryLimit.String() != "" {
log.Info(fmt.Sprintf("setting devfile component %s memory limit to %s", devfileComponent.Name, resourceMemoryLimit.String()))
devfileComponent.Container.MemoryLimit = resourceMemoryLimit.String()
compUpdateRequired = true
}
// Storage Limit
resourceStorageLimit := limits[corev1.ResourceStorage]
if len(devfileComponent.Attributes) == 0 {
devfileComponent.Attributes = attributes.Attributes{}
}
if resourceStorageLimit.String() != "0" {
log.Info(fmt.Sprintf("setting devfile component %s attribute storage limit to %s", devfileComponent.Name, resourceStorageLimit.String()))
devfileComponent.Attributes = devfileComponent.Attributes.PutString(storageLimitKey, resourceStorageLimit.String())
compUpdateRequired = true
}
// Ephemeral Storage Limit
resourceEphemeralStorageLimit := limits[corev1.ResourceEphemeralStorage]
if len(devfileComponent.Attributes) == 0 {
devfileComponent.Attributes = attributes.Attributes{}
}
if resourceEphemeralStorageLimit.String() != "0" {
log.Info(fmt.Sprintf("updating devfile component %s attribute ephemeral storage limit to %s", devfileComponent.Name, resourceEphemeralStorageLimit.String()))
devfileComponent.Attributes = devfileComponent.Attributes.PutString(ephemeralStorageLimitKey, resourceEphemeralStorageLimit.String())
compUpdateRequired = true
}
}
// Update for requests
requests := component.Spec.Resources.Requests
if len(requests) > 0 {
// CPU Request
resourceCPURequest := requests[corev1.ResourceCPU]
if resourceCPURequest.String() != "" {
log.Info(fmt.Sprintf("setting devfile component %s cpu request to %s", devfileComponent.Name, resourceCPURequest.String()))
devfileComponent.Container.CpuRequest = resourceCPURequest.String()
compUpdateRequired = true
}
// Memory Request
resourceMemoryRequest := requests[corev1.ResourceMemory]
if resourceMemoryRequest.String() != "" {
log.Info(fmt.Sprintf("setting devfile component %s memory request to %s", devfileComponent.Name, resourceMemoryRequest.String()))
devfileComponent.Container.MemoryRequest = resourceMemoryRequest.String()
compUpdateRequired = true
}
// Storage Request
resourceStorageRequest := requests[corev1.ResourceStorage]
if len(devfileComponent.Attributes) == 0 {
devfileComponent.Attributes = attributes.Attributes{}
}
if resourceStorageRequest.String() != "0" {
log.Info(fmt.Sprintf("updating devfile component %s attribute storage request to %s", devfileComponent.Name, resourceStorageRequest.String()))
devfileComponent.Attributes = devfileComponent.Attributes.PutString(storageRequestKey, resourceStorageRequest.String())
compUpdateRequired = true
}
// Ephemeral Storage Request
resourceEphemeralStorageRequest := requests[corev1.ResourceEphemeralStorage]
if len(devfileComponent.Attributes) == 0 {
devfileComponent.Attributes = attributes.Attributes{}
}
if resourceEphemeralStorageRequest.String() != "0" {
log.Info(fmt.Sprintf("setting devfile component %s attribute ephemeral storage limit to %s", devfileComponent.Name, resourceEphemeralStorageRequest.String()))
devfileComponent.Attributes = devfileComponent.Attributes.PutString(ephemeralStorageRequestKey, resourceEphemeralStorageRequest.String())
compUpdateRequired = true
}
}
if compUpdateRequired {
// Update the devfileComponent once it has been updated with the Component data
log.Info(fmt.Sprintf("updating devfile component name %s ...", devfileComponent.Name))
err := hasCompDevfileData.UpdateComponent(devfileComponent)
if err != nil {
return err
}
}
}
return nil
}
func (r *ComponentReconciler) updateApplicationDevfileModel(hasAppDevfileData data.DevfileData, component appstudiov1alpha1.Component) error {
if component.Spec.Source.GitSource == nil {
return fmt.Errorf("component git source is nil")
}
newProject := devfileAPIV1.Project{
Name: component.Spec.ComponentName,
ProjectSource: devfileAPIV1.ProjectSource{
Git: &devfileAPIV1.GitProjectSource{
GitLikeProjectSource: devfileAPIV1.GitLikeProjectSource{
Remotes: map[string]string{
"origin": component.Spec.Source.GitSource.URL,
},
},
},
},
}
projects, err := hasAppDevfileData.GetProjects(common.DevfileOptions{})
if err != nil {
return err
}
for _, project := range projects {
if project.Name == newProject.Name {
return fmt.Errorf("application already has a project with name %s", newProject.Name)
}
}
err = hasAppDevfileData.AddProjects([]devfileAPIV1.Project{newProject})
if err != nil {
return err
}
return nil
}
func (r *ComponentDetectionQueryReconciler) updateComponentStub(componentDetectionQuery *appstudiov1alpha1.ComponentDetectionQuery, devfilesMap map[string][]byte) error {
if componentDetectionQuery == nil {
return fmt.Errorf("componentDetectionQuery is nil")
}
log := r.Log.WithValues("ComponentDetectionQuery", "updateComponentStub")
if len(componentDetectionQuery.Status.ComponentDetected) == 0 {
componentDetectionQuery.Status.ComponentDetected = make(appstudiov1alpha1.ComponentDetectionMap)
}
log.Info(fmt.Sprintf("Devfiles detected: %v", len(devfilesMap)))
for context, devfileBytes := range devfilesMap {
log.Info(fmt.Sprintf("Currently reading the devfile from context %v", context))
// Parse the Component Devfile
compDevfileData, err := devfile.ParseDevfileModel(string(devfileBytes))
if err != nil {
return err
}
devfileMetadata := compDevfileData.GetMetadata()
devfileContainerComponents, err := compDevfileData.GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{
ComponentType: devfileAPIV1.ContainerComponentType,
},
})
if err != nil {
return err
}
componentStub := appstudiov1alpha1.ComponentSpec{
ComponentName: devfileMetadata.Name,
Application: "insert-application-name",
Context: context,
Source: appstudiov1alpha1.ComponentSource{
ComponentSourceUnion: appstudiov1alpha1.ComponentSourceUnion{
GitSource: &componentDetectionQuery.Spec.GitSource,
},
},
}
// Since a devfile can have N container components, we only try to populate the stub with the first container component
if len(devfileContainerComponents) != 0 {
// Devfile Env
for _, devfileEnv := range devfileContainerComponents[0].Container.Env {
componentStub.Env = append(componentStub.Env, corev1.EnvVar{
Name: devfileEnv.Name,
Value: devfileEnv.Value,
})
}
// Devfile Port
for i, endpoint := range devfileContainerComponents[0].Container.Endpoints {
if i == 0 {
componentStub.TargetPort = endpoint.TargetPort
break
}
}
// Devfile Route
componentStub.Route = devfileContainerComponents[0].Attributes.GetString(routeKey, &err)
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
}
}
// Devfile Replica
componentStub.Replicas = int(devfileContainerComponents[0].Attributes.GetNumber(replicaKey, &err))
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
}
}
// Devfile Limits
if len(componentStub.Resources.Limits) == 0 {
componentStub.Resources.Limits = make(corev1.ResourceList)
}
limits := componentStub.Resources.Limits
// CPU Limit
if devfileContainerComponents[0].Container.CpuLimit != "" {
cpuLimit, err := resource.ParseQuantity(devfileContainerComponents[0].Container.CpuLimit)
if err != nil {
return err
}
limits[corev1.ResourceCPU] = cpuLimit
}
// Memory Limit
if devfileContainerComponents[0].Container.MemoryLimit != "" {
memoryLimit, err := resource.ParseQuantity(devfileContainerComponents[0].Container.MemoryLimit)
if err != nil {
return err
}
limits[corev1.ResourceMemory] = memoryLimit
}
// Storage Limit
storageLimitString := devfileContainerComponents[0].Attributes.GetString(storageLimitKey, &err)
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
}
}
if storageLimitString != "" {
storageLimit, err := resource.ParseQuantity(storageLimitString)
if err != nil {
return err
}
limits[corev1.ResourceStorage] = storageLimit
}
// Ephemeral Storage Limit
ephemeralStorageLimitString := devfileContainerComponents[0].Attributes.GetString(ephemeralStorageLimitKey, &err)
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
}
}
if ephemeralStorageLimitString != "" {
ephemeralStorageLimit, err := resource.ParseQuantity(ephemeralStorageLimitString)
if err != nil {
return err
}
limits[corev1.ResourceEphemeralStorage] = ephemeralStorageLimit
}
// Devfile Request
if len(componentStub.Resources.Requests) == 0 {
componentStub.Resources.Requests = make(corev1.ResourceList)
}
requests := componentStub.Resources.Requests
// CPU Request
if devfileContainerComponents[0].Container.CpuRequest != "" {
CpuRequest, err := resource.ParseQuantity(devfileContainerComponents[0].Container.CpuRequest)
if err != nil {
return err
}
requests[corev1.ResourceCPU] = CpuRequest
}
// Memory Request
if devfileContainerComponents[0].Container.MemoryRequest != "" {
memoryRequest, err := resource.ParseQuantity(devfileContainerComponents[0].Container.MemoryRequest)
if err != nil {
return err
}
requests[corev1.ResourceMemory] = memoryRequest
}
// Storage Request
storageRequestString := devfileContainerComponents[0].Attributes.GetString(storageRequestKey, &err)
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
}
}
if storageRequestString != "" {
storageRequest, err := resource.ParseQuantity(storageRequestString)
if err != nil {
return err
}
requests[corev1.ResourceStorage] = storageRequest
}
// Ephemeral Storage Request
ephemeralStorageRequestString := devfileContainerComponents[0].Attributes.GetString(ephemeralStorageRequestKey, &err)
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
}
}
if ephemeralStorageRequestString != "" {
ephemeralStorageRequest, err := resource.ParseQuantity(ephemeralStorageRequestString)
if err != nil {
return err
}
requests[corev1.ResourceEphemeralStorage] = ephemeralStorageRequest
}
}
componentDetectionQuery.Status.ComponentDetected[devfileMetadata.Name] = appstudiov1alpha1.ComponentDetectionDescription{
DevfileFound: true,
Language: devfileMetadata.Language,
ProjectType: devfileMetadata.ProjectType,
ComponentStub: componentStub,
}
}
return nil
}