feat: add ingress score #32
| @ -10,6 +10,23 @@ use crate::{ | |||||||
| 
 | 
 | ||||||
| use super::resource::{K8sResourceInterpret, K8sResourceScore}; | use super::resource::{K8sResourceInterpret, K8sResourceScore}; | ||||||
| 
 | 
 | ||||||
|  | #[derive(Debug, Clone, Serialize)] | ||||||
|  | pub enum PathType { | ||||||
|  | |||||||
|  |     ImplementationSpecific, | ||||||
|  |     Exact, | ||||||
|  |     Prefix, | ||||||
|  | } | ||||||
| 
				
					
						johnride
						commented  should be integer https://docs.redhat.com/en/documentation/openshift_container_platform/4.15/html/network_apis/ingress-networking-k8s-io-v1#spec-rules-http-paths-backend-service-port | |||||||
|  | 
 | ||||||
|  | impl PathType { | ||||||
|  |     fn as_str(&self) -> &'static str { | ||||||
| 
				
					
						johnride
						commented  What about path, path_type and namespace? These certainly don't support any String? What about path, path_type and namespace? These certainly don't support any String? 
				
					
						taha
						commented  Seems like validating the  Fixed NS and PathType though Seems like validating the `path` is not as straightforward as the rest, as it has different validations based on the PathType and other things: https://github.com/kubernetes/ingress-nginx/issues/11176
Fixed NS and PathType though 
				
					
						johnride
						commented  https://kubernetes.io/docs/reference/kubernetes-api/service-resources/ingress-v1/#IngressSpec | |||||||
|  |         match self { | ||||||
|  |             PathType::ImplementationSpecific => "ImplementationSpecific", | ||||||
|  |             PathType::Exact => "Exact", | ||||||
|  |             PathType::Prefix => "Prefix", | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[derive(Debug, Clone, Serialize)] | #[derive(Debug, Clone, Serialize)] | ||||||
| pub struct K8sIngressScore { | pub struct K8sIngressScore { | ||||||
|     pub name: fqdn::FQDN, |     pub name: fqdn::FQDN, | ||||||
| @ -17,8 +34,8 @@ pub struct K8sIngressScore { | |||||||
|     pub backend_service: fqdn::FQDN, |     pub backend_service: fqdn::FQDN, | ||||||
|     pub port: u16, |     pub port: u16, | ||||||
|     pub path: Option<String>, |     pub path: Option<String>, | ||||||
|     pub path_type: Option<String>, |     pub path_type: Option<PathType>, | ||||||
|     pub namespace: Option<String>, |     pub namespace: Option<fqdn::FQDN>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl<T: Topology + K8sclient> Score<T> for K8sIngressScore { | impl<T: Topology + K8sclient> Score<T> for K8sIngressScore { | ||||||
| @ -29,7 +46,7 @@ impl<T: Topology + K8sclient> Score<T> for K8sIngressScore { | |||||||
|         }; |         }; | ||||||
|         let path_type = match self.path_type.clone() { |         let path_type = match self.path_type.clone() { | ||||||
|             Some(p) => p, |             Some(p) => p, | ||||||
|             None => "Prefix".to_string(), |             None => PathType::Prefix, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         let ingress = json!( |         let ingress = json!( | ||||||
| @ -44,7 +61,7 @@ impl<T: Topology + K8sclient> Score<T> for K8sIngressScore { | |||||||
|                                 "paths": [ |                                 "paths": [ | ||||||
|                                     { |                                     { | ||||||
|                                         "path": path, |                                         "path": path, | ||||||
|                                         "pathType": path_type, |                                         "pathType": path_type.as_str(), | ||||||
|                                         "backend": [ |                                         "backend": [ | ||||||
|                                             { |                                             { | ||||||
|                                                 "service": self.backend_service, |                                                 "service": self.backend_service, | ||||||
| @ -62,7 +79,12 @@ impl<T: Topology + K8sclient> Score<T> for K8sIngressScore { | |||||||
| 
 | 
 | ||||||
|         let ingress: Ingress = serde_json::from_value(ingress).unwrap(); |         let ingress: Ingress = serde_json::from_value(ingress).unwrap(); | ||||||
|         Box::new(K8sResourceInterpret { |         Box::new(K8sResourceInterpret { | ||||||
|             score: K8sResourceScore::single(ingress.clone(), self.namespace.clone()), |             score: K8sResourceScore::single( | ||||||
|  |                 ingress.clone(), | ||||||
|  |                 self.namespace | ||||||
|  |                     .clone() | ||||||
|  |                     .map(|f| f.as_c_str().to_str().unwrap().to_string()), | ||||||
|  |             ), | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -145,7 +145,9 @@ impl<T: Topology + K8sclient + HelmCommand> Interpret<T> for LAMPInterpret { | |||||||
|             port: 8080, |             port: 8080, | ||||||
|             path: None, |             path: None, | ||||||
|             path_type: None, |             path_type: None, | ||||||
|             namespace: self.get_namespace().map(|nbs| nbs.to_string()), |             namespace: self | ||||||
|  |                 .get_namespace() | ||||||
|  |                 .map(|nbs| fqdn!(nbs.to_string().as_str())), | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         lamp_ingress |         lamp_ingress | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	
I think it's now worth the time to use correct types for each k8s field types.
Name should not be a string as it doesn't support all utf-8 strings, host should probably be a URL, etc. Look at the k8s spec and implement the right types.
This will allow us to create macros eventually for a compile-safe DX/UX.