Skip to content

Commit e6286f6

Browse files
committed
fix: tidy up logging
1 parent c75384f commit e6286f6

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

internal/run.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ func Run(ctx context.Context, group, namespace, labels string, hostPortOffset in
106106
fmt.Printf(color(resource.Name, "[%s/%s] (%s) %s: %s\n"), resource.Name, un.GetName(), phase, reason, message)
107107
}
108108

109-
fmt.Printf("watching %s\n", resource.Name)
110109
watch, err := dynamicClient.Resource(gvr).Namespace(namespace).Watch(ctx, metav1.ListOptions{
111110
LabelSelector: labels,
112111
ResourceVersion: resourceVersion,
@@ -180,13 +179,23 @@ func Run(ctx context.Context, group, namespace, labels string, hostPortOffset in
180179
logging.Store(key, true)
181180
defer logging.Delete(key)
182181

182+
pods := clientset.CoreV1().Pods(pod.Namespace)
183+
184+
// the pod might have been deleted before we get here, if so just exit quietly
185+
_, err := pods.Get(ctx, pod.Name, metav1.GetOptions{})
186+
if err != nil {
187+
return
188+
}
189+
183190
defer func() {
184191
if r := recover(); r != nil {
185-
fmt.Printf(color("pods", "[pods/%s] %s: error while tailing logs: %v\n"), pod.Name, ctr.Name, r)
192+
fmt.Printf(color("pods", "[pods/%s/%s] error while tailing logs: %v\n"), pod.Name, ctr.Name, r)
193+
} else {
194+
fmt.Printf(color("pods", "[pods/%s/%s] tailing logs stopped\n"), pod.Name, ctr.Name)
186195
}
187196
}()
188197

189-
req := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
198+
req := pods.GetLogs(pod.Name, &corev1.PodLogOptions{
190199
Follow: true,
191200
Container: ctr.Name,
192201
SinceTime: &metav1.Time{Time: time.Now()},
@@ -215,13 +224,12 @@ func Run(ctx context.Context, group, namespace, labels string, hostPortOffset in
215224
mu.Lock()
216225
defer mu.Unlock()
217226

218-
fmt.Printf(color("pods", "[pods/%s] %s port-forwarding %d -> %d\n"), pod.Name, ctr.Name, containerPort, hostPort)
219-
220227
defer func() {
221228
if r := recover(); r != nil {
222-
fmt.Printf(color("pods", "[pods/%s] %s error while port-forwarding: %d: %v\n"), pod.Name, ctr.Name, hostPort, r)
229+
fmt.Printf(color("pods", "[pods/%s/%s] error while port-forwarding: %d -> %d: %v\n"), pod.Name, ctr.Name, hostPort, containerPort, r)
230+
} else {
231+
fmt.Printf(color("pods", "[pods/%s/%s] port-forwarding %d -> %d stopped\n"), pod.Name, ctr.Name, hostPort, containerPort)
223232
}
224-
fmt.Printf(color("pods", "[pods/%s] %s port-forwarding %d -> %d stopped\n"), pod.Name, ctr.Name, containerPort, hostPort)
225233
}()
226234

227235
req := clientset.CoreV1().RESTClient().Post().
@@ -268,6 +276,8 @@ func Run(ctx context.Context, group, namespace, labels string, hostPortOffset in
268276
}
269277
}()
270278

279+
fmt.Printf(color("pods", "[pods/%s/%s] port-forwarding %d -> %d\n"), pod.Name, ctr.Name, hostPort, containerPort)
280+
271281
if err := fw.ForwardPorts(); err != nil {
272282
panic(err)
273283
}

0 commit comments

Comments
 (0)