Skip to content

Commit

Permalink
Update matcher (#431)
Browse files Browse the repository at this point in the history
Co-authored-by: Ian Stanton <ian@coredb.io>
Co-authored-by: Adam Hendel <ChuckHend@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 19, 2023
1 parent 75d7a42 commit 32c370f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
8 changes: 4 additions & 4 deletions tembo-operator/src/app_service/ingress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub fn generate_ingress_tcp_routes(
appsvc: &AppService,
resource_name: &str,
namespace: &str,
host_matcher: String,
host_matcher_tcp: String,
coredb_name: &str,
) -> Option<Vec<IngressRouteTCPRoutes>> {
match appsvc.routing.clone() {
Expand All @@ -269,7 +269,6 @@ pub fn generate_ingress_tcp_routes(
continue;
}

let matcher = format!("{host_matcher} && PathPrefix(`{}`)", path);
let middlewares: Option<Vec<IngressRouteTCPRoutesMiddlewares>> =
route.middlewares.clone().map(|names| {
names
Expand All @@ -281,7 +280,7 @@ pub fn generate_ingress_tcp_routes(
.collect()
});
let route = IngressRouteTCPRoutes {
r#match: matcher.clone(),
r#match: host_matcher_tcp.clone(),
services: Some(vec![IngressRouteTCPRoutesServices {
name: resource_name.to_string(),
port: IntOrString::Int(route.port as i32),
Expand Down Expand Up @@ -398,9 +397,10 @@ pub async fn reconcile_ingress_tcp(
desired_routes: Vec<IngressRouteTCPRoutes>,
desired_middlewares: Vec<Middleware>,
entry_points_tcp: Vec<String>,
app_name: &str,
) -> Result<(), kube::Error> {
let ingress_api: Api<IngressRouteTCP> = Api::namespaced(client.clone(), ns);
let name = format!("{}-apps", coredb_name);
let name = format!("{}-{}", coredb_name, app_name);

let middleware_api: Api<TraefikMiddleware> = Api::namespaced(client.clone(), ns);
let desired_middlewares = generate_middlewares(&name, ns, oref.clone(), desired_middlewares);
Expand Down
58 changes: 36 additions & 22 deletions tembo-operator/src/app_service/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ fn generate_resource(
host_matcher.clone(),
coredb_name,
);

let host_matcher_tcp = format!(
"HostSNI(`{subdomain}.{domain}`)",
subdomain = coredb_name,
domain = domain
);

let ingress_tcp_routes =
generate_ingress_tcp_routes(appsvc, &resource_name, namespace, host_matcher, coredb_name);
generate_ingress_tcp_routes(appsvc, &resource_name, namespace, host_matcher_tcp, coredb_name);
// fetch entry points where ingress type is http
let entry_points: Option<Vec<String>> = appsvc.routing.as_ref().map(|routes| {
routes
Expand Down Expand Up @@ -646,6 +653,9 @@ pub async fn reconcile_app_services(cdb: &CoreDB, ctx: Arc<Context>) -> Result<(
"localhost".to_string()
}
};

// Iterate over each AppService and process routes

let resources: Vec<AppServiceResources> = appsvcs
.iter()
.map(|appsvc| generate_resource(appsvc, &coredb_name, &ns, oref.clone(), domain.to_owned()))
Expand Down Expand Up @@ -705,29 +715,33 @@ pub async fn reconcile_app_services(cdb: &CoreDB, ctx: Arc<Context>) -> Result<(
}
}

match reconcile_ingress_tcp(
client.clone(),
&coredb_name,
&ns,
oref.clone(),
desired_tcp_routes,
desired_middlewares,
desired_entry_points_tcp,
)
.await
{
Ok(_) => {
debug!("Updated/applied IngressRouteTCP for {}.{}", ns, coredb_name,);
}
Err(e) => {
error!(
"Failed to update/apply IngressRouteTCP {}.{}: {}",
ns, coredb_name, e
);
has_errors = true;
for appsvc in appsvcs.iter() {
let app_name = appsvc.name.clone();

match reconcile_ingress_tcp(
client.clone(),
&coredb_name,
&ns,
oref.clone(),
desired_tcp_routes.clone(),
desired_middlewares.clone(),
desired_entry_points_tcp.clone(),
&app_name,
)
.await
{
Ok(_) => {
debug!("Updated/applied IngressRouteTCP for {}.{}", ns, coredb_name,);
}
Err(e) => {
error!(
"Failed to update/apply IngressRouteTCP {}.{}: {}",
ns, coredb_name, e
);
has_errors = true;
}
}
}

if has_errors || apply_errored {
return Err(Action::requeue(Duration::from_secs(300)));
}
Expand Down
7 changes: 2 additions & 5 deletions tembo-operator/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3091,7 +3091,7 @@ mod test {
);

// Check for IngressRouteTCP
let ing_name = format!("{}-apps", cdb_name);
let ing_name = format!("{cdb_name}-ferretdb");
let ingresses_tcp: Result<Vec<IngressRouteTCP>, errors::OperatorError> =
list_resources(client.clone(), &ing_name, &namespace, 1).await;
let ingress_tcp = ingresses_tcp.unwrap();
Expand All @@ -3100,10 +3100,7 @@ mod test {
let routes_tcp = ingress_route_tcp.spec.clone().routes.clone();
assert_eq!(routes.len(), 1);
let route_tcp = routes_tcp[0].clone();
assert_eq!(
route_tcp.r#match,
format!("Host(`{}.localhost`) && PathPrefix(`/ferretdb/v1`)", cdb_name)
);
assert_eq!(route_tcp.r#match, format!("HostSNI(`{}.localhost`)", cdb_name));

// Assert entry_points includes only ferretdb
assert_eq!(
Expand Down

0 comments on commit 32c370f

Please sign in to comment.