@@ -22,14 +22,14 @@ import (
22
22
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
23
23
"github.com/devfile/api/v2/pkg/utils/overriding"
24
24
"github.com/devfile/api/v2/pkg/validation/variables"
25
+ "k8s.io/apimachinery/pkg/api/errors"
26
+ "k8s.io/apimachinery/pkg/types"
27
+ "sigs.k8s.io/controller-runtime/pkg/client"
28
+
25
29
"github.com/devfile/devworkspace-operator/pkg/library/annotate"
26
30
registry "github.com/devfile/devworkspace-operator/pkg/library/flatten/internal_registry"
27
31
"github.com/devfile/devworkspace-operator/pkg/library/flatten/network"
28
32
"github.com/devfile/devworkspace-operator/pkg/library/flatten/web_terminal"
29
- "k8s.io/apimachinery/pkg/api/errors"
30
- "k8s.io/apimachinery/pkg/api/resource"
31
- "k8s.io/apimachinery/pkg/types"
32
- "sigs.k8s.io/controller-runtime/pkg/client"
33
33
)
34
34
35
35
const (
@@ -123,7 +123,7 @@ func recursiveResolve(workspace *dw.DevWorkspaceTemplateSpec, tooling ResolverTo
123
123
}
124
124
}
125
125
126
- if err := mergeVolumeComponents (resolvedContent , resolvedParent , pluginSpecContents ... ); err != nil {
126
+ if err := mergeDevWorkspaceElements (resolvedContent , resolvedParent , pluginSpecContents ... ); err != nil {
127
127
return nil , fmt .Errorf ("failed to merge DevWorkspace volumes: %w" , err )
128
128
}
129
129
resolvedContent , err := overriding .MergeDevWorkspaceTemplateSpec (resolvedContent , resolvedParent , pluginSpecContents ... )
@@ -300,85 +300,6 @@ func resolveElementByURI(
300
300
return dwt , nil
301
301
}
302
302
303
- // mergeVolumeComponents merges volume components sharing the same name according to the following rules
304
- // * If a volume is defined in main and duplicated in parent/plugins, the copy in parent/plugins is removed
305
- // * If a volume is defined in parent and duplicated in plugins, the copy in plugins is removed
306
- // * If a volume is defined in multiple plugins, all but the first definition is removed
307
- // * If a volume is defined as persistent, all duplicates will be persistent
308
- // * If duplicate volumes set a size, the larger size will be used.
309
- // Following the invocation of this function, there are no duplicate volumes defined across the main devworkspace, its
310
- // parent, and its plugins.
311
- func mergeVolumeComponents (main , parent * dw.DevWorkspaceTemplateSpecContent , plugins ... * dw.DevWorkspaceTemplateSpecContent ) error {
312
- volumeComponents := map [string ]dw.Component {}
313
- for _ , component := range main .Components {
314
- if component .Volume == nil {
315
- continue
316
- }
317
- if _ , exists := volumeComponents [component .Name ]; exists {
318
- return fmt .Errorf ("duplicate volume found in devfile: %s" , component .Name )
319
- }
320
- volumeComponents [component .Name ] = component
321
- }
322
-
323
- mergeVolumeComponents := func (spec * dw.DevWorkspaceTemplateSpecContent ) error {
324
- var newComponents []dw.Component
325
- for _ , component := range spec .Components {
326
- if component .Volume == nil {
327
- newComponents = append (newComponents , component )
328
- continue
329
- }
330
- if existingVol , exists := volumeComponents [component .Name ]; exists {
331
- if err := mergeVolume (existingVol .Volume , component .Volume ); err != nil {
332
- return err
333
- }
334
- } else {
335
- newComponents = append (newComponents , component )
336
- volumeComponents [component .Name ] = component
337
- }
338
- }
339
- spec .Components = newComponents
340
- return nil
341
- }
342
- if err := mergeVolumeComponents (parent ); err != nil {
343
- return err
344
- }
345
-
346
- for _ , plugin := range plugins {
347
- if err := mergeVolumeComponents (plugin ); err != nil {
348
- return err
349
- }
350
- }
351
-
352
- return nil
353
- }
354
-
355
- func mergeVolume (into , from * dw.VolumeComponent ) error {
356
- // If the new volume is persistent, make the original persistent
357
- if ! from .Ephemeral {
358
- into .Ephemeral = false
359
- }
360
- intoSize := into .Size
361
- if intoSize == "" {
362
- intoSize = "0"
363
- }
364
- intoSizeQty , err := resource .ParseQuantity (intoSize )
365
- if err != nil {
366
- return err
367
- }
368
- fromSize := from .Size
369
- if fromSize == "" {
370
- fromSize = "0"
371
- }
372
- fromSizeQty , err := resource .ParseQuantity (fromSize )
373
- if err != nil {
374
- return err
375
- }
376
- if fromSizeQty .Cmp (intoSizeQty ) > 0 {
377
- into .Size = from .Size
378
- }
379
- return nil
380
- }
381
-
382
303
// canImportDW returns true if a DevWorkspace in dwNamespace is allowed to reference the provided DevWorkspaceTemplate
383
304
// DevWorkspaces can by default only read DevWorkspaceTemplates in their own namespace, unless the DevWorkspaceTemplate
384
305
// has the controller.devfile.io/allow-import-from annotation.
0 commit comments