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

Make GrpcRoute watches optional #12917

Merged
merged 2 commits into from
Jul 31, 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
5 changes: 4 additions & 1 deletion cli/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func configureAndRunChecks(cmd *cobra.Command, wout io.Writer, werr io.Writer, o
}

crdManifest := bytes.Buffer{}
err = renderCRDs(&crdManifest, valuespkg.Options{}, "yaml")
err = renderCRDs(&crdManifest, valuespkg.Options{
// GatewayAPI CRDs are optional so don't check for them.
Values: []string{"enableHttpRoutes=false"},
}, "yaml")
Comment on lines +146 to +149
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a separate change Unrelated to the policy controller. Can we split it out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I consider both of these to be part of the same feature -- that Linkerd should work when the GrpcRoute CRD is absent.

if err != nil {
return err
}
Expand Down
73 changes: 52 additions & 21 deletions policy-controller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,33 +247,51 @@ async fn main() -> Result<()> {
.instrument(info_span!("networkauthentications")),
);

let http_routes = runtime.watch_all::<k8s::policy::HttpRoute>(watcher::Config::default());
let http_routes_indexes = IndexList::new(inbound_index.clone())
.push(outbound_index.clone())
.push(status_index.clone())
.shared();
tokio::spawn(
kubert::index::namespaced(http_routes_indexes.clone(), http_routes)
.instrument(info_span!("httproutes.policy.linkerd.io")),
);

let gateway_http_routes =
runtime.watch_all::<k8s_gateway_api::HttpRoute>(watcher::Config::default());
tokio::spawn(
kubert::index::namespaced(http_routes_indexes, gateway_http_routes)
.instrument(info_span!("httproutes.gateway.networking.k8s.io")),
);
if api_resource_exists::<k8s::policy::HttpRoute>(&runtime.client()).await {
let http_routes = runtime.watch_all::<k8s::policy::HttpRoute>(watcher::Config::default());

let gateway_grpc_routes =
runtime.watch_all::<k8s_gateway_api::GrpcRoute>(watcher::Config::default());
let gateway_grpc_routes_indexes = IndexList::new(outbound_index.clone())
.push(inbound_index.clone())
.push(status_index.clone())
.shared();
tokio::spawn(
kubert::index::namespaced(gateway_grpc_routes_indexes.clone(), gateway_grpc_routes)
.instrument(info_span!("grpcroutes.gateway.networking.k8s.io")),
);
tokio::spawn(
kubert::index::namespaced(http_routes_indexes.clone(), http_routes)
.instrument(info_span!("httproutes.policy.linkerd.io")),
);
} else {
tracing::warn!("httproutes.policy.linkerd.io resource kind not found, skipping watches");
}

if api_resource_exists::<k8s_gateway_api::HttpRoute>(&runtime.client()).await {
let gateway_http_routes =
runtime.watch_all::<k8s_gateway_api::HttpRoute>(watcher::Config::default());
tokio::spawn(
kubert::index::namespaced(http_routes_indexes, gateway_http_routes)
.instrument(info_span!("httproutes.gateway.networking.k8s.io")),
);
} else {
tracing::warn!(
"httproutes.gateway.networking.k8s.io resource kind not found, skipping watches"
);
}

if api_resource_exists::<k8s_gateway_api::GrpcRoute>(&runtime.client()).await {
let gateway_grpc_routes =
runtime.watch_all::<k8s_gateway_api::GrpcRoute>(watcher::Config::default());
let gateway_grpc_routes_indexes = IndexList::new(outbound_index.clone())
.push(inbound_index.clone())
.push(status_index.clone())
.shared();
tokio::spawn(
kubert::index::namespaced(gateway_grpc_routes_indexes.clone(), gateway_grpc_routes)
.instrument(info_span!("grpcroutes.gateway.networking.k8s.io")),
);
} else {
tracing::warn!(
"grpcroutes.gateway.networking.k8s.io resource kind not found, skipping watches"
);
}

let services = runtime.watch_all::<k8s::Service>(watcher::Config::default());
let services_indexes = IndexList::new(outbound_index.clone())
Expand Down Expand Up @@ -435,3 +453,16 @@ async fn init_lease(client: Client, ns: &str, deployment_name: &str) -> Result<L
.await
.map_err(Into::into)
}

async fn api_resource_exists<T>(client: &Client) -> bool
where
T: Resource,
T::DynamicType: Default,
{
let dt = Default::default();
let resources = client
.list_api_group_resources(&T::api_version(&dt))
.await
.expect("Failed to list API group resources");
resources.resources.iter().any(|r| r.kind == T::kind(&dt))
}
Loading