Skip to content

Commit 727dbd8

Browse files
committed
support bare resource names, don't require explicitly specifying deployment, statefulset, daemonset
We allow users to pass just a bare resource name, searching the resource controllers for it in this priority: 1. deployment 2. statefulset 3. daemonset This priority maps to the common ones used in Segment by application developers, as opposed to infrastructure engineers who focus more on daemonsets.
1 parent 0e7a458 commit 727dbd8

File tree

3 files changed

+455
-25
lines changed

3 files changed

+455
-25
lines changed

curl.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,31 @@ func run(ctx context.Context) error {
246246
log.Printf("kubectl get -n %s pod/%s", namespace, podName)
247247
pod, err := client.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
248248
if err != nil {
249-
return err
249+
if debug {
250+
_, _ = fmt.Fprintf(os.Stderr, "Pod %q not found, attempting fallback to resource controllers...\n", podName)
251+
}
252+
// Try as deployment, statefulset, daemonset in order
253+
fallbackTypes := []string{"deployment", "statefulset", "daemonset"}
254+
var fallbackErr error
255+
for _, fallbackType := range fallbackTypes {
256+
pods, resolvedPodName, resErr := resolvePodFromResource(ctx, client, namespace, fallbackType, podName)
257+
if resErr == nil && len(pods) > 0 {
258+
if debug {
259+
_, _ = fmt.Fprintf(os.Stderr, "Resolved %s/%s to pod/%s\n", fallbackType, podName, resolvedPodName)
260+
}
261+
podName = resolvedPodName
262+
pod, err = client.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
263+
break
264+
} else if resErr != nil {
265+
fallbackErr = resErr
266+
}
267+
}
268+
if pod == nil || err != nil {
269+
if fallbackErr != nil {
270+
return fallbackErr
271+
}
272+
return err
273+
}
250274
}
251275
if pod.Status.Phase != corev1.PodRunning {
252276
return fmt.Errorf("unable to forward port because pod is not running. Current status=%v", pod.Status.Phase)

0 commit comments

Comments
 (0)