Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

make resource required and also fix examples to be consistent with spec #435

Merged
merged 2 commits into from
Nov 14, 2019
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
4 changes: 2 additions & 2 deletions docs/concepts/component-schematic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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||
Expand Down Expand Up @@ -213,7 +213,7 @@ resources:
cpu:
required: "0.5"
memory:
required: 100M
required: "128"
```

#### `volumes`
Expand Down
32 changes: 31 additions & 1 deletion examples/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
cpu:
required: 0.5
memory:
required: 100M
required: "128"
---
apiVersion: core.oam.dev/v1alpha1
kind: ComponentSchematic
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -88,3 +113,8 @@ spec:
containers:
- name: worker
image: nginx:latest
resources:
cpu:
required: 0.1
memory:
required: "128"
5 changes: 5 additions & 0 deletions examples/helloworld-python-component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ spec:
- type: tcp
containerPort: 9999
name: http
resources:
cpu:
required: "0.1"
memory:
required: "128"
parameters:
- name: target
type: string
Expand Down
5 changes: 5 additions & 0 deletions examples/image-pull-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions examples/nginx-component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ spec:
- type: tcp
containerPort: 80
name: http
resources:
cpu:
required: 0.1
memory:
required: "128"
parameters:
- name: poet
type: string
Expand Down
4 changes: 4 additions & 0 deletions examples/volumes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ spec:
disk:
required: "50M"
ephemeral: false
cpu:
required: 0.1
memory:
required: "128"
---
apiVersion: core.oam.dev/v1alpha1
kind: ApplicationConfiguration
Expand Down
29 changes: 14 additions & 15 deletions src/schematic/component.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -525,8 +523,8 @@ type ExtendedResources = Vec<ExtendedResource>;
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Resources {
pub cpu: Option<CPU>,
pub memory: Option<Memory>,
pub cpu: CPU,
pub memory: Memory,
pub gpu: Option<GPU>,
pub volumes: Option<Vec<Volume>>,
pub extended: Option<ExtendedResources>,
Expand All @@ -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),
Expand All @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions src/schematic/component_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ fn test_container_deserialize() {
"livenessProbe": {},
"resources": {
"memory": {
"required": "2G"
"required": "128"
},
"cpu": {
"required": 0.1
},
"volumes": [
{
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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(),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/schematic/configuration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ fn test_application_configuration() {
.expect("JSON must parse");

assert!(conf.variables.is_some());
}
}