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
36 changes: 32 additions & 4 deletions src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ pub struct VolumeMount {
pub path: String,
}

fn default_container_type() -> String {
"main".to_string()
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Default)]
pub struct Container {
pub name: String,
pub image: String,
#[serde(rename = "type", default, skip_serializing_if = "String::is_empty")]
#[serde(
rename = "type",
default = "default_container_type",
skip_serializing_if = "String::is_empty"
)]
pub container_type: String,
#[serde(
rename = "shared_volume_path",
Expand All @@ -137,6 +145,26 @@ pub struct Container {
pub ports: Vec<Port>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub volume_mounts: Vec<VolumeMount>,
#[serde(default = "default_cpu", skip_serializing_if = "is_default_cpu")]
pub cpu: u32,
#[serde(default = "default_memory", skip_serializing_if = "is_default_memory")]
pub memory: u32,
}

fn default_cpu() -> u32 {
1
}

fn default_memory() -> u32 {
1024
}

fn is_default_cpu(cpu: &u32) -> bool {
*cpu == 1
}

fn is_default_memory(memory: &u32) -> bool {
*memory == 1024
}

impl Display for Container {
Expand All @@ -155,7 +183,7 @@ impl Tabled for Container {
self.ports
.iter()
.map(|p| match p.publish {
Some(true) => format!("{} (published)", p.target),
true => format!("{} (published)", p.target),
_ => format!("{}", p.target),
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -197,8 +225,8 @@ impl Tabled for Container {
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Port {
pub target: u16,
#[serde(skip_serializing_if = "Option::is_none")]
pub publish: Option<bool>,
#[serde(default, skip_serializing_if = "is_default")]
pub publish: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down
19 changes: 4 additions & 15 deletions src/commands/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,11 @@ impl ComposeBuilder {
let mut container = Container {
name: container_name,
image: full_image,
container_type: String::new(),
shared_volume_path: String::new(),
volume_mounts: vec![],
ports: vec![Port {
target: container_port,
publish: Some(true),
publish: true,
}],
environment: IndexMap::new(),
secrets: IndexMap::new(),
command: Vec::new(),
..Default::default()
};

// Ask for entrypoint
Expand Down Expand Up @@ -588,13 +583,7 @@ impl ImageName {
service.containers.0.push(Container {
name: format!("{}-main", &self.service),
image: full_image.clone(),
container_type: String::new(),
shared_volume_path: String::new(),
ports: Vec::new(),
environment: IndexMap::new(),
secrets: IndexMap::new(),
command: Vec::new(),
volume_mounts: vec![],
..Default::default()
});
}
}
Expand Down Expand Up @@ -744,7 +733,7 @@ pub fn read_manifest(path: &str) -> Result<ComposeFile> {
for old_service in hybrid.services {
let mut container = old_service.clone();
container.name = "main".to_string();
container.ports[0].publish = Some(true);
container.ports[0].publish = true;
container.container_type = "main".to_string();

let new_service = ComposeService {
Expand Down
36 changes: 27 additions & 9 deletions src/manifest_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use anyhow::Result;
use indexmap::IndexMap;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use tempfile::tempdir;

use crate::api::types::{
Expand Down Expand Up @@ -48,9 +46,11 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![Port {
target: 80,
publish: Some(true),
publish: true,
}],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
},
Container {
name: "api".to_string(),
Expand All @@ -66,9 +66,11 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![Port {
target: 3000,
publish: Some(true),
publish: true,
}],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
},
],
};
Expand All @@ -90,9 +92,11 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![Port {
target: 80,
publish: Some(true),
publish: true,
}],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
}]),
},
ComposeService {
Expand All @@ -113,9 +117,11 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![Port {
target: 3000,
publish: Some(true),
publish: true,
}],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
},
Container {
name: "redis".to_string(),
Expand All @@ -127,9 +133,11 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![Port {
target: 6379,
publish: None,
publish: false,
}],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
},
]),
},
Expand Down Expand Up @@ -211,9 +219,11 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![Port {
target: 8080,
publish: Some(true),
publish: true,
}],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
}],
};

Expand All @@ -238,9 +248,11 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![Port {
target: 8080,
publish: Some(true),
publish: true,
}],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
},
Container {
// Added sidecar container
Expand All @@ -253,6 +265,8 @@ mod tests {
secrets: IndexMap::new(),
ports: vec![],
volume_mounts: vec![],
cpu: 1,
memory: 1024,
},
]),
}],
Expand Down Expand Up @@ -368,6 +382,8 @@ mod tests {
environment: IndexMap::new(),
secrets: IndexMap::new(),
ports: vec![],
cpu: 1,
memory: 1024,
volume_mounts: vec![
VolumeMount {
volume_name: "app_data".to_string(),
Expand All @@ -388,6 +404,8 @@ mod tests {
environment: IndexMap::new(),
secrets: IndexMap::new(),
ports: vec![],
cpu: 1,
memory: 1024,
volume_mounts: vec![
VolumeMount {
volume_name: "shared_logs".to_string(),
Expand Down