Repository: appvia/kev
Pull request #500 (2021-04-30 13:14:06)

kev-458 move cpu


Patch:
diff --git a/pkg/kev/config/extensions.go b/pkg/kev/config/extensions.go
index 8ba8237..cd10ae9 100644
--- a/pkg/kev/config/extensions.go
+++ b/pkg/kev/config/extensions.go
@@ -139,7 +139,7 @@ func K8SCfgFromCompose(svc *composego.ServiceConfig) (K8SConfiguration, error) {
 		return K8SConfiguration{}, err
 	}
 
-	resource, err := ResourcesFromCompose(svc)
+	resource, err := ResourceFromCompose(svc)
 	if err != nil {
 		return K8SConfiguration{}, err
 	}
@@ -158,20 +158,26 @@ func K8SCfgFromCompose(svc *composego.ServiceConfig) (K8SConfiguration, error) {
 	return cfg, nil
 }
 
-func ResourcesFromCompose(svc *composego.ServiceConfig) (Resource, error) {
+func ResourceFromCompose(svc *composego.ServiceConfig) (Resource, error) {
 	var memLimit string
+	var cpuLimit string
 	if svc.Deploy != nil && svc.Deploy.Resources.Limits != nil {
 		memLimit = getMemoryQuantity(int64(svc.Deploy.Resources.Limits.MemoryBytes))
+		cpuLimit = svc.Deploy.Resources.Limits.NanoCPUs
 	}
 
 	var memRequest string
+	var cpuRequest string
 	if svc.Deploy != nil && svc.Deploy.Resources.Reservations != nil {
 		memRequest = getMemoryQuantity(int64(svc.Deploy.Resources.Reservations.MemoryBytes))
+		cpuRequest = svc.Deploy.Resources.Reservations.NanoCPUs
 	}
 
 	return Resource{
 		MaxMemory: memLimit,
 		Memory:    memRequest,
+		CPU:       cpuRequest,
+		MaxCPU:    cpuLimit,
 	}, nil
 }
 
@@ -388,6 +394,8 @@ type Workload struct {
 type Resource struct {
 	Memory    string `yaml:"memory,omitempty"`
 	MaxMemory string `yaml:"maxMemory,omitempty"`
+	CPU       string `yaml:"cpu,omitempty"`
+	MaxCPU    string `yaml:"maxCpu,omitempty"`
 }
 
 type ImagePull struct {
diff --git a/pkg/kev/config/labels.go b/pkg/kev/config/labels.go
index 6503ee7..f971cce 100644
--- a/pkg/kev/config/labels.go
+++ b/pkg/kev/config/labels.go
@@ -31,18 +31,6 @@ const (
 	// LabelWorkloadRollingUpdateMaxSurge max number of nodes updated at once
 	LabelWorkloadRollingUpdateMaxSurge = LabelPrefix + "workload.rolling-update-max-surge"
 
-	// LabelWorkloadMemory defines Memory request for workload
-	// LabelWorkloadMemory = LabelPrefix + "workload.memory"
-
-	// LabelWorkloadCPU defines CPU request for workload
-	LabelWorkloadCPU = LabelPrefix + "workload.cpu"
-
-	// LabelWorkloadMaxMemory defines max Memory limit for workload
-	// LabelWorkloadMaxMemory = LabelPrefix + "workload.max-memory"
-
-	// LabelWorkloadMaxCPU defines max CPU limit for workload
-	LabelWorkloadMaxCPU = LabelPrefix + "workload.max-cpu"
-
 	// LabelWorkloadSecurityContextRunAsUser sets pod security context RunAsUser attribute
 	LabelWorkloadSecurityContextRunAsUser = LabelPrefix + "workload.pod-security-run-as-user"
 
diff --git a/pkg/kev/converter/kubernetes/project_service.go b/pkg/kev/converter/kubernetes/project_service.go
index a24b598..fc06fee 100644
--- a/pkg/kev/converter/kubernetes/project_service.go
+++ b/pkg/kev/converter/kubernetes/project_service.go
@@ -299,7 +299,7 @@ func (p *ProjectService) resourceRequests() (*int64, *int64) {
 		memRequest, _ = v.AsInt64()
 	}
 
-	if val, ok := p.Labels[config.LabelWorkloadCPU]; ok {
+	if val := p.K8SConfig.Workload.Resource.CPU; val != "" {
 		v, _ := resource.ParseQuantity(val)
 		cpuRequest = v.ToDec().MilliValue()
 	}
@@ -328,7 +328,7 @@ func (p *ProjectService) resourceLimits() (*int64, *int64) {
 		memLimit, _ = v.AsInt64()
 	}
 
-	if val, ok := p.Labels[config.LabelWorkloadMaxCPU]; ok {
+	if val := p.K8SConfig.Workload.Resource.MaxCPU; val != "" {
 		v, _ := resource.ParseQuantity(val)
 		cpuLimit = v.ToDec().MilliValue()
 	}
diff --git a/pkg/kev/extract.go b/pkg/kev/extract.go
index 5f340e9..a70662f 100644
--- a/pkg/kev/extract.go
+++ b/pkg/kev/extract.go
@@ -57,8 +57,6 @@ func extractVolumesLabels(c *ComposeProject, out *composeOverride) {
 //TODO: Remove once all functions have been moved over.
 // extractDeploymentLabels extracts deployment related into a label's Service.
 func extractDeploymentLabels(source composego.ServiceConfig, target *ServiceConfig) {
-	extractWorkloadResourceRequests(source, target)
-	extractWorkloadResourceLimits(source, target)
 	extractWorkloadRollingUpdatePolicy(source, target)
 }
 
@@ -69,17 +67,3 @@ func extractWorkloadRollingUpdatePolicy(source composego.ServiceConfig, target *
 		target.Labels.Add(config.LabelWorkloadRollingUpdateMaxSurge, value)
 	}
 }
-
-// extractWorkloadResourceLimits extracts deployment's resource limits.
-func extractWorkloadResourceLimits(source composego.ServiceConfig, target *ServiceConfig) {
-	if source.Deploy != nil && source.Deploy.Resources.Limits != nil {
-		target.Labels.Add(config.LabelWorkloadMaxCPU, source.Deploy.Resources.Limits.NanoCPUs)
-	}
-}
-
-// extractWorkloadResourceRequests extracts deployment's resource requests.
-func extractWorkloadResourceRequests(source composego.ServiceConfig, target *ServiceConfig) {
-	if source.Deploy != nil && source.Deploy.Resources.Reservations != nil {
-		target.Labels.Add(config.LabelWorkloadCPU, source.Deploy.Resources.Reservations.NanoCPUs)
-	}
-}


Test patch:
diff --git a/pkg/kev/converter/kubernetes/project_service_test.go b/pkg/kev/converter/kubernetes/project_service_test.go
index a7d3b79..73d699e 100644
--- a/pkg/kev/converter/kubernetes/project_service_test.go
+++ b/pkg/kev/converter/kubernetes/project_service_test.go
@@ -690,9 +690,7 @@ var _ = Describe("ProjectService", func() {
 
 			Context("and CPU request is specified via label", func() {
 				BeforeEach(func() {
-					labels = composego.Labels{
-						config.LabelWorkloadCPU: "0.2",
-					}
+					k8sconf.Workload.Resource.CPU = "0.2"
 				})
 
 				It("returns CPU request as defined by the label value ", func() {
@@ -747,9 +745,7 @@ var _ = Describe("ProjectService", func() {
 
 			Context("and CPU limit is specified via label", func() {
 				BeforeEach(func() {
-					labels = composego.Labels{
-						config.LabelWorkloadMaxCPU: "0.2",
-					}
+					k8sconf.Workload.Resource.MaxCPU = "0.2"
 				})
 
 				It("returns CPU limit as defined by the label value ", func() {
diff --git a/pkg/kev/converter/kubernetes/transform_test.go b/pkg/kev/converter/kubernetes/transform_test.go
index 8a39f58..a0d4aef 100644
--- a/pkg/kev/converter/kubernetes/transform_test.go
+++ b/pkg/kev/converter/kubernetes/transform_test.go
@@ -2063,9 +2063,16 @@ var _ = Describe("Transform", func() {
 
 		Context("with cpu request provided in configuration", func() {
 			BeforeEach(func() {
-				projectService.Labels = composego.Labels{
-					config.LabelWorkloadCPU: "0.1",
+				k8sconf := config.DefaultK8SConfig()
+				k8sconf.Workload.Resource.CPU = "0.1"
+
+				ext, err := k8sconf.ToMap()
+				Expect(err).NotTo(HaveOccurred())
+				projectService.Extensions = map[string]interface{}{
+					config.K8SExtensionKey: ext,
 				}
+
+				projectService, err = NewProjectService(projectService.ServiceConfig)
 			})
 
 			It("sets container cpu request as expected", func() {
@@ -2076,9 +2083,16 @@ var _ = Describe("Transform", func() {
 
 		Context("with cpu limit provided in configuration", func() {
 			BeforeEach(func() {
-				projectService.Labels = composego.Labels{
-					config.LabelWorkloadMaxCPU: "0.5",
+				k8sconf := config.DefaultK8SConfig()
+				k8sconf.Workload.Resource.MaxCPU = "0.5"
+
+				ext, err := k8sconf.ToMap()
+				Expect(err).NotTo(HaveOccurred())
+				projectService.Extensions = map[string]interface{}{
+					config.K8SExtensionKey: ext,
 				}
+
+				projectService, err = NewProjectService(projectService.ServiceConfig)
 			})
 
 			It("sets container cpu limit as expected", func() {
diff --git a/pkg/kev/kev_reconcile_test.go b/pkg/kev/kev_reconcile_test.go
index 33f0491..9085a9f 100644
--- a/pkg/kev/kev_reconcile_test.go
+++ b/pkg/kev/kev_reconcile_test.go
@@ -247,6 +247,8 @@ var _ = Describe("Reconcile", func() {
 						},
 						Workload: config.Workload{
 							Resource: config.Resource{
+								CPU:       "0.1",
+								MaxCPU:    "0.5",
 								Memory:    "10Mi",
 								MaxMemory: "500Mi",
 							},
@@ -287,6 +289,8 @@ var _ = Describe("Reconcile", func() {
 							Replicas:      3,
 							RestartPolicy: "always",
 							Resource: config.Resource{
+								CPU:       "0.25",
+								MaxCPU:    "0.25",
 								Memory:    "20Mi",
 								MaxMemory: "50Mi",
 							},
@@ -317,6 +321,8 @@ var _ = Describe("Reconcile", func() {
 							Resource: config.Resource{
 								Memory:    "10Mi",
 								MaxMemory: "500Mi",
+								CPU:       "0.1",
+								MaxCPU:    "0.5",
 							},
 						},
 						Service: config.Service{
diff --git a/pkg/kev/testdata/init-default/compose-yaml/output.yaml b/pkg/kev/testdata/init-default/compose-yaml/output.yaml
index f7d5994..c3ca80f 100644
--- a/pkg/kev/testdata/init-default/compose-yaml/output.yaml
+++ b/pkg/kev/testdata/init-default/compose-yaml/output.yaml
@@ -26,6 +26,8 @@ services:
           type: none
         replicas: 1
         resource:
+          cpu: "0.1"
+          maxCpu: "0.5"
           maxMemory: 500Mi
           memory: 10Mi
         restartPolicy: always