Skip to content

Commit 7d37d5a

Browse files
committed
Revert targetPort usage
Turns out, targetPort is meant for the CNI to forward to pods, not for accessing the Service itself. The service itself will listen to the real `port`, so Chisel should connect to that instead
1 parent 8977075 commit 7d37d5a

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

src/deployment.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,6 @@ use tracing::{info, instrument, trace};
2020

2121
const CHISEL_IMAGE: &str = "jpillora/chisel";
2222

23-
/// The function takes a ServicePort struct and returns a string representation of the target port
24-
/// and protocol (if specified).
25-
///
26-
/// Arguments:
27-
///
28-
/// * `svcport`: `svcport` is a variable of type `ServicePort`, which represents a service port in
29-
/// Kubernetes. The function extracts the target port (what pods listen on) for use in chisel tunnels.
30-
///
31-
/// Returns:
32-
///
33-
/// a string that represents the target port with protocol suffix. If a numeric target_port is specified,
34-
/// it is used; otherwise falls back to the service port. Named target ports (strings) fall back to
35-
/// the service port since they cannot be resolved without pod container port information.
36-
fn get_target_port(svcport: &ServicePort) -> i32 {
37-
use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;
38-
39-
// Use numeric target_port if specified, otherwise fall back to the service port.
40-
// Named ports (strings like "web", "http") cannot be resolved here since we'd need
41-
// to look up the Pod's container ports, so we fall back to service port.
42-
match &svcport.target_port {
43-
Some(IntOrString::Int(p)) => *p,
44-
Some(IntOrString::String(_)) => svcport.port, // Can't resolve named ports
45-
None => svcport.port,
46-
}
47-
}
48-
4923
fn get_protocol_suffix(svcport: &ServicePort) -> &'static str {
5024
svcport
5125
.protocol
@@ -128,10 +102,17 @@ pub fn generate_tunnel_args(svc: &Service) -> Result<Vec<String>, ReconcileError
128102
.ok_or(ReconcileError::NoPortsSet)?
129103
.iter()
130104
.map(|p| {
131-
// The target port is what we expose externally and what the backend listens on
132-
let target_port = get_target_port(p);
105+
// service_port = what the Service/ClusterIP listens on
106+
// (targetPort is only used internally by k8s to forward to pods)
107+
// Chisel connects to ClusterIP:service_port, k8s handles the rest
108+
109+
// NOTE: Reverted from targetPort to using port directly to avoid confusion.
110+
// Turns out targetPort is meant for accessing the pods, not the Service itself.
111+
112+
// If anyone knows the specifics of how CNIs actually handle this, please enlighten me.
113+
let service_port = p.port;
133114
let protocol = get_protocol_suffix(p);
134-
format!("{target_ip}:{target_port}:{cluster_ip}:{target_port}{protocol}")
115+
format!("{target_ip}:{service_port}:{cluster_ip}:{service_port}{protocol}")
135116
})
136117
.collect();
137118

0 commit comments

Comments
 (0)