diff --git a/docs/concepts/component-schematic.md b/docs/concepts/component-schematic.md index 50e31be4..a0f564f0 100644 --- a/docs/concepts/component-schematic.md +++ b/docs/concepts/component-schematic.md @@ -159,7 +159,7 @@ A component schematic requires one or more containers, each consisting of the fo | :-- | :--| :-- | :-- | :-- | | **name** | Name of the container. | string. Must be unique per component. | ☑ || | **image**| A path or URI of the location of the container image. | string. Best practice is to include a tag suffix.| ☑ || -| **resources**| The runtime resources (such as CPU, memory, and storage) required by the container.| string. See [resources](#resources) section for details.|| +| **resources**| The runtime resources (such as CPU, memory, and storage) required by the container.| string. See [resources](#resources) section for details.| ☑ | | **ports**| The ports exposed by the container.| See [ports](#ports) section for details.|| | **cmd**| The command to run when the container starts.| string. Supply any arguments using the `args` field (see below).|| | **args**| Arguments to the `cmd` entrypoint.| string|| @@ -213,7 +213,7 @@ resources: cpu: required: "0.5" memory: - required: 100M + required: "128" ``` #### `volumes` diff --git a/examples/components.yaml b/examples/components.yaml index 33d77e3e..8528a789 100644 --- a/examples/components.yaml +++ b/examples/components.yaml @@ -15,7 +15,7 @@ spec: cpu: required: 0.5 memory: - required: 100M + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ComponentSchematic @@ -30,6 +30,11 @@ spec: - name: http containerPort: 80 protocol: TCP + resources: + cpu: + required: 0.1 + memory: + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ComponentSchematic @@ -44,6 +49,11 @@ spec: - name: http containerPort: 80 protocol: TCP + resources: + cpu: + required: 0.1 + memory: + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ComponentSchematic @@ -55,6 +65,11 @@ spec: containers: - name: runner image: alpine:latest + resources: + cpu: + required: 0.1 + memory: + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ComponentSchematic @@ -66,6 +81,11 @@ spec: containers: - name: runner image: alpine:latest + resources: + cpu: + required: 0.1 + memory: + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ComponentSchematic @@ -77,6 +97,11 @@ spec: containers: - name: worker image: nginx:latest + resources: + cpu: + required: 0.1 + memory: + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ComponentSchematic @@ -88,3 +113,8 @@ spec: containers: - name: worker image: nginx:latest + resources: + cpu: + required: 0.1 + memory: + required: "128" diff --git a/examples/helloworld-python-component.yaml b/examples/helloworld-python-component.yaml index 75cd31c6..06ad519b 100644 --- a/examples/helloworld-python-component.yaml +++ b/examples/helloworld-python-component.yaml @@ -17,6 +17,11 @@ spec: - type: tcp containerPort: 9999 name: http + resources: + cpu: + required: "0.1" + memory: + required: "128" parameters: - name: target type: string diff --git a/examples/image-pull-secret.yaml b/examples/image-pull-secret.yaml index 84f83812..f8242574 100644 --- a/examples/image-pull-secret.yaml +++ b/examples/image-pull-secret.yaml @@ -15,6 +15,11 @@ spec: - name: runner image: alpine:latest imagePullSecret: example-image-pull-secret + resources: + cpu: + required: "0.1" + memory: + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ApplicationConfiguration diff --git a/examples/nginx-component.yaml b/examples/nginx-component.yaml index 16ec69be..2625b10a 100644 --- a/examples/nginx-component.yaml +++ b/examples/nginx-component.yaml @@ -27,6 +27,11 @@ spec: - type: tcp containerPort: 80 name: http + resources: + cpu: + required: 0.1 + memory: + required: "128" parameters: - name: poet type: string diff --git a/examples/volumes.yaml b/examples/volumes.yaml index 1013bd18..d507ac88 100644 --- a/examples/volumes.yaml +++ b/examples/volumes.yaml @@ -15,6 +15,10 @@ spec: disk: required: "50M" ephemeral: false + cpu: + required: 0.1 + memory: + required: "128" --- apiVersion: core.oam.dev/v1alpha1 kind: ApplicationConfiguration diff --git a/src/schematic/component.rs b/src/schematic/component.rs index 7bd208d9..157b6970 100644 --- a/src/schematic/component.rs +++ b/src/schematic/component.rs @@ -1,7 +1,5 @@ use k8s_openapi::api::core::v1 as core; -use k8s_openapi::apimachinery::pkg::{ - api::resource::Quantity, util::intstr::IntOrString, -}; +use k8s_openapi::apimachinery::pkg::{api::resource::Quantity, util::intstr::IntOrString}; use log::info; use std::collections::BTreeMap; use std::path::Path; @@ -525,8 +523,8 @@ type ExtendedResources = Vec; #[serde(rename_all = "camelCase")] #[serde(default)] pub struct Resources { - pub cpu: Option, - pub memory: Option, + pub cpu: CPU, + pub memory: Memory, pub gpu: Option, pub volumes: Option>, pub extended: Option, @@ -535,14 +533,11 @@ pub struct Resources { impl Resources { fn to_resource_requirements(&self) -> core::ResourceRequirements { let mut requests = BTreeMap::new(); - - self.cpu - .clone() - .and_then(|cpu| requests.insert("cpu".to_string(), Quantity(cpu.required.to_string().clone()))); - self.memory - .clone() - .and_then(|mem| requests.insert("memory".to_string(), Quantity(mem.required.clone()))); - + requests.insert("cpu".to_string(), Quantity(self.cpu.required.to_string().clone())); + requests.insert( + "memory".to_string(), + Quantity(self.memory.required.clone() + "Mi"), + ); // TODO: Kubernetes does not have a built-in type for GPUs. What do we use? core::ResourceRequirements { requests: Some(requests), @@ -554,8 +549,12 @@ impl Resources { impl Default for Resources { fn default() -> Self { Resources { - cpu: None, - memory: None, + cpu: CPU { + required: 0.1, + }, + memory: Memory { + required: "128".into(), + }, gpu: None, volumes: None, extended: None, diff --git a/src/schematic/component_test.rs b/src/schematic/component_test.rs index a08ce2fe..aa393728 100644 --- a/src/schematic/component_test.rs +++ b/src/schematic/component_test.rs @@ -115,7 +115,10 @@ fn test_container_deserialize() { "livenessProbe": {}, "resources": { "memory": { - "required": "2G" + "required": "128" + }, + "cpu": { + "required": 0.1 }, "volumes": [ { @@ -175,11 +178,8 @@ fn test_container_deserialize() { let res = &container.resources; - assert_eq!( - "2G", - res.memory.as_ref().expect("memory should be set").required - ); - assert!(res.cpu.is_none()); + assert_eq!("128", res.memory.required); + assert_eq!(0.1, res.cpu.required); let vols = res.volumes.clone().expect("expected volumes"); let path1 = vols.get(0).expect("expect a first volume"); @@ -508,12 +508,12 @@ fn test_to_volume_mounts() { name: "test_container".into(), image: "test/image".into(), resources: Resources { - cpu: Some(CPU { - required: 1.into(), - }), - memory: Some(Memory { + cpu: CPU { + required: 0.1.into(), + }, + memory: Memory { required: "128".into(), - }), + }, gpu: Some(GPU { required: 0.into(), }), diff --git a/src/schematic/configuration_test.rs b/src/schematic/configuration_test.rs index 22e56fac..e0ceffb4 100644 --- a/src/schematic/configuration_test.rs +++ b/src/schematic/configuration_test.rs @@ -41,4 +41,4 @@ fn test_application_configuration() { .expect("JSON must parse"); assert!(conf.variables.is_some()); -} +} \ No newline at end of file