Skip to content

Commit 9719254

Browse files
authored
[tmpnet] Avoid port forwarding when running in a kube cluster (#3997)
1 parent 697b668 commit 9719254

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

tests/fixture/tmpnet/kube_runtime.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ func (p *KubeRuntime) GetLocalURI(ctx context.Context) (string, func(), error) {
144144
}
145145
}
146146

147-
// TODO(marun) Detect whether this test code is running inside the cluster
148-
// and use the URI directly
147+
// Use direct pod URI if running inside the cluster
148+
if isRunningInCluster() {
149+
return p.node.URI, func() {}, nil
150+
}
149151

150152
port, stopChan, err := p.forwardPort(ctx, config.DefaultHTTPPort)
151153
if err != nil {
@@ -164,8 +166,10 @@ func (p *KubeRuntime) GetLocalStakingAddress(ctx context.Context) (netip.AddrPor
164166
}
165167
}
166168

167-
// TODO(marun) Detect whether this test code is running inside the cluster
168-
// and use the URI directly
169+
// Use direct pod staking address if running inside the cluster
170+
if isRunningInCluster() {
171+
return p.node.StakingAddress, func() {}, nil
172+
}
169173

170174
port, stopChan, err := p.forwardPort(ctx, config.DefaultStakingPort)
171175
if err != nil {
@@ -837,3 +841,11 @@ func configureExclusiveScheduling(template *corev1.PodTemplateSpec, labelKey str
837841
},
838842
}
839843
}
844+
845+
// isRunningInCluster detects if this code is running inside a Kubernetes cluster
846+
// by checking for the presence of the service account token that's automatically
847+
// mounted in every pod.
848+
func isRunningInCluster() bool {
849+
_, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token")
850+
return err == nil
851+
}

0 commit comments

Comments
 (0)