Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate enums for Gateway condition types and reasons #37

Merged
merged 1 commit into from
May 14, 2024
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
75 changes: 75 additions & 0 deletions gateway-api/src/apis/experimental/constants.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions gateway-api/src/apis/experimental/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// WARNING! generated file do not edit
pub mod constants;
mod enum_defaults;
pub mod gatewayclasses;
pub mod gateways;
Expand Down
75 changes: 75 additions & 0 deletions gateway-api/src/apis/standard/constants.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions gateway-api/src/apis/standard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// WARNING! generated file do not edit
pub mod constants;
mod enum_defaults;
pub mod gatewayclasses;
pub mod gateways;
Expand Down
28 changes: 24 additions & 4 deletions gateway-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -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)
Expand All @@ -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(())
Expand Down
15 changes: 15 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
Expand All @@ -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
56 changes: 54 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(),
}
}
Expand All @@ -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<dyn std::error::Error>;

fn gen_condition_constants() -> Result<(), DynError> {
let gateway_condition_types = env::var("GATEWAY_CONDITION_CONSTANTS")?;
shaneutt marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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<String>, module: String) -> String {
let mut stmt = String::from(format!("use super::{}::{{", module));
for item in items {
Expand Down
Loading