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

Commit

Permalink
Merge pull request #46 from fibonacci1729/master
Browse files Browse the repository at this point in the history
rust fmt
  • Loading branch information
fibonacci1729 committed Jul 22, 2019
2 parents 62dda51 + d4fd598 commit 498e389
Show file tree
Hide file tree
Showing 24 changed files with 205 additions and 133 deletions.
87 changes: 61 additions & 26 deletions src/instigator.rs
@@ -1,18 +1,20 @@
use kube::{api::Reflector, api::Resource, client::APIClient};
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as meta;
use kube::{api::Reflector, api::Resource, client::APIClient};
use std::collections::BTreeMap;

use crate::{
lifecycle::Phase,
schematic::{
component::Component,
configuration::OperationalConfiguration,
configuration::ComponentConfiguration,
configuration::OperationalConfiguration,
parameter::{resolve_parameters, resolve_values, ParameterValue},
traits::{Autoscaler, HydraTrait, Ingress, ManualScaler, TraitBinding, Empty},
traits::{Autoscaler, Empty, HydraTrait, Ingress, ManualScaler, TraitBinding},
Status,
},
workload_type::{CoreWorkloadType, ReplicatedService, Singleton, Task, ReplicatedTask, HYDRA_API_VERSION},
workload_type::{
CoreWorkloadType, ReplicatedService, ReplicatedTask, Singleton, Task, HYDRA_API_VERSION,
},
};

/// Type alias for the results that all instantiation operations return
Expand Down Expand Up @@ -102,9 +104,15 @@ impl Instigator {

// Instantiate components
let inst_name = component.instance_name.clone();
let workload = self.load_workload_type(name.clone(), inst_name.clone(), comp_def, &params, owner_ref.clone())?;
let workload = self.load_workload_type(
name.clone(),
inst_name.clone(),
comp_def,
&params,
owner_ref.clone(),
)?;
// Load all of the traits related to this component.
let mut trait_manager = TraitManager{
let mut trait_manager = TraitManager {
config_name: name.clone(),
instance_name: inst_name.clone(),
component: component.clone(),
Expand All @@ -118,24 +126,50 @@ impl Instigator {
match phase {
Phase::Add => {
info!("Adding component {}", component.name.clone());
trait_manager.exec(DEFAULT_NAMESPACE.into(), self.client.clone(), Phase::PreAdd)?;
trait_manager.exec(
DEFAULT_NAMESPACE.into(),
self.client.clone(),
Phase::PreAdd,
)?;
workload.add()?;
trait_manager.exec(DEFAULT_NAMESPACE.into(), self.client.clone(), Phase::Add)?;
trait_manager.exec(
DEFAULT_NAMESPACE.into(),
self.client.clone(),
Phase::Add,
)?;
}
Phase::Modify => {
info!("Modifying component {}", component.name.clone());
trait_manager.exec(DEFAULT_NAMESPACE.into(), self.client.clone(), Phase::PreModify)?;
trait_manager.exec(
DEFAULT_NAMESPACE.into(),
self.client.clone(),
Phase::PreModify,
)?;
workload.modify()?;
trait_manager.exec(DEFAULT_NAMESPACE.into(), self.client.clone(), Phase::Modify)?;
trait_manager.exec(
DEFAULT_NAMESPACE.into(),
self.client.clone(),
Phase::Modify,
)?;
}
Phase::Delete => {
info!("Deleting component {}", component.name.clone());
trait_manager.exec(DEFAULT_NAMESPACE.into(), self.client.clone(), Phase::PreDelete)?;
trait_manager.exec(
DEFAULT_NAMESPACE.into(),
self.client.clone(),
Phase::PreDelete,
)?;
workload.delete()?;
trait_manager.exec(DEFAULT_NAMESPACE.into(), self.client.clone(), Phase::Delete)?;
trait_manager.exec(
DEFAULT_NAMESPACE.into(),
self.client.clone(),
Phase::Delete,
)?;
}
_ => {
return Err(format_err!("Illegal phase: only Add, Modify, and Delete are supported here"))
return Err(format_err!(
"Illegal phase: only Add, Modify, and Delete are supported here"
))
}
}
}
Expand Down Expand Up @@ -203,7 +237,7 @@ impl Instigator {
owner_ref: owner,
};
Ok(CoreWorkloadType::TaskType(task))
},
}
"core.hydra.io/v1alpha1.ReplicableTask" => {
let task = ReplicatedTask {
name: config_name,
Expand All @@ -217,19 +251,20 @@ impl Instigator {
replica_count: Some(1), // Every(1) needs Some(1) to love.
};
Ok(CoreWorkloadType::ReplicatedTaskType(task))
},
}
_ => Err(format_err!(
"workloadType {} is unknown",
comp.spec.workload_type
)),
}
}


}

/// Build an owner reference for the given parent UID of kind Configuration.
pub fn config_owner_reference(parent_name: String, parent_uid: Option<String>) -> Option<Vec<meta::OwnerReference>> {
pub fn config_owner_reference(
parent_name: String,
parent_uid: Option<String>,
) -> Option<Vec<meta::OwnerReference>> {
match parent_uid {
Some(uid) => {
let owner_ref = meta::OwnerReference {
Expand All @@ -241,7 +276,7 @@ pub fn config_owner_reference(parent_name: String, parent_uid: Option<String>) -
name: parent_name.clone(),
};
Some(vec![owner_ref])
},
}
None => {
info!("Mysteriously, no UID was created. Ancient version of Kubernetes?");
None
Expand Down Expand Up @@ -279,7 +314,7 @@ impl TraitManager {
fn load_trait(&self, binding: &TraitBinding) -> Result<HydraTrait, failure::Error> {
let trait_values = resolve_values(
binding.parameter_values.clone().unwrap_or(vec![]),
self.parent_params.clone()
self.parent_params.clone(),
)?;
debug!("Trait binding params: {:?}", &binding.parameter_values);
match binding.name.as_str() {
Expand All @@ -289,7 +324,7 @@ impl TraitManager {
self.instance_name.clone(),
self.component.name.clone(),
trait_values,
self.owner_ref.clone()
self.owner_ref.clone(),
);
Ok(HydraTrait::Ingress(ing))
}
Expand All @@ -299,10 +334,10 @@ impl TraitManager {
self.instance_name.clone(),
self.component.name.clone(),
trait_values,
self.owner_ref.clone()
self.owner_ref.clone(),
);
Ok(HydraTrait::Autoscaler(auto))
},
}
"manual-scaler" => {
let scaler = ManualScaler::from_params(
self.config_name.clone(),
Expand All @@ -317,7 +352,7 @@ impl TraitManager {
// Empty is a debugging tool for checking whether the traits system is functioning independently of
// its environment.
"empty" => {
let empty = Empty{};
let empty = Empty {};
Ok(HydraTrait::Empty(empty))
}
_ => Err(format_err!("unknown trait {}", binding.name)),
Expand All @@ -337,5 +372,5 @@ impl TraitManager {
}
}
Ok(())
}
}
}
}
16 changes: 9 additions & 7 deletions src/instigator_test.rs
Expand Up @@ -5,10 +5,12 @@ fn test_config_owner_reference() {
let name = "configuration".to_string();
let uid = "87707f2d-9ddc-11e9-b1c3-4ec16ac9a10f".to_string();

config_owner_reference(name.clone(), Some(uid.clone())).and_then(|v| {
let owner = v[0].clone();
assert_eq!(owner.name, name);
assert_eq!(owner.uid, uid);
Some(v)
}).expect("expected owner reference");
}
config_owner_reference(name.clone(), Some(uid.clone()))
.and_then(|v| {
let owner = v[0].clone();
assert_eq!(owner.name, name);
assert_eq!(owner.uid, uid);
Some(v)
})
.expect("expected owner reference");
}
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -15,6 +15,6 @@ pub mod schematic;
pub mod workload_type;

#[cfg(test)]
mod workload_type_test;
mod instigator_test;
#[cfg(test)]
mod instigator_test;
mod workload_type_test;
10 changes: 5 additions & 5 deletions src/lifecycle.rs
Expand Up @@ -3,15 +3,15 @@
/// The order of operations is this:
///
/// ADD
/// - Kubernetes Add
/// - Kubernetes Add
/// - Configuration
/// - PreAdd (traits only): Before components are added
/// - Component Configuration
/// - Traits
/// - Add: Resources added and initialized
/// - Components
/// - Traits
///
///
/// MODIFY
/// - Kubernetes Update
/// - Configuration
Expand All @@ -20,7 +20,7 @@
/// - Modify: Resources are modified
/// - Components
/// - Traits
///
///
/// DELETE
/// - Kubernetes Delete
/// - Configuration
Expand All @@ -29,7 +29,7 @@
/// - Delete:
/// - Components
/// - Traits
///
///
/// Note that in deletion operations, Kubernetes will delete by owner reference before PreDelete. This means
/// that the components will likely be unavailable by the time PreDelete fires. It is only guaranteed to fire
/// before the component's Delete operation is fired.
Expand All @@ -42,4 +42,4 @@ pub enum Phase {
Modify,
PreDelete,
Delete,
}
}
12 changes: 4 additions & 8 deletions src/schematic/traits.rs
@@ -1,5 +1,5 @@
use crate::schematic::parameter::ParameterValue;
use crate::lifecycle::Phase;
use crate::schematic::parameter::ParameterValue;
use kube::client::APIClient;

// Re-exports
Expand All @@ -14,14 +14,14 @@ pub use crate::schematic::traits::manual_scaler::ManualScaler;
mod util;
use crate::schematic::traits::util::*;

#[cfg(test)]
mod util_test;
#[cfg(test)]
mod autoscaler_test;
#[cfg(test)]
mod ingress_test;
#[cfg(test)]
mod manual_scaler_test;
#[cfg(test)]
mod util_test;

/// Trait describes Hydra traits.
///
Expand Down Expand Up @@ -91,7 +91,7 @@ pub trait TraitImplementation {
info!("Support {} by default", name);
true
}
fn pre_add(&self, _ns: &str, _client: APIClient) -> TraitResult {
fn pre_add(&self, _ns: &str, _client: APIClient) -> TraitResult {
Ok(())
}
fn pre_modify(&self, _ns: &str, _client: APIClient) -> TraitResult {
Expand All @@ -101,7 +101,3 @@ pub trait TraitImplementation {
Ok(())
}
}




12 changes: 9 additions & 3 deletions src/schematic/traits/autoscaler.rs
@@ -1,4 +1,4 @@
use crate::schematic::traits::{TraitImplementation, util::*};
use crate::schematic::traits::{util::*, TraitImplementation};
use crate::workload_type::{ParamMap, REPLICATED_SERVICE_NAME, REPLICATED_TASK_NAME};
use k8s_openapi::api::autoscaling::v2beta1 as hpa;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as meta;
Expand All @@ -17,7 +17,13 @@ pub struct Autoscaler {
}

impl Autoscaler {
pub fn from_params(name: String, instance_name: String, component_name: String, params: ParamMap, owner_ref: OwnerRefs) -> Self {
pub fn from_params(
name: String,
instance_name: String,
component_name: String,
params: ParamMap,
owner_ref: OwnerRefs,
) -> Self {
Autoscaler {
name: name,
component_name: component_name,
Expand Down Expand Up @@ -97,4 +103,4 @@ impl TraitImplementation for Autoscaler {
// Only support replicated service and task right now.
name == REPLICATED_SERVICE_NAME || name == REPLICATED_TASK_NAME
}
}
}
10 changes: 8 additions & 2 deletions src/schematic/traits/autoscaler_test.rs
Expand Up @@ -29,7 +29,13 @@ fn test_autoscaler() {
params.insert("minimum".into(), json!(6));
params.insert("maximum".into(), json!(7));

let autoscaler = Autoscaler::from_params("release".into(), "instance".into(), "component".into(), params, None);
let autoscaler = Autoscaler::from_params(
"release".into(),
"instance".into(),
"component".into(),
params,
None,
);
let kauto = autoscaler.to_horizontal_pod_autoscaler();
assert_eq!(
Some("instance-trait-autoscaler".to_string()),
Expand All @@ -47,4 +53,4 @@ fn test_autoscaler() {
.expect("a resource")
.target_average_utilization
);
}
}
8 changes: 5 additions & 3 deletions src/schematic/traits/empty.rs
@@ -1,4 +1,4 @@
use crate::schematic::traits::{TraitImplementation, util::*};
use crate::schematic::traits::{util::*, TraitImplementation};
use kube::client::APIClient;

pub struct Empty {}
Expand All @@ -7,5 +7,7 @@ impl TraitImplementation for Empty {
fn supports_workload_type(_name: &str) -> bool {
true
}
fn add(&self, _ns: &str, _client: APIClient) -> TraitResult { Ok(()) }
}
fn add(&self, _ns: &str, _client: APIClient) -> TraitResult {
Ok(())
}
}
10 changes: 8 additions & 2 deletions src/schematic/traits/ingress.rs
@@ -1,4 +1,4 @@
use crate::schematic::traits::{TraitImplementation, util::*};
use crate::schematic::traits::{util::*, TraitImplementation};
use crate::workload_type::ParamMap;
use k8s_openapi::api::extensions::v1beta1 as ext;
use k8s_openapi::apimachinery::pkg::{apis::meta::v1 as meta, util::intstr::IntOrString};
Expand All @@ -18,7 +18,13 @@ pub struct Ingress {
pub owner_ref: OwnerRefs,
}
impl Ingress {
pub fn from_params(name: String, instance_name: String, component_name: String, params: ParamMap, owner_ref: OwnerRefs) -> Self {
pub fn from_params(
name: String,
instance_name: String,
component_name: String,
params: ParamMap,
owner_ref: OwnerRefs,
) -> Self {
// Right now, we're relying on the higher level validation logic to validate types.
Ingress {
name: name,
Expand Down

0 comments on commit 498e389

Please sign in to comment.