Skip to content

Commit 341bee2

Browse files
committed
Added tcproute support.
1 parent daf2d71 commit 341bee2

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ pub struct I2GArgs {
2222
/// Whether to use experimental gateway-api resources like TCPRoutes.
2323
#[arg(long, env = "I2G_EXPERIMENTAL", default_value_t = false)]
2424
pub experimental: bool,
25+
26+
#[arg(long, env = "I2G_LOG_LEVEL", default_value_t = tracing::level_filters::LevelFilter::INFO)]
27+
pub log_level: tracing::level_filters::LevelFilter,
2528
}

src/main.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use k8s_openapi::api::{
1717
networking::v1::{Ingress, IngressServiceBackend, ServiceBackendPort},
1818
};
1919
use kube::{Api, Resource, ResourceExt, api::PatchParams, runtime::controller::Action};
20+
use tracing::Instrument;
2021

2122
use crate::{
2223
err::{I2GError, I2GResult},
@@ -62,6 +63,7 @@ async fn get_svc_port_number(
6263

6364
async fn create_http_route(
6465
ctx: Arc<ctx::Context>,
66+
section_name: Option<&String>,
6567
ingress_namespace: &str,
6668
http: &k8s_openapi::api::networking::v1::HTTPIngressRuleValue,
6769
hostname: &str,
@@ -149,8 +151,8 @@ async fn create_http_route(
149151
kind: Some(gw_kind.to_string()),
150152
name: ctx.args.default_gateway_name.clone(),
151153
namespace: Some(ctx.args.default_gateway_namespace.clone()),
152-
port: Some(80),
153-
section_name: None,
154+
port: None,
155+
section_name: section_name.cloned(),
154156
}]
155157
.to_vec(),
156158
),
@@ -161,6 +163,7 @@ async fn create_http_route(
161163

162164
async fn create_tcp_route(
163165
ctx: Arc<ctx::Context>,
166+
section_name: Option<&String>,
164167
namespace: &str,
165168
svc: &IngressServiceBackend,
166169
hostname: &str,
@@ -212,8 +215,8 @@ async fn create_tcp_route(
212215
kind: Some(gw_kind.to_string()),
213216
name: ctx.args.default_gateway_name.clone(),
214217
namespace: Some(ctx.args.default_gateway_namespace.clone()),
215-
port: Some(80),
216-
section_name: None,
218+
port: None,
219+
section_name: section_name.cloned(),
217220
}]
218221
.to_vec(),
219222
),
@@ -235,6 +238,14 @@ pub async fn reconcile(ingress: Arc<Ingress>, ctx: Arc<ctx::Context>) -> I2GResu
235238
let ingress_namespace = ingress
236239
.namespace()
237240
.ok_or_else(|| anyhow::anyhow!("Ingress doesn't have a namespace"))?;
241+
242+
let desired_section_name = ingress
243+
.meta()
244+
.annotations
245+
.as_ref()
246+
.and_then(|ann| ann.get("i2g-operator/sectionName"))
247+
.cloned();
248+
238249
let default_backend = ingress_spec.default_backend.as_ref();
239250

240251
for rule in ingress_rules {
@@ -244,8 +255,14 @@ pub async fn reconcile(ingress: Arc<Ingress>, ctx: Arc<ctx::Context>) -> I2GResu
244255
};
245256

246257
if let Some(http) = &rule.http {
247-
let Ok(mut route) =
248-
create_http_route(ctx.clone(), &ingress_namespace, &http, &host).await
258+
let Ok(mut route) = create_http_route(
259+
ctx.clone(),
260+
desired_section_name.as_ref(),
261+
&ingress_namespace,
262+
&http,
263+
&host,
264+
)
265+
.await
249266
else {
250267
tracing::warn!("Failed to create HTTPRoute for host {}", host);
251268
continue;
@@ -263,12 +280,14 @@ pub async fn reconcile(ingress: Arc<Ingress>, ctx: Arc<ctx::Context>) -> I2GResu
263280
},
264281
&kube::api::Patch::Apply(route),
265282
)
283+
.instrument(tracing::info_span!("Applying generated HTTPRoute"))
266284
.await?;
267285
} else {
268286
if !ctx.args.experimental {
269287
tracing::warn!(
270288
"Skipping rule non-http rule. In order to migrate it to TCPRoute, please add --experimental flag to i2g-operator."
271289
);
290+
continue;
272291
}
273292
// In case if rule.http is None
274293
let Some(backend) = default_backend else {
@@ -280,8 +299,14 @@ pub async fn reconcile(ingress: Arc<Ingress>, ctx: Arc<ctx::Context>) -> I2GResu
280299
continue;
281300
};
282301

283-
let Ok(mut route) =
284-
create_tcp_route(ctx.clone(), &ingress_namespace, backend_svc, &host).await
302+
let Ok(mut route) = create_tcp_route(
303+
ctx.clone(),
304+
desired_section_name.as_ref(),
305+
&ingress_namespace,
306+
backend_svc,
307+
&host,
308+
)
309+
.await
285310
else {
286311
tracing::warn!("Failed to create TCPRoute for host {}", host);
287312
continue;
@@ -300,6 +325,7 @@ pub async fn reconcile(ingress: Arc<Ingress>, ctx: Arc<ctx::Context>) -> I2GResu
300325
},
301326
&kube::api::Patch::Apply(route),
302327
)
328+
.instrument(tracing::info_span!("Applying generated TCPRoute"))
303329
.await?;
304330
}
305331
}
@@ -315,9 +341,13 @@ fn on_error(obj: Arc<Ingress>, _err: &I2GError, _ctx: Arc<ctx::Context>) -> Acti
315341
#[tokio::main]
316342
async fn main() -> anyhow::Result<()> {
317343
dotenvy::dotenv().ok();
318-
tracing_subscriber::fmt::init();
319344

320345
let ctx = Arc::new(ctx::Context::new().await?);
346+
tracing_subscriber::fmt()
347+
.with_max_level(ctx.args.log_level)
348+
.init();
349+
tracing::info!("Staring operator");
350+
tracing::info!("CLI argument: {:?}", ctx.args);
321351

322352
kube::runtime::Controller::new(
323353
Api::<Ingress>::all(ctx.client.clone()),

0 commit comments

Comments
 (0)