Skip to content

Commit

Permalink
Calculate dedicated instance price and add tag tenancy and nested_vir…
Browse files Browse the repository at this point in the history
…tualization
  • Loading branch information
vincentBaer authored and Vincent Baer committed Feb 12, 2024
1 parent af53cfe commit a33f9f5
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "osc-cost"
version = "0.4.2"
version = "0.5.0"
edition = "2021"
authors = ["OpenSource Team <[email protected]>"]
license = "BSD-3-Clause"
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Read license for more details.
- Volumes
- Public Ips
- Snapshots (🚨 Warning: snapshot computation is currently known to be over-priced.)
- Dedicated Vm (🚨 Warning: dedicated vm computation can be overpriced depending on the situation (Look at example of Pricing for Dedicated VMs [Getting the Price of Your Resources](https://docs.outscale.com/en/userguide/Getting-the-Price-of-Your-Resources.html#_calculating_the_price_of_dedicated_vms)) .)
- Load Balancers
- Flexible GPU
- VPN Connections
Expand Down
2 changes: 1 addition & 1 deletion helm/osccost/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ version: 0.4.0
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.4.2"
appVersion: "0.5.0"
4 changes: 2 additions & 2 deletions helm/osccost/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# osccost

![Version: 0.4.0](https://img.shields.io/badge/Version-0.4.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.4.2](https://img.shields.io/badge/AppVersion-0.4.2-informational?style=flat-square)
![Version: 0.4.0](https://img.shields.io/badge/Version-0.4.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.5.0](https://img.shields.io/badge/AppVersion-0.5.0-informational?style=flat-square)

Exporter prometheus to allow Outscale users to estimate their cloud costs.

Expand All @@ -9,7 +9,7 @@ Exporter prometheus to allow Outscale users to estimate their cloud costs.
| Key | Type | Default | Description |
|-----|------|---------|-------------|
| osccost.deployment.containers.image | string | `"outscale/osc-cost"` | Outscale provider image |
| osccost.deployment.containers.imageTag | string | `"v0.4.2"` | Outscale provider image tag |
| osccost.deployment.containers.imageTag | string | `"v0.5.0"` | Outscale provider image tag |
| osccost.deployment.containers.osccostExtraParams | string | `""` | |
| osccost.deployment.containers.pullPolicy | string | `"Always"` | ImagePullPolcy to use (IfNotPresent, Never, Always) |
| osccost.deployment.containers.resources.cpu.limits | string | `"600m"` | Container cpu limts |
Expand Down
2 changes: 1 addition & 1 deletion helm/osccost/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ osccost:
# -- Outscale provider image
image: outscale/osc-cost
# -- Outscale provider image tag
imageTag: "v0.4.2"
imageTag: "v0.5.0"
# -- ImagePullPolcy to use (IfNotPresent, Never, Always)
pullPolicy: Always
# -- Additional securityContext to add
Expand Down
17 changes: 17 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::error;
use std::fmt;
use strum_macros::EnumString;

use self::dedicated_instances::DedicatedInstance;
use self::flexible_gpus::FlexibleGpu;
use self::load_balancers::LoadBalancer;
use self::nat_services::NatServices;
Expand All @@ -17,6 +18,7 @@ use self::vpn::Vpn;

static HOURS_PER_MONTH: f32 = (365_f32 * 24_f32) / 12_f32;

pub mod dedicated_instances;
pub mod digest;
pub mod flexible_gpus;
pub mod load_balancers;
Expand All @@ -41,6 +43,7 @@ pub enum Resource {
LoadBalancer(LoadBalancer),
Vpn(Vpn),
Oos(Oos),
DedicatedInstance(DedicatedInstance),
}

pub struct Resources {
Expand All @@ -61,6 +64,7 @@ impl Resources {
Resource::LoadBalancer(load_balancer) => load_balancer.compute()?,
Resource::Vpn(vpn) => vpn.compute()?,
Resource::Oos(oos) => oos.compute()?,
Resource::DedicatedInstance(dedicated_instance) => dedicated_instance.compute()?,
}
}
Ok(())
Expand Down Expand Up @@ -133,6 +137,9 @@ impl Resources {
Resource::Oos(oos) => {
total += oos.price_per_hour()?;
}
Resource::DedicatedInstance(dedicated_instance) => {
total += dedicated_instance.price_per_hour()?;
}
}
}
Ok(total)
Expand Down Expand Up @@ -245,6 +252,16 @@ impl From<Resource> for Aggregate {
aggregated_resource_type: "NatServices".to_string(),
count: 1,
},
Resource::DedicatedInstance(dedicated_instance) => Aggregate {
osc_cost_version: dedicated_instance.osc_cost_version,
account_id: dedicated_instance.account_id,
read_date_rfc3339: dedicated_instance.read_date_rfc3339,
region: dedicated_instance.region,
price_per_hour: dedicated_instance.price_per_hour,
price_per_month: dedicated_instance.price_per_month,
aggregated_resource_type: "DedicatedInstance".to_string(),
count: 1,
},
Resource::Aggregate(aggregate) => aggregate,
Resource::FlexibleGpu(flexible_gpu) => Aggregate {
osc_cost_version: flexible_gpu.osc_cost_version,
Expand Down
41 changes: 41 additions & 0 deletions src/core/dedicated_instances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use serde::{Deserialize, Serialize};

use super::{ResourceError, ResourceTrait, HOURS_PER_MONTH};
use crate::VERSION;

#[derive(Serialize, Deserialize, Debug)]
pub struct DedicatedInstance {
pub osc_cost_version: Option<String>,
pub account_id: Option<String>,
pub read_date_rfc3339: Option<String>,
pub region: Option<String>,
pub price_per_hour: Option<f32>,
pub price_per_month: Option<f32>,
}

impl ResourceTrait for DedicatedInstance {
fn compute(&mut self) -> Result<(), ResourceError> {
self.price_per_month = Some(self.price_per_hour.unwrap_or_default() * HOURS_PER_MONTH);
Ok(())
}

fn price_per_hour(&self) -> Result<f32, ResourceError> {
match self.price_per_hour {
Some(price) => Ok(price),
None => Err(ResourceError::NotComputed),
}
}
}

impl Default for DedicatedInstance {
fn default() -> Self {
Self {
osc_cost_version: Some(String::from(VERSION)),
account_id: Some("".to_string()),
read_date_rfc3339: Some("".to_string()),
region: Some("".to_string()),
price_per_hour: Some(0.0),
price_per_month: Some(0.0),
}
}
}
13 changes: 10 additions & 3 deletions src/core/vms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ pub struct Vm {
pub price_box_per_hour: f32,
// Mandatory to compute price for all vm types
pub price_license_per_ram_gb_per_hour: f32,
pub nested_virtualization: Option<bool>,
// Mandatory to compute price for dedicated instance
pub factor_vm_additional_cost: f32,
pub nested_virtualization: bool,
pub tenancy: String,
pub price_license_per_cpu_per_hour: f32,
pub price_license_per_vm_per_hour: f32,
pub license_codes: String,
Expand All @@ -41,12 +44,14 @@ impl ResourceTrait for Vm {
price_per_hour += (self.vm_ram_gb as f32) * self.price_license_per_ram_gb_per_hour;
price_per_hour += self.price_license_per_vm_per_hour;
price_per_hour += self.price_box_per_hour;
self.price_per_hour = Some(price_per_hour);
self.price_per_hour = Some(price_per_hour * self.factor_vm_additional_cost);
self.price_per_month = Some(price_per_hour * HOURS_PER_MONTH);
Ok(())
}

fn price_per_hour(&self) -> Result<f32, ResourceError> {
// formula to calculate dedicated instance (https://docs.outscale.com/en/userguide/Getting-the-Price-of-Your-Resources.html)

match self.price_per_hour {
Some(price) => Ok(price),
None => Err(ResourceError::NotComputed),
Expand All @@ -70,13 +75,15 @@ impl Default for Vm {
vm_image: None,
vm_vcpu: usize::MIN,
vm_ram_gb: usize::MIN,
nested_virtualization: Some(false),
nested_virtualization: false,
tenancy: "default".to_string(),
price_vcpu_per_hour: 0.0,
price_ram_gb_per_hour: 0.0,
price_box_per_hour: 0.0,
price_license_per_ram_gb_per_hour: 0.0,
price_license_per_cpu_per_hour: 0.0,
price_license_per_vm_per_hour: 0.0,
factor_vm_additional_cost: 1.0,
license_codes: "".to_string(),
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/oapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type ImageId = String;
type CatalogId = String;
type VmTypeName = String;

mod dedicated_instances;
mod digest;
mod flexible_gpus;
mod load_balancers;
Expand All @@ -78,6 +79,7 @@ pub struct Input {
pub vms_images: HashMap<ImageId, Image>,
pub catalog: HashMap<CatalogId, CatalogEntry>,
pub need_vm_types_fetch: bool,
pub use_dedicated_instance: bool,
pub vm_types: HashMap<VmTypeName, VmType>,
pub account: Option<Account>,
pub region: Option<String>,
Expand Down Expand Up @@ -117,6 +119,7 @@ impl Input {
public_ips: HashMap::new(),
filters: None,
flexible_gpus: HashMap::new(),
use_dedicated_instance: false,
load_balancers: Vec::new(),
vpns: Vec::new(),
buckets: HashMap::new(),
Expand Down Expand Up @@ -197,6 +200,7 @@ impl Input {
if self.need_vm_types_fetch {
self.fetch_vm_types()?;
}
self.fetch_dedicated_instances()?;
self.fetch_account()?;
self.fetch_region()?;
self.fetch_volumes()?;
Expand Down Expand Up @@ -381,7 +385,7 @@ impl Input {
}

impl From<Input> for Resources {
fn from(input: Input) -> Self {
fn from(mut input: Input) -> Self {
let mut resources = Resources {
resources: Vec::new(),
};
Expand All @@ -394,6 +398,7 @@ impl From<Input> for Resources {
input.fill_resource_load_balancers(&mut resources);
input.fill_resource_vpns(&mut resources);
input.fill_resource_oos(&mut resources);
input.fill_resource_dedicated_instances(&mut resources);
resources
}
}
42 changes: 42 additions & 0 deletions src/oapi/dedicated_instances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::error;

use log::{info, warn};

use crate::{
core::{dedicated_instances::DedicatedInstance, Resource, Resources},
VERSION,
};

use super::Input;

impl Input {
pub fn fetch_dedicated_instances(&self) -> Result<(), Box<dyn error::Error>> {
if self.use_dedicated_instance {
info!("Use dedicated instance")
}
Ok(())
}
pub fn fill_resource_dedicated_instances(&self, resources: &mut Resources) {
if self.use_dedicated_instance {
let Some(price_per_hour) =
self.catalog_entry("TinaOS-FCU", "UseDedicated", "RunDedicatedInstances")
else {
warn!("warning: could not retrieve catalog for dedicated instance");
return;
};
let core_resource = DedicatedInstance {
osc_cost_version: Some(String::from(VERSION)),
account_id: self.account_id(),
read_date_rfc3339: self.fetch_date.map(|date| date.to_rfc3339()),
region: self.region.clone(),
price_per_hour: Some(price_per_hour),
price_per_month: None,
};
resources
.resources
.push(Resource::DedicatedInstance(core_resource));
} else {
info!("Use default instance")
}
}
}
Loading

0 comments on commit a33f9f5

Please sign in to comment.