Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 0 additions & 100 deletions pkg/library/flatten/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
"fmt"
"reflect"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
)

Expand Down Expand Up @@ -70,100 +67,3 @@ func formatImportCycle(end *resolutionContextTree) string {
}
return cycle
}

func parseResourcesFromComponent(component *dw.Component) (*corev1.ResourceRequirements, error) {
if component.Container == nil {
return nil, fmt.Errorf("attemped to parse resource requirements from a non-container component")
}
memLimitStr := component.Container.MemoryLimit
if memLimitStr == "" {
memLimitStr = "0Mi"
}
memRequestStr := component.Container.MemoryRequest
if memRequestStr == "" {
memRequestStr = "0Mi"
}
cpuLimitStr := component.Container.CpuLimit
if cpuLimitStr == "" {
cpuLimitStr = "0m"
}
cpuRequestStr := component.Container.CpuRequest
if cpuRequestStr == "" {
cpuRequestStr = "0m"
}

memoryLimit, err := resource.ParseQuantity(memLimitStr)
if err != nil {
return nil, fmt.Errorf("failed to parse memory limit for container component %s: %w", component.Name, err)
}
memoryRequest, err := resource.ParseQuantity(memRequestStr)
if err != nil {
return nil, fmt.Errorf("failed to parse memory request for container component %s: %w", component.Name, err)
}
cpuLimit, err := resource.ParseQuantity(cpuLimitStr)
if err != nil {
return nil, fmt.Errorf("failed to parse CPU limit for container component %s: %w", component.Name, err)
}
cpuRequest, err := resource.ParseQuantity(cpuRequestStr)
if err != nil {
return nil, fmt.Errorf("failed to parse CPU request for container component %s: %w", component.Name, err)
}

return &corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: memoryLimit,
corev1.ResourceCPU: cpuLimit,
},
Requests: corev1.ResourceList{
corev1.ResourceMemory: memoryRequest,
corev1.ResourceCPU: cpuRequest,
},
}, nil
}

func addResourceRequirements(resources *corev1.ResourceRequirements, toAdd *dw.Component) error {
componentResources, err := parseResourcesFromComponent(toAdd)
if err != nil {
return err
}

memoryLimit := resources.Limits[corev1.ResourceMemory]
memoryLimit.Add(componentResources.Limits[corev1.ResourceMemory])
resources.Limits[corev1.ResourceMemory] = memoryLimit

cpuLimit := resources.Limits[corev1.ResourceCPU]
cpuLimit.Add(componentResources.Limits[corev1.ResourceCPU])
resources.Limits[corev1.ResourceCPU] = cpuLimit

memoryRequest := resources.Requests[corev1.ResourceMemory]
memoryRequest.Add(componentResources.Requests[corev1.ResourceMemory])
resources.Requests[corev1.ResourceMemory] = memoryRequest

cpuRequest := resources.Requests[corev1.ResourceCPU]
cpuRequest.Add(componentResources.Requests[corev1.ResourceCPU])
resources.Requests[corev1.ResourceCPU] = cpuRequest

return nil
}

func applyResourceRequirementsToComponent(container *dw.ContainerComponent, resources *corev1.ResourceRequirements) {
memLimit := resources.Limits[corev1.ResourceMemory]
if !memLimit.IsZero() {
container.MemoryLimit = memLimit.String()
}

cpuLimit := resources.Limits[corev1.ResourceCPU]
if !cpuLimit.IsZero() {
container.CpuLimit = cpuLimit.String()
}

memRequest := resources.Requests[corev1.ResourceMemory]
if !memRequest.IsZero() {
container.MemoryRequest = memRequest.String()
}

cpuRequest := resources.Requests[corev1.ResourceCPU]
if !cpuRequest.IsZero() {
container.CpuRequest = cpuRequest.String()
}
}
7 changes: 4 additions & 3 deletions pkg/library/flatten/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/devfile/api/v2/pkg/attributes"
"github.com/devfile/api/v2/pkg/utils/overriding"
"github.com/devfile/devworkspace-operator/pkg/constants"
dwResources "github.com/devfile/devworkspace-operator/pkg/library/resources"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"
)
Expand Down Expand Up @@ -269,7 +270,7 @@ func mergeContributionsInto(mergeInto *dw.Component, contributions []dw.Componen
if mergeInto == nil || mergeInto.Container == nil {
return nil, fmt.Errorf("attempting to merge container contributions into a non-container component")
}
totalResources, err := parseResourcesFromComponent(mergeInto)
totalResources, err := dwResources.ParseResourcesFromComponent(mergeInto)
if err != nil {
return nil, err
}
Expand All @@ -293,7 +294,7 @@ func mergeContributionsInto(mergeInto *dw.Component, contributions []dw.Componen
mergedComponentNames = append(mergedComponentNames, component.Attributes.GetString(constants.PluginSourceAttribute, nil))
delete(component.Attributes, constants.PluginSourceAttribute)
}
if err := addResourceRequirements(totalResources, &component); err != nil {
if err := dwResources.AddResourceRequirements(totalResources, &component); err != nil {
return nil, err
}
component.Container.MemoryLimit = ""
Expand Down Expand Up @@ -327,7 +328,7 @@ func mergeContributionsInto(mergeInto *dw.Component, contributions []dw.Componen
}

mergedComponent := mergedSpecContent.Components[0]
applyResourceRequirementsToComponent(mergedComponent.Container, totalResources)
dwResources.ApplyResourceRequirementsToComponent(mergedComponent.Container, totalResources)

if mergedComponent.Attributes == nil {
mergedComponent.Attributes = attributes.Attributes{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ output:
- name: checode
path: /checode
memoryLimit: 3Gi
memoryRequest: 256Mi
cpuLimit: 500m
cpuRequest: 30m
env:
- name: GOPATH
value: /projects:/home/user/go
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: "Only adds contributioon resources if they are defined in merge target"

input:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merge-contribution: true
container:
image: test-image
memoryLimit: 1Gi
# memoryRequest is not defined
# cpuLimit is not defined
cpuRequest: 15m
- name: first-contribution
plugin:
uri: first-contribution.yaml
- name: second-contribution
plugin:
uri: second-contribution.yaml

devfileResources:
first-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: first-contribution
components:
- name: first-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
memoryLimit: 512Mi
memoryRequest: 1.5G
cpuLimit: "0.5"
# cpuRequest is not defined
second-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: second-contribution
components:
- name: second-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
# memoryLimit is not defined
# memoryRequest is not defined
# cpuLimit is not defined
cpuRequest: 100m

output:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merged-contributions: "first-contribution,second-contribution"
container:
image: test-image
memoryLimit: 1536Mi # 1Gi + 512Mi (from first-contribution)
cpuRequest: 115m # 15m + 100m (from second-contributioon)

Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ output:
- name: checode
path: /checode
memoryLimit: 3Gi
memoryRequest: 256Mi
cpuLimit: 500m
cpuRequest: 30m
env:
- name: GOPATH
value: /projects:/home/user/go
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: "Only adds contributioon resources if they are defined in merge target"

input:
devworkspace:
components:
- name: test-component
container:
image: test-image
memoryLimit: 1Gi
# memoryRequest is not defined
# cpuLimit is not defined
cpuRequest: 15m
- name: first-contribution
plugin:
uri: first-contribution.yaml
- name: second-contribution
plugin:
uri: second-contribution.yaml

devfileResources:
first-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: first-contribution
components:
- name: first-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
memoryLimit: 512Mi
memoryRequest: 1.5G
cpuLimit: "0.5"
# cpuRequest is not defined
second-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: second-contribution
components:
- name: second-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
# memoryLimit is not defined
# memoryRequest is not defined
# cpuLimit is not defined
cpuRequest: 100m

output:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merged-contributions: "first-contribution,second-contribution"
container:
image: test-image
memoryLimit: 1536Mi # 1Gi + 512Mi (from first-contribution)
cpuRequest: 115m # 15m + 100m (from second-contributioon)

Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ output:
- name: checode
path: /checode
memoryLimit: 3Gi
memoryRequest: 256Mi
cpuLimit: 500m
cpuRequest: 30m
env:
- name: GOPATH
value: /projects:/home/user/go
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: "Only adds contributioon resources if they are defined in merge target"

input:
devworkspace:
components:
- name: test-component
container:
image: test-image
memoryLimit: 1Gi
# memoryRequest is not defined
# cpuLimit is not defined
cpuRequest: 15m
contributions:
- name: first-contribution
uri: first-contribution.yaml
- name: second-contribution
uri: second-contribution.yaml

devfileResources:
first-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: first-contribution
components:
- name: first-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
memoryLimit: 512Mi
memoryRequest: 1.5G
cpuLimit: "0.5"
# cpuRequest is not defined
second-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: second-contribution
components:
- name: second-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
# memoryLimit is not defined
# memoryRequest is not defined
# cpuLimit is not defined
cpuRequest: 100m

output:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merged-contributions: "first-contribution,second-contribution"
container:
image: test-image
memoryLimit: 1536Mi # 1Gi + 512Mi (from first-contribution)
cpuRequest: 115m # 15m + 100m (from second-contributioon)

Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ output:
- name: checode
path: /checode
memoryLimit: 3Gi
memoryRequest: 256Mi
cpuLimit: 500m
cpuRequest: 30m
env:
- name: GOPATH
value: /projects:/home/user/go
Expand Down
Loading