diff --git a/gateway-api/src/apis/experimental/constants.rs b/gateway-api/src/apis/experimental/constants.rs new file mode 100644 index 0000000..4d0008a --- /dev/null +++ b/gateway-api/src/apis/experimental/constants.rs @@ -0,0 +1,75 @@ +// WARNING: generated file - manual changes will be overriden + +#[derive(Debug)] +pub enum GatewayConditionType { + Programmed, + Accepted, + Ready, +} + +impl std::fmt::Display for GatewayConditionType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +pub enum GatewayConditionReason { + Programmed, + Invalid, + NoResources, + AddressNotAssigned, + AddressNotUsable, + Accepted, + ListenersNotValid, + Pending, + UnsupportedAddress, + InvalidParameters, + Ready, + ListenersNotReady, +} + +impl std::fmt::Display for GatewayConditionReason { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +pub enum ListenerConditionType { + Conflicted, + Accepted, + ResolvedRefs, + Programmed, + Ready, +} + +impl std::fmt::Display for ListenerConditionType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +pub enum ListenerConditionReason { + HostnameConflict, + ProtocolConflict, + NoConflicts, + Accepted, + PortUnavailable, + UnsupportedProtocol, + ResolvedRefs, + InvalidCertificateRef, + InvalidRouteKinds, + RefNotPermitted, + Programmed, + Invalid, + Pending, + Ready, +} + +impl std::fmt::Display for ListenerConditionReason { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} diff --git a/gateway-api/src/apis/experimental/mod.rs b/gateway-api/src/apis/experimental/mod.rs index 0cbda9c..2c13095 100644 --- a/gateway-api/src/apis/experimental/mod.rs +++ b/gateway-api/src/apis/experimental/mod.rs @@ -1,4 +1,5 @@ // WARNING! generated file do not edit +pub mod constants; mod enum_defaults; pub mod gatewayclasses; pub mod gateways; diff --git a/gateway-api/src/apis/standard/constants.rs b/gateway-api/src/apis/standard/constants.rs new file mode 100644 index 0000000..4d0008a --- /dev/null +++ b/gateway-api/src/apis/standard/constants.rs @@ -0,0 +1,75 @@ +// WARNING: generated file - manual changes will be overriden + +#[derive(Debug)] +pub enum GatewayConditionType { + Programmed, + Accepted, + Ready, +} + +impl std::fmt::Display for GatewayConditionType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +pub enum GatewayConditionReason { + Programmed, + Invalid, + NoResources, + AddressNotAssigned, + AddressNotUsable, + Accepted, + ListenersNotValid, + Pending, + UnsupportedAddress, + InvalidParameters, + Ready, + ListenersNotReady, +} + +impl std::fmt::Display for GatewayConditionReason { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +pub enum ListenerConditionType { + Conflicted, + Accepted, + ResolvedRefs, + Programmed, + Ready, +} + +impl std::fmt::Display for ListenerConditionType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +pub enum ListenerConditionReason { + HostnameConflict, + ProtocolConflict, + NoConflicts, + Accepted, + PortUnavailable, + UnsupportedProtocol, + ResolvedRefs, + InvalidCertificateRef, + InvalidRouteKinds, + RefNotPermitted, + Programmed, + Invalid, + Pending, + Ready, +} + +impl std::fmt::Display for ListenerConditionReason { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} diff --git a/gateway-api/src/apis/standard/mod.rs b/gateway-api/src/apis/standard/mod.rs index 1588383..be25b39 100644 --- a/gateway-api/src/apis/standard/mod.rs +++ b/gateway-api/src/apis/standard/mod.rs @@ -1,4 +1,5 @@ // WARNING! generated file do not edit +pub mod constants; mod enum_defaults; pub mod gatewayclasses; pub mod gateways; diff --git a/gateway-api/src/lib.rs b/gateway-api/src/lib.rs index 5f69846..91c67e0 100644 --- a/gateway-api/src/lib.rs +++ b/gateway-api/src/lib.rs @@ -16,10 +16,15 @@ mod tests { use tower::ServiceBuilder; use uuid::Uuid; - use crate::apis::standard::gateways::GatewayStatusAddresses; use crate::apis::standard::{ + constants::{ + GatewayConditionReason, GatewayConditionType, ListenerConditionReason, + ListenerConditionType, + }, gatewayclasses::{GatewayClass, GatewayClassSpec}, - gateways::{Gateway, GatewaySpec, GatewayStatus}, + gateways::{ + Gateway, GatewaySpec, GatewayStatus, GatewayStatusAddresses, GatewayStatusListeners, + }, }; // ------------------------------------------------------------------------- @@ -76,13 +81,26 @@ mod tests { let mut gw_status = GatewayStatus::default(); gw_status.addresses = Some(vec![GatewayStatusAddresses::default()]); + gw_status.listeners = Some(vec![GatewayStatusListeners { + name: "tcp".into(), + attached_routes: 0, + supported_kinds: vec![], + conditions: vec![Condition { + last_transition_time: Time(Utc::now()), + message: "testing gateway".to_string(), + observed_generation: Some(1), + reason: ListenerConditionReason::Programmed.to_string(), + status: "True".to_string(), + type_: ListenerConditionType::Programmed.to_string(), + }], + }]); gw_status.conditions = Some(vec![Condition { last_transition_time: Time(Utc::now()), message: "testing gateway".to_string(), observed_generation: Some(1), - reason: "GatewayTesting".to_string(), + reason: GatewayConditionReason::Programmed.to_string(), status: "True".to_string(), - type_: "IntegrationTest".to_string(), + type_: GatewayConditionType::Programmed.to_string(), }]); gw = Api::default_namespaced(client) @@ -94,8 +112,10 @@ mod tests { })), ) .await?; + assert!(gw.status.is_some()); assert!(gw.status.clone().unwrap().addresses.is_some()); + assert!(gw.status.clone().unwrap().listeners.is_some()); assert!(gw.status.clone().unwrap().conditions.is_some()); Ok(()) diff --git a/update.sh b/update.sh index 7b86629..df397e6 100755 --- a/update.sh +++ b/update.sh @@ -71,6 +71,16 @@ ENUMS_WITH_DEFAULTS=${ENUMS_WITH_DEFAULTS:1} GATEWAY_API_ENUMS=${ENUMS_WITH_DEFAULTS} cargo xtask gen_enum_defaults >> gateway-api/src/apis/standard/enum_defaults.rs echo "mod enum_defaults;" >> gateway-api/src/apis/standard/mod.rs +GATEWAY_CONDITION_CONSTANTS="GatewayConditionType=Programmed,Accepted,Ready" +GATEWAY_REASON_CONSTANTS="GatewayConditionReason=Programmed,Invalid,NoResources,AddressNotAssigned,AddressNotUsable,Accepted,ListenersNotValid,Pending,UnsupportedAddress,InvalidParameters,Ready,ListenersNotReady" +LISTENER_CONDITION_CONSTANTS="ListenerConditionType=Conflicted,Accepted,ResolvedRefs,Programmed,Ready" +LISTENER_REASON_CONSTANTS="ListenerConditionReason=HostnameConflict,ProtocolConflict,NoConflicts,Accepted,PortUnavailable,UnsupportedProtocol,ResolvedRefs,InvalidCertificateRef,InvalidRouteKinds,RefNotPermitted,Programmed,Invalid,Pending,Ready" + +GATEWAY_CONDITION_CONSTANTS=${GATEWAY_CONDITION_CONSTANTS} GATEWAY_REASON_CONSTANTS=${GATEWAY_REASON_CONSTANTS} \ + LISTENER_CONDITION_CONSTANTS=${LISTENER_CONDITION_CONSTANTS} LISTENER_REASON_CONSTANTS=${LISTENER_REASON_CONSTANTS} \ + cargo xtask gen_condition_constants >> gateway-api/src/apis/standard/constants.rs +echo "pub mod constants;" >> gateway-api/src/apis/standard/mod.rs + echo "// WARNING! generated file do not edit" > gateway-api/src/apis/experimental/mod.rs for API in "${EXPERIMENTAL_APIS[@]}" @@ -97,5 +107,10 @@ ENUMS_WITH_DEFAULTS=${ENUMS_WITH_DEFAULTS:1} GATEWAY_API_ENUMS=${ENUMS_WITH_DEFAULTS} cargo xtask gen_enum_defaults >> gateway-api/src/apis/experimental/enum_defaults.rs echo "mod enum_defaults;" >> gateway-api/src/apis/experimental/mod.rs +GATEWAY_CONDITION_CONSTANTS=${GATEWAY_CONDITION_CONSTANTS} GATEWAY_REASON_CONSTANTS=${GATEWAY_REASON_CONSTANTS} \ + LISTENER_CONDITION_CONSTANTS=${LISTENER_CONDITION_CONSTANTS} LISTENER_REASON_CONSTANTS=${LISTENER_REASON_CONSTANTS} \ + cargo xtask gen_condition_constants >> gateway-api/src/apis/experimental/constants.rs +echo "pub mod constants;" >> gateway-api/src/apis/experimental/mod.rs + # Format the code. cargo fmt diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 843fb1c..1a04592 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,12 +1,13 @@ use std::{collections::BTreeMap, env}; -use codegen::{Function, Scope}; +use codegen::{Enum, Function, Scope, Variant}; fn main() { let task = env::args().nth(1); match task.as_deref() { Some("gen_enum_defaults") => gen_enum_defaults().unwrap(), + Some("gen_condition_constants") => gen_condition_constants().unwrap(), _ => print_help(), } } @@ -16,12 +17,59 @@ fn print_help() { "Tasks: gen_enum_defaults generates Default trait impls for enums +gen_constants generates constants used for Conditions " ) } type DynError = Box; +fn gen_condition_constants() -> Result<(), DynError> { + let gateway_condition_types = env::var("GATEWAY_CONDITION_CONSTANTS")?; + let gateway_reason_types = env::var("GATEWAY_REASON_CONSTANTS")?; + let listener_condition_types = env::var("LISTENER_CONDITION_CONSTANTS")?; + let listener_reason_types = env::var("LISTENER_REASON_CONSTANTS")?; + + let mut scope = Scope::new(); + gen_const_enums(&mut scope, gateway_condition_types); + gen_const_enums(&mut scope, gateway_reason_types); + gen_const_enums(&mut scope, listener_condition_types); + gen_const_enums(&mut scope, listener_reason_types); + println!("{}", gen_generated_file_warning()); + println!("{}", scope.to_string()); + Ok(()) +} + +fn gen_const_enums(scope: &mut Scope, constants: String) { + let enum_type_and_variants: Vec<&str> = constants.split('=').collect(); + let enum_type = enum_type_and_variants[0]; + let variants: Vec<&str> = enum_type_and_variants[1].split(',').collect(); + + let mut enumeration = Enum::new(enum_type); + enumeration.derive("Debug"); + enumeration.vis("pub"); + + for variant in variants { + let var = Variant::new(variant); + enumeration.push_variant(var); + } + scope.push_enum(enumeration); + + gen_display_impl(scope, enum_type); +} + +fn gen_display_impl(scope: &mut Scope, ty: &str) { + let mut func = Function::new("fmt".to_string()); + func.arg_ref_self(); + func.arg("f", "&mut std::fmt::Formatter"); + func.ret("std::fmt::Result"); + func.line("write!(f, \"{:?}\", self)"); + scope + .new_impl(ty) + .impl_trait("std::fmt::Display") + .push_fn(func); +} + fn gen_enum_defaults() -> Result<(), DynError> { // GATEWAY_API_ENUMS provides the enum names along with their default variant to be used in the // generated Default impl. For eg: GATEWAY_API_ENUMS=enum1=default1,enum2=default2. @@ -52,7 +100,7 @@ fn gen_enum_defaults() -> Result<(), DynError> { } } - println!("// WARNING: generated file - manual changes will be overriden\n"); + println!("{}", gen_generated_file_warning()); // Generate use statements for the enums. if httproute_enums.len() > 0 { @@ -68,6 +116,10 @@ fn gen_enum_defaults() -> Result<(), DynError> { Ok(()) } +fn gen_generated_file_warning() -> String { + "// WARNING: generated file - manual changes will be overriden\n".into() +} + fn gen_use_stmt(items: Vec, module: String) -> String { let mut stmt = String::from(format!("use super::{}::{{", module)); for item in items {